Watch, Follow, &
Connect with Us

Developer Tools Community.

Welcome, Guest
Guest Settings

Thread: Does anyone have code to change "forty two" to "42" ?

Replies: 6 - Last Post: Jul 20, 2017 10:56 AM Last Post By: Robert Evans
 Posts: 64 Registered: 9/23/02
 Does anyone have code to change "forty two" to "42" ? Posted: Jul 17, 2017 7:05 PM three hundred four = 304 one thousand two hundred seventy five = 1275 etc. This is for a speech recognition project I am working on. No one would ever type the above of course, but a speech recognizer would offer it. I was wondering someone has any code to do these things, either to share, or sell.
 Posts: 100 Registered: 9/18/10
 Re: Does anyone have code to change "forty two" to "42" ? Posted: Jul 17, 2017 11:27 PM   in response to: John Mitchell On 18/07/2017 05:05, John Mitchell wrote: three hundred four = 304 one thousand two hundred seventy five = 1275 etc. This is for a speech recognition project I am working on. No one would ever type the above of course, but a speech recognizer would offer it. I was wondering someone has any code to do these things, either to share, or sell. what i have is number to string function NumberToTextEnglish(Amount:currency):WideString; var Num : LongInt; Fracture : Integer; function Num2Str(Num: LongInt): String; Const hundred = 100; thousand = 1000; million = 1000000; billion = 1000000000; begin if Num >= billion then if (Num mod billion) = 0 then Num2Str := Num2Str(Num div billion) + ' Billion' else Num2Str := Num2Str(Num div billion) + ' Billion ' + Num2Str(Num mod billion) else if Num >= million then if (Num mod million) = 0 then Num2Str := Num2Str(Num div million) + ' Million' else Num2Str := Num2Str(Num div million) + ' Million ' + Num2Str(Num mod million) else if Num >= thousand then if (Num mod thousand) = 0 then Num2Str := Num2Str(Num div thousand) + ' Thousand' else Num2Str := Num2Str(Num div thousand) + ' Thousand ' + Num2Str(Num mod thousand) else if Num >= hundred then if (Num mod hundred) = 0 then Num2Str := Num2Str(Num div hundred) + ' Hundred' else Num2Str := Num2Str(Num div hundred) + ' Hundred ' + Num2Str(Num mod hundred) else case (Num div 10) of 6,7,9: if (Num mod 10) = 0 then Num2Str := Num2Str(Num div 10) + 'ty' else Num2Str := Num2Str(Num div 10) + 'ty-' + Num2Str(Num mod 10); 8: if Num = 80 then Num2Str := 'Eighty' else Num2Str := 'Eighty-' + Num2Str(Num mod 10); 5: if Num = 50 then Num2Str := 'Fifty' else Num2Str := 'Fifty-' + Num2Str(Num mod 10); 4: if Num = 40 then Num2Str := 'Forty' else Num2Str := 'Forty-' + Num2Str(Num mod 10); 3: if Num = 30 then Num2Str := 'Thirty' else Num2Str := 'Thirty-' + Num2Str(Num mod 10); 2: if Num = 20 then Num2Str := 'Twenty' else Num2Str := 'Twenty-' + Num2Str(Num mod 10); 0,1: case Num of 0: Num2Str := 'Zero'; 1: Num2Str := 'One'; 2: Num2Str := 'Two'; 3: Num2Str := 'Three'; 4: Num2Str := 'Four'; 5: Num2Str := 'Five'; 6: Num2Str := 'Six'; 7: Num2Str := 'Seven'; 8: Num2Str := 'Eight'; 9: Num2Str := 'Nine'; 10: Num2Str := 'Ten'; 11: Num2Str := 'Eleven'; 12: Num2Str := 'Twelve'; 13: Num2Str := 'Thirteen'; 14: Num2Str := 'Fourteen'; 15: Num2Str := 'Fifteen'; 16: Num2Str := 'Sixteen'; 17: Num2Str := 'Seventeen'; 18: Num2Str := 'Eightteen'; 19: Num2Str := 'Nineteen' end end end {Num2Str}; begin Num:= Trunc(Abs(Amount)); Fracture:= Round(1000*Frac(Abs(Amount))); Result := Num2Str(Num); if Fracture > 0 then Result := Result + ' and '+IntToStr(Fracture) + '/1000'; end;
 Posts: 7,731 Registered: 9/22/99
 Re: Does anyone have code to change "forty two" to "42" ? Posted: Jul 18, 2017 12:44 AM   in response to: Shlomo Abuisak shlomo abuisak wrote: This is for a speech recognition project I am working on. No one would ever type the above of course, but a speech recognizer would offer it. I was wondering someone has any code to do these things, either to share, or sell. what i have is number to string function NumberToTextEnglish(Amount:currency):WideString; That's nice, but ISTM that he needs something like EnglishTextToNum, which could be a little less easy to do. -- Rudy Velthuis http://www.rvelthuis.de "There is no reason anyone would want a computer in their home." -- Ken Olson, president, chairman and founder of Digital Equipment Corp., 1977
 Posts: 100 Registered: 9/18/10
 Re: Does anyone have code to change "forty two" to "42" ? Posted: Jul 18, 2017 8:24 AM   in response to: Rudy Velthuis (... On 18/07/2017 10:44, Rudy Velthuis (TeamB, MVP) wrote: shlomo abuisak wrote: This is for a speech recognition project I am working on. No one would ever type the above of course, but a speech recognizer would offer it. I was wondering someone has any code to do these things, either to share, or sell. what i have is number to string function NumberToTextEnglish(Amount:currency):WideString; That's nice, but ISTM that he needs something like EnglishTextToNum, which could be a little less easy to do. You are talking about parsing your text. It should be easy. Unless you need voice recognition.
 Posts: 64 Registered: 9/23/02
 Re: Does anyone have code to change "forty two" to "42" ? Posted: Jul 18, 2017 8:30 AM   in response to: Shlomo Abuisak shlomo abuisak wrote: You are talking about parsing your text. It should be easy. Unless you need voice recognition. I already have speech recognition, and I know how to parse the text. The question is if anyone has existing code that I can use to convert the English words into actual numbers. I have "Three hundred sixteen" in a string, and I want to get an integer 316.
 Posts: 114 Registered: 10/16/99
 Re: Does anyone have code to change "forty two" to "42" ? Posted: Jul 20, 2017 10:56 AM   in response to: John Mitchell In article <926634 at forums dot embarcadero dot com>, John Mitchell wrote I already have speech recognition, and I know how to parse the text. The question is if anyone has existing code that I can use to convert the English words into actual numbers. I have "Three hundred sixteen" in a string, and I want to get an integer 316. An interesting problem - and one that gets worse if you are dealing with natural speech and are unable to constrain the vocabulary quite tightly. An American English speaker might say "Three hundred sixteen" but a British English speaker is more likely to say "Three hundred and sixteen", or "Three one six". Then there is the question of "Zero" vs "Oh" vs "Nought" vs "None". And let's not forget "Double-Oh Seven" "Do you expect me to talk?" "No, Mr Bond, I expect you to DIE ^H^H^H^H speak clearly and employ a limited vocabulary!" (With apologies to Ian Fleming, Sean Connery, et al.) -- Bob Evans
 Posts: 7,731 Registered: 9/22/99
 Re: Does anyone have code to change "forty two" to "42" ? Posted: Jul 18, 2017 8:44 AM   in response to: Shlomo Abuisak shlomo abuisak wrote: That's nice, but ISTM that he needs something like EnglishTextToNum, which could be a little less easy to do. You are talking about parsing your text. It should be easy. Unless you need voice recognition. No, I am not. John Mitchell is, and he already has voice recognition. -- Rudy Velthuis http://www.rvelthuis.de "Comparing to another activity is useful if it helps you formulate questions, it's dangerous when you use it to justify answers." -- Martin Fowler
Legend