package saga

  1. Overview
  2. Docs

Source file unicode.ml

1
2
3
4
5
6
7
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
type normalization =
  | NFC (* Canonical Decomposition, followed by Canonical Composition *)
  | NFD (* Canonical Decomposition *)
  | NFKC (* Compatibility Decomposition, followed by Canonical Composition *)
  | NFKD (* Compatibility Decomposition *)

type char_category =
  | Letter
  | Number
  | Punctuation
  | Symbol
  | Whitespace
  | Control
  | Other

let categorize_char u =
  match Uucp.Gc.general_category u with
  | `Lu | `Ll | `Lt | `Lm | `Lo -> Letter
  | `Nd | `Nl | `No -> Number
  | `Pc | `Pd | `Ps | `Pe | `Pi | `Pf | `Po -> Punctuation
  | `Sm | `Sc | `Sk | `So -> Symbol
  | `Zs | `Zl | `Zp -> Whitespace
  | `Cc | `Cf | `Cs | `Co | `Cn -> Control
  | _ -> Other

let is_whitespace u =
  match categorize_char u with Whitespace -> true | _ -> false

let is_punctuation u =
  match categorize_char u with Punctuation -> true | _ -> false

let is_word_char u =
  match categorize_char u with Letter | Number -> true | _ -> false

let is_cjk u =
  let cp = Uchar.to_int u in
  (* Common CJK ranges *)
  (cp >= 0x4E00 && cp <= 0x9FFF)
  (* CJK Unified Ideographs *)
  || (cp >= 0x3400 && cp <= 0x4DBF)
  (* CJK Extension A *)
  || (cp >= 0x3040 && cp <= 0x309F)
  (* Hiragana *)
  || (cp >= 0x30A0 && cp <= 0x30FF)
  ||
  (* Katakana *)
  (cp >= 0xAC00 && cp <= 0xD7AF)
(* Hangul Syllables *)

let normalize form text =
  try
    let b = Buffer.create (String.length text) in
    let add u = Uutf.Buffer.add_utf_8 b u in

    (* Apply normalization based on form *)
    let normalize_char =
      match form with
      | NFC ->
          fun u ->
            (* For now, just pass through - proper implementation needs full
               Unicode tables *)
            add u
      | NFD ->
          fun u ->
            (* Decompose but don't recompose *)
            add u
      | NFKC ->
          fun u ->
            (* Compatibility decomposition + composition *)
            add u
      | NFKD ->
          fun u ->
            (* Compatibility decomposition *)
            add u
    in

    (* Process each character *)
    let decoder = Uutf.decoder (`String text) in
    let rec loop () =
      match Uutf.decode decoder with
      | `Uchar u ->
          normalize_char u;
          loop ()
      | `End -> ()
      | `Malformed _ ->
          (* Skip malformed sequences *)
          loop ()
      | `Await -> assert false
    in
    loop ();
    Buffer.contents b
  with e ->
    Nx_core.Error.invalid ~op:"normalize" ~what:"unicode in text"
      ~reason:(Printexc.to_string e) ()

