canon.ml 18.3 KB
Newer Older
huet's avatar
huet committed
1 2 3 4 5 6
(**************************************************************************)
(*                                                                        *)
(*                     The Sanskrit Heritage Platform                     *)
(*                                                                        *)
(*                              Gérard Huet                               *)
(*                                                                        *)
Gérard Huet's avatar
Gérard Huet committed
7
(* ©2018 Institut National de Recherche en Informatique et en Automatique *)
huet's avatar
huet committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
(**************************************************************************)

(*i module Canon = struct i*)

(* Inverse of [Transduction.code_raw] - word to VH transliteration *)
value canon = fun
  [ 0  -> "-" (* notation for suffixes and segmentation hint in compounds *)
  | 1  -> "a"
  | 2  -> "aa"
  | 3  -> "i"
  | 4  -> "ii"
  | 5  -> "u"
  | 6  -> "uu"
  | 7  -> ".r"
  | 8  -> ".rr"
  | 9  -> ".l"
  | 10 -> "e"
  | 11 -> "ai"
  | 12 -> "o"
  | 13 -> "au"
  | 14 -> ".m" (* anusvaara *)
  | 15 -> "~~" (* anunaasika candrabindu *)
  | 16 -> ".h"
  | 17 -> "k"
  | 18 -> "kh"
  | 19 -> "g"
  | 20 -> "gh"
  | 21 -> "f" (* used to be "\"n" -- fragile *)
  | 22 -> "c"
  | 23 -> "ch"
  | 24 -> "j"
  | 25 -> "jh"
  | 26 -> "~n"
  | 27 -> ".t"
  | 28 -> ".th"
  | 29 -> ".d"
  | 30 -> ".dh"
  | 31 -> ".n"
  | 32 -> "t"
  | 33 -> "th"
  | 34 -> "d"
  | 35 -> "dh"
  | 36 -> "n"
  | 37 -> "p"
  | 38 -> "ph"
  | 39 -> "b"
  | 40 -> "bh"
  | 41 -> "m"
  | 42 -> "y"
  | 43 -> "r"
  | 44 -> "l" (* Vedic l not accommodated *)
  | 45 -> "v"
  | 46 -> "z" (* used to be "\"s" -- fragile *)
  | 47 -> ".s"
  | 48 -> "s"
  | 49 -> "h"
  | 50 -> "_" (* hiatus *)
  | -1 -> "'" (* avagraha *)
  | -2 -> "[-]" (* amuissement - lopa of a or aa in preceding preverb *)
  | -3 -> "aa|a" (* sandhi of aa and a *a *)
  | -4 -> "aa|i" (* sandhi of aa and i *i *)
  | -5 -> "aa|u" (* sandhi of aa and u *u *)
  | -6 -> "aa|r" (* sandhi of aa and .r *r *)
  | -7 -> "aa|I" (* sandhi of aa and ii *I *)
  | -8 -> "aa|U" (* sandhi of aa and uu *U *)
  | -9 -> "aa|A" (* sandhi of aa and aa *A *)
  | -10 -> "+" (* notation for segmentation hint *)
  | 124 -> failwith "Canon: Unrestored special phoneme j'"  (* j/z *)
  | 149 -> failwith "Canon: Unrestored special phoneme h'"  (* h/gh *)
  | 249 -> failwith "Canon: Unrestored special phoneme h''" (* h/dh *)
  | n -> if n<0 || n>59 then failwith mess 
            where mess = "Canon: Illegal char " ^ string_of_int n
         else "#" ^ Char.escaped (Char.chr (n-2)) (* homo index 1 to 9 *)
                                 (* n-2 above since (ASCII) Char.chr 48 = '0' *)

  ]
