Nine thousand nine hundred and eighty-one -> 9,981
Eighty-five -> 85
I know it would be a fairly simple DCG task, but I'm hoping to save a
little time. If you know where I can find one, then please leave a
URL.
Thanks.
I found the following DCG years ago in a newsgroup, but unfortunately
can no longer identify its author. The DCG is limited to numbers up to
999, but it should be no problem to extend it.
% DCG to translate numbers up to 999 into words and vice versa
number(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]
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.txt
This prolog program reads from a file called 'ques5.data' (also located
in that folder: http://sc0g.com/college/CS310/assign11/ques5.data) and
places each char in a List called
"CodesList".
If you insert "write(CodesList)" into run:- you can notice that each
Character is listed as a syntax number in CodesList. Syntax 46 is a
period -- figure out what the syntax for a (space) is, pull each
element off of CodeList off and see if its a (space). If not, continue
pulling 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 you
want to use a list, of course...
Good luck.