ltp 0.1.9

Language Technology Platform For Rust.
Documentation
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
use cedarwood::Cedar;
use std::cmp::Ordering;
use std::collections::HashMap;
use anyhow::{Result, anyhow};

#[derive(Debug, Clone)]
struct Record {
    freq: usize,
}

impl Record {
    #[inline(always)]
    fn new(freq: usize) -> Self {
        Self { freq }
    }
}

#[derive(Clone, Debug)]
pub struct Hook {
    records: Vec<Record>,
    cedar: Cedar,
    total: usize,
    longest_word_len: usize,
}

impl Default for Hook {
    fn default() -> Self {
        Self::new()
    }
}


impl Hook {
    pub fn new() -> Hook {
        Hook {
            records: Vec::new(),
            cedar: Cedar::new(),
            total: 0,
            longest_word_len: 0,
        }
    }

    pub fn total(&self) -> usize {
        self.total
    }

    pub fn add_word(&mut self, word: &str, freq: Option<usize>) -> usize {
        let freq = freq.unwrap_or(1);

        match self.cedar.exact_match_search(word) {
            Some((word_id, _, _)) => {
                let old_freq = self.records[word_id as usize].freq;
                self.records[word_id as usize].freq = freq;

                self.total += freq;
                self.total -= old_freq;
            }
            None => {
                self.records.push(Record::new(freq));
                let word_id = (self.records.len() - 1) as i32;

                self.cedar.update(word, word_id);
                self.total += freq;
            }
        };

        let curr_word_len = word.chars().count();
        if self.longest_word_len < curr_word_len {
            self.longest_word_len = curr_word_len;
        }

        freq
    }

    fn dag(&self, sentence: &str, words: &[&str], dag: &mut Dag) -> Result<()> {
        let mut byte_start_bias = 0;
        for &word in words {
            let word_len = word.len();
            let is_first = true;
            let mut char_indices = word.char_indices().peekable();
            while let Some((byte_start, _)) = char_indices.next() {
                dag.start(byte_start + byte_start_bias);
                // check is valid ?
                if let Some(haystack) = sentence.get(byte_start + byte_start_bias..) {
                    // Char
                    let cur_char_len = char_indices.peek().map(|(next_start, _)| next_start - byte_start);
                    // 外部分词结果
                    let mut nch_flag = cur_char_len.is_none();
                    let mut per_flag = !is_first;
                    for (_, end_index) in self.cedar.common_prefix_iter(haystack) {
                        let white_space_len = haystack[end_index + 1..].chars().take_while(|ch| ch.is_whitespace()).count();
                        if is_first && end_index + white_space_len + 1 == word_len {
                            per_flag = true;
                        }
                        if let Some(char_len) = cur_char_len {
                            if end_index + white_space_len + 1 == char_len {
                                nch_flag = true;
                            }
                        }
                        dag.insert(byte_start_bias + byte_start + end_index + white_space_len + 1);
                    }
                    if !nch_flag {
                        dag.insert(byte_start_bias + byte_start + cur_char_len.unwrap());
                        if byte_start + cur_char_len.unwrap() == word_len {
                            per_flag = true;
                        }
                    }
                    if is_first && !per_flag {
                        dag.insert(byte_start_bias + word_len);
                    }
                    dag.commit();
                } else {
                    return Err(anyhow!("Invalid UTF-8 sentence!"));
                }
            }
            byte_start_bias += word_len;
        }
        Ok(())
    }

