Repository navigation
Expand file tree
/
Copy pathmecab_controller.py
More file actions
144 lines (125 loc) · 5.5 KB
/
Copy pathmecab_controller.py
File metadata and controls
144 lines (125 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Copyright: Ajatt-Tools and contributors; https://github.com/Ajatt-Tools
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import dataclasses
import io
import re
from collections.abc import Iterable, Sequence
try:
from .basic_mecab_controller import BasicMecabController, MecabLaunchOpts
from .basic_types import (
COMPONENTS,
Inflection,
MecabParsedToken,
PartOfSpeech,
Separators,
)
from .consts import DEMO_EXPRESSIONS
from .format import format_output
from .kana_conv import is_kana_str, to_hiragana, to_katakana
from .lru_cache import LRUCache
from .replace_mistakes import replace_mistakes
except ImportError:
from basic_mecab_controller import BasicMecabController, MecabLaunchOpts
from basic_types import (
COMPONENTS,
Inflection,
MecabParsedToken,
PartOfSpeech,
Separators,
)
from consts import DEMO_EXPRESSIONS
from format import format_output
from kana_conv import is_kana_str, to_hiragana, to_katakana
from lru_cache import LRUCache
from replace_mistakes import replace_mistakes
# Mecab
##########################################################################
def escape_text(text: str) -> str:
"""Strip characters that trip up mecab."""
text = text.replace("\n", " ")
text = text.replace("\uff5e", "~")
text = re.sub(r"<[^<>]+>", "", text)
text = re.sub(r"\[sound:[^]]+]", "", text)
text = re.sub(r"\[\[type:[^]]+]]", "", text)
return text.strip()
class MecabController:
"""Analyze expressions, correct known tokenization mistakes, and format cached furigana results."""
_additional_mecab_args: Sequence[str] = (
"--node-format=" + Separators.component.join(component for component in COMPONENTS) + Separators.node,
"--unk-format=" + COMPONENTS.word + Separators.node,
"--eos-format=" + Separators.footer,
)
_mecab: BasicMecabController
_verbose: bool
_cache: LRUCache[str, Sequence[MecabParsedToken]]
def __init__(
self,
opts: MecabLaunchOpts | None = None,
verbose: bool = False,
cache_max_size: int = 1024,
) -> None:
"""Configure the native controller and the maximum cached expression count."""
opts = opts or MecabLaunchOpts()
self._mecab = BasicMecabController(
# _analyze() requires a specific output format, including when custom launch options are supplied.
mecab_cmd=(*opts.make_mecab_cmd(), *self._additional_mecab_args),
verbose=verbose,
)
# Different dictionaries must never reuse each other's parsed readings.
self._cache = LRUCache(cache_max_size)
self._verbose = verbose
def translate(self, expr: str) -> Sequence[MecabParsedToken]:
try:
return self._cache[expr]
except KeyError:
return self._cache.setdefault(expr, tuple(self._translate(expr)))
def _translate(self, expr: str) -> Iterable[MecabParsedToken]:
"""Analyzes expr with mecab. Fixes mecab's mistakes. Returns a parsed token for each word in expr."""
for token in replace_mistakes(self._analyze(expr)):
if self._verbose:
print(*dataclasses.astuple(token), sep="\t")
yield token
def _analyze(self, expr: str) -> Iterable[MecabParsedToken]:
"""Analyzes expr with mecab. Returns a parsed token for each word in expr."""
for section in self._mecab.run(escape_text(expr)).split(Separators.node):
if not section:
# ignore empty sections (can be at the end of a node)
continue
if section == Separators.footer:
break
components = section.split(Separators.component)
try:
word, headword, katakana_reading, part_of_speech, inflection = components
except ValueError:
# unknown to mecab, gave the same word back
word, headword, katakana_reading = components * 3
part_of_speech, inflection = None, None
if is_kana_str(word) or to_katakana(word) == to_katakana(katakana_reading):
# Kana and unchanged surfaces need no separate annotation, but token readings stay string-valued.
katakana_reading = ""
yield MecabParsedToken(
word=word,
headword=headword,
katakana_reading=katakana_reading,
part_of_speech=PartOfSpeech(part_of_speech or None),
inflection_type=Inflection(inflection or None),
)
def reading(self, expr: str) -> str:
"""Formats furigana using Anki syntax, e.g. 野獣[やじゅう]の 様[よう]な 男[おとこ]."""
buf = io.StringIO()
for out in self.translate(expr):
if out.katakana_reading and to_katakana(out.katakana_reading) != to_katakana(out.word):
buf.write(format_output(out.word, to_hiragana(out.katakana_reading)))
else:
buf.write(out.word)
return buf.getvalue()
def main() -> None:
"""Display formatted readings and parsed tokens for the library's demonstration expressions."""
mecab = MecabController()
for idx, expr in enumerate(DEMO_EXPRESSIONS):
print(f"expr #{idx:02d}: {mecab.reading(expr)}")
for jdx, token in enumerate(mecab.translate(expr)):
print(f"token #{jdx:02d}: {token}")
print("." * 20)
if __name__ == "__main__":
main()