Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
167 lines (148 loc) · 4.96 KB

File metadata and controls

167 lines (148 loc) · 4.96 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import logging
import tempfile
try:
import click
except ImportError:
print("Please run")
print(" pip install click")
print("before you continue")
raise
import importlib_resources
from pathlib import Path
from cppinyin import Encoder
def load_dict(user_dict: Path):
assert user_dict.is_file(), f"File not exists, filename : {user_dict}"
lines = []
user_phrases = set()
with open(user_dict, "r") as fi:
for line in fi:
toks = line.strip().split()
if len(toks) < 3:
logging.error(
"Each line in vocab should contain at lease three items "
"(seperate by space), "
"the first one is Chinese word/character, the second one is "
f"score, the others are the corresponding pinyins given : {line}"
)
raise
try:
score = float(toks[1])
except ValueError:
logging.error(
"Each line in vocab should contain at lease three items "
"(seperate by space), "
"the first one is Chinese word/character, the second one is "
f"score, the others are the corresponding pinyins given : {line}"
)
raise
user_phrases.add(toks[0].strip())
lines.append(line)
return lines, user_phrases
def get_dict_path(dict_path: Path, user_dict_path: Path):
if user_dict_path is not None or dict_path is not None:
ref = importlib_resources.files("cppinyin") / "resources/pinyin.raw"
with importlib_resources.as_file(ref) as path:
default_vacab = path
dict_lines = []
user_dict_lines = []
user_phrases = set()
if dict_path is not None:
dict_lines, _ = load_dict(dict_path)
else:
dict_lines, _ = load_dict(default_vacab)
if user_dict_path is not None:
user_dict_lines, user_phrases = load_dict(user_dict_path)
temp_dict_path = ".cppinyin.tmp"
with open(temp_dict_path, "w") as fo:
for line in user_dict_lines:
fo.write(line)
for line in dict_lines:
toks = line.strip().split()
if toks[0].strip() in user_phrases:
continue
else:
fo.write(line)
return temp_dict_path
else:
ref = importlib_resources.files("cppinyin") / "resources/pinyin.dict"
with importlib_resources.as_file(ref) as path:
default_vacab = path
return str(default_vacab)
@click.group()
def cli():
"""
The shell entry point to cppinyin.
"""
logging.basicConfig(
format="%(asctime)s %(levelname)s [%(filename)s:%(lineno)d] %(message)s",
level=logging.INFO,
)
@cli.command(name="build")
@click.argument("output", type=click.Path())
@click.option(
"--dict-path",
type=Path,
help="The path to the dictionary, if not provided using default.",
)
@click.option(
"--user-dict-path", type=Path, help="The path to user customized dict."
)
def build(output: Path, dict_path: Path, user_dict_path: Path):
"""
Build raw dictionary (in text format) to binary format.
Note:
The user_dict has a higher priority than dict (also default dict).
"""
output = Path(output)
output.parent.mkdir(parents=True, exist_ok=True)
encoder = Encoder(get_dict_path(dict_path, user_dict_path))
encoder.save(str(output))
@cli.command(name="encode")
@click.argument("input", type=str)
@click.option(
"--dict-path",
type=Path,
help="The path to the dictionary, if not provided using default.",
)
@click.option(
"--user-dict-path", type=Path, help="The path to user customized dict."
)
@click.option(
"--tone",
type=click.Choice(["normal", "number", "none"]),
default="number",
help="Choose the tone style of pinyin",
)
@click.option(
"--partial",
is_flag=True,
show_default=True,
default=False,
help="Whether to split pinyins into initials and finals or not.",
)
def encode(
input: str,
dict_path: Path,
user_dict_path: Path,
tone: str,
partial: bool,
):
"""
Encode a input Chinese sentence into pinyin sequences given dictionary
(if not provided will use the default one) and user defined dictionary.
The input could be a string or a file path.
Note:
The user_dict has a higher priority than dict (also default dict).
"""
encoder = Encoder(get_dict_path(dict_path, user_dict_path))
if Path(input).is_file():
with open(input, "r") as fi:
for line in fi:
pinyin = " ".join(
encoder.encode(line.strip(), tone=tone, partial=partial)
)
click.echo(f"{line.strip()}\t{pinyin}")
else:
click.echo(
" ".join(encoder.encode(input.strip(), tone=tone, partial=partial))
)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.