pseudo-chinese/pseudo-chinese.py
2021-08-14 20:36:09 +08:00

98 lines
2.4 KiB
Python

import MeCab
import sys
# 形態素解析する関数
# Function for morphological analysis.
# 形态学分析功能
def parse(sentence):
mecab_tagger = MeCab.Tagger()
raw_result = mecab_tagger.parse(sentence).split('\n')
result = []
for i in raw_result[:-2]:
j = i.split('\t')
item = dict()
item['form'] = j[0] # 食べ
print(j)
if len(j) > 1:
item['lemma'] = j[3] # 食べる
item['pos'] = j[4] # 動詞-一般
item['features'] = j[6] # 連用形-一般
else:
item['lemma'] = j[0]
item["pos"] = ""
item["features"] = ""
result.append(item)
return result
# ひらがなを削除する関数
# Function to delete hiragana.
# 删除平假名的功能
def hira_to_blank(str):
return "".join(["" if ("" <= ch <= "") else ch for ch in str])
if __name__ == "__main__":
document = "私は明日、伊豆大島に行きたい"
args = sys.argv
if len(args) >= 2:
document = str(args[1])
parse_document = parse(document)
#print(parse_document)
result_list = list()
for token in parse_document:
# 形態素解析結果に置き換えルールを適用する
if (token["pos"] != "助詞-格助詞"
and token["pos"] != "助詞-接続助詞"
and token["pos"] != "助詞-終助詞"
and token["pos"] != "助詞-接続助詞" ):
if '終止形-一般' in token["features"]:
if ("為る" in token["lemma"]) or ("ます" in token["lemma"]):
prime = "" # don't translate it.
elif "たい" in token["lemma"]:
prime = ""
elif token["lemma"] in ["ない", "無い"]:
prime = ""
elif token['lemma'] == '':
prime = ""
else:
prime = token["lemma"]
else:
prime = token["lemma"]
if token['lemma'] == '私-代名詞':
prime = ''
if (token['lemma'] == '' or token['lemma'] == '貴方' or token['lemma'] == 'お前'):
prime = ''
if token['lemma'] == '円-助数詞':
prime = ''
if len(token["features"]) != 0:
if "連体形-一般" in token['features']:
if token['lemma'] == 'ない':
prime = "無之"
else:
prime = ""
result_list.append(hira_to_blank(prime))
if token['lemma'] == '' and token['pos'] == "助詞-格助詞":
prime = ""
result_list.append(hira_to_blank(prime))
if token["form"] == "" and token['pos'] == '助詞-終助詞':
prime = ""
result_list.append(hira_to_blank(prime))
print(''.join(result_list))