在 Mathematica 中使用单词表达数字
我听说Mathematica中可以用英文单词来表达数字。比如用一百来表示100。哪个函数可以做到这一点?
I heard that we can use the English words to express the number in Mathematica. Like using One hundred to express 100. Which function can do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
基本上相当于 dreeves 解决方案的解决方案(但在他回答时不可用)是调用
WolframAlpha[]
直接来自 Mathematica(这需要互联网连接)。例如,返回字符串
因此我们可以构造以下函数来返回实际数字
它也适用于小数和负数,例如,
textToNumber["minus one point one"]
。请注意,我们可以要求除
"Plaintext"
输出之外的其他内容。了解可用选项的最简单方法是输入一些数字,例如WolframAlpha["12"]
,然后按每个“pod”右侧的 ⨁ 符号探索可用选项。还值得探索文档,您可以在其中找到有用的输出“格式”,例如” MathematicaParse"
和"PodIDs"
。我们也可以朝另一个方向走:
我找不到正确的咒语来获得非整数的口语短语形式。如果有人知道正确的咒语,或者如果 W|A 获得了此能力,请随时更新此答案。遗憾的是
SpokenString
没有读取数字作为其值的选项口语短语。A solution basically equivalent to dreeves's solution (but not available at the time of his answer) would be to call
WolframAlpha[]
directly from Mathematica (this requires an internet connection). For example,returns the string
So we can construct the following function that returns the actual number
It also works with decimals and negative numbers, e.g.,
textToNumber["minus one point one"]
.Note that we could ask for things other than
"Plaintext"
output. The easiest way to find out what's available is to enter some number, eg,WolframAlpha["twelve"]
, and explore the options available when you press the ⨁ signs on the right of each "pod". It is also worth exploring the documentation, where you find useful output "formats" such as"MathematicaParse"
and"PodIDs"
.We can also go in the other direction:
I couldn't find the right incantations to get the spoken phrase form for non-integers. If someone knows the right spell, or if W|A gains this ability, please feel free to update this answer. It's a shame that
SpokenString
does not have an option for reading numbers as their spoken phrases.我发现 Wolfram Alpha 可以做到这一点,所以这里有一个笨拙的小函数,它将英文字符串发送到 Wolfram Alpha 并解析结果:
示例:
Wolfram Alpha 是否提供实际的 API?那真是太棒了!
PS:他们现在有一个,但价格昂贵:http://products.wolframalpha.com/api/
PPS:我注意到 Wolframalpha 结果页面发生了一些变化,我的抓取不再有效。不过,该正则表达式的一些变体应该可以工作。
I see that Wolfram Alpha can do that, so here's a kludgy little function that sends the English string to Wolfram Alpha and parses the result:
Example:
Does Wolfram Alpha provide an actual API? That would be really great!
PS: They have one now but it's expensive: http://products.wolframalpha.com/api/
PPS: I notice that the wolframalpha results page changed a bit and my scraping no longer works. Some variant on that regular expression should work though.
这是代码:
这是输出:
This is the code:
This is the output:
09 年不可用...
或
(注意我的拼写错误没有将其定相..)
顺便说一句,这两个函数并不相同,
not available back in '09...
or
( notice my spelling error didn't phase it..)
The two functions are not the same by the way,