Debian Codegraph @

hindi_scm/hindi_token.scm

raw
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;; ;;
3 ;;; Rules to map tokens to words ;;
4 ;;; ;;
5 ;;; Copyright (c) 2006, Priti Patil, janabhaaratii, C-DAC, Mumbai ;;
6 ;;; <[email protected]>, <[email protected]> ;;
7 ;;; ;;
8 ;;; Copyright (c) 2005, Chaitanya Kamisetty <[email protected]> ;;
9 ;;; ;;
10 ;;; This program is a part of festival-hi. ;;
11 ;;; ;;
12 ;;; festival-hi is free software; you can redistribute it and/or modify ;;
13 ;;; it under the terms of the GNU General Public License as published by ;;
14 ;;; the Free Software Foundation; either version 2 of the License, or ;;
15 ;;; (at your option) any later version. ;;
16 ;;; ;;
17 ;;; This program is distributed in the hope that it will be useful, ;;
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;;
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;;
20 ;;; GNU General Public License for more details. ;;
21 ;;; ;;
22 ;;; You should have received a copy of the GNU General Public License ;;
23 ;;; along with this program; if not, write to the Free Software ;;
24 ;;; Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA ;;
25 ;;; ;;
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27
28 ;; Abbreviations when followed by a dot and a space ex: "Dr. Hello"
29 ;; or when followed by one of the symbols listed in hindi_abbr_markers_table
30 ;; are replaced with their full forms
31 (defvar hindi_dotted_abbr_list
32 '(
33 ;; Time
34 ("ता" "घंटे") ;; Hours
35 ("मि" "मिनीट") ;; Minutes
36 ;; Designations
37 ("डॉ" "डाक्टर") ;; Doctor
38 ("चि" "चिरंजीवी") ;; A title used for men
39 ("सौ" "सौभाग्यवती") ;; A title used for women
40 ("कु" "कुमारी") ;; A title used for bachlorettes
41 ;; Morning, Afternoon, Evening, Night - There are four zones instead of just AM PM
42 ("" "") ;; Early morning
43 ("" "") ;; Morning
44 ("" "") ;; Afternoon
45 ("" "") ;; Evening
46 ("रा" "रात") ;; Night
47 ;; Rough equivalents of AM and PM from Sanskrit
48 ("पू" "पूर्वान्ह:") ;; poorvaahna
49 ("अ" "अपरान्ह:") ;; apraahna
50 ))
51
52 ;; Above list contains items that will be followed by a dot or other character
53 ;; Here the list contains abbreviations that are not followed by any puncuation
54 ;; but need to be expanded anyway
55 (defvar hindi_abbr_list
56 '(
57 ;; Weekdays (as defined by the POSIX hi_IN locale
58 ("रवि" "रविवार") ;; Sunday
59 ("सोम" "सोमवार") ;; Monday
60 ("मंगल" "मंगलवार") ;; Tuesday
61 ("बुध" "बुधवार") ;; Wednesday
62 ("बृ" "बृहस्पतिवार") ;; Thirsday
63 ("शुक्र" "शुकरवार") ;; Friday
64 ("शनि" "शनिवार") ;; Saturday
65 ("1/2" "आधा") ;; Half
66 ("1/4" "पाव") ;; Quarter
67 ))
68
69 ;; If these words follow a currency, then we need to
70 ;; change the order of pronounciation for this word,
71 ;; number and the currency symbol
72 (defvar hindi_denominations_list
73 '(
74 ("बिलिअन्स") ("बिलिअन")
75 ("अरब")
76 ("करोड़")
77 ("लाख")
78 ("हजार")
79 ("सौ")
80 ))
81
82 ;; This list the symbols that need to be spoken out
83 ;; If a symbol is found in hindi_currency_list it should have
84 ;; an expansion in this list. Other than currency list, all other
85 ;; symbols are the last to be processed in the tokenising process
86 (defvar hindi_common_symbols_table
87 '(
88 ("#" "हॅश") ;; Hash
89 ("$" "डॉलर") ;; Dollor
90 ("%" "टक्का") ;; Percentage
91 ("&" "और") ;; And
92 ("*" "") ;; Multiply
93 ("+" "अधिक") ;; Plus
94 ("," "") ;; Comma
95 ("-" "") ;; Minus
96 ("." "") ;; eat up the dot
97 ("/" "") ;; By
98 ;; (":" "कोलन") ;; Colon
99 (":" "") ;; Colon
100 (";" "") ;; eat up the semi colon
101 ("<" ("से" "छोटा")) ;; Less Than
102 ("=" "बराबर") ;; Equal to
103 (">" ("से" "बडा")) ;; Greater than
104 ("?" "") ;; eat up the question mark
105 ("@" "अॅट") ;; At
106 ("^" "") ;; eat up Caret
107 ("_" ("अंडर" "स्कोर")) ;; Under Score
108 ("`" ("बॅक" "कोट")) ;; Back Quote
109 ("~" "टिल्डा") ;; Tilde
110 ("©" "कॉपीराईट") ;; Copyright Sign
111 ("®" "रजिस्टर्ड") ;; Registered Sign
112 ("£" "पाऊंड") ;; Pound
113 ("€" "युरो") ;; Euro
114 ("¥" "येन") ;; Yen
115 ("¢" "सेन्ट") ;; Cent
116 ("।" "") ;; Eat up Devanagari Danda
117 ("॥" "") ;; Eat up Devanagari Double Danda
118 ;;ensure digits are always at the end of the table
119 ;;so that they will be used as separators after other symbols have been considered
120 ("0" "शून्य") ;; Zero
121 ("1" "एक") ;; One
122 ("2" "दो") ;; Two
123 ("3" "तीन") ;; Three
124 ("4" "चार") ;; Four
125 ("5" "पाँच") ;; Five
126 ("6" "छः") ;; Six
127 ("7" "सात") ;; Seven
128 ("8" "आठ") ;; Eight
129 ("9" "नौ") ;; Nine
130 ))
131
132 ;; These are the additional characters allowed by the string
133 ;; cleanup function
134 (defvar hindi_supplementary_char_list
135 '("£" "€" "¥" "©" "®" "¢" "।" "॥"))
136
137 ;; The text is checked if it contains these items almost at the
138 ;; begining of the processing and full forms will be substitued
139 ;; by taking the full form from common symbols table
140 (defvar hindi_currency_list
141 '( "$" "£" "€" "¥"))
142
143 ;; If the items in dotted_abbr_list is followed by these symbols
144 ;; Then they are conisered a match and expanded otherwise
145 ;; they are not a match. Dot will be parsed by festival specially
146 ;; so there is also a special handling for it in the code
147 (defvar hindi_abbr_markers_table
148 '( ("." "") ("।" "") ("॥" "") ))
149
150 ;; Hindi numbers are converted to westren numerals during the
151 ;; string cleanup
152 (defvar hindi_numbers_table
153 '(
154 ("०" "0") ;; Zero
155 ("१" "1") ;; One
156 ("२" "2") ;; Two
157 ("३" "3") ;; Three
158 ("४" "4") ;; Four
159 ("५" "5") ;; Five
160 ("६" "6") ;; Six
161 ("७" "7") ;; Seven
162 ("८" "8") ;; Eight
163 ("९" "9") ;; Nine
164 ))
165
166 ;; This is main function of this file. This gets called
167 ;; from outside. the function is expected to return a string
168 ;; of words that is the normalised form of the string
169 ;; The output string is later sent to letter to sound rules
170 (define (hindi_token_to_words token name)
171 (flatten (hindi_token_to_words_ token name))
172 )
173
174 ;; A way to convert a given char to its ascii value
175 ;; Could not find any better/direct way to do it
176 (defvar ascii_char_hash (cons-array 256));
177 (defvar defined_ascii_char_hash)
178 (define (initialize_ascii_char_hash)
179 (let ((char 0))
180 (while (<= char 255)
181 (hset ascii_char_hash (string-append (format nil "%c" char)) char)
182 (set! char (+ char 1)))
183 (set! defined_ascii_char_hash t)
184 ))
185
186 ;; Convert a char into ascii value using above initialised table
187 (define (ascii_of char)
188 (if (not defined_ascii_char_hash) (initialize_ascii_char_hash))
189 (href ascii_char_hash char)
190 )
191
192 ;; A utility function for looking up text in the tables
193 (define (hindi_match_string_start_with_list input_string string_list)
194 ;; Checks if any of the strings in string_list match the start of input_string
195 ;; Returns the matched string from string_list
196 (let (substr (matched_substr ""))
197 (while (and (not (null? string_list)) (string-equal matched_substr ""))
198 (set! substr (car string_list))
199 (set! string_list (cdr string_list))
200 (if (string-equal substr "$")
201 (set! match_string (string-append "\\" substr ".*"))
202 (set! match_string (string-append substr ".*")))
203 (if (string-matches input_string match_string) (set! matched_substr substr)))
204 matched_substr
205 ))
206
207 ;; Cleans up the string to discard any charater that we are not going to handle
208 ;; What we are going to handle is ascii, Devanagari unicode block, special symbols like euro etc.
209 (define (hindi_string_cleanup input_string)
210 ;; Ensures the string has characters with are either in the hindi unicode range,
211 ;; ascii punctuation or represent a currency symbol
212 (let (curr_char next_char next_next_char (clean_string "") matched_string)
213 ;; Convert Devanagari unicode digits to ascii
214 (while (and (set! matched_string (hindi_match_string_with_table input_string hindi_numbers_table))
215 (not (string-equal matched_string "")))
216 (set! input_string (string-append (string-before input_string matched_string)
217 (car (hindi_table_lookup matched_string hindi_numbers_table)) (string-after input_string matched_string))))
218 ;; Iterate over all the characters in the string
219 ;; We also need 2 look ahead characters for dirty handling for UTF-8 text
220 (while (not (equal? input_string ""))
221 (set! curr_char (ascii_of (substring input_string 0 1)))
222 (set! next_char (ascii_of (substring input_string 1 1)))
223 (set! next_next_char (ascii_of (substring input_string 2 1)))
224 (cond
225 ;; Basic ascii
226 ((or (or (and (>= curr_char 33) (<= curr_char 64)) (and (>= curr_char 91) (<= curr_char 96)))
227 (and (>= curr_char 123) (<= curr_char 126)))
228 (set! num_chars_valid 1))
229 ;; FIXME: bring this part out so that code be separate from language specific knowledge
230 ;; Hindi unicode block (of the form \340 {\244 \245} [\200 .. \277])
231 ((and (equal? curr_char 224)
232 (or (equal? next_char 164) (equal? next_char 165))
233 (and (>= next_next_char 128) (<= next_next_char 191)))
234 (set! num_chars_valid 3))
235 ;; Supplementary chars like yen, pound, cent, copyright, registered mark, Euro
236 ((not (string-equal (set! matched_string
237 (hindi_match_string_start_with_list input_string hindi_supplementary_char_list)) ""))
238 (set! num_chars_valid (string-length matched_string)))
239 ;; Does not match anything, throw away the char
240 (t (set! num_chars_valid 0))
241 )
242 (set! clean_string (string-append clean_string (substring input_string 0 num_chars_valid)))
243 ;; Does not match anything, throw away the char
244 (if (equal? num_chars_valid 0) (set! num_chars_valid 1))
245 (set! input_string (substring input_string num_chars_valid (- (string-length input_string) num_chars_valid))))
246 clean_string
247 ))
248
249 ;; A utility function for looking up text in the tables
250 (define (hindi_match_string_with_table input_string string_table)
251 ;; Checks is any of the strings in string_table match a substring of input_string
252 ;; Returns the matched string from string_table
253 (let (substr (matched_substr ""))
254 (while (and (not (null? string_table)) (string-equal matched_substr ""))
255 (set! substr (car (car string_table)))
256 (set! string_table (cdr string_table))
257 (if (or (string-equal substr "$") (string-equal substr "^") (string-equal substr "?")
258 (string-equal substr "*") (string-equal substr "+") (string-equal substr "."))
259 (set! match_string (string-append ".*\\" substr ".*"))
260 (set! match_string (string-append ".*" substr ".*")))
261 (if (string-matches input_string match_string) (set! matched_substr substr)))
262 matched_substr
263 ))
264
265 ;; Most of the work happens here. From here we call all the other functions in the file
266 ;; Called by hindi_tokne_to_words
267 (define (hindi_token_to_words_ token name)
268 (set! name (hindi_string_cleanup name))
269
270 ;; Numbers are to be matched a lot place and they contain commas and decimals
271 (let ((number_regex "[0-9,]+\\(\\.[0-9]+\\)?") matched_substr matched_start_string prev_token_matched_start_string
272 currency_name time_segments date_segments)
273
274 (set! matched_start_string (hindi_match_string_start_with_list name hindi_currency_list))
275 (cond
276
277 ;; FIXME: rewrite this so that code be separate from language specific knowledge
278 ;; FIXME: the message translation can be generalised using $1 $2 etc. inside the strings
279
280 ;; Currencies
281 ;; When currencies are followed by billions etc., we need to change the order
282 ;; Rs. 1000 Billion will become 1000 billion rupees
283 ((and (not (string-equal matched_start_string ""))
284 (string-matches (string-after name matched_start_string) number_regex))
285 (set! currency_name (string-append (car (hindi_table_lookup matched_start_string hindi_common_symbols_table)) ""))
286 (if (hindi_list_lookup (item.feat token "n.name") hindi_denominations_list)
287 (hindi_token_to_words_ token (substring name 1 (- (string-length name) 1)))
288 (list (hindi_token_to_words_ token (substring name 1 (- (string-length name) 1))) currency_name)))
289 ((and (not (string-equal (set! prev_token_matched_start_string (hindi_match_string_start_with_list
290 (item.feat token "p.name") hindi_currency_list)) ""))
291 (string-matches (string-after (item.feat token "p.name") prev_token_matched_start_string) number_regex)
292 (hindi_list_lookup name hindi_denominations_list))
293 (list name (string-append
294 (car (hindi_table_lookup prev_token_matched_start_string hindi_common_symbols_table)) "")))
295
296 ;; Rupees
297 ((string-matches name (string-append "रू\\.[" number_regex))
298 (if (hindi_list_lookup (item.feat token "n.name") hindi_denominations_list)
299 (hindi_token_to_words_ token (string-after name "रू."))
300 (list (hindi_token_to_words_ token (string-after name "रू.")) "रूपए")))
301 ((and (string-matches (item.feat token "p.name") (string-append "रू\\.[" number_regex))
302 (hindi_list_lookup name hindi_denominations_list) ) (list name "रूपए"))
303
304 ;; Cents - the symbol occurs after the number
305 ((string-matches name (string-append number_regex "¢"))
306 (list (hindi_token_to_words_ token (string-before name "¢")) "सेंट्स")) ;; Cents
307
308 ;; Simple numbers
309 ((string-matches name "[0-9]+") (hindi_number_to_words name))
310 ;; Numbers with decimal point
311 ((string-matches name "[0-9]*\\.[0-9]+")
312 (list (hindi_number_to_words (string-before name ".")) '("पुर्णांक") ;; Point
313 (mapcar hindi_number_to_words (mapcar string-append (symbolexplode (string-after name "."))))))
314 ;; Numbers of the form dd,dd,ddd.dd
315 ((string-matches name "\\([0-9]+,[0-9]*\\)+\\(\.[0-9]+\\)?")
316 (hindi_token_to_words_ token (hindi_removechar name ",")))
317 ;; Numbers with a minus in the front
318 ((string-matches name (string-append "-" number_regex)) (list '("उणे") ;; Minus
319 (hindi_token_to_words_ token (string-after name "-"))))
320 ;; Numbers with a plus in the front
321 ((string-matches name (string-append "\\+" number_regex)) (list '("अधिक") ;; adhika - Plus
322 (hindi_token_to_words_ token (string-after name "+"))))
323
324 ;; Line of characters
325 ((string-matches name "_____+") (list '"अंडर" "स्कोरकी" "रेषा")) ;; A line of under scores
326 ((string-matches name "=====+") (list "इक्वलटू" "की" "रेषा")) ;; FIXME: unnecessary splits
327 ((string-matches name "-----+") (list "हायफन्सकी" "रेषा")) ;; A line of hyphens, FIXME: unnecessary splits
328 ((string-matches name "\\*\\*\\*\\*\\*+") (list "अॅस्टरिस्क्सकी" "रेषा")) ;; : a line of asterisks
329
330 ;; Time and Date
331 ((set! time_segments (hindi_string_matches_time name)) (mapcar hindi_token_to_words_
332 (list token token token) (mapcar string-append time_segments)))
333 ((set! date_segments (hindi_string_matches_date name)) (mapcar hindi_token_to_words_
334 (list token token token) (mapcar string-append date_segments)))
335
336 ;; Abbreviations
337 ;; Dotted abbreviations when followed by space ex: "Dr. Hello"
338 ((and (hindi_table_lookup name hindi_dotted_abbr_list) (string-equal (item.feat token "punc") "."))
339 (hindi_table_lookup name hindi_dotted_abbr_list))
340
341 ;; Abbreviation follwed by markers .,:,devanagari danda, double danda
342 ((and (set! matched_substr (hindi_match_string_with_table name hindi_abbr_markers_table))
343 (not (string-equal matched_substr ""))
344 (hindi_table_lookup (string-before name matched_substr) hindi_dotted_abbr_list))
345 (list (hindi_table_lookup (string-before name matched_substr) hindi_dotted_abbr_list)
346 (hindi_token_to_words_ token (string-after name matched_substr))))
347
348 ;; Abbreviations not followed by .
349 ((hindi_table_lookup name hindi_abbr_list) (hindi_table_lookup name hindi_abbr_list))
350
351 ;; Number followed by percentages
352 ((string-matches name (string-append number_regex "%"))
353 (list (hindi_token_to_words_ token (string-before name "%")) '("टक्के"))) ;; Takke - Percentage
354 ;; Separators and connectors {#,$,%,&,*,+,-,/,<,=,>,@,\,^,_,`,copyright,registered trademark}
355 ((and (set! matched_substr (hindi_match_string_with_table name hindi_common_symbols_table) )
356 (not (string-equal matched_substr "")))
357 (list (hindi_token_to_words_ token (string-before name matched_substr))
358 (hindi_table_lookup matched_substr hindi_common_symbols_table)
359 (hindi_token_to_words_ token (string-after name matched_substr))))
360
361 ;; Nothing else todo, return
362 (t (if (lts.in.alphabet (hindi_string_cleanup name) 'hindi) (list (hindi_string_cleanup name)) (list '(""))))
363 )))
364
365 ;; Time matching function
366 (define (hindi_string_matches_time input_string)
367 ;; Returns input string is in HH:MM(:SS) format
368 (let ((hrs (parse-number (string-before input_string ":"))) mins secs)
369 (cond
370 ((not (string-matches input_string "[0-9][0-9]?:[0-9][0-9]\\(:[0-9][0-9]\\)?")) nil)
371 (t
372 (set! input_string (string-after input_string ":"))
373 (set! mins (string-before input_string ":"))
374 (if (string-equal mins "") (set! mins (parse-number input_string)) (set! mins (parse-number mins)))
375 (set! secs (string-after input_string ":"))
376
377 ;; Checking for HH,MM,SS to be in valid 24 hour format
378 (if (and (>= hrs 0) (<= hrs 23) (>= mins 0) (<= mins 59) (>= (parse-number secs) 0) (<= (parse-number secs) 59))
379 (list hrs mins secs) nil)
380 )
381 )))
382
383 ;; Storing the number of days in a month
384 ;; so we can check for a valid date
385 (defvar hindi_no_of_days_in_month
386 '( (1 31) (2 29)(3 31) (4 30)
387 (5 31) (6 30) (7 31) (8 31)
388 (9 30) (10 31) (11 30) (12 31)))
389
390 ;; Date matching function
391 (define (hindi_string_matches_date input_string)
392 ;; Returns true is the input string is in DD/MM(/YY/YYYY) format,
393 (let ((date_segment1 (parse-number (string-before input_string "/"))) date_segment2 date_segment3 days_in_month)
394 (cond
395 ((not (string-matches input_string "[0-9][0-9]?/[0-9][0-9]?\\(/[0-9][0-9]\\([0-9][0-9]\\)?\\)?")) nil)
396 (t
397 (set! input_string (string-after input_string "/"))
398 (set! date_segment2 (string-before input_string "/"))
399 (if (string-equal date_segment2 "") (set! date_segment2 (parse-number input_string))
400 (set! date_segment2 (parse-number date_segment2)))
401 (set! date_segment3 (string-after input_string "/"))
402 ;; Checking for DD,MM to be in valid
403 (set! days_in_month (car (hindi_table_lookup date_segment2 hindi_no_of_days_in_month)))
404 (if (and days_in_month (> date_segment1 0) (<= date_segment1 days_in_month))
405 (list date_segment1 date_segment2 date_segment3) nil))
406 )))
407
408 ;; Convert numbers in the string to words in the language
409 ;; Inturn calls another function
410 (define (hindi_number_to_words number)
411 (let (input_string)
412 (flatten (hindi_number_to_words_rec input_string number))
413 ))
414
415 ;; Convert numbers in the string to words in the language
416 (define (hindi_number_to_words_rec token_list name)
417 ;; FIXME: rewrite so that code be separate from language specific knowledge
418 (set! name (hindi_strip_leading_zeros name)) ;; Remove leading zeros
419 (let ((number_length (string-length name)))
420 (cond
421 ;; If number is in crores we handle it here
422 ((>= number_length 8)
423 ;; Special case for 1 crore
424 (if (string-equal name "10000000") (append token_list '("करोड़")) ;; Crore
425 ;; Case for round figured crores like 2 crores and 23 crores
426 (if (string-matches name "[0-9]+0000000")
427 (append (hindi_number_to_words_rec token_list (substring name 0 (- number_length 7))) '("करोड़")) ;; crore
428 ;;Case for one crore plus change
429 (if (string-matches name "1[0-9][0-9][0-9][0-9][0-9][0-9][0-9]")
430 (append token_list '("एक" "करोड़") (hindi_number_to_words_rec token_list (substring name (- number_length 7) 7))) ;; One crore 12987600, 10099887, etc
431 ;; Default case if the number is other than above
432 (append (hindi_number_to_words_rec token_list (substring name 0 (- number_length 7))) '("करोड़")
433 (hindi_number_to_words_rec token_list (substring name (- number_length 7) 7)))))))
434 ;; If number is in lakhs we handle it here
435 ((and (<= number_length 7) (>= number_length 6))
436 ;; Special case for 1 lakh
437 (if (string-equal name "100000") (append token_list '("लाख")) ;; Lakh
438 ;; Case for round figured lakhs like 2 lakhs and 56 lakhs
439 (if (string-matches name "[0-9]+00000")
440 (append token_list (hindi_two_digit_number_to_words (substring name 0 (- number_length 5))) '("लाख")) ;; lakhs 200000, 500000, etc
441 ;;Case for one lakh plus change
442 (if (string-matches name "1[0-9][0-9][0-9][0-9][0-9]")
443 (hindi_number_to_words_rec (append token_list '("एक" "लाख")) (substring name (- number_length 5) 5)) ;; One laksha 198734, 178900, etc
444 ;; Default case if the number is other than above
445 (hindi_number_to_words_rec (append token_list (hindi_two_digit_number_to_words
446 (substring name 0 (- number_length 5))) '("लाख")) (substring name (- number_length 5) 5))))))
447 ;; If number is in thousands we handle it here
448 ((and (<= number_length 5) (>= number_length 4))
449 ;; Special case for 1 thousand
450 (if (string-equal name "1000") (append token_list '("हजार")) ;; Thousand
451 ;; Case for round figured thousands like 2 thousand and 56 thousand
452 (if (string-matches name "[0-9]+000")
453 (append token_list (hindi_two_digit_number_to_words (substring name 0 (- number_length 3))) '("हजार")) ;; Thousands 2000, 7000, 3000, etc
454 ;; Case for 4 digit numbers like 1100, 1200, 2300
455 (if (string-matches name "[0-9][1-9]00")
456 (append token_list (hindi_two_digit_number_to_words
457 (substring name 0 (- number_length 2))) '("सौ")) ;; hundreds 2800, 1100, 8900, etc
458 ;; Case for one thousand plus change
459 (if (string-matches name "1[0-9][0-9][0-9]")
460 (hindi_number_to_words_rec (append token_list (hindi_two_digit_number_to_words
461 (substring name 0 (- number_length 3))) '("हजार")) (substring name (- number_length 3) 3)) ;; thousand
462 ;; (hindi_number_to_words_rec (append (hindi_two_digit_number_to_words (substring name 0 2))
463 ;; token_list '("सौ")) (substring name 2 2)) ;; hundreds 1234, 5673, 9009, etc
464 ;; Default case if the number is other than above
465 (hindi_number_to_words_rec (append token_list (hindi_two_digit_number_to_words
466 (substring name 0 (- number_length 3))) '("हजार")) (substring name (- number_length 3) 3))))))) ;; thousand
467 ;; If number is less than thousand, we handle it here
468 ((eq number_length 3)
469 ;; Special case for one hundred
470 (if (string-equal name "100") (append token_list '("एक" "सौ")) ;; - Hundred rupees
471 ;; Case for round figured hundreds like 2 hundred and 9 hundred
472 (if (string-matches name "[2-9]00")
473 (append token_list (hindi_two_digit_number_to_words
474 (substring name 0 (- number_length 2))) '("सौ")) ;; hundred 200, 300, 400, etc
475 ;; Case for one hundred plus change
476 (if (string-matches name "1[0-9][0-9]") (append token_list '("एक" "सौ") ;; Hundred and.. 111, 109, 178, etc
477 (hindi_two_digit_number_to_words (substring name 1 2)))
478 ;; Default case if the number is other than above
479 (hindi_number_to_words_rec (append token_list (hindi_two_digit_number_to_words
480 (substring name 0 (- number_length 2))) '("सौ")) (substring name (- number_length 2) 2)))))) ;; hundred 987, 290, etc
481 ;; If the number is less than hundred we call as function to handle it
482 ((<= number_length 2) (append token_list (hindi_two_digit_number_to_words name)))
483 ((= number_length 1) (append token_list (hindi_number_to_words name)))
484 )))
485
486 ;; This table is queried by the two digit number to word converter
487 ;; for coverting the units place
488 (defvar hindi_basic_number_table
489 '(
490 ("0" "शून्य") ;; Zero
491 ("1" "एक") ;; One
492 ("2" "दो") ;; Two
493 ("3" "तीन") ;; Three
494 ("4" "चार") ;; Four
495 ("5" "पाँच") ;; Five
496 ("6" "छे") ;; Six
497 ("7" "सात") ;; Seven
498 ("8" "आठ") ;; Eight
499 ("9" "नौ") ;; Nine
500 ))
501
502 ;; This table is queried by the two digit number to word converter
503 ;; for coverting the tens place and special numbers from 11 to 19
504 (defvar hindi_two_digit_numbers_table
505 '(
506 ("10" "दस") ;; Ten
507 ("11" "ग्यारह") ;; Eleven
508 ("12" "बारह") ;; Twelve
509 ("13" "तेरह") ;; Thirteen
510 ("14" "चौदह") ;; Fourteen
511 ("15" "पन्द्रह") ;; Fifteen
512 ("16" "सोलह") ;; Sixteen
513 ("17" "सतरह") ;; Seventeen
514 ("18" "अठारह") ;; Eighteen
515 ("19" "उन्नीस") ;; Nineteen
516 ("20" "बीस") ;; Twenty
517
518 ("21" "इक्कीस")
519 ("22" "बाईस")
520 ("23" "तेईस")
521 ("24" "चौबीस")
522 ("25" "पच्चीस")
523 ("26" "छब्बीस")
524 ("27" "सत्ताईस")
525 ("28" "अठांईस")
526 ("29" "उनतीस")
527 ("30" "तीस") ;; Thirty
528
529 ("31" "इकतीस")
530 ("32" "बत्तीस")
531 ("33" "तैंतीस")
532 ("34" "चौंतीस")
533 ("35" "पैंतीस")
534 ("36" "छत्तीस")
535 ("37" "सैंतीस")
536 ("38" "अड़तीस")
537 ("39" "उनचालीस")
538 ("40" "चालीस") ;; Fourty
539
540 ("41" "इकतालीस")
541 ("42" "बयालीस")
542 ("43" "तितालीस")
543 ("44" "चौवालीस")
544 ("45" "पैंतालीस")
545 ("46" "छियालीस")
546 ("47" "सैंतालीस")
547 ("48" "अड़तालीस")
548 ("49" "उनचास")
549 ("50" "पचास") ;; Fifty
550
551 ("51" "इकावन")
552 ("52" "बावन")
553 ("53" "त्रेपन")
554 ("54" "चौवन")
555 ("55" "पचपन")
556 ("56" "छप्पन")
557 ("57" "सत्तावन")
558 ("58" "अठावन")
559 ("59" "उनसठ")
560 ("60" "साठ") ;; Sixty
561
562 ("61" "एकसठ")
563 ("62" "बासठ")
564 ("63" "तिरसठ")
565 ("64" "चौंसठ")
566 ("65" "पैंसठ")
567 ("66" "छियासठ")
568 ("67" "सरसठ")
569 ("68" "अड़सठ")
570 ("69" "उनहतर")
571 ("70" "सत्तर") ;; Seventy
572
573 ("71" "इकत्तर")
574 ("72" "बहत्तर")
575 ("73" "तिहत्तर")
576 ("74" "चौहत्तर")
577 ("75" "पचहत्तर")
578 ("76" "छिहत्तर")
579 ("77" "सतहत्तर")
580 ("78" "अठहत्तर")
581 ("79" "उन्नासी")
582 ("80" "अस्सी") ;; Eighty
583
584 ("81" "इकासी")
585 ("82" "बयासी")
586 ("83" "तिरासी")
587 ("84" "चौरासी")
588 ("85" "पचासी")
589 ("86" "छियासी")
590 ("87" "सत्तासी")
591 ("88" "अठासी")
592 ("89" "नवासी")
593 ("90" "नब्बे") ;; Ninety
594
595 ("91" "इकानवे")
596 ("92" "बानवे")
597 ("93" "तिरानवे")
598 ("94" "चौरानवे")
599 ("95" "पंचानवे")
600 ("96" "छियानवे")
601 ("97" "सत्तानवे")
602 ("98" "अठानवे")
603 ("99" "निन्यानवे")
604 ))
605
606 ;; Utility function for list lookup
607 (define (hindi_list_lookup abbr lst)
608 (assoc_string abbr lst))
609
610 ;; Utility function for table lookup
611 (define (hindi_table_lookup abbr table)
612 (cdr (assoc_string abbr table)))
613
614 ;; Convert two digit numbers to words
615 ;; Used by number to words function
616 ;; Takes non zero numers
617 (define (hindi_two_digit_number_to_words name)
618 ;; Number to words for 0-99
619 ;; FIXME: is this language dependent?
620 (let (lst units tens)
621 (set! lst (reverse (symbolexplode name)))
622 (set! units (car lst))
623 (set! tens (car (cdr lst)))
624 ;; Supress the units digit if it is 0
625 (if (and (string-equal units "0") (not (or (eq tens nil) (string-equal tens "1")))) (set! units ""))
626 ;; Remove leading zero
627 (if (string-equal tens "0") (set! tens ""))
628 (if (string-matches tens "[1-9]") (hindi_table_lookup name hindi_two_digit_numbers_table)
629 (cons (hindi_table_lookup tens hindi_two_digit_numbers_table) (hindi_table_lookup units hindi_basic_number_table))
630
631 ;; above line is valid till "2 digit numbers to words" are english alike viz. 21-twenty-one, 95-ninety-five etc, hence not applicable to all languages like in hindi 21-ekavIsa (२१-एकवीस) etc.
632 ;; (hindi_table_lookup name hindi_two_digit_numbers_table)
633 ;; (hindi_table_lookup name hindi_basic_number_table)
634 )))
635
636 ;; Used by number for words before any processing to remove
637 ;; the leading zeros
638 (define (hindi_strip_leading_zeros number)
639 ;; Removes leading zeros for a number, if single zero, leave it as it is
640 (if (string-matches number "0+") (set! number "0")
641 (while (string-matches number "0[0-9]*") (set! number (string-after number "0"))))
642 number
643 )
644
645 ;; Utility function to remove a particular charater from a string
646 (define (hindi_removechar input_string char)
647 ;; Removes all occurences of char from input_string
648 (let ((has_matched 1) match_string)
649 (if (string-equal char "\\") (set! match_string (string-append ".*\\\\.*"))
650 (set! match_string (string-append ".*" char ".*")))
651 (while has_matched
652 (if (string-matches input_string match_string)
653 (set! input_string (string-append (string-before input_string char) (string-after input_string char)))
654 (set! has_matched nil)))
655 input_string))
656
657 (provide 'hindi_token)