Pure MoonBit SymSpell spelling correction, compound splitting and merging, and Unicode word segmentation.
moon add albertoforni/symspellmoon run cmd/main --target nativeimport {
"albertoforni/symspell",
}///|
test {
let dictionary = @symspell.SymSpell::new()
let report = dictionary.load_dictionary(
"members 226656153\nmember 100000\nremember 90000\n",
)
assert_eq(report.added_words, 3)
let suggestions = dictionary.lookup("memebers")
assert_eq(suggestions[0].term, "members")
assert_eq(suggestions[0].distance, 1)
assert_eq(suggestions[0].count, 226656153L)
}///|
test {
let dictionary = @symspell.SymSpell::new()
let _ = dictionary.load_dictionary("cat 10\nbat 50\ncut 20")
assert_eq(dictionary.lookup("hat", verbosity=Top)[0].term, "bat")
assert_eq(dictionary.lookup("hat", verbosity=Closest).length(), 2)
assert_eq(dictionary.lookup("cat", verbosity=All).length(), 3)
assert_eq(dictionary.lookup("hat", max_edit_distance=0), [])
let fallback = dictionary.lookup("unknown", include_unknown=true)
assert_eq(fallback[0].term, "unknown")
assert_eq(fallback[0].count, 0L)
assert_eq(fallback[0].distance, 3)
}///|
test {
let dictionary = @symspell.SymSpell::new()
let _ = dictionary.load_dictionary("where 2\nis 2\nwhereas 2\n")
let _ = dictionary.load_bigram_dictionary("where is 10")
let correction = dictionary.lookup_compound("whereiz")[0]
assert_eq(correction.term, "where is")
assert_eq(correction.distance, 2)
assert_eq(correction.count, 10L)
}///|
test {
let dictionary = @symspell.SymSpell::new()
let _ = dictionary.load_dictionary("hello 100\nworld 90")
let joined = dictionary.word_segmentation("helloworld")
assert_eq(joined.corrected_string, "hello world")
assert_eq(joined.distance_sum, 1)
let corrected = dictionary.word_segmentation("helo world")
assert_eq(corrected.segmented_string, "helo world")
assert_eq(corrected.corrected_string, "hello world")
assert_eq(corrected.distance_sum, 1)
}///|
test {
let dictionary = @symspell.SymSpell::new(corpus_size=3L)
let report = dictionary.create_dictionary("hello, world! hello")
assert_eq(report, { parsed_tokens: 3, added_words: 2, })
assert_eq(dictionary.frequency("hello"), Some(2L))
assert_eq(dictionary.lookup("helo", verbosity=Top)[0].term, "hello")
}///|
test {
let dictionary = @symspell.SymSpell::new(count_threshold=3L)
let _ = dictionary.load_dictionary("hello 100\nworld 90\nrare 2")
let _ = dictionary.set_bigram_count("hello world", 50L)
let bytes = dictionary.to_snapshot()
// Save or transport these bytes using the application's I/O facilities.
let restored = @symspell.SymSpell::from_snapshot(bytes)
assert_eq(restored.lookup("helo"), dictionary.lookup("helo"))
assert_eq(
restored.word_segmentation("helloworld").corrected_string,
"hello world",
)
assert_eq(restored.bigram_frequency("hello world"), Some(50L))
assert_true(restored.create_dictionary_entry("rare", 1L))
assert_eq(restored.frequency("rare"), Some(3L))
assert_eq(dictionary.frequency("rare"), None)
}python scripts/benchmark.py --words 10000
python scripts/benchmark.py --words 0moon fmt --check
moon check --deny-warn --target all
moon test --deny-warn --target all
moon build --deny-warn --target all
moon info --target allpython -m venv .venv
.venv/bin/python -m pip install -r requirements-dev.txt
.venv/bin/python scripts/generate_fixtures.py
.venv/bin/python scripts/generate_phrase_fixtures.py
.venv/bin/python scripts/generate_snapshot_fixtures.py
.venv/bin/python scripts/generate_corpus_fixtures.py
python scripts/generate_corpus_unicode.py
moon fmtpub(all) suberror SymSpellError {
InvalidEditDistance(Int)
InvalidPrefixLength(Int)
InvalidCountThreshold(Int64)
InvalidColumns(Int, Int)
InvalidDictionaryLine(Int, String)
InvalidCorpusSize(Int64)
InvalidFrequencyCount(Int64)
InvalidBigramTerm(String)
InvalidSegmentationWordLength(Int)
InvalidSnapshot(Int, String)
UnsupportedSnapshotVersion(UInt)
} derive(Eq, Debug)pub struct SymSpell {
// private fields
}fn SymSpell::load_bigram_dictionary(self : SymSpell, text : String, term_index? : Int, count_index? : Int, separator? : Char, strict? : Bool) -> BigramLoadReport raise SymSpellErrorfn SymSpell::load_dictionary(self : SymSpell, text : String, term_index? : Int, count_index? : Int, separator? : Char, strict? : Bool) -> LoadReport raise SymSpellErrorfn SymSpell::lookup(self : SymSpell, term : String, verbosity? : Verbosity, max_edit_distance? : Int, include_unknown? : Bool) -> Array[Suggestion] raise SymSpellErrorfn SymSpell::lookup_compound(self : SymSpell, phrase : String, max_edit_distance? : Int) -> Array[Suggestion] raise SymSpellErrorfn SymSpell::new(max_dictionary_edit_distance? : Int, prefix_length? : Int, count_threshold? : Int64, corpus_size? : Int64) -> SymSpell raise SymSpellErrorfn SymSpell::set_bigram_count(self : SymSpell, term : String, count : Int64) -> Bool raise SymSpellErrorfn SymSpell::word_segmentation(self : SymSpell, phrase : String, max_edit_distance? : Int, max_segmentation_word_length? : Int) -> Composition raise SymSpellErrorInstall
Download zipPure MoonBit SymSpell spelling correction, compound splitting and merging, and Unicode word segmentation.