ユーザ用ツール

サイト用ツール


korean:mecab:python

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン前のリビジョン
次のリビジョン
前のリビジョン
korean:mecab:python [2021/11/06 10:43] yoshikorean:mecab:python [2021/11/14 22:49] (現在) – [形態素解析の実行] yoshi
行 3: 行 3:
 ===== はじめに ===== ===== はじめに =====
  
-ここではHanDicをPythonで利用する方法について紹介します.+ここでは[[https://ja.osdn.net/projects/handic/|HanDic]]をPythonで利用する方法について紹介します.
  
 Pythonのバージョンは3.7.11,HanDicはhandic-mecab-20210116を用いました.MeCabを扱うためのモジュールmecab-python3ほか,必要なモジュールがインストールされていることが前提です.HanDicはPythonスクリプトのあるディレクトリ下,''handic''という名前のディレクトリに保存されているとします. Pythonのバージョンは3.7.11,HanDicはhandic-mecab-20210116を用いました.MeCabを扱うためのモジュールmecab-python3ほか,必要なモジュールがインストールされていることが前提です.HanDicはPythonスクリプトのあるディレクトリ下,''handic''という名前のディレクトリに保存されているとします.
  
-なお,''dicrc''に記述の不備があり,「bos-feature」行の末尾に「'',*,*''」を追加しました.+なお,HanDicに含まれるファイルのうち''dicrc''に記述の不備があり,「bos-feature」行の末尾に「'',*,*''」を追加しました.
  
 ==== 注意 ==== ==== 注意 ====
行 17: 行 17:
 HanDicでは完成型のハングルではなく,字母に分解した文字列を入力として用いるため,完成形ハングルを字母に分解するスクリプトを用意します. HanDicでは完成型のハングルではなく,字母に分解した文字列を入力として用いるため,完成形ハングルを字母に分解するスクリプトを用意します.
  
-<code python>+<file python k2jamo.py>
 # k2jamo # k2jamo
-substitute_main(match)+substitute(text)
 import re import re
  
-initial = ["ᄀ", "ᄁ", "ᄂ", "ᄃ", "ᄄ", "ᄅ", "ᄆ", "ᄇ", "ᄈ", "ᄉ", "ᄊ", "ᄋ", "ᄌ", "ᄍ", "ᄎ", "ᄏ", "ᄐ", "ᄑ", "ᄒ"+REGEX '[가-힣]' 
-medial ["ᅡ", "ᅢ", "ᅣ", "ᅤ", "ᅥ", "ᅦ", "ᅧ", "ᅨ", "ᅩ", "ᅪ", "ᅫ", "ᅬ", "ᅭ", "ᅮ", "ᅯ", "ᅰ", "ᅱ", "ᅲ", "ᅳ", "ᅴ", "ᅵ", ""+pattern re.compile(REGEX)
-final = ["", "ᆨ", "ᆩ", "ᆪ", "ᆫ", "ᆬ", "ᆭ", "ᆮ", "ᆯ", "ᆰ", "ᆱ", "ᆲ", "ᆳ", "ᆴ", "ᆵ", "ᆶ", "ᆷ", "ᆸ", "ᆹ", "ᆺ", "ᆻ", "ᆼ", "ᆽ", "ᆾ", "ᆿ", "ᇀ", "ᇁ", "ᇂ"]+
  
-regex = u'[가-힣]' 
-pattern = re.compile(regex) 
  
 def convert_main(match): def convert_main(match):
 +    '正規表現でマッチした完成形ハングル<match>を字母に分解する'
     value = ord(match.group(0))     value = ord(match.group(0))
     my_int = value - 44032     my_int = value - 44032
     my_int_index = int(my_int / 588)     my_int_index = int(my_int / 588)
     my_final_index = my_int % 28     my_final_index = my_int % 28
-    my_medial_index = int((my_int - (my_int_index * 588) - my_final_index) / 28) +    my_medial_index = 
-    result = initial[my_int_index] + medial[my_medial_index] + final[my_final_index]+        int((my_int - (my_int_index * 588) - my_final_index) / 28) 
 +    result = initial[my_int_index] + 
 +        medial[my_medial_index] + 
 +        final[my_final_index]
     return result     return result
  
-def substitute_main(text):+ 
 +def substitute(text): 
 +    '正規表現で完成形ハングルにマッチした入力<text>を置換する'
     result = re.sub(pattern, convert_main, text)     result = re.sub(pattern, convert_main, text)
     return result     return result
-</code>+ 
 + 
 +if __name__ == '__main__': 
 +    for letters in ['가', '안녕', '한국어 문장입니다.', '영문자 a/b를 포함']: 
 +        output = substitute(letters) 
 +        print(letters, '=>', output) 
 +</file>
  
 これを''k2jamo.py''として保存します. これを''k2jamo.py''として保存します.
行 51: 行 60:
 <code python> <code python>
 import MeCab import MeCab
-from k2jamo import substitute_main+import k2jamo
 tokenizer = MeCab.Tagger('-d handic') tokenizer = MeCab.Tagger('-d handic')
 tokenizer.parse('') tokenizer.parse('')
行 58: 行 67:
 ===== 形態素解析の実行 ===== ===== 形態素解析の実行 =====
  
-''k2jamo.py''にある関数''substitute_main()''を使って字母に分解し,MeCabで解析します.+''k2jamo.py''にある関数''substitute()''を使って字母に分解し,MeCabで解析します.
  
 <code python> <code python>
 input = u'한상혁 방송통신위원장이 코로나19 확진 판정을 받으면서 김부겸 국무총리를 비롯한 국무위원들이 코로나 검사 대상이 됐다.' input = u'한상혁 방송통신위원장이 코로나19 확진 판정을 받으면서 김부겸 국무총리를 비롯한 국무위원들이 코로나 검사 대상이 됐다.'
-# 한겨레 2021年11月5日付け記事より(https://www.khan.co.kr/politics/politics-general/article/202111050751001/) +# 한겨레 2021年11月5日付け記事(https://www.khan.co.kr/politics/politics-general/article/202111050751001/より 
-input = substitute_main(input)+input = k2jamo.substitute(input)
 print(tokenizer.parse(input)) print(tokenizer.parse(input))
 </code> </code>
行 108: 行 117:
 うまくいっているようです. うまくいっているようです.
  
- {{indexmenu_n>204}}+====== 参考にしたページ ====== 
 + 
 +  * [[https://taku910.github.io/mecab/bindings.html|スクリプト言語のバインディング]] 
 +  * [[https://atmarkit.itmedia.co.jp/ait/articles/2102/05/news027.html|[文章生成]MeCabをインストールして分かち書きを試してみよう]] 
 +  * [[https://water2litter.net/rum/post/python_wordcloud_jp/|PythonでWord Cloudを作ってみた(和文編)]] 
 +  * [[https://analytics-note.xyz/programming/frequencies-word-cloud/|単語の頻度データからWord Cloudを作成する方法]] 
 + 
 +{{indexmenu_n>204}}
korean/mecab/python.1636163026.txt.gz · 最終更新: 2021/11/06 10:43 by yoshi