Skip to content

Commit 25ce8bc

Browse files
authored
[feat] support command line tools wetn and weitn (#105)
1 parent 8ad5c31 commit 25ce8bc

8 files changed

Lines changed: 127 additions & 102 deletions

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@
2424
pip install WeTextProcessing
2525
```
2626

27+
Command-usage:
28+
29+
```bash
30+
wetn --text "2.5平方电线"
31+
weitn --text "二点五平方电线"
32+
```
33+
34+
Python usage:
35+
2736
```py
2837
# tn usage
2938
>>> from tn.chinese.normalizer import Normalizer

inverse_normalize.py

Lines changed: 0 additions & 59 deletions
This file was deleted.

inverse_normalize.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
itn/main.py

itn/__main__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from itn.main import main
2+
3+
if __name__ == '__main__':
4+
main()

itn/main.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright (c) 2022 Xingchen Song (sxc19@tsinghua.org.cn)
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import argparse
16+
17+
# TODO(xcsong): multi-language support
18+
from itn.chinese.inverse_normalizer import InverseNormalizer
19+
20+
def str2bool(s, default=False):
21+
s = s.lower()
22+
if s == 'true':
23+
return True
24+
elif s == 'false':
25+
return False
26+
else:
27+
return default
28+
29+
def main():
30+
parser = argparse.ArgumentParser()
31+
parser.add_argument('--text', help='input string')
32+
parser.add_argument('--file', help='input file path')
33+
parser.add_argument('--overwrite_cache', action='store_true',
34+
help='rebuild *.fst')
35+
parser.add_argument('--enable_standalone_number', type=str,
36+
default='True',
37+
help='enable standalone number')
38+
parser.add_argument('--enable_0_to_9', type=str,
39+
default='True',
40+
help='enable convert number 0 to 9')
41+
args = parser.parse_args()
42+
43+
normalizer = InverseNormalizer(
44+
cache_dir='itn', overwrite_cache=args.overwrite_cache,
45+
enable_standalone_number=str2bool(args.enable_standalone_number),
46+
enable_0_to_9=str2bool(args.enable_0_to_9))
47+
48+
if args.text:
49+
print(normalizer.tag(args.text))
50+
print(normalizer.normalize(args.text))
51+
elif args.file:
52+
with open(args.file) as fin:
53+
for line in fin:
54+
print(normalizer.tag(line.strip()))
55+
print(normalizer.normalize(line.strip()))
56+
57+
58+
if __name__ == '__main__':
59+
main()

normalize.py

Lines changed: 0 additions & 43 deletions
This file was deleted.

normalize.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tn/main.py

setup.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@
3636
"itn": ["*.fst"],
3737
},
3838
install_requires=['pynini', 'importlib_resources'],
39+
entry_points={
40+
"console_scripts": [
41+
"wetn = tn.main:main",
42+
"weitn = itn.main:main",
43+
]
44+
},
3945
tests_require=['pytest'],
4046
classifiers=[
4147
"Programming Language :: Python :: 3",

tn/__main__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from tn.main import main
2+
3+
if __name__ == '__main__':
4+
main()

tn/main.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright (c) 2022 Zhendong Peng (pzd17@tsinghua.org.cn)
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import argparse
16+
17+
# TODO(pzd17): multi-language support
18+
from tn.chinese.normalizer import Normalizer
19+
20+
21+
def main():
22+
parser = argparse.ArgumentParser()
23+
parser.add_argument('--text', help='input string')
24+
parser.add_argument('--file', help='input file path')
25+
parser.add_argument('--overwrite_cache', action='store_true',
26+
help='rebuild *.fst')
27+
args = parser.parse_args()
28+
29+
normalizer = Normalizer(cache_dir='tn',
30+
overwrite_cache=args.overwrite_cache)
31+
32+
if args.text:
33+
print(normalizer.tag(args.text))
34+
print(normalizer.normalize(args.text))
35+
elif args.file:
36+
with open(args.file) as fin:
37+
for line in fin:
38+
print(normalizer.tag(line.strip()))
39+
print(normalizer.normalize(line.strip()))
40+
41+
42+
if __name__ == '__main__':
43+
main()

0 commit comments

Comments
 (0)