You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
2.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# -*- coding: utf-8 -*-
"""
Created on Fri Apr 18 08:47:33 2025
@author: 缄默
"""
def Snt2Int(text):
# 定义数字词与值的映射
number_words = {
'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5,'six': 6, 'seven': 7, 'eight': 8, 'nine': 9, 'ten': 10,
'eleven': 11, 'twelve': 12, 'thirteen': 13, 'fourteen': 14, 'fifteen': 15, 'sixteen': 16, 'seventeen': 17, 'eighteen': 18,'nineteen': 19,
'twenty': 20, 'thirty': 30, 'forty': 40,'fifty': 50, 'sixty': 60, 'seventy': 70, 'eighty': 80, 'ninety': 90
}
units = {'hundred': 100, 'thousand': 1000, 'million': 1000000, 'billion': 1000000000}
words = text.replace('-', ' ').split()
result = []
current_sequence = []
for i in range(len(words)):
# 判断是否为数字词或单位词忽略“and”
word = words[i].lower()
if word == "a":
if i+1<len(words) and words[i+1] in units:
current_sequence.append('one')
else:
result.append(words[i])
elif word in number_words or word in units:
current_sequence.append(word)
else:
if current_sequence:
result.append(Seq2Int(current_sequence, number_words, units))
current_sequence = []
result.append(words[i])
# 处理最后的数字序列
if current_sequence:
result.append(Seq2Int(current_sequence, number_words, units))
return ' '.join(map(str, result))
def Seq2Int(sequence, number_words, units):
has_units = any(word in units for word in sequence)
if has_units:
total = 0
current_value = 0
for word in sequence:
if word in units:
unit = units[word]
if unit == 100:
current_value *= unit
total += current_value
current_value = 0
elif unit in (1000, 1000000000):
total += current_value
total *= unit
current_value = 0
else:
current_value += number_words[word]
total += current_value
return total
else:
return int(''.join(str(number_words[word]) for word in sequence))