korean:mecab:python
差分
このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
korean:mecab:python [2021/11/06 17:31] – [はじめに] yoshi | korean:mecab:python [2021/11/14 22:49] (現在) – [形態素解析の実行] yoshi | ||
---|---|---|---|
行 17: | 行 17: | ||
HanDicでは完成型のハングルではなく,字母に分解した文字列を入力として用いるため,完成形ハングルを字母に分解するスクリプトを用意します. | HanDicでは完成型のハングルではなく,字母に分解した文字列を入力として用いるため,完成形ハングルを字母に分解するスクリプトを用意します. | ||
- | <code python> | + | <file python |
# k2jamo | # k2jamo | ||
- | # substitute_main(match) | + | # substitute(text) |
import re | import re | ||
- | initial | + | REGEX = '[가-힣]' |
- | medial | + | pattern |
- | final = ["", | + | |
- | regex = u' | ||
- | pattern = re.compile(regex) | ||
def convert_main(match): | def convert_main(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] | + | |
+ | result = initial[my_int_index] + \ | ||
+ | | ||
+ | | ||
return result | return result | ||
- | def substitute_main(text): | + | |
+ | def substitute(text): | ||
+ | ' | ||
result = re.sub(pattern, | result = re.sub(pattern, | ||
return result | return result | ||
- | </code> | + | |
+ | |||
+ | if __name__ == ' | ||
+ | for letters in [' | ||
+ | output = substitute(letters) | ||
+ | print(letters, | ||
+ | </file> | ||
これを'' | これを'' | ||
行 51: | 行 60: | ||
<code python> | <code python> | ||
import MeCab | import MeCab | ||
- | from k2jamo | + | import |
tokenizer = MeCab.Tagger(' | tokenizer = MeCab.Tagger(' | ||
tokenizer.parse('' | tokenizer.parse('' | ||
行 58: | 行 67: | ||
===== 形態素解析の実行 ===== | ===== 形態素解析の実行 ===== | ||
- | '' | + | '' |
<code python> | <code python> | ||
input = u' | input = u' | ||
# 한겨레 2021年11月5日付け記事(https:// | # 한겨레 2021年11月5日付け記事(https:// | ||
- | input = substitute_main(input) | + | input = k2jamo.substitute(input) |
print(tokenizer.parse(input)) | print(tokenizer.parse(input)) | ||
</ | </ |
korean/mecab/python.1636187515.txt.gz · 最終更新: 2021/11/06 17:31 by yoshi