-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.py
63 lines (55 loc) · 1.85 KB
/
demo.py
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
# encoding=utf-8
import argparse
import chinesetone2pinyin as cp
from ChineseRhythmPredictor import *
from ChineseRhythmPredictor.experiment import test_sentences, load_model
import chaifen
import time
# import sys
# reload(sys)
# sys.setdefaultencoding('utf-8')
import sys
import codecs
sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())
def with_Rhythm(chinese, model, split=True):
pinyin_input = test_sentences(model, [chinese])
chinese_Normal, pinyin = cp.chinese2pinyin(pinyin_input)
if split:
pinyin = chaifen.split_sheng(pinyin)
pinyin = pinyin.replace('#0', '').replace('#2', '#1')
punctuation = [',', '.', '!', '?']
if pinyin[-1] in punctuation:
pinyin = pinyin[:-2] + '#2 ' + pinyin[-1]
else:
pinyin = pinyin + ' #2'
pinyin = pinyin.replace(' r 5 ', ' er5 ')
pinyin = pinyin.replace(' ', ' ')
return chinese_Normal, pinyin
def without_Rhythm(chinese, split=True):
chinese_Normal, pinyin = cp.chinese2pinyin(chinese)
if split:
pinyin = chaifen.split_sheng(pinyin)
return chinese_Normal, pinyin
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--cn_file', default='')
parser.add_argument('--pinyin_file', default='pinyin.txt')
args = parser.parse_args()
cn_file = args.cn_file
pinyin_file = args.pinyin_file
model = load_model()
# chinese='时间~是下午2点36分。您想要什么啊,这个东西2983.07克或12345.60米。'
# # pinyin_input = '今天是个好日子,心想的事儿都能成。儿子已经好了'
# chinese_Normal, pinyin = with_Rhythm(chinese, model)
# chinese_Normal, pinyin = without_Rhythm(chinese)
f_out = open(pinyin_file, "w")
with open(cn_file, "r", encoding="utf-8") as f:
while 1:
line = f.readline().strip()
print(line)
if not line:
break
chinese_Normal, pinyin = with_Rhythm(line, model)
print(pinyin)
f_out.write(pinyin + "\n")
f.close()