1
1
Fork
You've already forked wordcut-engine
0
A word segmentation library in Rust for Southeast Asian and other languages. Currently, it supports Chinese, Japanese, Khmer, Myanmar (Burmese), Lao, Shan, Thai, and other space-delimited languages.
  • Rust 99%
  • Shell 1%
2025年06月28日 18:25:49 +07:00
data feat: add Shan language 2024年05月13日 01:26:21 +07:00
example example 2021年07月27日 13:42:05 +07:00
script feat: mix wordlists 2024年05月02日 13:20:17 +07:00
src Extract edge 2025年06月28日 18:25:49 +07:00
.gitignore Ignore Emacs backup files 2025年06月28日 18:23:53 +07:00
.gitpod.yml Add gitpod 2022年03月10日 06:38:47 +00:00
Cargo.toml Update regex-automata 2025年06月28日 16:22:41 +07:00
Lao-Dictionary-LICENSE.txt feat: import language resources from Chamkho 2023年04月09日 17:11:36 +07:00
LICENSE Update LICENSE to match tdict 2025年06月28日 16:22:28 +07:00
LICENSE-cjdict Update LICENSE to match tdict 2025年06月28日 16:22:28 +07:00
LICENSE-khmerdict feat: add Khmer wordlist and a cluster rule 2022年12月13日 11:02:35 +07:00
LICENSE-laowords Update LICENSE to match tdict 2025年06月28日 16:22:28 +07:00
LICENSE-myanmar-dict feat: import language resources from Chamkho 2023年04月09日 17:11:36 +07:00
LICENSE-shan Update LICENSE to match tdict 2025年06月28日 16:22:28 +07:00
LICENSE-tdict Update LICENSE to match tdict 2025年06月28日 16:22:28 +07:00
README.md Update README.md 2025年06月28日 11:35:23 +02:00
repl.md docs: fix cluster rules path 2024年02月19日 20:46:37 +07:00

wordcut-engine

A word segmentation library in Rust for Southeast Asian and other languages. Currently, it supports Chinese, Japanese, Khmer, Myanmar (Burmese), Lao, Shan, Thai, and other space-delimited languages.

Example

usewordcut_engine::load_dict;usewordcut_engine::Wordcut;usestd::path::Path;fn main(){letdict_path=Path::new(concat!(env!("CARGO_MANIFEST_DIR"),"/dict.txt"));letdict=load_dict(dict_path).unwrap();letwordcut=Wordcut::new(dict);println!("{}",wordcut.put_delimiters("หมากินไก่","|"));}

For Evcxr

Load this project as a dependency

:dep .

Import symbols

usewordcut_engine::load_dict;usewordcut_engine::Wordcut;usewordcut_engine::Dict;usestd::path::Path;

Initialize

letdict: Dict=load_dict("data/thai.txt").unwrap();letwordcut=Wordcut::new(dict);

Running

lettxt="หมากินไก่";wordcut.put_delimiters(txt,"|")wordcut.build_path(txt,&txt.chars().collect::<Vec<_>>())dbg!(wordcut.build_path(txt,&txt.chars().collect::<Vec<_>>()));

Algorithm

wordcut-engine has three steps:

  1. Identifying clusters, which are substrings that must not be split
  2. Identifying edges of split directed acyclic graph (split-DAG); The program does not add edges that break any cluster to the graph.
  3. Tokenizing a string by finding the shortest path in the split-DAG

Identifying clusters

Identifying clusters identify which substrings that must not be split.

  1. Wrapping regular expressions with parentheses

For example,

[ก-ฮ]็
[ก-ฮ][่-๋]
[ก-ฮ][่-๋][ะาํา]

The above rules are wrapped with parentheses as shown below:

([ก-ฮ]็)
([ก-ฮ][่-๋])
([ก-ฮ][่-๋][ะาํา])
  1. Joining regular expressions with vertical bars (|)

for example,

([ก-ฮ]็)|([ก-ฮ][่-๋])|([ก-ฮ][่-๋][ะาํา])
  1. Building a DFA from the joined regular expression using regex-automata

  2. Creating a directed acyclic graph (DAG) by adding edges using the DFA

  3. Identifying clusters following a shortest path of a DAG from step above

Note: wordcut-engine does not allow a context sensitive rule, since it hurts the performance too much. Moreover, instead of longest matching, we use a DAG, and its shortest path to contraint cluster boundary by another cluster, therefore newmm-style context sensitive rules are not required.

Identifying split-DAG edges

In contrary to identifying clusters, identifying split-DAG edges identify what must be split. Split-DAG edge makers, wordcut-engine has three types of split-DAG edge maker, that are:

  1. Dictionary-based maker
  2. Rule-based maker
  3. Default maker (Unk edge builder)

The dictionary-based maker traverses a prefix tree, which is particularly a trie in wordcut-engine and create an edge that matched word in the prefix tree. Rule-based maker uses regex-automata's Regex matcher built from split rules to find longest matched substrings, and add corresponding edges to the graph. wordcut-engine removes edges that break clusters. The example of split rules are shown below:

[\r\t\n ]+
[A-Za-z]+
[0-9]+
[๐-๙]+
[\(\)"'`\[\]{}\\/]

If there is no edge for each of character indice yet, a default maker create a edge that connected the known rightmost boundary.