padapatha.ml 9.63 KB
Newer Older
huet's avatar
huet committed
1 2 3 4 5 6
(**************************************************************************)
(*                                                                        *)
(*                     The Sanskrit Heritage Platform                     *)
(*                                                                        *)
(*                       Gérard Huet & Pawan Goyal                        *)
(*                                                                        *)
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
(**************************************************************************)
(* Chunking mechanism for guessing partial padapatha form from list of chunks *)

value sanskrit_chunk encode s = 
  match encode s with (* avagraha reverts to a *)
  [ [ -1 :: l ] -> [ 1 :: l ]  (* only initial avagraha reverts to a *)
  | x -> x
  ]
;
(* Preprocessing of corpus to prepare padapatha form from list of chunks   *)
(* This is extremely important from the segmenter complexity point of view *)
(* Since it takes hints at parallel treatment from non-ambiguous blanks. *)

exception Hiatus 
;
exception Glue
;
(* We raise Glue below when there are multiple ways to obtain the current break,
   in which case we do not profit of the sandhi hint. Furthermore, this is 
   incomplete, notably when one of the sandhied forms is a vocative. *)
(* Chunk [w] is adjusted for padapatha in view of next character [c] *)
(* No attempt is made to change [c] and thus tacchrutvaa is not chunkable. *)
(* This function defines the maximal separability of devanaagarii into chunks
   but is not always able to go as far as creating the full padapaa.tha *)
