MCPcopy Create free account
hub / github.com/MoonInTheRiver/DiffSinger / chn2num

Function chn2num

utils/text_norm.py:233–316  ·  view source on GitHub ↗
(chinese_string, numbering_type=NUMBERING_TYPES[1])

Source from the content-addressed store, hash-verified

231
232
233def chn2num(chinese_string, numbering_type=NUMBERING_TYPES[1]):
234 def get_symbol(char, system):
235 for u in system.units:
236 if char in [u.traditional, u.simplified, u.big_s, u.big_t]:
237 return u
238 for d in system.digits:
239 if char in [d.traditional, d.simplified, d.big_s, d.big_t, d.alt_s, d.alt_t]:
240 return d
241 for m in system.math:
242 if char in [m.traditional, m.simplified]:
243 return m
244
245 def string2symbols(chinese_string, system):
246 int_string, dec_string = chinese_string, ''
247 for p in [system.math.point.simplified, system.math.point.traditional]:
248 if p in chinese_string:
249 int_string, dec_string = chinese_string.split(p)
250 break
251 return [get_symbol(c, system) for c in int_string], \
252 [get_symbol(c, system) for c in dec_string]
253
254 def correct_symbols(integer_symbols, system):
255 """
256 一百八 to 一百八十
257 一亿一千三百万 to 一亿 一千万 三百万
258 """
259
260 if integer_symbols and isinstance(integer_symbols[0], CNU):
261 if integer_symbols[0].power == 1:
262 integer_symbols = [system.digits[1]] + integer_symbols
263
264 if len(integer_symbols) > 1:
265 if isinstance(integer_symbols[-1], CND) and isinstance(integer_symbols[-2], CNU):
266 integer_symbols.append(
267 CNU(integer_symbols[-2].power - 1, None, None, None, None))
268
269 result = []
270 unit_count = 0
271 for s in integer_symbols:
272 if isinstance(s, CND):
273 result.append(s)
274 unit_count = 0
275 elif isinstance(s, CNU):
276 current_unit = CNU(s.power, None, None, None, None)
277 unit_count += 1
278
279 if unit_count == 1:
280 result.append(current_unit)
281 elif unit_count > 1:
282 for i in range(len(result)):
283 if isinstance(result[-i - 1], CNU) and result[-i - 1].power < current_unit.power:
284 result[-i - 1] = CNU(result[-i - 1].power +
285 current_unit.power, None, None, None, None)
286 return result
287
288 def compute_value(integer_symbols):
289 """
290 Compute the value.

Callers 3

chntext2cardinalMethod · 0.85
chntext2fractionMethod · 0.85
chntext2percentageMethod · 0.85

Calls 4

create_systemFunction · 0.85
string2symbolsFunction · 0.85
correct_symbolsFunction · 0.85
compute_valueFunction · 0.85

Tested by

no test coverage detected