Re: Is there any library for converting common English words to its equivalent numeric value in digits?



"Steven M. Haflich" <smh@xxxxxxxxxxxx> writes:

Here is an implementation of the reverse, written long ago.

This isn't very fault tolerant, takes kind of a long time to load,
takes more storage than you'd like, and doesn't work over the full
range of numbers, but has a bit of elegance in its simplicity...

(defun parse-english-number (string)
(or (gethash string
;; Might take a while (and a bit of space) to load.
;; Oh, and I recommend you do this compiled, not interpreted.
(load-time-value
(let ((table (make-hash-table :test #'equalp)))
;; Doesn't work for really huge numbers
(dotimes (i 1000000)
(setf (gethash (format nil "~R" i) table) i)) table)
nil))
(error "Can't parse English number: ~A" string)))

This one is even shorter, uses less space, works over a more general
range, but runs a bit slower in the worst case (not too bad in the
best case, though):

(defun parse-english-number (string)
(loop for i from 0 do
(when (equal string (format nil "~R" i))
(return i))))

[There's a trivial variant of this that would find negatives, too.]

(No, these are not serious suggestions.)
.