| 1 |
# Unicode Text Segmentation for Go |
| 2 |
|
| 3 |
This is an implementation of the Unicode Text Segmentation specification for Go. |
| 4 |
Specifically, it currently includes only the "grapheme cluster" segmentation |
| 5 |
algorithm. |
| 6 |
|
| 7 |
## Unicode Version Support |
| 8 |
|
| 9 |
Each major version of Unicode includes a set of tables that define how each |
| 10 |
codepoint participates in the segmentation algorithms. Therefore any caller |
| 11 |
of this library must select a specific version of Unicode to support. |
| 12 |
|
| 13 |
To allow for each caller to make that decision separately even though |
| 14 |
multiple callers may coexist in the same program, there is a separate |
| 15 |
major release of this module for each supported major Unicode version. |
| 16 |
Therefore you can select the specific version you want by module |
| 17 |
path. For example, to use the algorithm and tables defined by Unicode |
| 18 |
version 13: |
| 19 |
|
| 20 |
``` |
| 21 |
go get github.com/apparentlymart/go-textseg/v13 |
| 22 |
``` |
| 23 |
|
| 24 |
```go |
| 25 |
import ( |
| 26 |
"github.com/apparentlymart/go-textseg/v13/textseg" |
| 27 |
) |
| 28 |
``` |
| 29 |
|
| 30 |
However, each release of Go also includes some Unicode-version-specific |
| 31 |
functionality and you may prefer to use the text segmentation definitions |
| 32 |
that are relevant to the version of Unicode that your Go runtime is |
| 33 |
using elsewhere. To enable that, this repository has a special separate |
| 34 |
module which uses the current Go runtime version to select a suitable |
| 35 |
versioned implementation automatically: |
| 36 |
|
| 37 |
``` |
| 38 |
go get github.com/apparentlymart/go-textseg/autoversion |
| 39 |
``` |
| 40 |
|
| 41 |
```go |
| 42 |
import ( |
| 43 |
"github.com/apparentlymart/go-textseg/autoversion/textseg" |
| 44 |
) |
| 45 |
``` |
| 46 |
|
| 47 |
**IMPORTANT:** This "autoversion" wrapper uses Go build tags to select |
| 48 |
a `go-textseg` major version based on the current Go version. We use |
| 49 |
this strategy to ensure that only one version of `go-textseg` will |
| 50 |
be compiled into your program, but the downside is that `go-textseg` |
| 51 |
must be updated for each new Go release. If you use this library in |
| 52 |
your program, you will need to fetch a new version of it each time |
| 53 |
you switch to a new version of Go, even if that version of Go does |
| 54 |
not introduce a new Unicode version. |
| 55 |
|
| 56 |
## Usage |
| 57 |
|
| 58 |
The most important function in each `textseg` package is |
| 59 |
`ScanGraphemeClusters`, which is a function compatible with the |
| 60 |
signature of `bufio.Scanner` in the Go standard library. Each |
| 61 |
time the `Scan` function is called, the function will produce one |
| 62 |
full grapheme cluster. |