在 Mathematica 中使用单词表达数字

发布于 2024-08-04 01:48:07 字数 68 浏览 3 评论 0原文

我听说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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

另类 2024-08-11 01:48:07

基本上相当于 dreeves 解决方案的解决方案(但在他回答时不可用)是调用 WolframAlpha[] 直接来自 Mathematica(这需要互联网连接)。例如,

WolframAlpha["6 million 2 hundred and 12 thousand and fifty two", 
             {{"Input", 1}, "Plaintext"}]

返回字符串

"6212052"

因此我们可以构造以下函数来返回实际数字

textToNumber[num_String] := 
 Module[{in = WolframAlpha[num, {{"Input", 1}, "Plaintext"}]},
  If[StringMatchQ[in, NumberString], ToExpression[in], $Failed]]

它也适用于小数和负数,例如,textToNumber["minus one point one"]

请注意,我们可以要求除 "Plaintext" 输出之外的其他内容。了解可用选项的最简单方法是输入一些数字,例如WolframAlpha["12"],然后按每个“pod”右侧的 ⨁ 符号探索可用选项。还值得探索文档,您可以在其中找到有用的输出“格式”,例如” MathematicaParse""PodIDs"

我们也可以朝另一个方向走:

numberToText[num_Integer] := WolframAlpha[ToString[num], 
                               {{"NumberName", 1}, "Plaintext"}]

我找不到正确的咒语来获得非整数的口语短语形式。如果有人知道正确的咒语,或者如果 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,

WolframAlpha["6 million 2 hundred and 12 thousand and fifty two", 
             {{"Input", 1}, "Plaintext"}]

returns the string

"6212052"

So we can construct the following function that returns the actual number

textToNumber[num_String] := 
 Module[{in = WolframAlpha[num, {{"Input", 1}, "Plaintext"}]},
  If[StringMatchQ[in, NumberString], ToExpression[in], $Failed]]

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:

numberToText[num_Integer] := WolframAlpha[ToString[num], 
                               {{"NumberName", 1}, "Plaintext"}]

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.

拥抱影子 2024-08-11 01:48:07

我发现 Wolfram Alpha 可以做到这一点,所以这里有一个笨拙的小函数,它将英文字符串发送到 Wolfram Alpha 并解析结果:

w2n[s_String] := ToExpression[StringCases[
  Import["http://www.wolframalpha.com/input/?i=" <> StringReplace[s, " "->"+"],
        "String"], 
  RegularExpression["Hold\\[([^\\]]*)\\]"] -> "$1"][[1]]]

示例:

w2n["two million six hundred sixty-six"]

> 2000666

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:

w2n[s_String] := ToExpression[StringCases[
  Import["http://www.wolframalpha.com/input/?i=" <> StringReplace[s, " "->"+"],
        "String"], 
  RegularExpression["Hold\\[([^\\]]*)\\]"] -> "$1"][[1]]]

Example:

w2n["two million six hundred sixty-six"]

> 2000666

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.

半夏半凉 2024-08-11 01:48:07

这是代码:

IntegerName[78372112345]

这是输出:

780亿3.72亿11.2万345

This is the code:

IntegerName[78372112345]

This is the output:

78 billion 372 million 112 thousand 345

一片旧的回忆 2024-08-11 01:48:07

09 年不可用...

SemanticInterpretation["one hundred fifty thousand three hunded and six"]

Interpreter["SemanticNumber"]["one hundred fifty thousand three hunded and six"]

150306

(注意我的拼写错误没有将其定相..)

顺便说一句,这两个函数并不相同,

SemanticInterpretation["six and forty two thousandths"]//N  (* 6.042 *)
Interpreter["SemanticNumber"]["six and forty two thousandths"] (*fails*)

not available back in '09...

SemanticInterpretation["one hundred fifty thousand three hunded and six"]

or

Interpreter["SemanticNumber"]["one hundred fifty thousand three hunded and six"]

150306

( notice my spelling error didn't phase it..)

The two functions are not the same by the way,

SemanticInterpretation["six and forty two thousandths"]//N  (* 6.042 *)
Interpreter["SemanticNumber"]["six and forty two thousandths"] (*fails*)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文