let case_fold text =
  try
    let b = Buffer.create (String.length text) in
    let decoder = Uutf.decoder (`String text) in
    let rec loop () =
      match Uutf.decode decoder with
      | `Uchar u ->
          (* Use Uucp for proper case folding *)
          let folded =
            match Uucp.Case.Fold.fold u with `Self -> [ u ] | `Uchars us -> us
          in
          List.iter (Uutf.Buffer.add_utf_8 b) folded;
          loop ()
      | `End -> ()
      | `Malformed _ -> loop ()
      | `Await -> assert false
    in
    loop ();
    Buffer.contents b
  with e ->
    Nx_core.Error.invalid ~op:"case_fold" ~what:"unicode in text"
      ~reason:(Printexc.to_string e) ()

let strip_accents text =
  try
    let b = Buffer.create (String.length text) in
    let decoder = Uutf.decoder (`String text) in
    let rec loop () =
      match Uutf.decode decoder with
      | `Uchar u ->
          (* Check if it's a combining diacritical mark *)
          if
            not
              (Uucp.Gc.general_category u = `Mn
              || Uucp.Gc.general_category u = `Mc
              || Uucp.Gc.general_category u = `Me)
          then Uutf.Buffer.add_utf_8 b u;
          loop ()
      | `End -> ()
      | `Malformed _ -> loop ()
      | `Await -> assert false
    in
    loop ();
    Buffer.contents b
  with e ->
    Nx_core.Error.invalid ~op:"strip_accents" ~what:"unicode in text"
      ~reason:(Printexc.to_string e) ()

let clean_text ?(remove_control = true) ?(normalize_whitespace = true) text =
  try
    let b = Buffer.create (String.length text) in
    let decoder = Uutf.decoder (`String text) in
    let last_was_space = ref false in

    let rec loop () =
      match Uutf.decode decoder with
      | `Uchar u ->
          let cat = categorize_char u in
          (match cat with
          | Control when remove_control -> () (* Skip control characters *)
          | Whitespace when normalize_whitespace ->
              if not !last_was_space then (
                Uutf.Buffer.add_utf_8 b (Uchar.of_int 0x20);
                (* Regular space *)
                last_was_space := true)
          | _ ->
              Uutf.Buffer.add_utf_8 b u;
              last_was_space := false);
          loop ()
      | `End -> ()
      | `Malformed _ -> loop ()
      | `Await -> assert false
    in
    loop ();

    (* Trim result *)
    let result = Buffer.contents b in
    if normalize_whitespace then String.trim result else result
  with e ->
    Nx_core.Error.invalid ~op:"clean_text" ~what:"unicode in text"
      ~reason:(Printexc.to_string e) ()

let split_words text =
  try
    let words = ref [] in
    let current_word = Buffer.create 16 in
    let decoder = Uutf.decoder (`String text) in

    let flush_word () =
      let word = Buffer.contents current_word in
      if word <> "" then words := word :: !words;
      Buffer.clear current_word
    in

    let rec loop () =
      match Uutf.decode decoder with
      | `Uchar u ->
          if is_cjk u then (
            (* For CJK, flush current word and treat each character as a word *)
            flush_word ();
            Uutf.Buffer.add_utf_8 current_word u;
            flush_word ())
          else if is_word_char u then Uutf.Buffer.add_utf_8 current_word u
          else flush_word ();
          loop ()
      | `End ->
          flush_word ();
          List.rev !words
      | `Malformed _ -> loop ()
      | `Await -> assert false
    in
    loop ()
  with e ->
    Nx_core.Error.invalid ~op:"split_words" ~what:"unicode in text"
      ~reason:(Printexc.to_string e) ()

let grapheme_count text =
  try
    let decoder = Uutf.decoder (`String text) in
    let count = ref 0 in
    let in_grapheme = ref false in

    let rec loop () =
      match Uutf.decode decoder with
      | `Uchar u ->
          (* Simple approximation - proper implementation needs grapheme
             segmentation *)
          let is_combining =
            match Uucp.Gc.general_category u with
            | `Mn | `Mc | `Me -> true
            | _ -> false
          in
          if not is_combining then (
            incr count;
            in_grapheme := true);
          loop ()
      | `End -> !count
      | `Malformed _ -> loop ()
      | `Await -> assert false
    in
    loop ()
  with e ->
    Nx_core.Error.invalid ~op:"grapheme_count" ~what:"unicode in text"
      ~reason:(Printexc.to_string e) ()

let is_valid_utf8 text =
  let decoder = Uutf.decoder ~encoding:`UTF_8 (`String text) in
  let rec loop () =
    match Uutf.decode decoder with
    | `Uchar _ -> loop ()
    | `End -> true
    | `Malformed _ -> false
    | `Await -> assert false
  in
  loop ()

let remove_emoji text =
  try
    let b = Buffer.create (String.length text) in
    let decoder = Uutf.decoder (`String text) in
    let rec loop () =
      match Uutf.decode decoder with
      | `Uchar u ->
          let cat = categorize_char u in
          if cat <> Symbol then Uutf.Buffer.add_utf_8 b u;
          loop ()
      | `End -> ()
      | `Malformed _ -> loop ()
      | `Await -> assert false
    in
    loop ();
    Buffer.contents b
  with e ->
    Nx_core.Error.invalid ~op:"remove_emoji" ~what:"unicode in text"
      ~reason:(Printexc.to_string e) ()