Debian Codegraph @

third_party/rust/icu_segmenter/README.md

raw
1 # icu_segmenter [![crates.io](https://img.shields.io/crates/v/icu_segmenter)](https://crates.io/crates/icu_segmenter)
2
3 <!-- cargo-rdme start -->
4
5 Segment strings by lines, graphemes, words, and sentences.
6
7 This module is published as its own crate ([`icu_segmenter`](https://docs.rs/icu_segmenter/latest/icu_segmenter/))
8 and as part of the [`icu`](https://docs.rs/icu/latest/icu/) crate. See the latter for more details on the ICU4X project.
9
10 This module contains segmenter implementation for the following rules.
11
12 - Line segmenter that is compatible with [Unicode Standard Annex #14][UAX14], _Unicode Line
13 Breaking Algorithm_, with options to tailor line-breaking behavior for CSS [`line-break`] and
14 [`word-break`] properties.
15 - Grapheme cluster segmenter, word segmenter, and sentence segmenter that are compatible with
16 [Unicode Standard Annex #29][UAX29], _Unicode Text Segmentation_.
17
18 [UAX14]: https://www.unicode.org/reports/tr14/
19 [UAX29]: https://www.unicode.org/reports/tr29/
20 [`line-break`]: https://drafts.csswg.org/css-text-3/#line-break-property
21 [`word-break`]: https://drafts.csswg.org/css-text-3/#word-break-property
22
23 ## Examples
24
25 ### Line Break
26
27 Find line break opportunities:
28
29 ```rust
30 use icu::segmenter::LineSegmenter;
31
32 let segmenter = LineSegmenter::new_auto();
33
34 let breakpoints: Vec<usize> = segmenter
35 .segment_str("Hello World. Xin chào thế giới!")
36 .collect();
37 assert_eq!(&breakpoints, &[0, 6, 13, 17, 23, 29, 36]);
38 ```
39
40 See [`LineSegmenter`] for more examples.
41
42 ### Grapheme Cluster Break
43
44 Find all grapheme cluster boundaries:
45
46 ```rust
47 use icu::segmenter::GraphemeClusterSegmenter;
48
49 let segmenter = GraphemeClusterSegmenter::new();
50
51 let breakpoints: Vec<usize> = segmenter
52 .segment_str("Hello World. Xin chào thế giới!")
53 .collect();
54 assert_eq!(
55 &breakpoints,
56 &[
57 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
58 19, 21, 22, 23, 24, 25, 28, 29, 30, 31, 34, 35, 36
59 ]
60 );
61 ```
62
63 See [`GraphemeClusterSegmenter`] for more examples.
64
65 ### Word Break
66
67 Find all word boundaries:
68
69 ```rust
70 use icu::segmenter::WordSegmenter;
71
72 let segmenter = WordSegmenter::new_auto();
73
74 let breakpoints: Vec<usize> = segmenter
75 .segment_str("Hello World. Xin chào thế giới!")
76 .collect();
77 assert_eq!(
78 &breakpoints,
79 &[0, 5, 6, 11, 12, 13, 16, 17, 22, 23, 28, 29, 35, 36]
80 );
81 ```
82
83 See [`WordSegmenter`] for more examples.
84
85 ### Sentence Break
86
87 Segment the string into sentences:
88
89 ```rust
90 use icu::segmenter::SentenceSegmenter;
91
92 let segmenter = SentenceSegmenter::new();
93
94 let breakpoints: Vec<usize> = segmenter
95 .segment_str("Hello World. Xin chào thế giới!")
96 .collect();
97 assert_eq!(&breakpoints, &[0, 13, 36]);
98 ```
99
100 See [`SentenceSegmenter`] for more examples.
101
102 <!-- cargo-rdme end -->
103
104 ## More Information
105
106 For more information on development, authorship, contributing etc. please visit [`ICU4X home page`](https://github.com/unicode-org/icu4x).