;
(* Hiatus-conscious catenation [b=True] iff [s] starts with vowel *)
value catenate c (s,b) = 
  let b'= c>0 && c<14  (* Phonetics.vowel c *) in
  let protected = if b && b' then "_" ^ s else s in
  (canon c ^ protected , b')
;
(* [decode : word -> string] *)
value decode word = 
  let (s,_) = List.fold_right catenate word ("",False) in s 
;
value robust_decode c = (* used in [Morpho_tex.print_inverse_map_txt] *)
  let render n = 
    try canon n with
    [ Failure _ -> match n with
       [ 124 -> "j'" | 149 -> "h'" | 249 -> "h''"
       | _ -> string_of_int n 
       ]
    ] in 
  let conc s s' = render s ^ s' in
  List.fold_right conc c "" (* note no hiatus computation *)
;
(*i Debug when ordinary decode fails 
value decode w = try decode w with 
   [ Failure _ -> failwith (robust_decode w) ]
; i*)

value rdecode w = decode (Word.mirror w)
;
(**************************************************************************)
(* Important information for corpus processing                            *)
(**************************************************************************)
(* Beware. [decode (code_raw s)] is [s] with spaces removed 
   but [code_raw (decode c)] may not be [c] because of VH ambiguities such as 
      [decode [1;3] = decode [11] = "ai"].
   Note that unsandhied text with spaces is wrongly parsed:
      [code_raw "a i" =  [11]] and not [[1; 50; 3]]. 
   Thus one should use underscore for hiatus in digitalised corpus: 
      [code_raw "a_i" =  [1; 3]]. The chunking of text by interpreting spaces 
   is done in a preliminary pass by Sanskrit.padapatha. *)

(* Support for other translitteration schemes *)

(* Wax decoding - University of Hyderabad *)
value canon_WX = fun 
  [ 0  -> "-"
  | 1  -> "a"
  | 2  -> "A"
  | 3  -> "i"
  | 4  -> "I"
  | 5  -> "u"
  | 6  -> "U"
  | 7  -> "q"
  | 8  -> "Q"
  | 9  -> "L"
  | 10 -> "e"
  | 11 -> "E"
  | 12 -> "o"
  | 13 -> "O"
  | 14 -> "M"
  | 15 -> "z"
  | 16 -> "H"
  | 17 -> "k"
  | 18 -> "K"
  | 19 -> "g"
  | 20 -> "G"
  | 21 -> "f"
  | 22 -> "c"
  | 23 -> "C"
  | 24 -> "j"
  | 25 -> "J"
  | 26 -> "F"
  | 27 -> "t"
  | 28 -> "T"
  | 29 -> "d"
  | 30 -> "D"
  | 31 -> "N"
  | 32 -> "w"
  | 33 -> "W"
  | 34 -> "x"
  | 35 -> "X"
  | 36 -> "n"
  | 37 -> "p"
  | 38 -> "P"
  | 39 -> "b"
  | 40 -> "B"
  | 41 -> "m"
  | 42 -> "y"
  | 43 -> "r"
  | 44 -> "l" (* Vedic l not accommodated *)
  | 45 -> "v"
  | 46 -> "S"
  | 47 -> "R"
  | 48 -> "s"
  | 49 -> "h"
  | 50 -> "_" (* hiatus *)
  | -1 -> "Z" (* avagraha *)
  | -2 -> "[-]" (* amuissement - lopa of current aa- or preceding a- or aa- *)
  | -3 -> "A|a" (* sandhi of aa and (a,aa) *a *)
  | -4 -> "A|i" (* sandhi of aa and (i,ii) *e *)
  | -5 -> "A|u" (* sandhi of aa and (u,uu) *u *)
  | -6 -> "A|r" (* sandhi of aa and .r     *r *)
  | -10 -> "+" (* explicit compound with no sandhi - experimental *)
  | n -> if n<0 || n>59 then failwith mess
            where mess = "Canon: Illegal char " ^ string_of_int n
         else "#" ^ Char.escaped (Char.chr (n-2)) (* homo index 1 to 9 *)
  ]
;
value decode_WX word =
  List.fold_right (fun c s -> (canon_WX c) ^ s) word ""
;
(* Sanskrit Library SLP1 decoding *)
value canon_SL = fun
  [ 0 -> "-"
  | -10 -> "+"
  | 1  -> "a"
  | 2  -> "A"
  | 3  -> "i"
  | 4  -> "I"
  | 5  -> "u"
  | 6  -> "U"
  | 7  -> "f"
  | 8  -> "F"
  | 9  -> "x"
  | 10 -> "e"
  | 11 -> "E"
  | 12 -> "o"
  | 13 -> "O"
  | 14 -> "M"
  | 15 -> "~"
  | 16 -> "H"
  | 17 -> "k"
  | 18 -> "K"
  | 19 -> "g"
  | 20 -> "G"
  | 21 -> "N"
  | 22 -> "c"
  | 23 -> "C"
  | 24 -> "j"
  | 25 -> "J"
  | 26 -> "Y"
  | 27 -> "w"
  | 28 -> "W"
  | 29 -> "q"
  | 30 -> "Q"
  | 31 -> "R"
  | 32 -> "t"
  | 33 -> "T"
  | 34 -> "d"
  | 35 -> "D"
  | 36 -> "n"
  | 37 -> "p"
  | 38 -> "P"
  | 39 -> "b"
  | 40 -> "B"
  | 41 -> "m"
  | 42 -> "y"
  | 43 -> "r"
  | 44 -> "l" (* Vedic l not accommodated *)
  | 45 -> "v"
  | 46 -> "S"
  | 47 -> "z"
  | 48 -> "s"
  | 49 -> "h"
  | 50 -> "_" (* hiatus *)
  | -1 -> "Z" (* avagraha *)
  | n -> if n<0 || n>59 then failwith mess
            where mess = "Canon: Illegal char " ^ string_of_int n
         else "#" ^ Char.escaped (Char.chr (n-2)) (* homo index 1 to 9 *)
  ]
;
value decode_SL word =
  List.fold_right (fun c s -> (canon_SL c) ^ s) word ""
;
(* Kyoto-Harvard decoding *)
value canon_KH = fun
  [ 0 -> "-"
  | -10 -> "+"
  | 1  -> "a"
  | 2  -> "A"
  | 3  -> "i"
  | 4  -> "I"
  | 5  -> "u"
  | 6  -> "U"
  | 7  -> "R"
  | 8  -> "RR"
  | 9  -> "L"
  | 10 -> "e"
  | 11 -> "ai"
  | 12 -> "o"
  | 13 -> "au"
  | 14 -> "M"
  | 15 -> "M" (* candrabindu absent *)
  | 16 -> "H"
  | 17 -> "k"
  | 18 -> "kh"
  | 19 -> "g"
  | 20 -> "gh"
  | 21 -> "G"
  | 22 -> "c"
  | 23 -> "ch"
  | 24 -> "j"
  | 25 -> "jh"
  | 26 -> "J"
  | 27 -> ".t"
  | 28 -> ".th"
  | 29 -> ".d"
  | 30 -> ".dh"
  | 31 -> ".n"
  | 32 -> "t"
  | 33 -> "th"
  | 34 -> "d"
  | 35 -> "dh"
  | 36 -> "n"
  | 37 -> "p"
  | 38 -> "ph"
  | 39 -> "b"
  | 40 -> "bh"
  | 41 -> "m"
  | 42 -> "y"
  | 43 -> "r"
  | 44 -> "l" (* Vedic l not accommodated *)
  | 45 -> "v"
  | 46 -> "z"
  | 47 -> "S"
  | 48 -> "s"
  | 49 -> "h"
  | 50 -> "_" (* hiatus *)
  | -1 -> "'" (* avagraha *)
  | n -> if n<0 || n>59 then failwith mess
            where mess = "Canon: Illegal char " ^ string_of_int n
         else "#" ^ Char.escaped (Char.chr (n-2)) (* homo index 1 to 9 *)
  ]
;
value decode_KH word =
  List.fold_right (fun c s -> (canon_KH c) ^ s) word ""
;
value switch_decode = fun (* normalizes anusvaara in its input *)
  [ "VH" -> decode 
  | "WX" -> decode_WX
  | "KH" -> decode_KH
  | "SL" -> decode_SL
  | _ -> failwith "Unexpected transliteration scheme"
  ] 
;
(* Decoding without double quotes *)
value canon2 = fun
  [ 0 -> "-"
  | -10 -> "+"
  | 1  -> "a"
  | 2  -> "A"
  | 3  -> "i"
  | 4  -> "I"
  | 5  -> "u"
  | 6  -> "U"
  | 7  -> ".r"
  | 8  -> ".R"
  | 9  -> ".l"
  | 10 -> "e"
  | 11 -> "E"
  | 12 -> "o"
  | 13 -> "O"
  | 14 -> ".m"
  | 15 -> "M" 
  | 16 -> ".h"
  | 17 -> "k"
  | 18 -> "K"
  | 19 -> "g"
  | 20 -> "G"
  | 21 -> "N"
  | 22 -> "c"
  | 23 -> "C"
  | 24 -> "j"
  | 25 -> "J"
  | 26 -> "~n"
  | 27 -> ".t"
  | 28 -> ".T"
  | 29 -> ".d"
  | 30 -> ".D"
  | 31 -> ".n"
  | 32 -> "t"
  | 33 -> "T"
  | 34 -> "d"
  | 35 -> "D"
  | 36 -> "n"
  | 37 -> "p"
  | 38 -> "P"
  | 39 -> "b"
  | 40 -> "B"
  | 41 -> "m"
  | 42 -> "y"
  | 43 -> "r"
  | 44 -> "l"
  | 45 -> "v"
  | 46 -> "z"
  | 47 -> ".s"
  | 48 -> "s"
  | 49 -> "h"
  | 50 -> "_" (* hiatus *)
  | -1 -> "'"
  | -2 -> "[-]" (* Inconsistent with previous versions *)
  | -3 -> "A|a" (* sandhi of A and (a,A) - phantom phoneme *)
  | -4 -> "A|i" (* sandhi of A and (i,I) - phantom phoneme *) 
  | -5 -> "A|u" (* sandhi of A and (u,U) - phantom phoneme *)
  | -6 -> "A|.r" (* sandhi of A and .r) - phantom phoneme *)
  | n -> if n<0 || n>59 then failwith ("canon2: " ^ string_of_int n)
         else ("#" ^ Char.escaped (Char.chr (n-2)))
  ]
