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