I was wondering if somebody could give me some insight and help on how to convert words to numbers and back again. Maybe a function?
on 2007-11-24 13:21
on 2007-11-24 14:05
Check http://www.deveiate.org/projects/Linguistics/ I think numwords is what you are looking for.
on 2007-11-24 14:11
I don't know exactly what you mean. Do you have something like: "123" <- a String which you want to conert into an integer? like 123 ? if so, there are these functions: str.to_i(base=10) => integer Returns the result of interpreting leading characters in str as an integer base base (2, 8, 10, or 16). Extraneous characters past the end of a valid number are ignored. If there is not a valid number at the start of str, 0 is returned. This method never raises an exception. "12345".to_i #=> 12345 "99 red balloons".to_i #=> 99 "0a".to_i #=> 0 "0a".to_i(16) #=> 10 "hello".to_i #=> 0 "1100101".to_i(2) #=> 101 "1100101".to_i(8) #=> 294977 "1100101".to_i(10) #=> 1100101 "1100101".to_i(16) #=> 17826049 orstr.to_f => floatReturns the result of interpreting leading characters in str as a floating point number. Extraneous characters past the end of a valid number are ignored. If there is not a valid number at the start of str, 0.0 is returned. This method never raises an exception. "123.45e1".to_f #=> 1234.5 "45.67 degrees".to_f #=> 45.67 "thx1138".to_f #=> 0.0 bye sala----- Original Message ----- From: "Andrei Maxim" <andrei@andreimaxim.ro> To: "ruby-talk ML" <ruby-talk@ruby-lang.org> Sent: Saturday, November 24, 2007 2:05 PM Subject: Re: Convert words to numbers and back?
on 2007-11-24 14:22
I think the OP wants to know how he could turn 3 into "three" or 2001 into "two thousand and one".
on 2007-11-24 14:48
2007/11/24, Andrei Maxim <andrei@andreimaxim.ro>: > I think the OP wants to know how he could turn 3 into "three" or 2001 into > "two thousand and one". I could also imagine that the OP wants conversion of char sequences to a single number (see below). Amazing: three replies and three different interpretations of the question: 1. convert a textual number description into a number object 2. convert a string representation of a number into a number object 3. encode a char sequence in a single number (reminds me faintly of Goedel numbers) I'd say 1 and 3 can be matched to the OP's question, while 2 clearly cannot with 1 having the highest likelyhood to match the actual requirements. Jordon, what is it that you are looking for? Kind regards robert
on 2007-11-24 15:06
Hm, I'm pretty sure I meant " turn 3 into "three" or "two thousand and one" into 2001". :)
on 2007-11-24 16:58
2007/11/24, Andrei Maxim <andrei@andreimaxim.ro>: > Hm, I'm pretty sure I meant " turn 3 into "three" or "two thousand and one" > into 2001". :) You are not the OP. > > Amazing: three replies and three different interpretations of the > > cannot with 1 having the highest likelyhood to match the actual > > requirements. > > > > Jordon, what is it that you are looking for? ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ see what I mean? robert
on 2007-11-24 22:57
On 11/24/07, Robert Klemme <shortcutter@googlemail.com> wrote: > > 2007/11/24, Andrei Maxim <andrei@andreimaxim.ro>: > > Hm, I'm pretty sure I meant " turn 3 into "three" or "two thousand and > one" > > into 2001". :) > > You are not the OP. > Gee, isn't that a surprise.
on 2007-11-25 08:03
What I need to do is convert "22/11/2007" to
"twenty-two/eleven/two-thousand-seven" and then convert it back to
"22/11/2007", what I will be doing is building a plugin for Mephisto to
highjack my archive URIs to words over numbers and then converting it
back to Mephisto when it comes back in. Out as word in as number. That
way no major changes have to be made to the backend itself. It doesn't
have to add in the dashes since I can simply do that with split (" ")
and join ("-"). I did look into Linguistics but it is a one way
conversion, I need 2 way conversion. Thanks.
on 2007-11-25 17:05
On Nov 25, 12:03 am, Jordon Bedwell <jor...@envygeeks.com> wrote: > What I need to do is convert "22/11/2007" to > "twenty-two/eleven/two-thousand-seven" and then convert it back to > "22/11/2007", what I will be doing is building a plugin for Mephisto to > highjack my archive URIs to words over numbers and then converting it > back to Mephisto when it comes back in. Out as word in as number. That > way no major changes have to be made to the backend itself. It doesn't > have to add in the dashes since I can simply do that with split (" ") > and join ("-"). I did look into Linguistics but it is a one way > conversion, I need 2 way conversion. Thanks. For 7 -> "seven" see: http://www.rubyquiz.com/quiz25.html The reverse would make a fun problem to solve, too. Here, let me get you started: ENGLISH_VALUE = {} %w| zero one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen |.each_with_index{ |word,i| ENGLISH_VALUE[word] = i } %w| zero ten twenty thirty forty fifty sixty seventy eighty ninety|.each_with_index{ |word,i| ENGLISH_VALUE[word] = i*10 } ENGLISH_VALUE['hundred'] = 100 %w| one thousand million billion trillion|.each_with_index{ |word,i| ENGLISH_VALUE[word] = 10**(i*3) } class Integer def self.from_english( words ) values = words.downcase.split( /\W+/ ).map{ |word| ENGLISH_VALUE[word] } # Put your magic here end end TESTS = { 'one'=>1, 'seventy three'=>73, 'ninety nine'=>99, 'one hundred'=>100, 'one hundred one'=>101, 'one hundred twenty'=>120, 'three hundred sixty four'=>364, 'eight thousand five'=>8_005, 'forty-three thousand twelve'=>43_012, 'two billion one hundred thousand seventeen'=>2_000_100_117 } TESTS.each{ |word,expected_value| actual_value = Integer.from_english( word ) unless actual_value == expected_value warn "From '#{word}', " << "expected: #{expected_value}, " << "actual: #{actual_value.inspect}" end } #=> From 'forty-three thousand twelve', expected: 43012, actual: [40, 3, 1000, 12] #=> From 'one hundred one', expected: 101, actual: [1, 100, 1] #=> From 'ninety nine', expected: 99, actual: [90, 9] #=> From 'one', expected: 1, actual: [1] #=> From 'eight thousand five', expected: 8005, actual: [8, 1000, 5] #=> From 'one hundred twenty', expected: 120, actual: [1, 100, 20] #=> From 'two billion one hundred thousand seventeen', expected: 2000100117, actual: [2, 1000000000, 1, 100, 1000, 17] #=> From 'three hundred sixty four', expected: 364, actual: [3, 100, 60, 4] #=> From 'one hundred', expected: 100, actual: [1, 100] #=> From 'seventy three', expected: 73, actual: [70, 3]
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.