νκ΅μ΄ μμ°μ΄μ²λ¦¬λ₯Ό ν μ μλ νμ΄μ¬ ν¨ν€μ§, KoNLPyμ customized versionμ λλ€.
customized_KoNLPyλ νμ€ν μκ³ μλ λ¨μ΄λ€μ λν΄μλ λΌμ΄λΈλ¬λ¦¬λ₯Ό κ±°μΉμ§ μκ³ μ£Όμ΄μ§ μ΄μ μ μλ λ¨μ΄λ€λ‘ ν ν¬λμ΄μ§ / νμ¬νλ³μ νλ κΈ°λ₯μ μ 곡ν©λλ€. μ΄λ₯Ό μν΄ template κΈ°λ° ν ν¬λμ΄μ§μ μνν©λλ€.
μ¬μ : {'μμ΄μ€μμ΄': 'Noun', 'λ': 'Josa'} ν¬νλ¦Ώ: Noun + Josa μμ κ°μ λ¨μ΄ 리μ€νΈμ ν¬νλ¦Ώμ΄ μλ€λ©΄ 'μμ΄μ€μμ΄λ' μ΄λΌλ μ΄μ μ [('μμ΄μ€μμ΄', 'Noun'), ('λ', 'Josa')]λ‘ λΆλ¦¬λ©λλ€.
$ git clone https://github.com/lovit/customized_konlpy.git $ pip install customized_konlpy - JPype >= 0.6.1
- KoNLPy >= 0.4.4
KoNLPyμ λμΌνκ² Twitter.pos(phrase)λ₯Ό μ λ ₯ν©λλ€. κ° μ΄μ λ³λ‘ μ¬μ©μ μ¬μ μ μλ €μ§ λ¨μ΄κ° μΈμλλ©΄ customized_taggerλ‘ μ΄μ μ λΆλ¦¬νλ©°, μ¬μ©μ μ¬μ μ μλ €μ§μ§ μμ λ¨μ΄λ‘ ꡬμ±λ μ΄μ μ νΈμν° ννμ λΆμκΈ°λ‘ μ²λ¦¬ν©λλ€.
twitter.pos('μ°λ¦¬μμ΄μ€μμ΄λ μ΄λ»μ')[('μ°λ¦¬', 'Noun'), ('μμ΄μ€', 'Noun'), ('μμ΄', 'Noun'), ('λ', 'Josa'), ('μ΄λ»', 'Adjective'), ('μ', 'Eomi')] 'μμ΄μ€μμ΄'κ° μλ €μ§ λ¨μ΄κ° μλμκΈ° λλ¬Έμ νΈμν° λΆμκΈ°μμ λ¨μ΄λ₯Ό μ λλ‘ μΈμνμ§ λͺ»ν©λλ€. μλμ μ¬μ©μ μ¬μ μΌλ‘ λ¨μ΄ μΆκ°λ₯Ό ν λ€ λμΌν μμ μ μννλ©΄ μλμ κ°μ κ²°κ³Όλ₯Ό μ»μ μ μμ΅λλ€.
twitter.pos('μ°λ¦¬μμ΄μ€μμ΄λ μ΄λ»μ')[('μ°λ¦¬', 'Modifier'), ('μμ΄μ€μμ΄', 'Noun'), ('λ', 'Josa'), ('μ΄λ»', 'Adjective'), ('μ', 'Eomi')] twitter.pos('νΈμμ΄μ€ttλ μ’μμ')[('νΈμμ΄μ€', 'Noun'), ('tt', 'Noun'), ('λ', 'Josa'), ('μ’', 'Adjective'), ('μμ', 'Eomi')] ckonlpy.tagμ Twitterλ add_dictionaryλ₯Ό ν΅νμ¬ str νΉμ list of str νμμ μ¬μ©μ μ¬μ μ μΆκ°ν μ μμ΅λλ€.
from ckonlpy.tag import Twitter twitter.add_dictionary('μμ΄μ€μμ΄', 'Noun') twitter.add_dictionary(['νΈμμ΄μ€', 'tt'], 'Noun')νΈμν° νκ΅μ΄ λΆμκΈ°μμ μ΄μ©νμ§ μλ νμ¬ (λ¨μ΄ ν΄λμ€)λ₯Ό μΆκ°νκ³ μΆμ κ²½μ°μλ λ°λμ force=Trueλ‘ μ€μ ν΄μΌ ν©λλ€.
twitter.add_dictionary('lovit', 'Name', force=True)νμ¬ μ¬μ©μ€μΈ ν¬νλ¦Ώ κΈ°λ° ν ν¬λμ΄μ λ μ½λ μ¬μ© μ€ ν¬νλ¦Ώμ μΆκ°ν μ μμ΅λλ€. νμ¬ μ¬μ©μ€μΈ ν¬νλ¦Ώμ 리μ€νΈλ μλμ²λΌ νμΈν μ μμ΅λλ€.
twitter.template_tagger.templates[('Noun', 'Josa'), ('Modifier', 'Noun'), ('Modifier', 'Noun', 'Josa')] ν¬νλ¦Ώμ tuple of str νμμΌλ‘ μ λ ₯ν©λλ€.
twitter.template_tagger.add_a_template(('Noun', 'Noun', 'Josa'))Templatesλ₯Ό μ΄μ©νμ¬λ νλ³΄κ° μ¬λ¬ κ° λμ¬ μ μμ΅λλ€. μ¬λ¬ κ° ν보 μ€μμ best λ₯Ό μ ννλ ν¨μλ₯Ό μ§μ λμμΈ ν μ λ μμ΅λλ€. μ΄μ²λΌ λͺ κ°μ μ μ κΈ°μ€μ λ§λ€κ³ , κ° κΈ°μ€μ weightλ₯Ό λΆμ¬νλ λ°©μμ νΈμν° λΆμκΈ°μμ μ΄μ©νλ λ°©μμΈλ°, μ§κ΄μ μ΄κ³ νλ κ°λ₯ν΄μ λ§€μ° μ’μ λ°©μμ΄λΌ μκ°ν©λλ€.
my_weights = [ ('num_nouns', -0.1), ('num_words', -0.2), ('no_noun', -1), ('len_sum_of_nouns', 0.2) ] def my_evaluate_function(candidate): num_nouns = len([word for word, pos, begin, e in candidate if pos == 'Noun']) num_words = len(candidate) has_no_nouns = (num_nouns == 0) len_sum_of_nouns = 0 if has_no_nouns else sum( (len(word) for word, pos, _, _ in candidate if pos == 'Noun')) scores = (num_nouns, num_words, has_no_nouns, len_sum_of_nouns) score = sum((score * weight for score, (_, weight) in zip(scores, my_weights))) return scoreμμ μμ μ²λΌ my_weights μ my_evaluate_function ν¨μλ₯Ό μ μνμ¬ twitter.set_evaluator()μ μ λ ₯νλ©΄, ν΄λΉ ν¨μ κΈ°μ€μΌλ‘ best candidateλ₯Ό μ νν©λλ€.
twitter.set_evaluator(my_weights, my_evaluate_function)passwords, stopwords, passtags, λ¨μ΄ μΉνμ μν νμ²λ¦¬λ₯Ό ν μ μμ΅λλ€.
passwords μ λ±λ‘λ λ¨μ΄, (λ¨μ΄, νμ¬)λ§ μΆλ ₯λ©λλ€.
from ckonlpy.tag import Postprocessor passwords = {'μμ΄μ€μμ΄', ('μ λ§', 'Noun')} postprocessor = Postprocessor(twitter, passwords = passwords) postprocessor.pos('μ°λ¦¬μμ΄μ€μμ΄λ μ λ§ μ΄λ»μ') # [('μμ΄μ€μμ΄', 'Noun'), ('μ λ§', 'Noun')]stopwords μ λ±λ‘λ λ¨μ΄, (λ¨μ΄, νμ¬)λ μΆλ ₯λμ§ μμ΅λλ€.
stopwords = {'λ'} postprocessor = Postprocessor(twitter, stopwords = stopwords) postprocessor.pos('μ°λ¦¬μμ΄μ€μμ΄λ μ λ§ μ΄λ»μ') # [('μ°λ¦¬', 'Modifier'), ('μμ΄μ€μμ΄', 'Noun'), ('μ λ§', 'Noun'), ('μ΄λ»', 'Adjective'), ('μ', 'Eomi')]νΉμ νμ¬λ₯Ό μ§μ νλ©΄, ν΄λΉ νμ¬λ§ μΆλ ₯λ©λλ€.
passtags = {'Noun'} postprocessor = Postprocessor(twitter, passtags = passtags) postprocessor.pos('μ°λ¦¬μμ΄μ€μμ΄λ μ λ§ μ΄λ»μ') # [('μμ΄μ€μμ΄', 'Noun'), ('μ λ§', 'Noun')]μΉνν λ¨μ΄, (λ¨μ΄, νμ¬)λ₯Ό dict νμμΌλ‘ μ μνλ©΄ tag μμ λ¨μ΄κ° μΉνλμ΄ μΆλ ₯λ©λλ€.
replace = {'μμ΄μ€μμ΄': 'μμ΄λ', ('μ΄λ»', 'Adjective'): 'μμλ€'} postprocessor = Postprocessor(twitter, replace = replace) postprocessor.pos('μ°λ¦¬μμ΄μ€μμ΄λ μ λ§ μ΄λ»μ') # [('μ°λ¦¬', 'Modifier'), ('μμ΄λ', 'Noun'), ('λ', 'Josa'), ('μ λ§', 'Noun'), ('μμλ€', 'Adjective'), ('μ', 'Eomi')]μ°μλ λ¨μ΄λ₯Ό νλμ λ¨μ΄λ£¨ λ¬ΆκΈ° μν΄μ nested tuple μ΄λ tuple of str νμμ ngram μ μ λ ₯ν μ μμ΅λλ€. tuple of str μ νμμΌλ‘ μ λ ₯λ ngram μ Noun μΌλ‘ μΈμλ©λλ€.
ngrams = [(('λ―Έμ€', 'ν¨λ¬΄λΌλΉ'), 'Noun'), ('λ°λ', 'μ', 'λλΌ')] postprocessor = Postprocessor(twitter, ngrams = ngrams) postprocessor.pos('λ―Έμ€ ν¨λ¬΄λΌλΉλ μ¬λ°λ λλΌλ§μ
λλ€') # [('λ―Έμ€ - ν¨λ¬΄λΌλΉ', 'Noun'), ('λ', 'Josa'), ('μ¬λ°λ', 'Adjective'), ('λλΌλ§', 'Noun'), ('μ
λ', 'Adjective'), ('λ€', 'Eomi')]utils μλ stopwords, passwords, replace word pair λ₯Ό νμΌλ‘ μ μ₯νμμ κ²½μ°, μ΄λ₯Ό μμ½κ² λΆλ¬μ€λ ν¨μκ° μμ΅λλ€.
load_wordset μ set of str νΉμ set of tuple μ return ν©λλ€. μμμ passwords.txt μ λ΄μ©μ μλμ κ°μ΅λλ€. λ¨μ΄μ νμ¬λ ν μΉΈ λμ΄μ°κΈ°λ‘ ꡬλΆν©λλ€. stopwords.txt λ λμΌν ν¬λ©§μ λλ€.
μμ΄μ€μμ΄ μμ΄μ€μμ΄ Noun κ³΅μ° load_wordset μ μ΄μ©νλ μμμ½λ μ λλ€.
from ckonlpy.utils import load_wordset passwords = load_wordset('./passwords.txt') print(passwords) # {('μμ΄μ€μμ΄', 'Noun'), 'μμ΄μ€μμ΄', '곡μ°'} stopwords = load_wordset('./stopwords.txt') print(stopwords) # {'μ', 'λ', ('μ΄', 'Josa')}μΉνν λ¨μ΄μμ tap ꡬλΆμ΄ λμ΄μμ΅λλ€. μΉνλ λ¨μ΄μ νμ¬ νκ·Έκ° μμ κ²½μ° ν μΉΈ λμ΄μ°κΈ°λ‘ ꡬλΆν©λλ€.
str\tstr str str\tstr μλλ replacewords.txt μ μμμ λλ€.
μλΉ μλ²μ§ μλ§ Noun μ΄λ¨Έλ load_replace_wordpair μ μ΄μ©νλ μμμ½λ μ λλ€.
from ckonlpy.utils import load_replace_wordpair replace = load_replace_wordpair('./replacewords.txt') print(replace) # {'μλΉ ': 'μλ²μ§', ('μλ§', 'Noun'): 'μ΄λ¨Έλ'}ngram λ¨μ΄λ€μ κ° λ¨μ΄λ ν μΉΈ λμ΄μ°κΈ°λ‘, ngram μ νμ¬λ tap μΌλ‘ ꡬλΆλμ΄ μμ΅λλ€.
str str str str\tstr μλλ ngrams.txt μ μμμ λλ€.
λ°λ μ λλΌ λ―Έμ€ ν¨λ¬΄λΌλΉ Noun load_ngram μ μ΄μ©νλ μμμ½λ μ λλ€.
from ckonlpy.utils import load_ngram ngrams = load_ngram('./ngrams.txt') print(ngrams) # [('λ°λ', 'μ', 'λλΌ'), (('λ―Έμ€', 'ν¨λ¬΄λΌλΉ'), 'Noun')]0.0.5x μμμ λ³μμ ν¨μμ μ΄λ¦, λ³μμ νμ μΌλΆλ₯Ό λ³κ²½νμμ΅λλ€.
| λ³κ²½ μ | λ³κ²½ ν |
|---|---|
| ckonlpy.tag.Twitter._loaded_twitter_default_dictionary | ckonlpy.tag.Twitter.use_twitter_dictionary |
| ckonlpy.tag.Twitter._dictionary | ckonlpy.tag.Twitter.dictionary |
| ckonlpy.tag.Twitter._customized_tagger | ckonlpy.tag.Twitter.template_tagger |
| ckonlpy.tag.Postprocessor.tag | ckonlpy.tag.Postprocessor.pos |
| ckonlpy.custom_tag.SimpleSelector | ckonlpy.custom_tag.SimpleEvalator |
| ckonlpy.custom_tag.SimpleSelector.score | ckonlpy.custom_tag.SimpleEvalator.evaluate |
| ckonlpy.tag.Twitter.set_selector | ckonlpy.tag.AbstractTagger.set_evaluator |
| ckonlpy.custom_tag.SimpleSelector.weight | ckonlpy.custom_tag.SimpleEvaluator.weight |
| λ³κ²½ ν | λ³κ²½ μ΄μ |
|---|---|
| ckonlpy.tag.Twitter.use_twitter_dictionary | konlpy.tag.Twitter μ μ¬μ μ¬μ© μ 무 |
| ckonlpy.tag.Twitter.dictionary | public μΌλ‘ λ³ννμμ΅λλ€ |
| ckonlpy.tag.Twitter.template_tagger | Template κΈ°λ°μΌλ‘ μλνλ tagger μμ λͺ μνκ³ , public μΌλ‘ λ³ννμμ΅λλ€ |
| ckonlpy.tag.Postprocessor.pos | κΈ°λ³Έ tagger μ κ²°κ³Όλ₯Ό νμ²λ¦¬νλ κΈ°λ₯μ΄κΈ° λλ¬Έμ λμΌν ν¨μλͺ μΌλ‘ ν΅μΌνμμ΅λλ€ |
| ckonlpy.custom_tag.SimpleEvalator | ν΄λμ€ μ΄λ¦μ Selector μμ Evaluator λ‘ λ³κ²½νμμ΅λλ€ |
| ckonlpy.custom_tag.SimpleEvalator.evaluate | νμ¬μ΄ ν보μ μ μ κ³μ° λΆλΆμ score --> evaluate λ‘ ν¨μλͺ μ λ³κ²½νμμ΅λλ€ |
| ckonlpy.tag.AbstractTagger.set_evaluator | νμ¬μ΄ ν보μ μ μ κ³μ° ν¨μλ₯Ό μ€μ νλ ν¨μμ μ΄λ¦μ λ³κ²½νμμ΅λλ€. ν΄λΉ ν¨μλ ckonlpy.tag.Twitter μμ ckonlpy.tag.AbstractTagger λ‘ μ΄λνμμ΅λλ€ |
| ckonlpy.custom_tag.SimpleEvaluator.weight | {str:float} νμμ weight λ₯Ό [(str, float)] νμμΌλ‘ λ³κ²½νμμ΅λλ€ |