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
Release v0.9.0-8-g978e7a3 – %%MAINTAINER%%
val is_uchar :
int -> bool
cpis an Unicode scalar value.
val unicode_version :
unicode_versionis the Unicode version supported by
[ `Custom of custom
| `Word ]
`Grapheme_clusterdetermines extended grapheme clusters boundaries according to UAX 29 (corresponds, for most scripts, to user-perceived characters).
`Worddetermines word boundaries according to UAX 29.
`Sentencedetermines sentence boundaries according to UAX 29.
`Line_breakdetermines mandatory line breaks and line break opportunities according to UAX 14.
val pp_boundary :
Format.formatter -> boundary -> unit
pp_boundary ppf bprints an unspecified representation of
[ `Await | `Boundary | `End | `Uchar of uchar ]
val create :
[< boundary ] -> t
create bis an Unicode text segmenter for boundaries of type
val boundary :
t -> boundary
boundary sis the type of boundaries detected by
val add :
t -> [ `Await | `End | `Uchar of uchar ] -> ret
add s vis:
`Boundaryif there is a boundary at that point in the sequence of characters. The client must then call
uis the next character in the sequence. The client must then call
`Awaitwhen the segmenter is ready to add a new
`Endwas added and all
`Uchar u to add a new character to the sequence to
`End to signal the end of sequence. After adding one
of these two values always call
`End is returned.
`End is added while
that last add did not return
`Await or if an
is added after an
`End was already added.
val mandatory :
t -> bool
trueif the last
Uuseg.addwas mandatory. This function only makes sense for
`Customsegmenters that sport that notion. For other segmenters or if no
`Boundarywas returned so far,
val copy :
t -> t
copy sis a copy of
sin its current state. Subsequent
sdo not affect the copy.
val pp_ret :
Format.formatter -> [< ret ] -> unit
pp_ret ppf vprints an unspecified representation of
val custom :
?mandatory:('a -> bool) ->
create:(unit -> 'a) ->
copy:('a -> 'a) ->
add:('a -> [ `Await | `End | `Uchar of uchar ] -> ret) ->
unit -> custom
create ~mandatory ~name ~create ~copy ~addis 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 the
Uuseg.mandatoryfunction. Defaults always returns
addis called with the segmenter value to define the result of the
Uuseg.addvalue. The returned value should respect the semantics of
Uuseg.add. Use the functions
Uuseg.adds error cases.
val err_exp_await :
[< ret ] -> 'a
err_exp_await fndshould be used by custom segmenters when the client tries to
`Endwhile the last returned value was not an
val err_ended :
[< ret ] -> 'a
err_ended ()should be used by custom segmenter when the client tries to
`Endwas already added.
`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
A segmenter is a stateful filter that inputs a sequence of characters
and outputs the same sequence except characters are interleaved
`Boundary values whenever the segmenter detects a boundary.
Uuseg.create returns a new segmenter for a given boundary
To add characters to the sequence to segment, call
let words = Uuseg.create `Word
`Uchar _. To end the sequence call
`End. The segmented sequence of characters is returned character by character, interleaved with
`Boundaryvalues at the appropriate places, by the successive calls to
The client and the segmenter must wait on each other to limit
internal buffering: each time the client adds to the sequence
`End it must continue to
`Await until the segmenter returns
`End. In practice this leads to the following kind of control flow:
For example to segment the sequence <
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 -> acc
"a b") to a list of characters interleaved with
`Bvalues on word boundaries we can write:
let seq = [`Uchar 0x0041; `Uchar 0x0020; `Uchar 0x0042] let seq_words = List.rev (add (List.fold_left add  seq) `End)
utf_8_segments seg s is the list of UTF-8 encoded
seg segments of
the UTF-8 encoded string
s. This example uses
Uutf to fold over
the characters of
s and to encode the characters in a standard
OCaml buffer .
Note that this function can derived directly from
let utf_8_segments seg s = let b = Buffer.create 42 in let flush_segment acc = let segment = Buffer.contents b in Buffer.clear b; if segment = "" then acc else segment :: acc in let seg = Uuseg.create (seg :> Uuseg.boundary) in let rec add acc v = match Uuseg.add seg v with | `Uchar u -> Uutf.Buffer.add_utf_8 b u; add acc `Await | `Boundary -> add (flush_segment acc) `Await | `Await -> acc in let rec uchar acc _ = function | `Uchar _ as u -> add acc u | `Malformed _ -> add acc (`Uchar Uutf.u_rep) in List.rev (flush_segment (add (Uutf.String.fold_utf_8 uchar  s) `End))