Convert words to numbers and back?

J

Jordon Bedwell

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?
 
S

saladin.mundi

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" <[email protected]>
To: "ruby-talk ML" <[email protected]>
Sent: Saturday, November 24, 2007 2:05 PM
Subject: Re: Convert words to numbers and back?
 
A

Andrei Maxim

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

I think the OP wants to know how he could turn 3 into "three" or 2001 into
"two thousand and one".

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" <[email protected]>
To: "ruby-talk ML" <[email protected]>
Sent: Saturday, November 24, 2007 2:05 PM
Subject: Re: Convert words to numbers and back?
 
R

Robert Klemme

2007/11/24 said:
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
 
A

Andrei Maxim

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

Hm, I'm pretty sure I meant " turn 3 into "three" or "two thousand and one"
into 2001". :)
 
R

Robert Klemme

2007/11/24 said:
Hm, I'm pretty sure I meant " turn 3 into "three" or "two thousand and one"
into 2001". :)

You are not the OP.

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ see what I mean?

robert
 
J

Jordon Bedwell

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.
 
P

Phrogz

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]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top