Names to numbers: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(2 intermediate revisions by 2 users not shown)
Line 10:
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">V Names = [‘one’ = Int64(1),
‘two’ = 2,
‘three’ = 3,
Line 93:
 
L(name) names
print(‘#20’.format(nameToNum(name))‘ = ’name)</langsyntaxhighlight>
 
{{out}}
Line 116:
=={{header|Common Lisp}}==
A counterpart to (format t "~R" ...).
<langsyntaxhighlight Lisplang="lisp">(defpackage number-names
(:use cl))
 
Line 310:
(format t "~a => ~a => ~a~%" number word (parse word))))
test-numbers))
(values))</langsyntaxhighlight>
Running the test procedure:
<langsyntaxhighlight lang="none">CL-USER> (number-names::test)
number => (format t "~R" number) => (parse (format t "~R" number))
0 => zero => 0
Line 337:
-2 => negative two => -2
0 => zero => 0
; No value</langsyntaxhighlight>
 
=={{header|D}}==
This uses the D module from the Number names task.
{{trans|Python}}
<langsyntaxhighlight lang="d">import std.stdio, std.array, std.string, std.algorithm, std.bigint,
std.range, number_names;
 
Line 408:
auto num = txt.bigIntFromWords;
writefln("%12d <%s> %s", n, (n == num) ? "==" : "??", txt);
}</langsyntaxhighlight>
{{out}}
<pre>This shows <==> for a successful round trip, <??> otherwise:
Line 440:
{{libheader| System.Math}}
{{Trans|Kotlin}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Names_to_numbers;
 
Line 586:
Names.free;
Readln;
end.</langsyntaxhighlight>
{{out}}
<pre> 0 = none
Line 605:
=={{header|Factor}}==
This solution parses number names in the same format that Factor's existing <code>number>text</code> word outputs. Meaning that for numbers under 10^66, <code>number>text</code> and <code>text>number</code> ought to be inverses of each other.
<langsyntaxhighlight lang="factor">USING: arrays formatting grouping kernel math math.functions
math.parser multiline peg peg.ebnf sequences sequences.deep ;
 
Line 737:
[ dup text>number "%s => %u\n" printf ] each ;
 
names-to-numbers-demo</langsyntaxhighlight>
{{out}}
<pre>
Line 766:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 908:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 931:
=={{header|Haskell}}==
{{trans|Common Lisp}}
<langsyntaxhighlight Haskelllang="haskell">import Data.Char (toLower)
 
type Symbol = (String, Integer)
Line 1,052:
("zero":[]) -> 0
("negative":wx') -> negate $ parse illions wx'
wx' -> parse illions wx'</langsyntaxhighlight>
 
=={{header|J}}==
Define the verb usinv to convert number names to numbers.
File number_names.ijs contains the code of [[Number_names#J|number names project]].
<syntaxhighlight lang="j">
<lang J>
NB. standard profile defines tolower and delete extra blanks.
load'number_names.ijs'
Line 1,075:
M +/ .*+/"1 ,"2 (([: (* 100 ^ 2 ~: #) (#EN100)|EN100&i.)&>)D
)
</syntaxhighlight>
</lang>
<pre>
(-: [&.(us :.usinv))0
Line 1,090:
'''Works with gojq, the Go implementation of jq'''
 
<langsyntaxhighlight lang="jq">def check(cond; msg):
if cond then . else msg | error end;
 
Line 1,191:
 
tests[]
| "\(nameToNum|lpad(17)) = \(.)"</langsyntaxhighlight>
{{out}}
Exactly as for [[#Wren|Wren]].
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">const stext = ["one", "two", "three", "four", "five",
"six", "seven", "eight", "nine"]
const teentext = ["eleven", "twelve", "thirteen", "fourteen",
Line 1,308:
println(txt, " => ", texttointeger(txt))
end
</langsyntaxhighlight>{{out}}
<pre>
One Hundred and One Dalmatians => 101 Dalmatians
Line 1,334:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.2
 
val names = mapOf<String, Long>(
Line 1,441:
)
for (name in names) println("${"%20d".format(nameToNum(name))} = $name")
}</langsyntaxhighlight>
 
{{out}}
Line 1,464:
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">import re, sequtils, strformat, strutils, tables
 
const Names = {"one": 1u64,
Line 1,572:
 
for name in names:
echo ($nametoNum(name)).align(20), " = ", name</langsyntaxhighlight>
 
{{out}}
Line 1,592:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
use utf8;
binmode STDOUT, ':utf8';
 
my $phrases_with_numbers = <<'END';
Line 1,706 ⟶ 1,708:
 
say $_ . ' --> ' . numify($_) for grep { $_ } split "\n", $phrases_with_numbers;
say $_ . ' --> ' . comma numify($_) for grep { $_ } split "\n", $pure_numbers;</langsyntaxhighlight>
<pre>One Hundred and One Dalmatians --> 101 dalmatians
Two Thousand and One: A Space Odyssey --> 2001 : a space odyssey
Line 1,733 ⟶ 1,735:
=={{header|Phix}}==
Uses [[Number_names#Phix|Number_names]] as an executable library.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Names_to_numbers.exw
Line 1,803 ⟶ 1,805:
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
{{Out}}
<pre style="font-size: 8px">
Line 1,849 ⟶ 1,851:
 
<small>Note: This example and [[Number_names#Python]] need to be kept in sync</small>
<langsyntaxhighlight lang="python">from spell_integer import spell_integer, SMALL, TENS, HUGE
 
def int_from_words(num):
Line 1,901 ⟶ 1,903:
num = int_from_words(txt)
print('%12i <%s> %s' % (n, '==' if n == num else '??', txt))
print('')</langsyntaxhighlight>
{{out}}
<pre>##
Line 1,941 ⟶ 1,943:
Finally, the compiled nest is executed with <code>do</code>.
 
<langsyntaxhighlight Quackerylang="quackery"> [ 1 + ] is one ( --> n )
[ 2 + ] is two ( --> n )
[ 3 + ] is three ( --> n )
Line 1,994 ⟶ 1,996:
 
$ "one billion, two hundred and thirty four million, five hundred and sixty seven thousand, eight hundred and ninety"
name->number echo</langsyntaxhighlight>
 
{{out}}
Line 2,004 ⟶ 2,006:
Extension of the [[Number_names#Racket | number to names]] code:
 
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 2,069 ⟶ 2,071:
(printf "~s <-> ~a\n" n eng)
(printf "Fail: ~s -> ~a -> ~s\n" n eng n2)))
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,088 ⟶ 2,090:
(formerly Perl 6)
{{trans|Perl}}
<syntaxhighlight lang="raku" perl6line>my $phrases-with-numbers = q:to/END/;
One Hundred and One Dalmatians
Two Thousand and One: A Space Odyssey
Line 2,179 ⟶ 2,181:
 
say $_ ~ ' --> ' ~ .&numify for $phrases-with-numbers.split("\n").grep: *.so;
say $_ ~ ' --> ' ~ .&numify.&comma for $pure-numbers.split("\n").grep: *.so;</langsyntaxhighlight>
{{out}}
<pre>One Hundred and One Dalmatians --> 101 dalmatians
Line 2,208 ⟶ 2,210:
This solution uses "Number names" from [[Number_names#Ruby | here]]
{{trans|Python}}
<langsyntaxhighlight lang="ruby">require 'number_names'
 
def int_from_words(num)
Line 2,238 ⟶ 2,240:
end
negmult * (total + small)
end</langsyntaxhighlight>
 
Examples:
<langsyntaxhighlight lang="ruby">for n in (-10000..10000).step(17)
raise unless n == int_from_words(wordify(n))
end
Line 2,264 ⟶ 2,266:
break if n==0
n /= -10
end</langsyntaxhighlight>
 
{{out}}
Line 2,299 ⟶ 2,301:
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">func names_to_number(str) {
 
static nums = Hash.new(
Line 2,369 ⟶ 2,371:
str.gsub!(/\b(\d{2}) (\d{2})\b/, {|a,b| (a.to_i * 100) + b.to_i});
str.gsub!(regex[10], {|a| a.split(' ').map{.to_i}.sum });
}</langsyntaxhighlight>
 
Usage:
<langsyntaxhighlight lang="ruby">ARGF.each { |line|
say "#{line.chomp.dump} --> #{names_to_number(line).dump}";
}</langsyntaxhighlight>
 
Sample:
Line 2,394 ⟶ 2,396:
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
proc name2num name {
set words [regexp -all -inline {[a-z]+} [string tolower $name]]
Line 2,445 ⟶ 2,447:
}
return [expr {$sign * [tcl::mathop::+ {*}$groups]}]
}</langsyntaxhighlight>
Demonstrating/testing (based on [[#Perl|Perl]] code's samples):
<langsyntaxhighlight lang="tcl">set samples {
"Seventy-two dollars"
"Seventy two dollars"
Line 2,488 ⟶ 2,490:
foreach s $samples {
puts "$s => [name2num $s]"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,536 ⟶ 2,538:
{{libheader|Wren-fmt}}
Wren's built in Num type can only deal accurately with integers up to 16 digits long (plus or minus 2 ^ 53) so without resorting to BigInt (which would be overkill here) we can only deal with numbers up to nine quadrillion or so.
<langsyntaxhighlight ecmascriptlang="wren">import "./str" for Str
import "./pattern" for Pattern
import "./fmt" for Fmt
var names = {
"one": 1,
Line 2,635 ⟶ 2,637:
"minus nine quadrillion, one hundred thirty-seven"
]
for (name in tests) Fmt.print("$17d = $s", nameToNum.call(name), name)</langsyntaxhighlight>
 
{{out}}
Line 2,656 ⟶ 2,658:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">var names=T("zero","one","two","three","four","five","six","seven","eight",
"nine","ten","eleven","twelve","thirteen","fourteen","fifteen",
"sixteen","seventeen","eighteen","nineteen","twenty",
Line 2,685 ⟶ 2,687:
}
minus*(total + hund)
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">foreach s in (T("eighty-five thousand and one ",
" one hundred and fifty-five thousand and nineteen",
"one thousand, nine hundred and eighty-four","Nineteen Eighty-Four",
Line 2,695 ⟶ 2,697:
"six million, seven hundred and sixty-six thousand and twenty-seven")){
println("\"%s\" is %,d".fmt(s,stringToNumber(s)));
}</langsyntaxhighlight>
{{out}}
<pre>
9,487

edits