pyttsx3 字符串到数值
pyttsx3 string to numeric values
所以我正在尝试将口头字母数字字符串转换为可用变量。示例是口头 IP 地址。
例如:
string(one nine two period one six eight period zero period one slash twenty four)
把它变成
192.168.000.001/24
我了解一些字符串格式,但这超出了我的知识范围。我想我可以把它变成一本字典。使用变量并将字母数字值与数值进行比较。我试图在没有 google 服务的情况下执行此操作,因为它有 phone 号码和地址,但不是 IP 地址之类的东西。
感谢任何帮助。
如果我理解正确的话,你需要把一串拼写出来的字符(由空格分隔)变成一串字符本身。如果是这样,我认为这应该可以解决问题:
def convert_spelled_chars(string:str, mapping:dict):
# split at whitespace
tokens = string.split()
# map to values
chars = [str(mapping[t]) for t in tokens]
# join into one string again
result = "".join(chars)
return result
它接收字符串,returns IP 地址的字符串形式(我假设您想要它作为字符串?)。然后可以通过以下方式使用:
mapping = {
"one": 1,
"two": 2,
"slash": "/"
}
print(convert_spelled_chars("one two slash two one", mapping))
#> "12/21"
它假设您知道所有可能出现的口语字符,如果您仅将它用于 IP 地址,这似乎是一个合理的假设。
I TAKE NO CREDIT OF THIS ANSWER. FULL CREDITS GOES TO recursive WHO PROVIDED THIS BEAUTIFUL ANSWER IN THIS thread.
def text2int(textnum, numwords={}):
if not numwords:
units = [
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
"nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen",
]
tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
scales = ["hundred", "thousand", "million", "billion", "trillion"]
numwords["and"] = (1, 0)
for idx, word in enumerate(units): numwords[word] = (1, idx)
for idx, word in enumerate(tens): numwords[word] = (1, idx * 10)
for idx, word in enumerate(scales): numwords[word] = (10 ** (idx * 3 or 2), 0)
current = result = 0
for word in textnum.split():
if word not in numwords:
raise Exception("Illegal word: " + word)
scale, increment = numwords[word]
current = current * scale + increment
if scale > 100:
result += current
current = 0
return result + current
print text2int("seven billion one hundred million thirty one thousand three hundred thirty seven")
#7100031337
所以我正在尝试将口头字母数字字符串转换为可用变量。示例是口头 IP 地址。 例如:
string(one nine two period one six eight period zero period one slash twenty four)
把它变成
192.168.000.001/24
我了解一些字符串格式,但这超出了我的知识范围。我想我可以把它变成一本字典。使用变量并将字母数字值与数值进行比较。我试图在没有 google 服务的情况下执行此操作,因为它有 phone 号码和地址,但不是 IP 地址之类的东西。
感谢任何帮助。
如果我理解正确的话,你需要把一串拼写出来的字符(由空格分隔)变成一串字符本身。如果是这样,我认为这应该可以解决问题:
def convert_spelled_chars(string:str, mapping:dict):
# split at whitespace
tokens = string.split()
# map to values
chars = [str(mapping[t]) for t in tokens]
# join into one string again
result = "".join(chars)
return result
它接收字符串,returns IP 地址的字符串形式(我假设您想要它作为字符串?)。然后可以通过以下方式使用:
mapping = {
"one": 1,
"two": 2,
"slash": "/"
}
print(convert_spelled_chars("one two slash two one", mapping))
#> "12/21"
它假设您知道所有可能出现的口语字符,如果您仅将它用于 IP 地址,这似乎是一个合理的假设。
I TAKE NO CREDIT OF THIS ANSWER. FULL CREDITS GOES TO recursive WHO PROVIDED THIS BEAUTIFUL ANSWER IN THIS thread.
def text2int(textnum, numwords={}):
if not numwords:
units = [
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
"nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen",
]
tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
scales = ["hundred", "thousand", "million", "billion", "trillion"]
numwords["and"] = (1, 0)
for idx, word in enumerate(units): numwords[word] = (1, idx)
for idx, word in enumerate(tens): numwords[word] = (1, idx * 10)
for idx, word in enumerate(scales): numwords[word] = (10 ** (idx * 3 or 2), 0)
current = result = 0
for word in textnum.split():
if word not in numwords:
raise Exception("Illegal word: " + word)
scale, increment = numwords[word]
current = current * scale + increment
if scale > 100:
result += current
current = 0
return result + current
print text2int("seven billion one hundred million thirty one thousand three hundred thirty seven")
#7100031337