## Convert words to numbers?

Showing 1-3 of 3 messages
 Convert words to numbers? roschler 9/22/06 3:51 PM Has anybody seen a Prolog module that converts words to numbers?  Forexample:Nine thousand nine hundred and eighty-one -> 9,981Eighty-five -> 85I know it would be a fairly simple DCG task, but I'm hoping to save alittle time.  If you know where I can find one, then please leave aURL.Thanks. Re: Convert words to numbers? norbert...@gmail.com 9/23/06 1:17 AM I found the following DCG years ago in a newsgroup, but unfortunatelycan no longer identify its author. The DCG is limited to numbers up to999, but it should be no problem to extend it.% DCG to translate numbers up to 999 into words and vice versanumber(0)--> [zero].number(N)--> xxx(N).number(N)--> xx(N).xxx(N) --> digit(D), [hundred], rest_xxx(N1), {N is D*100 + N1}.rest_xxx(0)-->[].rest_xxx(N)-->[and], xx(N).xx(N)--> digit(N).xx(N)--> teen(N).xx(N)--> tens(T), rest_xx(N1), {N is T + N1}.rest_xx(0)--> [].rest_xx(N)--> digit(N).digit(1) --> [one].digit(2) --> [two].digit(3) --> [three].digit(4) --> [four].digit(5) --> [five].digit(6) --> [six].digit(7) --> [seven].digit(8) --> [eight].digit(9) --> [nine].teen(10) --> [ten].teen(11) --> [eleven].teen(12) --> [twelve].teen(13) --> [thirteen].teen(14) --> [fourteen].teen(15) --> [fifteen].teen(16) --> [sixteen].teen(17) --> [seventeen].teen(18) --> [eighteen].teen(19) --> [nineteen].tens(20) --> [twenty].tens(30) --> [thirty].tens(40) --> [forty].tens(50) --> [fifty].tens(60) --> [sixty].tens(70) --> [seventy].tens(80) --> [eighty].tens(90) --> [ninety].?- phrase(number(N), [one, hundred, and, twenty, seven]).N = 127?- phrase(number(127), L). L = [one, hundred, and, twenty, seven] Re: Convert words to numbers? Drew 9/30/06 10:03 AM roschler wrote:> Has anybody seen a Prolog module that converts words to numbers?>If you are reading from a stream (file), it is very possible indeed.Take this file for example:http://sc0g.com/college/CS310/assign11/ques5.txtThis prolog program reads from a file called 'ques5.data' (also locatedin that folder: http://sc0g.com/college/CS310/assign11/ques5.data) andplaces each char in a List called"CodesList".If you insert "write(CodesList)" into run:- you can notice that eachCharacter is listed as a syntax number in CodesList. Syntax 46 is aperiod -- figure out what the syntax for a (space) is, pull eachelement off of CodeList off and see if its a (space). If not, continuepulling characters off and put them in a list until you find a space.That would be a quick solution for translating words to numbers, if youwant to use a list, of course...Good luck.