Plana-Archive commited on
Commit
eb6c27d
·
verified ·
1 Parent(s): a0b1522

Clean up previous raw files

Browse files
Files changed (1) hide show
  1. Zero-CPU/text/chinese.py +0 -199
Zero-CPU/text/chinese.py DELETED
@@ -1,199 +0,0 @@
1
- import os
2
- import re
3
-
4
- import cn2an
5
- from pypinyin import lazy_pinyin, Style
6
-
7
- from text.symbols import punctuation
8
- from text.tone_sandhi import ToneSandhi
9
-
10
- current_file_path = os.path.dirname(__file__)
11
- pinyin_to_symbol_map = {
12
- line.split("\t")[0]: line.strip().split("\t")[1]
13
- for line in open(os.path.join(current_file_path, "opencpop-strict.txt")).readlines()
14
- }
15
-
16
- import jieba.posseg as psg
17
-
18
-
19
- rep_map = {
20
- ":": ",",
21
- ";": ",",
22
- ",": ",",
23
- "。": ".",
24
- "!": "!",
25
- "?": "?",
26
- "\n": ".",
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
- tone_modifier = ToneSandhi()
54
-
55
-
56
- def replace_punctuation(text):
57
- text = text.replace("嗯", "恩").replace("呣", "母")
58
- pattern = re.compile("|".join(re.escape(p) for p in rep_map.keys()))
59
-
60
- replaced_text = pattern.sub(lambda x: rep_map[x.group()], text)
61
-
62
- replaced_text = re.sub(
63
- r"[^\u4e00-\u9fa5" + "".join(punctuation) + r"]+", "", replaced_text
64
- )
65
-
66
- return replaced_text
67
-
68
-
69
- def g2p(text):
70
- pattern = r"(?<=[{0}])\s*".format("".join(punctuation))
71
- sentences = [i for i in re.split(pattern, text) if i.strip() != ""]
72
- phones, tones, word2ph = _g2p(sentences)
73
- assert sum(word2ph) == len(phones)
74
- assert len(word2ph) == len(text) # Sometimes it will crash,you can add a try-catch.
75
- phones = ["_"] + phones + ["_"]
76
- tones = [0] + tones + [0]
77
- word2ph = [1] + word2ph + [1]
78
- return phones, tones, word2ph
79
-
80
-
81
- def _get_initials_finals(word):
82
- initials = []
83
- finals = []
84
- orig_initials = lazy_pinyin(word, neutral_tone_with_five=True, style=Style.INITIALS)
85
- orig_finals = lazy_pinyin(
86
- word, neutral_tone_with_five=True, style=Style.FINALS_TONE3
87
- )
88
- for c, v in zip(orig_initials, orig_finals):
89
- initials.append(c)
90
- finals.append(v)
91
- return initials, finals
92
-
93
-
94
- def _g2p(segments):
95
- phones_list = []
96
- tones_list = []
97
- word2ph = []
98
- for seg in segments:
99
- # Replace all English words in the sentence
100
- seg = re.sub("[a-zA-Z]+", "", seg)
101
- seg_cut = psg.lcut(seg)
102
- initials = []
103
- finals = []
104
- seg_cut = tone_modifier.pre_merge_for_modify(seg_cut)
105
- for word, pos in seg_cut:
106
- if pos == "eng":
107
- continue
108
- sub_initials, sub_finals = _get_initials_finals(word)
109
- sub_finals = tone_modifier.modified_tone(word, pos, sub_finals)
110
- initials.append(sub_initials)
111
- finals.append(sub_finals)
112
-
113
- # assert len(sub_initials) == len(sub_finals) == len(word)
114
- initials = sum(initials, [])
115
- finals = sum(finals, [])
116
- #
117
- for c, v in zip(initials, finals):
118
- raw_pinyin = c + v
119
- # NOTE: post process for pypinyin outputs
120
- # we discriminate i, ii and iii
121
- if c == v:
122
- assert c in punctuation
123
- phone = [c]
124
- tone = "0"
125
- word2ph.append(1)
126
- else:
127
- v_without_tone = v[:-1]
128
- tone = v[-1]
129
-
130
- pinyin = c + v_without_tone
131
- assert tone in "12345"
132
-
133
- if c:
134
- # 多音节
135
- v_rep_map = {
136
- "uei": "ui",
137
- "iou": "iu",
138
- "uen": "un",
139
- }
140
- if v_without_tone in v_rep_map.keys():
141
- pinyin = c + v_rep_map[v_without_tone]
142
- else:
143
- # 单音节
144
- pinyin_rep_map = {
145
- "ing": "ying",
146
- "i": "yi",
147
- "in": "yin",
148
- "u": "wu",
149
- }
150
- if pinyin in pinyin_rep_map.keys():
151
- pinyin = pinyin_rep_map[pinyin]
152
- else:
153
- single_rep_map = {
154
- "v": "yu",
155
- "e": "e",
156
- "i": "y",
157
- "u": "w",
158
- }
159
- if pinyin[0] in single_rep_map.keys():
160
- pinyin = single_rep_map[pinyin[0]] + pinyin[1:]
161
-
162
- assert pinyin in pinyin_to_symbol_map.keys(), (pinyin, seg, raw_pinyin)
163
- phone = pinyin_to_symbol_map[pinyin].split(" ")
164
- word2ph.append(len(phone))
165
-
166
- phones_list += phone
167
- tones_list += [int(tone)] * len(phone)
168
- return phones_list, tones_list, word2ph
169
-
170
-
171
- def text_normalize(text):
172
- numbers = re.findall(r"\d+(?:\.?\d+)?", text)
173
- for number in numbers:
174
- text = text.replace(number, cn2an.an2cn(number), 1)
175
- text = replace_punctuation(text)
176
- return text
177
-
178
-
179
- def get_bert_feature(text, word2ph):
180
- from text import chinese_bert
181
-
182
- return chinese_bert.get_bert_feature(text, word2ph)
183
-
184
-
185
- if __name__ == "__main__":
186
- from text.chinese_bert import get_bert_feature
187
-
188
- text = "啊!但是《原神》是由,米哈\游自主, [研发]的一款全.新开放世界.冒险游戏"
189
- text = text_normalize(text)
190
- print(text)
191
- phones, tones, word2ph = g2p(text)
192
- bert = get_bert_feature(text, word2ph)
193
-
194
- print(phones, tones, word2ph, bert.shape)
195
-
196
-
197
- # # 示例用法
198
- # text = "这是一个示例文本:,你好!这是一个测试...."
199
- # print(g2p_paddle(text)) # 输出: 这是一个示例文本你好这是一个测试