/
lib.rs
164 lines (140 loc) · 4.65 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
mod utils;
use std::io::BufReader;
use std::sync::{OnceLock, RwLock};
use log::info;
#[cfg(target_family = "wasm")]
use wasm_bindgen::prelude::*;
use crate::utils::set_panic_hook;
#[cfg(not(target_family = "wasm"))]
type JsValue = String;
#[cfg(target_family = "wasm")]
fn to_value<T: serde::ser::Serialize + ?Sized>(value: &T) -> Result<JsValue, serde_wasm_bindgen::Error> {
value.serialize(&serde_wasm_bindgen::Serializer::new())
}
#[cfg(not(target_family = "wasm"))]
fn to_value<T: serde::ser::Serialize + ?Sized>(value: &T) -> Result<JsValue, serde_json::Error> {
serde_json::to_string(value)
}
#[cfg_attr(target_family = "wasm", wasm_bindgen)]
extern {}
fn instance() -> &'static RwLock<jieba_rs::Jieba> {
static INSTANCE: OnceLock<RwLock<jieba_rs::Jieba>> = OnceLock::new();
INSTANCE.get_or_init(|| {
set_panic_hook();
let new_instance = RwLock::new(
jieba_rs::Jieba::new());
info!("jieba instance created.");
// Only used for loading dicts.
macro_rules! include_dict {
($dict:literal)=>{
let dict = include_str!(concat!("dicts/",$dict));
new_instance.write().unwrap()
.load_dict(&mut BufReader::new(dict.as_bytes())).unwrap();
info!(concat!($dict," loaded."));
};
}
// Common Words
include_dict!("default.hans");
include_dict!("default.hant");
// 2-char dicts generated by build-dict.
// The hant dicts were machine translated from hans dicts by the way.
// To avoid, although very unlikely to occur, potential legal issues, I prefer not to
// recording the source of these dicts and the translation API I used.
// Mathematics
include_dict!("110.ime.hans");
include_dict!("110.ime.hant");
// Physics
include_dict!("140.ime.hans");
include_dict!("140.ime.hant");
// Computer Science
include_dict!("520.ime.hans");
include_dict!("520.ime.hant");
// Development
include_dict!("52040.ime.hans");
include_dict!("52040.ime.hant");
new_instance
})
}
#[cfg_attr(target_family = "wasm", wasm_bindgen)]
#[allow(non_snake_case)]
pub fn cut(text: &str, hmm: bool) -> Vec<JsValue> {
instance().read().unwrap()
.cut(text, hmm)
.into_iter()
.map(JsValue::from)
.collect()
}
#[cfg_attr(target_family = "wasm", wasm_bindgen)]
#[allow(non_snake_case)]
pub fn cutAll(text: &str) -> Vec<JsValue> {
instance().read().unwrap()
.cut_all(text)
.into_iter()
.map(JsValue::from)
.collect()
}
#[cfg_attr(target_family = "wasm", wasm_bindgen)]
#[allow(non_snake_case)]
pub fn cutForSearch(text: &str, hmm: bool) -> Vec<JsValue> {
instance().read().unwrap()
.cut_for_search(text, hmm)
.into_iter()
.map(JsValue::from)
.collect()
}
// wasm_bindgen needs to read this signature.
// I wish patch jieba-rs as little as possible.
#[cfg_attr(target_family = "wasm", wasm_bindgen)]
#[allow(non_snake_case)]
pub enum TokenizeMode {
Default,
Search,
}
// Inelegant, but no better way found.
impl TokenizeMode {
fn to_jieba(&self) -> jieba_rs::TokenizeMode {
match self {
TokenizeMode::Default => jieba_rs::TokenizeMode::Default,
TokenizeMode::Search => jieba_rs::TokenizeMode::Search,
}
}
}
#[cfg_attr(target_family = "wasm", wasm_bindgen)]
#[allow(non_snake_case)]
pub fn tokenize(text: &str, mode: Option<TokenizeMode>, hmm: bool) -> Vec<JsValue> {
instance().read().unwrap()
.tokenize(text, mode.map(|m| m.to_jieba()).unwrap(), hmm)
.into_iter()
.map(|t| to_value(&t).unwrap())
.collect()
}
// I don't know what this function is for actually.
// Implemented anyway.
#[cfg_attr(target_family = "wasm", wasm_bindgen)]
#[allow(non_snake_case)]
pub fn tag(sentence: &str, hmm: bool) -> Vec<JsValue> {
instance().read().unwrap()
.tag(sentence, hmm)
.into_iter()
.map(|t| to_value(&t).unwrap())
.collect()
}
#[cfg_attr(target_family = "wasm", wasm_bindgen)]
#[allow(non_snake_case)]
pub fn suggestFreq(segment: &str) -> usize {
instance().write().unwrap()
.suggest_freq(segment)
}
#[cfg_attr(target_family = "wasm", wasm_bindgen)]
#[allow(non_snake_case)]
pub fn addWord(word: &str, freq: Option<usize>, tag: Option<String>) -> usize {
instance().write().unwrap()
.add_word(word, freq, tag.as_deref())
}
#[cfg_attr(target_family = "wasm", wasm_bindgen)]
#[allow(non_snake_case)]
pub fn loadDict(dict: &str) {
let mut reader = BufReader::new(dict.as_bytes());
instance().write().unwrap()
.load_dict(&mut reader).unwrap();
}