Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve khmer segmenter performance by using fst segmenter #251

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions charabia/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ unicode-normalization = "0.1.22"
irg-kvariants = "0.1.0"
litemap = "0.6.1"
zerovec = "0.9.3"
icu = { version = "1.3.0", features = ["serde"] , optional = true }
icu_provider_blob = { version = "1.3.0", optional = true }
icu_provider = { version = "1.3.0", features = ["sync"], optional = true }

[features]
default = ["chinese", "hebrew", "japanese", "thai", "korean", "greek", "latin-camelcase", "latin-snakecase", "khmer"]
Expand Down Expand Up @@ -66,7 +63,7 @@ greek = []
# allow splitting camelCase latin words
latin-camelcase = ["dep:finl_unicode"]

khmer = ["dep:icu", "dep:icu_provider_blob", "dep:icu_provider"]
khmer = []

# allow splitting snake_case latin words
latin-snakecase = ["dep:finl_unicode"]
Expand Down
Binary file removed charabia/dictionaries/bin/icu4x-khmer-keys
Binary file not shown.
Binary file added charabia/dictionaries/fst/khmer/words.fst
Binary file not shown.
39 changes: 8 additions & 31 deletions charabia/src/segmenter/khmer.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
use std::vec;

use icu::segmenter::WordSegmenter;
use fst::raw::Fst;

// Import `Segmenter` trait.
use crate::segmenter::utils::FstSegmenter;
use crate::segmenter::Segmenter;

extern crate alloc; // required as my-data-mod is written for #[no_std]
use icu_provider_blob::BlobDataProvider;

//TIP: Some segmentation Libraries need to initialize a instance of the Segmenter.
// This initialization could be time-consuming and shouldn't be done at each call of `segment_str`.
// In this case, you may want to store the initialized instance in a lazy static like below and call it in `segment_str`.
// Otherwise, just remove below lines.
//
// Put this import at the top of the file.
use once_cell::sync::Lazy;
//
static SEGMENTER: Lazy<WordSegmenter> = Lazy::new(|| {
let blob = include_bytes!("../../dictionaries/bin/icu4x-khmer-keys");

let buffer_provider: BlobDataProvider =
BlobDataProvider::try_new_from_static_blob(blob).expect("failed to load khmer keys");
// dictionary source - https://github.com/unicode-org/icu/blob/main/icu4c/source/data/brkitr/dictionaries/khmerdict.txt
static WORDS_FST: Lazy<Fst<&[u8]>> =
Lazy::new(|| Fst::new(&include_bytes!("../../dictionaries/fst/khmer/words.fst")[..]).unwrap());

WordSegmenter::try_new_dictionary_with_buffer_provider(&buffer_provider)
.expect("failed to initialize khmer word segmenter")
});
static FST_SEGMENTER: Lazy<FstSegmenter> = Lazy::new(|| FstSegmenter::new(&WORDS_FST));

// Make a small documentation of the specialized Segmenter like below.
/// <Script/Language> specialized [`Segmenter`].
Expand All @@ -39,25 +34,7 @@ pub struct KhmerSegmenter;
// All specialized segmenters only need to implement the method `segment_str` of the `Segmenter` trait.
impl Segmenter for KhmerSegmenter {
fn segment_str<'o>(&self, to_segment: &'o str) -> Box<dyn Iterator<Item = &'o str> + 'o> {
let (_, positions) =
SEGMENTER.segment_str(to_segment).fold((None, vec![]), |mut acc, elem| {
if acc.0.is_some() {
acc.1.push((acc.0.unwrap(), elem));
}

acc.0 = Some(elem);

acc
});

// Return the created iterator wrapping it in a Box.
Box::new(
positions
.iter()
.map(|(start, end)| &to_segment[*start..*end])
.collect::<Vec<&str>>()
.into_iter(),
)
FST_SEGMENTER.segment_str(to_segment)
}
}

Expand Down