use wasm_bindgen::prelude::*;
use crate::PronunciationDict;
#[wasm_bindgen]
pub struct WasmDict {
inner: PronunciationDict,
}
#[wasm_bindgen]
impl WasmDict {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
Self {
inner: PronunciationDict::new(),
}
}
pub fn english() -> Self {
Self {
inner: PronunciationDict::english(),
}
}
pub fn english_minimal() -> Self {
Self {
inner: PronunciationDict::english_minimal(),
}
}
pub fn len(&self) -> usize {
self.inner.len()
}
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
pub fn lookup(&self, word: &str) -> Option<String> {
let phonemes = self.inner.lookup(word)?;
let ipa_strings: Vec<&str> = phonemes
.iter()
.filter_map(crate::ipa::phoneme_to_ipa)
.collect();
serde_json::to_string(&ipa_strings).ok()
}
pub fn variant_count(&self, word: &str) -> usize {
self.inner.lookup_entry(word).map_or(0, |entry| entry.len())
}
pub fn insert_user_ipa(&mut self, word: &str, ipa: &str) {
let phonemes = crate::ipa::parse_ipa_word(ipa);
if !phonemes.is_empty() {
self.inner.insert_user(word, &phonemes);
}
}
pub fn remove_user(&mut self, word: &str) -> bool {
self.inner.remove_user(word)
}
pub fn user_len(&self) -> usize {
self.inner.user_len()
}
pub fn prefix_search(&self, prefix: &str) -> String {
let results = self.inner.prefix_search(prefix);
serde_json::to_string(&results).unwrap_or_else(|_| "[]".to_string())
}
pub fn coverage(&self, text: &str) -> String {
let report = self.inner.coverage(text);
serde_json::to_string(&report).unwrap_or_else(|_| "{}".to_string())
}
}
impl Default for WasmDict {
fn default() -> Self {
Self::new()
}
}