working better readings word processor
This commit is contained in:
parent
24c23b397f
commit
dddc6a0b0e
|
|
@ -28,39 +28,33 @@ def get_node_reading(node) -> str:
|
|||
return katakana_to_hiragana(features[7])
|
||||
return node.surface
|
||||
|
||||
def annotate_token(surface: str, reading: str) -> str:
|
||||
if not KANJI_RE.search(surface):
|
||||
return surface
|
||||
def has_kanji(text: str) -> bool:
|
||||
return bool(KANJI_RE.search(text))
|
||||
|
||||
# full-kanji token like 学校 -> 学校(がっこう)
|
||||
if all(KANJI_RE.match(ch) for ch in surface):
|
||||
return f"{surface}({reading})"
|
||||
|
||||
# prefix kanji + suffix kana, e.g. 行きます / 食べる
|
||||
m = re.match(r"^([\u4E00-\u9FFF]+)([ぁ-んァ-ンー]*)$", surface)
|
||||
if m:
|
||||
kanji_part, kana_suffix = m.groups()
|
||||
hira_suffix = katakana_to_hiragana(kana_suffix)
|
||||
if reading.endswith(hira_suffix):
|
||||
kanji_reading = reading[:len(reading) - len(hira_suffix)] if hira_suffix else reading
|
||||
return f"{kanji_part}({kanji_reading}){kana_suffix}"
|
||||
|
||||
# fallback
|
||||
return f"{surface}({reading})"
|
||||
def get_node_reading(node) -> str:
|
||||
features = node.feature.split(",")
|
||||
## These depend on what dictionary is being used.
|
||||
if len(features) > 6 and features[6] != "*":
|
||||
return katakana_to_hiragana(features[6])
|
||||
return node.surface
|
||||
|
||||
def annotate_phrase(text: str) -> str:
|
||||
tagger = MeCab.Tagger()
|
||||
node = tagger.parseToNode(text)
|
||||
|
||||
vocab = ()
|
||||
parts = []
|
||||
while node:
|
||||
surface = node.surface
|
||||
if surface:
|
||||
reading = get_node_reading(node)
|
||||
parts.append(annotate_token(surface, reading))
|
||||
vocab += (surface,)
|
||||
if has_kanji(surface):
|
||||
reading = get_node_reading(node)
|
||||
parts.append(f"{surface}({reading})")
|
||||
else:
|
||||
parts.append(surface)
|
||||
node = node.next
|
||||
|
||||
return "".join(parts)
|
||||
return {'reading':("".join(parts)),'vocab':vocab}
|
||||
|
||||
#print(annotate_phrase("私は学校へ行きます"))
|
||||
|
||||
|
|
@ -79,7 +73,11 @@ for cd in dump:
|
|||
# if node.surface:
|
||||
# print(f"{node.surface}\t{node.feature}")
|
||||
# node = node.next
|
||||
print(annotate_phrase(dump[cd]['nm']))
|
||||
anottated = annotate_phrase(dump[cd]['nm'])
|
||||
if anottated['reading'] != dump[cd]['nm']:
|
||||
print(anottated['reading'])
|
||||
dump[cd]['kana'] = anottated['reading']
|
||||
#dump[cd]['vocab'] = anottated['vocab']
|
||||
print()
|
||||
|
||||
exit()
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user