    #[allow(clippy::ptr_arg)]
    fn calc(&self, sentence: &str, dag: &Dag, route: &mut Vec<(f64, usize)>) -> Result<()> {
        let str_len = sentence.len();

        if str_len + 1 > route.len() {
            route.resize(str_len + 1, (0.0, 0));
        }

        let logtotal = (self.total as f64).ln();
        let mut prev_byte_start = str_len;
        let curr = sentence.char_indices().map(|x| x.0).rev();
        for byte_start in curr {
            let pair = dag
                .iter_edges(byte_start)?
                .map(|byte_end| {
                    let wfrag = if byte_end == str_len {
                        &sentence[byte_start..]
                    } else {
                        &sentence[byte_start..byte_end]
                    };

                    let freq = if let Some((word_id, _, _)) = self.cedar.exact_match_search(wfrag) {
                        self.records[word_id as usize].freq
                    } else {
                        1
                    };

                    ((freq as f64).ln() - logtotal + route[byte_end].0, byte_end)
                })
                .max_by(|x, y| x.partial_cmp(y).unwrap_or(Ordering::Equal));

            if let Some(p) = pair {
                route[byte_start] = p;
            } else {
                let byte_end = prev_byte_start;
                let freq = 1;
                route[byte_start] = ((freq as f64).ln() - logtotal + route[byte_end].0, byte_end);
            }

            prev_byte_start = byte_start;
        }
        Ok(())
    }

    pub fn hook<'a>(&self, sentence: &'a str, cut_words: &[&str]) -> Result<Vec<&'a str>> {
        let mut hook_words = Vec::with_capacity(cut_words.len());
        let mut route = Vec::with_capacity(cut_words.len());
        let mut dag = Dag::with_size_hint(cut_words.len());

        self.inner_hook(sentence, cut_words, &mut hook_words, &mut route, &mut dag)?;
        Ok(hook_words)
    }

    fn inner_hook<'a>(
        &self,
        sentence: &'a str,
        cut_words: &[&str],
        words: &mut Vec<&'a str>,
        route: &mut Vec<(f64, usize)>,
        dag: &mut Dag,
    ) -> Result<()> {
        self.dag(sentence, cut_words, dag)?;
        self.calc(sentence, dag, route)?;
        let mut x = 0;
        let mut left: Option<usize> = None;

        while x < sentence.len() {
            let y = route[x].1;
            let l_str = if y < sentence.len() {
                &sentence[x..y]
            } else {
                &sentence[x..]
            };

            if l_str.chars().count() == 1 && l_str.chars().all(|ch| ch.is_ascii_alphanumeric()) {
                if left.is_none() {
                    left = Some(x);
                }
            } else {
                if let Some(byte_start) = left {
                    let word = &sentence[byte_start..x];
                    words.push(word);
                    left = None;
                }

                let word = if y < sentence.len() {
                    &sentence[x..y]
                } else {
                    &sentence[x..]
                };

                words.push(word);
            }
            x = y;
        }

        if let Some(byte_start) = left {
            let word = &sentence[byte_start..];
            words.push(word);
        }

        dag.clear();
        route.clear();
        Ok(())
    }
}

#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Dag {
    array: Vec<usize>,
    start_pos: HashMap<usize, usize>,
    size_hint_for_iterator: usize,
    curr_insertion_len: usize,
}

pub struct EdgeIter<'a> {
    dag: &'a Dag,
    cursor: usize,
}

impl<'a> Iterator for EdgeIter<'a> {
    type Item = usize;

    fn size_hint(&self) -> (usize, Option<usize>) {
        (0, Some(self.dag.size_hint_for_iterator))
    }

    fn next(&mut self) -> Option<Self::Item> {
        if self.dag.array[self.cursor] == 0 {
            self.cursor += 1;
            None
        } else {
            let v = self.dag.array[self.cursor] - 1;
            self.cursor += 1;
            Some(v)
        }
    }
}

impl Dag {
    pub(crate) fn with_size_hint(hint: usize) -> Self {
        Dag {
            array: Vec::with_capacity(hint * 5),
            start_pos: HashMap::default(),
            size_hint_for_iterator: 0,
            curr_insertion_len: 0,
        }
    }

    #[inline]
    pub(crate) fn start(&mut self, from: usize) {
        let idx = self.array.len();
        self.curr_insertion_len = 0;
        self.start_pos.insert(from, idx);
    }

