我正在尝试将写为单词的数值转换为整数.例如,iPhone有二十三万七百八十三个应用程序"会成为iPhone 作为 230783 个应用程序"
I am trying to convert numerical values written as words into integers. For example, "iPhone has two hundred and thirty thousand seven hundred and eighty three apps" would become "iPhone as 230783 apps"
在我开始编码之前,我想知道是否存在任何用于此转换的函数/代码.
Before i start coding, I would like to know if any function / code exists for this conversion.
有很多页面讨论从数字到单词的转换.反方向没有那么多.我能找到的最好的是 Ask Yahoo 上的一些伪代码.请参阅 http://answers.yahoo.com/question/index?qid=20090216103754AAONnDz 一个不错的算法:
There are lots of pages discussing the conversion from numbers to words. Not so many for the reverse direction. The best I could find was some pseudo-code on Ask Yahoo. See http://answers.yahoo.com/question/index?qid=20090216103754AAONnDz for a nice algorithm:
嗯,总的来说,您要做两件事:查找标记(转换为数字的单词)和应用语法.简而言之,您正在为一种非常有限的语言构建解析器.
Well, overall you are doing two things: Finding tokens (words that translates to numbers) and applying grammar. In short, you are building a parser for a very limited language.
您需要的令牌是:
功率:千、百万、十亿
百:百
十:二十、三十……九十
单位:一、二、三、...九、
特殊:十、十一、十二、……十九
POWER: thousand, million, billion
HUNDRED: hundred
TEN: twenty, thirty... ninety
UNIT: one, two, three, ... nine,
SPECIAL: ten, eleven, twelve, ... nineteen
(删除任何和",因为它们没有意义.将连字符分成两个标记.即 65 应该被处理为六十"五")
(drop any "and"s as they are meaningless. Break hyphens into two tokens. That is sixty-five should be processed as "sixty" "five")
标记字符串后,从右移到左.
Once you've tokenized your string, move from RIGHT TO LEFT.
从 RIGHT 中抓取所有标记,直到您击中 POWER 或整个字符串.
Grab all the tokens from the RIGHT until you hit a POWER or the whole string.
解析这些模式的停止点之后的标记:
Parse the tokens after the stop point for these patterns:
特别
十个
单位
十个单位
单位百
单位百特
单位 10
单位百单位
单位一百十单位
SPECIAL
TEN
UNIT
TEN UNIT
UNIT HUNDRED
UNIT HUNDRED SPECIAL
UNIT HUNDRED TEN
UNIT HUNDRED UNIT
UNIT HUNDRED TEN UNIT
(假设此语法中不允许使用一千七百")
(This assumes that "seventeen hundred" is not allowed in this grammar)
这将为您提供号码的最后三位.
This gives you the last three digits of your number.
如果你停在整个字符串,你就完成了.
If you stopped at the whole string you are done.
如果你停在一个功率,从第 1 步重新开始,直到你达到更高的功率或整个字符串.
If you stopped at a power, start again at step 1 until you reach a higher POWER or the whole string.
这篇关于在 PHP 中将单词转换为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!