Re: Convert words to numbers?
- From: "norbert.e.fuchs@xxxxxxxxx" <norbert.e.fuchs@xxxxxxxxx>
- Date: 23 Sep 2006 01:17:39 -0700
roschler wrote:
Has anybody seen a Prolog module that converts words to numbers? For
example:
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.
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]
.
- References:
- Convert words to numbers?
- From: roschler
- Convert words to numbers?
- Prev by Date: Re: Can't compile SWI on FreeBSD
- Next by Date: Re: Can't compile SWI on FreeBSD
- Previous by thread: Convert words to numbers?
- Next by thread: Can't compile SWI on FreeBSD
- Index(es):