单词到数字的转换 [英] words to number convertion

查看:94
本文介绍了单词到数字的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hai

我目前正在从事聊天机器人项目.
现在我想做一些数学计算.
为此,我需要识别用户给定的单词数字(例如:七十七),然后将其转换为数字(77)进行处理.
对于如此大量的数字,我该怎么做呢?是否有任何工具或代码可用于执行此操作?
即将单词转换为数字.
plz help

hai

i am currently working on a chatbot project.
now i would like to do perform some mathematical calculation.
for that i need to recognize the user given number in words (eg: seventy seven) and then convert it to number (77) for processing.
how could i do that for such large set of numbers ? is there any tool or codes available to do this?
ie converting words to number.
plz help

推荐答案

我在Google中找到了这个

如何将单词转换为等效数字? [将单词转换为PHP中的数字 [有没有办法将数字单词转换为整数? Python [ ^ ]
I found this in Google

How to convert words to equivalent numbers?[^]

Also found in Python & PHP(You can convert into C#)
Converting words to numbers in PHP[^]
Is there a way to Convert Number words to Integers? Python[^]


这是一个很好的解决方案:
使用System.Collections;
受保护的void button1_Click(对象发送者,EventArgs e)
{
Hashtable hashTable = new Hashtable();
hashTable.Add("one","1");
hashTable.Add("two","2");
hashTable.Add(三个","3");
hashTable.Add("four","4");
hashTable.Add("five","5");
hashTable.Add("six","6");
hashTable.Add("seven","7");
hashTable.Add("eight","8");
hashTable.Add("nine","9");
hashTable.Add("ten","10");
hashTable.Add("eleven","11");
hashTable.Add("twelve","12");
hashTable.Add(十三","13");
hashTable.Add(十四","14");
hashTable.Add("fifteen","15");
hashTable.Add("sixteen","16");
hashTable.Add("seventeen","17");
hashTable.Add("eighteen","18");
hashTable.Add("nineteen","19");
hashTable.Add("twenty","20");
hashTable.Add("thirty","30");
hashTable.Add("fourty","40");
hashTable.Add("fifty","50");
hashTable.Add("sixty","60");
hashTable.Add("seventy","70");
hashTable.Add(八十八","80");
hashTable.Add("ninety","90");
hashTable.Add("hundred","100");
hashTable.Add(千","1000");
hashTable.Add("lakh","100000");
hashTable.Add("crore","10000000");
字符串wordValue = TextBox1.Text.Trim().ToLower();
string [] strArray = wordValue.Split('''');
int值= 0;
int tempValue = 0;
foreach(strArray中的字符串s)
{
if(object.Equals(s,数百")|| object.Equals(s,千")|| object.Equals(s,"lakh")|| object.Equals(s,"crore")))
{
值+ = tempValue * int.Parse(hashTable [s] .ToString());
tempValue = 0;
}
其他
{
如果(hashTable.ContainsKey(s))
{
tempValue + = int.Parse(hashTable [s] .ToString());
}
}
}
TextBox1.Text =(值+ tempValue).ToString();
}
ya thats a very good solution :
using System.Collections;
protected void button1_Click(object sender, EventArgs e)
{
Hashtable hashTable = new Hashtable();
hashTable.Add("one", "1");
hashTable.Add("two", "2");
hashTable.Add("three", "3");
hashTable.Add("four", "4");
hashTable.Add("five", "5");
hashTable.Add("six", "6");
hashTable.Add("seven", "7");
hashTable.Add("eight", "8");
hashTable.Add("nine", "9");
hashTable.Add("ten", "10");
hashTable.Add("eleven", "11");
hashTable.Add("twelve", "12");
hashTable.Add("thirteen", "13");
hashTable.Add("fourteen", "14");
hashTable.Add("fifteen", "15");
hashTable.Add("sixteen", "16");
hashTable.Add("seventeen", "17");
hashTable.Add("eighteen", "18");
hashTable.Add("nineteen", "19");
hashTable.Add("twenty", "20");
hashTable.Add("thirty", "30");
hashTable.Add("fourty", "40");
hashTable.Add("fifty", "50");
hashTable.Add("sixty", "60");
hashTable.Add("seventy", "70");
hashTable.Add("eighty", "80");
hashTable.Add("ninety", "90");
hashTable.Add("hundred", "100");
hashTable.Add("thousand", "1000");
hashTable.Add("lakh", "100000");
hashTable.Add("crore", "10000000");
string wordValue = TextBox1.Text.Trim().ToLower();
string[] strArray = wordValue.Split('' '');
int value = 0;
int tempValue = 0;
foreach (string s in strArray)
{
if (object.Equals(s, "hundred") || object.Equals(s, "thousand") || object.Equals(s, "lakh") || object.Equals(s, "crore"))
{
value += tempValue * int.Parse(hashTable[s].ToString());
tempValue = 0;
}
else
{
if (hashTable.ContainsKey(s))
{
tempValue += int.Parse(hashTable[s].ToString());
}
}
}
TextBox1.Text = (value + tempValue).ToString();
}


这篇关于单词到数字的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