value adjust c w = match Word.mirror w with 
  [ [] -> failwith "adjust"
  | [ last :: rest ] -> match last with
        [ 14 (* .m *) -> Word.mirror [ 41 (* m *) :: rest ] (* revert .m to m *)
             (* note: .m coming from sandhi of n is followed by sibilant 
                and chunking is allowed only after this sibilant *)
        | 12 (* o *) -> if rest = [ 40 ] (* bh from bhos -> bho *) then 
                           Encode.code_string "bhos" (* "bho raama" "bho bhos" *)
                        else if rest = [ 49; 1 ] (* aho *) then 
                           Encode.code_string "aho" (* "bho raama" "bho bhos" *)
                        else if Phonetics.turns_visarg_to_o c || c=1 
                             (* zivoham must be entered as zivo'ham (avagraha) *)
                             then Word.mirror [ 16 :: [ 1 :: rest ] ] 
                             (* restore visarga, assuming original a.h form *)
                        else w 
        | 1 (* a *) -> if c=1 then w else
                       if Phonetics.vowel c then raise Hiatus else w
        | 2 (* aa *) -> if Phonetics.vowel c then raise Hiatus else 
                        if Phonetics.elides_visarg_aa c then raise Hiatus else w 
                        (* NB "baalaa devaa" must be written "baalaadevaa" *)
                        (* but also "tathaa hi" problematic *)
                        (* Worse "raama aadhaara.h" not parsable with vocative *)
                        (* also "vaa are" not analysed *)
        | 4 (* ii *) (* possible visarga vanishes, original vowel may be short *)
        | 6 (* uu *) -> if c=43 (* r *) then raise Glue else w
        (* next 4 rules attempt to revert [last] to 'd' in view of [c] *)
        | 34 (* d *) -> if c=35 (* dh *) then raise Glue else 
                        if Phonetics.is_voiced c 
                           then Word.mirror [ 32 :: rest ] (* d -> t *)
                           else w
        | 24 (* j *) -> if Phonetics.turns_t_to_j c (* tat+jara -> tajjara *)
                           then Word.mirror [ 32 :: rest ] (* j -> t *)
                           else w
        | 26 (* ~n *) -> match rest with
             [ [ 26 (* ~n *) :: ante ] -> match ante with
                 (* optional doubling of ~n in front of vowel *)
                 [ [ v :: _ ] -> if Phonetics.short_vowel v && Phonetics.vowel c
                                    then Word.mirror rest 
                                    else failwith "padapatha"
                 | _ -> failwith "padapatha"
                 ]
             | _ -> if c=23 (* ch could come from ch or z *)
                       then raise Glue 
                    else if Phonetics.turns_n_to_palatal c 
                            (* taan+zaastravimukhaan -> taa~nzaastravimukhaan *)
                         then Word.mirror [ 36 (* n *) :: rest ] (* n -> ~n *)
                         else w 
             ]
        | 29 (* .d *) -> if c=30 (* .dh *) then raise Glue else 
                         if Phonetics.is_voiced c 
                           then Word.mirror [ 27 :: rest ] (* .d -> .t *)
                           else w
        | 39 (* b *) -> if c=40 (* bh *) then raise Glue else 
                        if Phonetics.is_voiced c 
                           then Word.mirror [ 37 :: rest ] (* b -> p *)
                           else w
        | 19 (* g *) -> if c=20 (* gh *) then raise Glue else 
                        if Phonetics.is_voiced c (* vaak+vazya *)
                           then Word.mirror [ 17 :: rest ] (* g -> k *)
                           else w
        | 36 (* n *) -> match rest with
             [ [ 36 (* n *) :: ante ] -> match ante with
                   (* optional doubling of n in front of vowel *)
                 [ [ v :: _ ] -> if Phonetics.short_vowel v && Phonetics.vowel c
                                    then Word.mirror rest (* gacchann eva *)
                                 else w
                 | _ -> failwith "padapatha"
                 ]
             | _ -> if c=36 (* n *) || c=41 (* m *)
                       then raise Glue (* since d|m->nn and n|m -> nm *)
                         (* Word.mirror [ 32 :: rest ] (* n -> t *) *)
103
                         (* incompleteness: raajan naasiin vocatif raajan *)
huet's avatar
huet committed
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
                    else w
             ]
        | 22 (* c *) -> if c=22 then Word.mirror [ 32 :: rest ] (* c -> t *)
                        else if c=23 (* ch could come from ch or z *)
                             then raise Glue else w
        | 44 (* l *) -> if c=last 
                           then Word.mirror [ 32 :: rest ] (* l -> t *)
                           else w
        | 21 (* f *) -> match rest with
             [ [ 21 (* f *) :: ante ] -> match ante with
                   (* optional doubling of f in front of vowel *)
                 [ [ v :: _ ] -> if Phonetics.short_vowel v && Phonetics.vowel c
                                    then Word.mirror rest 
                                 else failwith "padapatha"
                 | _ -> failwith "padapatha"
                 ]
             | _ -> if c=41 (* m *) (* vaak+mayi *)
                            then Word.mirror [ 17 :: rest ] (* f -> k *)
                            else w
             ]
        (* NB if last is y, r or v and c is vowel, then it may come from resp. 
           {i,ii}, {.r,.rr}, {u,uu} and this choice means that we cannot make 
           a chunk break here *)
        | 42 (* y *) | 45 (* v *) -> if Phonetics.vowel c then raise Glue 
                                     else w (* will fail *)
        | 43 (* r *) -> if Phonetics.turns_visarg_to_o c || Phonetics.vowel c 
                           then Word.mirror [ 16 :: rest ] (* visarg restored *)
                        else w (* pb punar pitar etc *)
        | 46 (* z *) -> match rest with
              [ [ 14 (* .m *) :: b ] -> if c=22 || c=23 (* c ch *) then
                                           Word.mirror [ 36 (* n *) :: b ] 
                                        else w 
              | [ 26 (* ~n *) :: _ ] -> if c=46 (* z *) then
                                           Word.mirror [ 36 (* n *) :: rest ] 
                                        else w 
                                        (* c=23 (* ch *) could come from z *)
              | _ -> if c=22 || c=23 (* c ch *) then
                     Word.mirror [ 16 (* .h *) :: rest ] else w
              ]
        | 47 (* .s *) -> match rest with
              [ [ 14 (* .m *) :: b ] -> if c=27 || c=28 (* .t .th *) then
                                        Word.mirror [ 36 (* n *) :: b ] else w 
              | _ -> w
              ]
        | 48 (* s *) -> match rest with
149 150 151 152
              [ [ 14 (* .m *) :: b ] -> if c=32 || c=33 (* t th *) then
                                        Word.mirror [ 36 (* n *) :: b ] else w 
              | _ -> w
              ]
huet's avatar
huet committed
153 154 155 156
        | _ -> w
        ]
  ] 
;
157 158
(* Called from [Sanskrit.read_processed_skt_stream] for use in [read_sanskrit]
   with argument [read_chunk=sanskrit_chunk encode] *)
huet's avatar
huet committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
value padapatha read_chunk l = (* l is list of chunks separated by blanks *)
                     (* returns padapatha as list of forms in terminal sandhi *)
  let rec pad_rec = fun (* returns (c,l) with c first char of first pada in l *)
    [ [] -> (-1,[])
    | [ chk :: chks ] -> 
      let (c,padas) = pad_rec chks 
      and w = read_chunk chk (* initial avagraha reverts to a *) in 
      (List.hd w (* next c *), 
       try let pada = if c=(-1) then w (* last chunk *)
                      else adjust c w in
           [ pada :: padas ]
       with 
        [ Hiatus -> match padas with 
          [ [] -> failwith "padapatha"
          | [ p :: lp ] -> let conc = w @ [ 50 :: p ] in (* [w_p] *)
                           [ conc :: lp ] (* hiatus indicates a word boundary *)
          ]
        | Glue -> match padas with 
          [ [] -> failwith "padapatha"
          | [ p :: lp ] -> let conc = w @ p in
                           [ conc :: lp ] (* we lose the boundary indication *)
          ]
181
        ])
huet's avatar
huet committed
182 183 184 185 186
    ] in
  let (_,padas) = pad_rec l in padas
;