What is this?
bareunpy
is the python 3 library for bareun.
Bareun is a Korean NLP,
which provides tokenizing, POS tagging for Korean.
How to install
pip3 install bareunpy
How to get bareun
docker pull bareunai/bareun:latest
How to use, tagger
import sys
import google.protobuf.text_format as tf
from bareunpy import Tagger
API_KEY = "koba-ABCDEFG-1234567-LMNOPQR-7654321"
my_tagger = Tagger(API_KEY, 'localhost')
my_tagger = Tagger(API_KEY, '10.8.3.211', 15656)
res = tagger.tags(["안녕하세요.", "반가워요!"])
m = res.msg()
tf.PrintMessage(m, out=sys.stdout, as_utf8=True)
print(tf.MessageToString(m, as_utf8=True))
print(f'length of sentences is {len(m.sentences)}')
print(f'length of tokens in sentences[0] is {len(m.sentences[0].tokens)}')
print(f'length of morphemes of first token in sentences[0] is {len(m.sentences[0].tokens[0].morphemes)}')
print(f'lemma of first token in sentences[0] is {m.sentences[0].tokens[0].lemma}')
print(f'first morph of first token in sentences[0] is {m.sentences[0].tokens[0].morphemes[0]}')
print(f'tag of first morph of first token in sentences[0] is {m.sentences[0].tokens[0].morphemes[0].tag}')
for sent in m.sentences:
for token in sent.tokens:
for m in token.morphemes:
print(f'{m.text.content}/{m.tag}:{m.probability}:{m.out_of_vocab})
# get json object
jo = res.as_json()
print(jo)
# get tuple of pos tagging.
pa = res.pos()
print(pa)
# another methods
ma = res.morphs()
print(ma)
na = res.nouns()
print(na)
va = res.verbs()
print(va)
# custom dictionary
cust_dic = tagger.custom_dict("my")
cust_dic.copy_np_set({'내고유명사', '우리집고유명사'})
cust_dic.copy_cp_set({'코로나19'})
cust_dic.copy_cp_caret_set({'코로나^백신', '"독감^백신'})
cust_dic.update()
# laod prev custom dict
cust_dict2 = tagger.custom_dict("my")
cust_dict2.load()
tagger.set_domain('my')
tagger.pos('코로나19는 언제 끝날까요?')
How to use, tokenizer
import sys
import google.protobuf.text_format as tf
from bareunpy import Tokenizer
API_KEY = "koba-ABCDEFG-1234567-LMNOPQR-7654321"
my_tokenizer = Tokenizer(API_KEY, 'localhost')
my_tokenizer = Tagger(API_KEY, '10.8.3.211', 15656)
tokenized = tokenizer.tokenize_list(["안녕하세요.", "반가워요!"])
m = tokenized.msg()
tf.PrintMessage(m, out=sys.stdout, as_utf8=True)
print(tf.MessageToString(m, as_utf8=True))
print(f'length of sentences is {len(m.sentences)}')
print(f'length of tokens in sentences[0] is {len(m.sentences[0].tokens)}')
print(f'length of segments of first token in sentences[0] is {len(m.sentences[0].tokens[0].segments)}')
print(f'tagged of first token in sentences[0] is {m.sentences[0].tokens[0].tagged}')
print(f'first segment of first token in sentences[0] is {m.sentences[0].tokens[0].segments[0]}')
print(f'hint of first morph of first token in sentences[0] is {m.sentences[0].tokens[0].segments[0].hint}')
for sent in m.sentences:
for token in sent.tokens:
for m in token.segments:
print(f'{m.text.content}/{m.hint})
# get json object
jo = tokenized.as_json()
print(jo)
# get tuple of segments
ss = tokenized.segments()
print(ss)
ns = tokenized.nouns()
print(ns)
vs = tokenized.verbs()
print(vs)
# postpositions: 조사
ps = tokenized.postpositions()
print(ps)
# Adverbs, 부사
ass = tokenized.adverbs()
print(ass)
ss = tokenized.symbols()
print(ss)
How to use, spelling corrector
from bareunpy import Corrector
API_KEY = "koba-ABCDEFG-1234567-LMNOPQR-7654321"
corrector = Corrector(API_KEY)
response = corrector.correct_error("영수 도 줄기가 얇어서 시들을 것 같은 꽃에물을 주었다.")
print(f"Original: {response.origin}")
print(f"Corrected: {response.revised}")
corrector.print_results(response)
responses = corrector.correct_error_list([
"어머니 께서 만들어주신김치찌게가너무맵다며동생이울어버렸다.",
"영수 도 줄기가 얇어서 시들을 것 같은 꽃에물을 주었다."
])
for res in responses:
print(f"Original: {res.origin}")
print(f"Corrected: {res.revised}")
corrector.print_results(responses)
corrector.print_as_json(response)