;
(* hiatus-conscious catenation [b=True] iff [s] starts with vowel *)
value catenate2 c (s,b) = 
  let b'= c>0 && c<14  (* Phonetics.vowel c *) in
  let protected = if b && b' then "_" ^ s else s in
  (canon2 c ^ protected , b')
;
(* [decode2 : word -> string] *)
value decode2 word = 
  try let (s,_) = List.fold_right catenate2 word ("",False) in s
  with [ Failure _ -> failwith ("decode2: " ^ robust_decode (Word.mirror word)) ]
;
value canon_upper = fun
  [ 101 -> "A"
  | 102 -> "AA"
  | 103 -> "I"
  | 104 -> "II"
  | 105 -> "U"
  | 106 -> "UU"
  | 107 -> ".R"
  | 110 -> "E"
  | 111 -> "Ai"
  | 112 -> "O"
  | 113 -> "Au"
  | 117 -> "K"
  | 118 -> "Kh"
  | 119 -> "G"
  | 120 -> "Gh"
  | 122 -> "C"
  | 123 -> "Ch"
  | 124 -> "J"
  | 125 -> "Jh"
  | 127 -> ".T"
  | 128 -> ".Th"
  | 129 -> ".D"
  | 130 -> ".Dh"
  | 132 -> "T"
  | 133 -> "Th"
  | 134 -> "D"
  | 135 -> "Dh"
  | 136 -> "N"
  | 137 -> "P"
  | 138 -> "Ph"
  | 139 -> "B"
  | 140 -> "Bh"
  | 141 -> "M"
  | 142 -> "Y"
  | 143 -> "R"
  | 144 -> "L"
  | 145 -> "V"
  | 146 -> "Z"
  | 147 -> ".S"
  | 148 -> "S"
  | 149 -> "H"
  | n -> failwith ("Illegal upper case code : " ^ string_of_int n)
  ]
;
(* [decode_ref : word -> string] *)
value decode_ref word = 
  let canon c = if c>100 then canon_upper c else canon c in
  let canon_catenate c (s,b) = 
      let b'= c>0 && c<14  (* Phonetics.vowel c *) in
      let protected = if b && b' then "_" ^ s else s in
      (canon c ^ protected , b') in
  let (s,_) = List.fold_right canon_catenate word ("",False) in s 
;
value canon_html = fun
  [ 0  -> "-"
  | -10 -> "+"
  | 1  -> "a"
  | 2  -> "aa"
  | 3  -> "i"
  | 4  -> "ii"
  | 5  -> "u"
  | 6  -> "uu"
  | 7  -> ".r"
  | 8  -> ".rr"
  | 9  -> ".l"
  | 10 -> "e"
  | 11 -> "ai"
  | 12 -> "o"
  | 13 -> "au"
  | 14 -> ".m"
  | 15 -> "~"
  | 16 -> ".h"
  | 17 -> "k"
  | 18 -> "kh"
  | 19 -> "g"
  | 20 -> "gh"
  | 21 -> "f"
  | 22 -> "c"
  | 23 -> "ch"
  | 24 -> "j"
  | 25 -> "jh"
  | 26 -> "~n"
  | 27 -> ".t"
  | 28 -> ".th"
  | 29 -> ".d"
  | 30 -> ".dh"
  | 31 -> ".n"
  | 32 -> "t"
  | 33 -> "th"
  | 34 -> "d"
  | 35 -> "dh"
  | 36 -> "n"
  | 37 -> "p"
  | 38 -> "ph"
  | 39 -> "b"
  | 40 -> "bh"
  | 41 -> "m"
  | 42 -> "y"
  | 43 -> "r"
  | 44 -> "l"
  | 45 -> "v"
  | 46 -> "z"
  | 47 -> ".s"
  | 48 -> "s"
  | 49 -> "h"
  | 50 -> "_" (* hiatus *)
  | n -> if n<0 then 
            failwith ("Illegal letter to canon_html : " ^ string_of_int n)
         else ("#" ^ Char.escaped (Char.chr (n-2)))
  ]
;
value canon_upper_html = fun
  [ 101 -> "Ua"
  | 102 -> "Uaa"
  | 103 -> "Ui"
  | 104 -> "Uii"
  | 105 -> "Uu"
  | 106 -> "Uuu"
  | 107 -> "U.r"
  | 110 -> "Ue"
  | 111 -> "Uai"
  | 112 -> "Uo"
  | 113 -> "Uau"
  | 117 -> "Uk"
  | 118 -> "Ukh"
  | 119 -> "Ug"
  | 120 -> "Ugh"
  | 122 -> "Uc"
  | 123 -> "Uch"
  | 124 -> "Uj"
  | 125 -> "Ujh"
  | 127 -> "U.t"
  | 128 -> "U.th"
  | 129 -> "U.d"
  | 130 -> "U.dh"
  | 132 -> "Ut"
  | 133 -> "Uth"
  | 134 -> "Ud"
  | 135 -> "Udh"
  | 136 -> "Un"
  | 137 -> "Up"
  | 138 -> "Uph"
  | 139 -> "Ub"
  | 140 -> "Ubh"
  | 141 -> "Um"
  | 142 -> "Uy"
  | 143 -> "Ur"
  | 144 -> "Ul"
  | 145 -> "Uv"
  | 146 -> "Uz"
  | 147 -> "U.s"
  | 148 -> "Us"
  | 149 -> "Uh"
  | n -> failwith ("Illegal upper case code : " ^ string_of_int n)
  ]
;
(* Roman with diacritics Unicode - latin extended *)
value canon_uniromcode = fun
  [ 0 -> "-"
  | -10 -> "+"
  | 1  -> "a"
  | 2  -> "&#257;"
  | 3  -> "i"
  | 4  -> "&#299;"
  | 5  -> "u"
  | 6  -> "&#363;"
  | 7  -> "&#7771;"
  | 8  -> "&#7773;"
  | 9  -> "&#7735;"
  | 10 -> "e"
  | 11 -> "ai"
  | 12 -> "o"
  | 13 -> "au"
  | 14 -> "&#7747;" (* anusvaara as m with dot below *)
  | 15 -> "&#7745;" (* candrabindu as m with dot above (?) *)
  | 16 -> "&#7717;"
  | 17 -> "k"
  | 18 -> "kh"
  | 19 -> "g"
  | 20 -> "gh"
  | 21 -> "&#7749;"
  | 22 -> "c"
  | 23 -> "ch"
  | 24 -> "j"
  | 25 -> "jh"
  | 26 -> "&#241;"
  | 27 -> "&#7789;"
  | 28 -> "&#7789;h"
  | 29 -> "&#7693;"
  | 30 -> "&#7693;h"
  | 31 -> "&#7751;"
  | 32 -> "t"
  | 33 -> "th"
  | 34 -> "d"
  | 35 -> "dh"
  | 36 -> "n"
  | 37 -> "p"
  | 38 -> "ph"
  | 39 -> "b"
  | 40 -> "bh"
  | 41 -> "m"
  | 42 -> "y"
  | 43 -> "r"
  | 44 -> "l"
  | 45 -> "v"
  | 46 -> "&#347;"
  | 47 -> "&#7779;"
  | 48 -> "s"
  | 49 -> "h"
  | 50 -> "_"
  | -1 -> "'"
  | -2 -> "[-]" (* amuissement - lopa of current aa- or preceding a- or aa- *)
  | -3 -> "&#257;|a" (* sandhi of aa and (a,aa) *a *)
  | -4 -> "&#257;|i" (* sandhi of aa and (i,ii) *e *)
  | -5 -> "&#257;|u" (* sandhi of aa and (u,uu) *u *)
  | -6 -> "&#257;|r" (* sandhi of aa and .r     *r *)
  | 124 -> failwith "Canon: Unrestored special phoneme j'" 
  | 149 -> failwith "Canon: Unrestored special phoneme h'" 
  | 249 -> failwith "Canon: Unrestored special phoneme h''" 
  | n -> if n<0 then 
            failwith ("Illegal code to canon_unicode : " ^ string_of_int n)
         else ("_" ^ Char.escaped (Char.chr (n-2)))
  ]
;
(* Gives the Unicode representation of the romanisation of word *)
(* [unicode : word -> string] *)
value uniromcode word = 
  let catenate c (s,b) = 
      let b'= c>0 && c<14  (* Phonetics.vowel c *) in
      let protected = if b && b' then " " ^ s else s in
      (canon_uniromcode c ^ protected , b') in
  let (s,_) = List.fold_right catenate word ("",False) in s 
;
value halant = "&#x094D;"
(* and avagraha = "&#x093D;"
   and candrabindu = "&#x310;" *)
(* Numerals to come: 1="x0967;" ... 9="x0966F" *)
;
(* represents a stem word in romanization or VH transliteration *)
value stem_to_string html =
  if html then uniromcode (* UTF8 romanization with diacritics *)
          else decode (* VH *)
;

exception Hiatus
;
value indic_unicode_point = fun
  [ 0  | -10 -> (* - *)  "70"
  | 1  -> (* a *)  "05"
  | 2  -> (* aa *) "06"
  | 3  -> (* i *)  "07"
  | 4  -> (* ii *) "08"
  | 5  -> (* u *)  "09"
  | 6  -> (* uu *) "0A"
  | 7  -> (* .r *) "0B"
  | 8  -> (* .rr *)"60"
  | 9  -> (* .l *) "0C"
  | 10 -> (* e *)  "0F"
  | 11 -> (* ai *) "10"
  | 12 -> (* o *)  "13"
  | 13 -> (* au *) "14"
  | 14 -> (* .m *) "02"
  | 15 -> (* ~~ *) "01"
  | 16 -> (* .h *) "03" 
  | 17 -> (* k *)  "15"
  | 18 -> (* kh *) "16"
  | 19 -> (* g *)  "17"
  | 20 -> (* gh *) "18"
  | 21 -> (* 'n *) "19"
  | 22 -> (* c *)  "1A"
  | 23 -> (* ch *) "1B"
  | 24 -> (* j *)  "1C"
  | 25 -> (* jh *) "1D"
  | 26 -> (* ~n *) "1E"
  | 27 -> (* .t *) "1F"
  | 28 -> (* .th *)"20"
  | 29 -> (* .d *) "21"
  | 30 -> (* .dh *)"22"
  | 31 -> (* .n *) "23"
  | 32 -> (* t *)  "24"
  | 33 -> (* th *) "25"
  | 34 -> (* d *)  "26"
  | 35 -> (* dh *) "27"
  | 36 -> (* n *)  "28"
  | 37 -> (* p *)  "2A"
  | 38 -> (* ph *) "2B"
  | 39 -> (* b *)  "2C"
  | 40 -> (* bh *) "2D"
  | 41 -> (* m *)  "2E"
  | 42 -> (* y *)  "2F"
  | 43 -> (* r *)  "30"
  | 44 -> (* l *)  "32"
  | 45 -> (* v *)  "35"
  | 46 -> (* z *)  "36"
  | 47 -> (* .s *) "37"
  | 48 -> (* s *)  "38"
  | 49 -> (* h *)  "39"
  | 50 -> (* underscore *)  raise Hiatus
  | -1 -> (* avagraha *) "3D"
  | -2 -> "" (* amuissement *)
  | -3 -> "06" (* "aa|a" sandhi of aa and (a,aa) *)
  | -4 -> "0F" (* "aa|i" sandhi of aa and (i,ii) *)
  | -5 -> "13" (* "aa|u" sandhi of aa and (u,uu) *)
  | -6 -> "06" (* sandhi of aa and .r *)
  | c -> if c<0 || c>59 
            then failwith ("Illegal code to dev_unicode: " ^ string_of_int c)
         else "" (* homo index dropped *)
  ]
and matra_indic_unicode_point = fun
714 715
  [ -10   (* + *) (* necessary for word form ending in consonant *)
  | 0  -> (* - *)  "70" (* id for iics *)
huet's avatar
huet committed
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
  | 1  -> (* a *)  "" (* default *)
  | 2  -> (* aa *) "3E"
  | 3  -> (* i *)  "3F"
  | 4  -> (* ii *) "40"
  | 5  -> (* u *)  "41"
  | 6  -> (* uu *) "42"
  | 7  -> (* .r *) "43"
  | 8  -> (* .rr *)"44"
  | 9  -> (* .l *) "62"
  | 10 -> (* e *)  "47"
  | 11 -> (* ai *) "48"
  | 12 -> (* o *)  "4B"
  | 13 -> (* au *) "4C"
  | 15 -> (* ~~ *) "01"
  | c -> failwith ("Illegal code to matra_unicode : " ^ string_of_int c)
  ]
;
(* om 50 udatta 51 anudatta 52 grave 53 acute 54 avagraha 3D .ll 61 
   danda 64 ddanda 65 0 66 1 67 2 68 3 69 4 6A 5 6B 6 6C 7 6D 8 6E 9 6F ° 70 *)
value inject_point s = "&#x09" ^ s ^ ";"
;
value deva_unicode c = 
  let s = indic_unicode_point c in inject_point s 
and matra_unicode c =
  if c=1 then "" (* default *)
  else let s = matra_indic_unicode_point c in inject_point s 
;
(* Gives the Unicode representation of devanagari form of word;          *)
(* ligature construction is left to the font manager handling of halant. *)
(* Beware : word should not carry homophony index - use [code_strip].    *)
(* [unidevcode : word -> string] *)
value unidevcode word = 
  let ligature (s,b) c = (* b memorizes whether last char is consonant *)
     try let code = deva_unicode c in
         if c>16 (* Phonetics.consonant c *) then 
            if b (* add glyph *) then (s ^ halant ^ code,True)
            else (s ^ code,True) 
         else if b then 
              if c=0 (* - *) then (s ^ halant ^ code,False)
              else (* add matra *) let m = matra_unicode c in (s ^ m,False)
         else (s ^ code,False) 
     with (* hiatus represented by space in devanagarii output *)
          [ Hiatus -> (s ^ " ",False) ] in 
  let (s,b) =  List.fold_left ligature ("",False) word in
  if b then s ^ halant (* virama *) else s
;

(*i end; i*)