forked from openvanilla/McBopomofo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlmreader.py
More file actions
44 lines (29 loc) · 1.39 KB
/
Copy pathlmreader.py
File metadata and controls
44 lines (29 loc) · 1.39 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
from ..compilers.compiler_utils import HEADER
def read_raw_lm_entries(path: str) -> list[tuple[str, str, str]]:
"""Read a McBopomofo LM file and return the raw entries"""
entries = []
with open(path) as f:
lines = f.readlines()
if not lines or lines[0] != HEADER:
raise AssertionError(f"{path} is not a sorted McBopomofo LM file")
for line in lines[1:]:
# only split with one single whitespace, since split() can split
# with full-width spaces characters, which we actually want
reading, value, score = line.strip().split(" ")
entries.append((reading, value, score))
return entries
def read_lm(path: str) -> dict[str, list[tuple[str, str]]]:
"""Read a McBopomofo LM file and return the unigram mappings
Each reading maps to a list of (value, score) pair, but the score is
in string; this is to minimize diff, since reading floating point numbers
in and writing it out does not always guarantee to produce the same string
output, especially when different tools written in different languages
are involved."""
lm: dict[str, list[tuple[str, str]]] = {}
entries = read_raw_lm_entries(path)
for reading, value, score in entries:
if reading in lm:
lm[reading].append((value, score))
else:
lm[reading] = [(value, score)]
return lm