package uuseg
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha512=355139aee2a72baddf3d811e522948456147546ee946b6eca20f57711865770d4b8d32ea01a7338b8e6cdedb4423ee65cee387704bb9c0c057bcbd65012679b8
doc/uuseg/Uuseg/index.html
Module Uuseg
Unicode text segmentation.
Uuseg segments Unicode text. It implements the locale independent Unicode text segmentation algorithms to detect grapheme cluster, word and sentence boundaries and the Unicode line breaking algorithm to detect line break opportunities.
The module is independent from any IO mechanism or Unicode text data structure and it can process text without a complete in-memory representation.
The supported Unicode version is determined by the unicode_version value.
Consult the basics, limitations and examples of use.
References
- The Unicode Standard. (latest version)
- UAX #29 Unicode Text Segmentation. (latest version)
- UAX #14 Unicode Line Breaking Algorithm. (latest version)
- Web based ICU break utility.
Segment
The type for custom segmenters. See custom.
The type for boundaries.
val pp_boundary : Format.formatter -> boundary -> unitpp_boundary ppf b prints an unspecified representation of b on ppf.
add s v is:
`Boundaryif there is a boundary at that point in the sequence of characters. The client must then calladdwith`Awaituntil`Awaitis returned.`Uchar uifuis the next character in the sequence. The client must then calladdwith`Awaituntil`Awaitis returned.`Awaitwhen the segmenter is ready to add a new`Ucharor`End.`Endwhen`Endwas added and all`Boundaryand`Ucharwere output.
For v use `Uchar u to add a new character to the sequence to segment and `End to signal the end of sequence. After adding one of these two values always call add with `Await until `Await or `End is returned.
val mandatory : t -> boolmandatory s is true if the last `Boundary returned by add was mandatory. This function only makes sense for `Line_break segmenters or `Custom segmenters that sport that notion. For other segmenters or if no `Boundary was returned so far, true is returned.
copy s is a copy of s in its current state. Subsequent adds on s do not affect the copy.
val pp_ret : Format.formatter -> [< ret ] -> unitpp_ret ppf v prints an unspecified representation of v on ppf.
Custom segmenters
val custom :
?mandatory:('a -> bool) ->
name:string ->
create:(unit -> 'a) ->
copy:('a -> 'a) ->
add:('a -> [ `Uchar of Uchar.t | `Await | `End ] -> ret) ->
unit ->
customcreate ~mandatory ~name ~create ~copy ~add is a custom segmenter.
nameis a name to identify the segmenter.createis called when the segmenter is created it should return a custom segmenter value.copyis called with the segmenter value whenever the segmenter is copied. It should return a copy of the segmenter value.mandatoryis called with the segmenter value to define the result of themandatoryfunction. Defaults always returnstrue.addis called with the segmenter value to define the result of theaddvalue. The returned value should respect the semantics ofadd. Use the functionserr_exp_awaitanderr_endedto raiseInvalid_argumentexception inadds error cases.
val err_exp_await : [< ret ] -> 'aerr_exp_await fnd should be used by custom segmenters when the client tries to add an `Uchar or `End while the last returned value was not an `Await.
val err_ended : [< ret ] -> 'aerr_ended () should be used by custom segmenter when the client tries to add `Uchar or `End after `End was already added.
Limitations
A `Grapheme_cluster segmenter will always consume only a small bounded amount of memory on any text. Other segmenters will also do so on non-degenerate text, but it's possible to feed them with input that will make them buffer an arbitrary amount of characters.
Basics
A segmenter is a stateful filter that inputs a sequence of characters and outputs the same sequence except characters are interleaved with `Boundary values whenever the segmenter detects a boundary.
The function create returns a new segmenter for a given boundary type:
let words = Uuseg.create `WordTo add characters to the sequence to segment, call add on words with `Uchar _. To end the sequence call add on words with `End. The segmented sequence of characters is returned character by character, interleaved with `Boundary values at the appropriate places, by the successive calls to add.
The client and the segmenter must wait on each other to limit internal buffering: each time the client adds to the sequence by calling add with `Uchar or `End it must continue to call add with `Await until the segmenter returns `Await or `End. In practice this leads to the following kind of control flow:
let rec add acc v = match Uuseg.add words v with
| `Uchar u -> add (`Uchar u :: acc) `Await
| `Boundary -> add (`B :: acc) `Await
| `Await | `End -> accFor example to segment the sequence <U+0041, U+0020, U+0042> ("a b") to a list of characters interleaved with `B values on word boundaries we can write:
let uchar = `Uchar (Uchar.of_int u)
let seq = [uchar 0x0041; uchar 0x0020; uchar 0x0042]
let seq_words = List.rev (add (List.fold_left add [] seq) `End)Examples
utf_8_segments seg s is the list of UTF-8 encoded seg segments of the UTF-8 encoded string s.
let utf_8_segments seg s =
let flush_segment buf acc =
let segment = Buffer.contents buf in
Buffer.clear buf; if segment = "" then acc else segment :: acc
in
let rec add buf acc segmenter v = match Uuseg.add segmenter v with
| `Uchar u -> Buffer.add_utf_8_uchar buf u; add buf acc segmenter `Await
| `Boundary -> add buf (flush_segment buf acc) segmenter `Await
| `Await | `End -> acc
in
let rec loop buf acc s i max segmenter =
if i > max then flush_segment buf (add buf acc segmenter `End) else
let dec = String.get_utf_8_uchar s i in
let acc = add buf acc segmenter (`Uchar (Uchar.utf_decode_uchar dec)) in
loop buf acc s (i + Uchar.utf_decode_length dec) max segmenter
in
let buf = Buffer.create 42 in
let segmenter = Uuseg.create seg in
List.rev (loop buf [] s 0 (String.length s - 1) segmenter)Note that this function can be derived directly from Uuseg_string.fold_utf_8.