    #[inline]
    pub(crate) fn insert(&mut self, to: usize) {
        self.curr_insertion_len += 1;
        self.array.push(to + 1);
    }

    #[inline]
    pub(crate) fn commit(&mut self) {
        self.size_hint_for_iterator = std::cmp::max(self.curr_insertion_len, self.size_hint_for_iterator);
        self.array.push(0);
    }

    #[inline]
    pub(crate) fn iter_edges(&self, from: usize) -> Result<EdgeIter> {
        if let Some(&cursor) = self.start_pos.get(&from) {
            Ok(EdgeIter { dag: self, cursor })
        } else {
            Err(anyhow!("Invalid start position! Maybe invalid UTF-8 sentence!"))
        }
    }

    pub(crate) fn clear(&mut self) {
        self.array.clear();
        self.start_pos.clear();
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_fatal() {
        let raw_sentence = vec![194, 40];
        let word1 = vec![194];
        let word2 = vec![40];
        unsafe {
            let sentence = String::from_utf8_unchecked(raw_sentence);
            let word1 = String::from_utf8_unchecked(word1);
            let word2 = String::from_utf8_unchecked(word2);
            let cut_words: [&str; 2] = [word1.as_ref(), word2.as_ref()];
            let hook = Hook::new();

            let mut words = Vec::with_capacity(5);
            let mut route = Vec::with_capacity(5);

            let mut dag = Dag::with_size_hint(5);
            assert!(hook.inner_hook(&sentence, &cut_words, &mut words, &mut route, &mut dag).is_err());
        }
    }

    #[test]
    fn test_hook() {
        let sentence = "他叫汤姆去拿外衣。";
        let cut_words = ["", "", "汤姆", "", "", "外衣", ""];
        let mut hook = Hook::new();

        let mut words = Vec::with_capacity(5);
        let mut route = Vec::with_capacity(5);

        let mut dag = Dag::with_size_hint(5);
        assert!(hook.inner_hook(sentence, &cut_words, &mut words, &mut route, &mut dag).is_ok());

        assert_eq!(words, cut_words);

        hook.add_word("姆去拿", Some(2));
        words.clear();
        route.clear();
        dag.clear();

        assert!(hook.inner_hook(sentence, &cut_words, &mut words, &mut route, &mut dag).is_ok());
        println!("{:?}", words);
        assert_eq!(words, ["", "", "", "姆去拿", "外衣", ""]);
    }

    #[test]
    fn test_sep() {
        let sentence = "通讯系统[SEP]";
        let cut_words = ["通讯", "系统[SEP]"];
        let hook = Hook::new();

        let mut words = Vec::with_capacity(5);
        let mut route = Vec::with_capacity(5);

        let mut dag = Dag::with_size_hint(5);
        assert!(hook.inner_hook(sentence, &cut_words, &mut words, &mut route, &mut dag).is_ok());
    }

    #[test]
    fn test_space() {
        let sentence = "[ENT] Info";
        let cut_words = ["[", "ENT", "] Info"];
        let mut hook = Hook::new();
        hook.add_word("[ENT]", Some(2));

        let mut words = Vec::with_capacity(5);
        let mut route = Vec::with_capacity(5);

        let mut dag = Dag::with_size_hint(5);
        assert!(hook.inner_hook(sentence, &cut_words, &mut words, &mut route, &mut dag).is_ok());
        println!("{:?}", words);
    }

    #[test]
    fn test_dag() -> Result<()> {
        let mut dag = Dag::with_size_hint(5);
        let mut ans: Vec<Vec<usize>> = vec![Vec::new(); 5];
        for i in 0..=3 {
            dag.start(i);
            for j in (i + 1)..=4 {
                ans[i].push(j);
                dag.insert(j);
            }

            dag.commit()
        }

        assert_eq!(dag.size_hint_for_iterator, 4);

        for i in 0..=3 {
            let edges: Vec<usize> = dag.iter_edges(i)?.collect();
            assert_eq!(ans[i], edges);
        }

        Ok(())
    }
}