| 1 |
# 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] (Version 15.1.0, or |
| 13 |
Version 17.0.0 with the `*_17_*` and `*_neo_*` constructors) _Unicode Line Breaking Algorithm_, with options |
| 14 |
to tailor line-breaking behavior for CSS [`line-break`] and [`word-break`] properties. |
| 15 |
- Grapheme cluster segmenter, word segmenter, and sentence segmenter that are compatible with |
| 16 |
[Unicode Standard Annex #29][UAX29] (Version 17.0.0), _Unicode Text Segmentation_. |
| 17 |
|
| 18 |
[UAX14]: https://www.unicode.org/reports/tr14/tr14-51.html |
| 19 |
[UAX29]: https://www.unicode.org/reports/tr29/tr29-47.html |
| 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(Default::default()); |
| 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, options::WordBreakInvariantOptions}; |
| 71 |
|
| 72 |
let segmenter = |
| 73 |
WordSegmenter::new_auto(WordBreakInvariantOptions::default()); |
| 74 |
|
| 75 |
let breakpoints: Vec<usize> = segmenter |
| 76 |
.segment_str("Hello World. Xin chào thế giới!") |
| 77 |
.collect(); |
| 78 |
assert_eq!( |
| 79 |
&breakpoints, |
| 80 |
&[0, 5, 6, 11, 12, 13, 16, 17, 22, 23, 28, 29, 35, 36] |
| 81 |
); |
| 82 |
``` |
| 83 |
|
| 84 |
See [`WordSegmenter`] for more examples. |
| 85 |
|
| 86 |
### Sentence Break |
| 87 |
|
| 88 |
Segment the string into sentences: |
| 89 |
|
| 90 |
```rust |
| 91 |
use icu::segmenter::{ |
| 92 |
SentenceSegmenter, options::SentenceBreakInvariantOptions, |
| 93 |
}; |
| 94 |
|
| 95 |
let segmenter = |
| 96 |
SentenceSegmenter::new(SentenceBreakInvariantOptions::default()); |
| 97 |
|
| 98 |
let breakpoints: Vec<usize> = segmenter |
| 99 |
.segment_str("Hello World. Xin chào thế giới!") |
| 100 |
.collect(); |
| 101 |
assert_eq!(&breakpoints, &[0, 13, 36]); |
| 102 |
``` |
| 103 |
|
| 104 |
See [`SentenceSegmenter`] for more examples. |
| 105 |
|
| 106 |
<!-- cargo-rdme end --> |
| 107 |
|
| 108 |
## More Information |
| 109 |
|
| 110 |
For more information on development, authorship, contributing etc. please visit [`ICU4X home page`](https://github.com/unicode-org/icu4x). |