uqa-analysis 0.3.0

Tokenizers, char/token filters, and analyzers for UQA full-text search
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

//! Ordered rolling Viterbi with frontier commits and the reference's forced backtrace rule.

use uqa_core::memory::{Budgeted, BudgetedVec, MemoryBudget, MemoryReservation};

use super::lattice::{Lattice, Node, WordId};
use super::word::Word;
use super::{NoriLimits, NoriOptions, NoriOutput, NoriToken};
use crate::nori::error::{check_limit, invalid};
use crate::nori::{NoriDictionary, POSTag, UserDictionary};
use crate::AnalysisResult;

pub(super) struct State<'a> {
    pub input: &'a [u16],
    pub model: &'a NoriDictionary,
    pub user: Option<&'a UserDictionary>,
    pub options: NoriOptions,
    pub lattice: Lattice,
    pub position: usize,
    pub last_backtrace: usize,
    pub pending: BudgetedVec<NoriToken>,
    pub ngram: Option<u32>,
    pub budget: &'a MemoryBudget,
    limits: NoriLimits,
    total_tokens: usize,
    output_units: usize,
    work: usize,
    output_memory: MemoryReservation,
    pub poll: &'a mut dyn FnMut() -> AnalysisResult<()>,
}

pub(super) fn analyze(
    input: &[u16],
    model: &NoriDictionary,
    user: Option<&UserDictionary>,
    options: NoriOptions,
    limits: NoriLimits,
    budget: &MemoryBudget,
    poll: &mut impl FnMut() -> AnalysisResult<()>,
) -> AnalysisResult<Budgeted<NoriOutput>> {
    let ngram =
        if options.output_unknown_unigrams {
            let mut ngram = None;
            for (work, id) in (model.known_word_count()..model.word_count()).enumerate() {
                if work % 1024 == 0 {
                    poll()?;
                }
                if model
                    .word(id as u32)
                    .is_some_and(|word| word.original_id() == 0)
                {
                    ngram = Some(id as u32);
                    break;
                }
            }
            Some(ngram.ok_or_else(|| {
                invalid("Nori tokenizer", "unknown dictionary has no word ID zero")
            })?)
        } else {
            None
        };
    let mut state = State {
        input,
        model,
        user,
        options,
        lattice: Lattice::new(limits, budget, poll)?,
        position: 0,
        last_backtrace: 0,
        pending: BudgetedVec::new(budget),
        ngram,
        budget,
        limits,
        total_tokens: 0,
        output_units: 0,
        work: 0,
        output_memory: budget.empty_reservation(),
        poll,
    };
    let mut tokens = BudgetedVec::new(budget);
    loop {
        let ended = state.forward()?;
        tokens.reserve(state.pending.len())?;
        while let Some(token) = state.pending.pop() {
            state.tick()?;
            tokens.push(token)?;
        }
        if ended {
            break;
        }
    }
    (state.poll)()?;
    let (tokens, mut memory) = tokens.into_parts();
    memory.absorb(state.output_memory);
    Ok(Budgeted::new(
        NoriOutput::from_tokens(tokens, state.position, 0),
        memory,
    ))
}

impl State<'_> {
    pub fn tick(&mut self) -> AnalysisResult<()> {
        self.work = (self.work + 1) % 1024;
        if self.work == 0 {
            (self.poll)()?;
        }
        Ok(())
    }

    pub fn check_units(&self, additional: usize) -> AnalysisResult<()> {
        let total = self
            .output_units
            .checked_add(additional)
            .ok_or_else(|| invalid("Nori emission", "UTF-16 output size overflow"))?;
        check_limit(
            "Nori output UTF-16 units",
            total,
            self.limits.max_output_utf16,
        )?;
        Ok(())
    }

    pub fn push(&mut self, token: Budgeted<NoriToken>) -> AnalysisResult<()> {
        check_limit(
            "Nori output tokens",
            self.total_tokens
                .checked_add(1)
                .ok_or_else(|| invalid("Nori emission", "token count overflow"))?,
            self.limits.max_tokens,
        )?;
        let mut units = token.term_utf16.len();
        if let Some(reading) = &token.reading {
            units = units
                .checked_add(super::allocation::utf16_len(
                    reading,
                    usize::MAX,
                    self.poll,
                )?)
                .ok_or_else(|| invalid("Nori emission", "reading size overflow"))?;
        }
        for part in token.morphemes.iter().flatten() {
            self.tick()?;
            units = units
                .checked_add(part.surface_utf16.len())
                .ok_or_else(|| invalid("Nori emission", "morpheme size overflow"))?;
        }
        self.check_units(units)?;
        let (token, memory) = token.into_parts();
        self.pending.push(token)?;
        self.output_memory.absorb(memory);
        self.total_tokens += 1;
        self.output_units += units;
        Ok(())
    }

    fn forward(&mut self) -> AnalysisResult<bool> {
        // The reference resets this bound whenever a nonempty pending batch is consumed.
        let mut user_maximum = None;
        while self.position < self.input.len() {
            self.tick()?;
            self.lattice.ensure(self.position, self.poll)?;
            if self.lattice.get(self.position).is_empty() {
                self.position += 1;
                continue;
            }
            let frontier = self.lattice.next_pos() == self.position + 1;
            if self.position > self.last_backtrace
                && frontier
                && self.lattice.get(self.position).len() == 1
            {
                super::emission::backtrace(self, self.position, 0)?;
                self.lattice.rebase(self.position);
                if !self.pending.is_empty() {
                    return Ok(false);
                }
            }
            if self.position - self.last_backtrace >= 1024 {
                self.force_backtrace()?;
                if !self.pending.is_empty() {
                    return Ok(false);
                }
                continue;
            }
            let from = self.position;
            if self
                .model
                .unicode(u32::from(self.input[self.position]))
                .expect("complete Unicode table")
                .category
                == 12
                && self.position + 1 < self.input.len()
            {
                self.position += 1;
            }
            let mut matched = false;
            if let Some(user) = self.user {
                let mut longest = None;
                let mut cursor = user.cursor();
                for (offset, &unit) in self.input[self.position..].iter().enumerate() {
                    self.tick()?;
                    if cursor.advance(unit).is_none() {
                        break;
                    }
                    if let Some(id) = cursor.rank() {
                        longest = Some((offset + 1, id));
                    }
                }
                if let Some((length, id)) = longest {
                    matched = true;
                    let end = self.position + length;
                    if user_maximum.is_none_or(|previous| end > previous) {
                        self.add(from, self.position, end, WordId::User(id))?;
                        user_maximum = Some(end);
                    }
                }
            }
            if !matched {
                let model = self.model;
                let mut cursor = model.lexicon.cursor();
                for (offset, &unit) in self.input[self.position..].iter().enumerate() {
                    self.tick()?;
                    if cursor.advance(unit).is_none() {
                        break;
                    }
                    if let Some(rank) = cursor.rank() {
                        for id in model.surfaces[rank as usize].word_ids.clone() {
                            self.add(
                                from,
                                self.position,
                                self.position + offset + 1,
                                WordId::Known(id),
                            )?;
                            matched = true;
                        }
                    }
                }
            }
            self.unknown(from, matched)?;
            self.position += 1;
        }
        self.finish()?;
        Ok(true)
    }

    fn finish(&mut self) -> AnalysisResult<()> {
        if self.position > 0 {
            self.lattice.ensure(self.position, self.poll)?;
            let mut best = None;
            let mut least_cost = i32::MAX;
            for index in 0..self.lattice.get(self.position).len() {
                self.tick()?;
                let node = self.lattice.get(self.position)[index];
                let cost = node.cost.wrapping_add(i32::from(
                    self.model
                        .connection_cost(node.right, 0)
                        .expect("validated context"),
                ));
                if cost < least_cost {
                    least_cost = cost;
                    best = Some(index);
                }
            }
            let best = best.ok_or_else(|| invalid("Nori lattice", "no complete path"))?;
            super::emission::backtrace(self, self.position, best)?;
        }
        Ok(())
    }

    fn force_backtrace(&mut self) -> AnalysisResult<()> {
        let mut best = None;
        let mut least = i32::MAX;
        for position in self.position..self.lattice.next_pos() {
            self.tick()?;
            for index in 0..self.lattice.get(position).len() {
                self.tick()?;
                let node = self.lattice.get(position)[index];
                if node.cost < least {
                    least = node.cost;
                    best = Some((position, index));
                }
            }
        }
        let (position, index) =
            best.ok_or_else(|| invalid("Nori lattice", "no live path at forced backtrace"))?;
        self.lattice
            .prune(self.position, position, index, self.poll)?;
        super::emission::backtrace(self, position, 0)?;
        self.lattice.rebase(position);
        self.position = position;
        Ok(())
    }

    fn add(&mut self, from: usize, word_pos: usize, end: usize, id: WordId) -> AnalysisResult<()> {
        self.tick()?;
        let word = Word::resolve(id, self.model, self.user);
        let penalty = if word_pos > from && penalized(word.left_pos()) {
            3000
        } else {
            0
        };
        let mut least = i32::MAX;
        let mut best = None;
        for index in 0..self.lattice.get(from).len() {
            self.tick()?;
            let node = self.lattice.get(from)[index];
            let cost = node
                .cost
                .wrapping_add(i32::from(
                    self.model
                        .connection_cost(node.right, word.left())
                        .expect("validated contexts"),
                ))
                .wrapping_add(penalty);
            if cost < least {
                least = cost;
                best = Some(index);
            }
        }
        let back_index =
            best.ok_or_else(|| invalid("Nori lattice", "no incoming least-cost path"))?;
        self.lattice.push(
            end,
            Node {
                cost: least.wrapping_add(word.cost()),
                right: word.right(),
                back_pos: from,
                word_pos,
                back_index,
                word: id,
            },
            self.poll,
        )?;
        Ok(())
    }

    fn unknown(&mut self, from: usize, matched: bool) -> AnalysisResult<()> {
        let first = self.input[self.position];
        if matched && !self.model.invokes_unknown(first) {
            return Ok(());
        }
        let mut class = self.model.character_class(first);
        let mut length = 1;
        if self.model.groups_unknown(first) {
            let first_properties = self
                .model
                .unicode(u32::from(first))
                .expect("complete Unicode table");
            let mut script = first_properties.script;
            let punct = punctuation(first, first_properties.category);
            while length < 1024 && self.position + length < self.input.len() {
                self.tick()?;
                let unit = self.input[self.position + length];
                let properties = self
                    .model
                    .unicode(u32::from(unit))
                    .expect("complete Unicode table");
                let same_script = script == properties.script
                    || self.common(script)
                    || self.common(properties.script)
                    || properties.category == 6;
                if !same_script
                    || punctuation(unit, properties.category) != punct
                    || properties.is_digit != first_properties.is_digit
                    || !self.model.groups_unknown(unit)
                {
                    break;
                }
                length += 1;
                if self.common(script) && !self.common(properties.script) {
                    script = properties.script;
                    class = self.model.character_class(unit);
                }
            }
        }
        for id in self
            .model
            .unknown_words(class)
            .expect("validated unknown class")
        {
            self.add(
                from,
                self.position,
                self.position + length,
                WordId::Unknown(id),
            )?;
        }
        Ok(())
    }

    fn common(&self, script: u16) -> bool {
        matches!(
            self.model.unicode_script_name(script),
            Some("COMMON" | "INHERITED")
        )
    }
}

pub(super) fn punctuation(unit: u16, category: u8) -> bool {
    unit == 0x318d || matches!(category, 12..=16 | 20..=30)
}

fn penalized(tag: POSTag) -> bool {
    matches!(
        tag,
        POSTag::EP
            | POSTag::EF
            | POSTag::EC
            | POSTag::ETN
            | POSTag::ETM
            | POSTag::JKS
            | POSTag::JKC
            | POSTag::JKG
            | POSTag::JKO
            | POSTag::JKB
            | POSTag::JKV
            | POSTag::JKQ
            | POSTag::JX
            | POSTag::JC
            | POSTag::VCP
            | POSTag::XSA
            | POSTag::XSN
            | POSTag::XSV
    )
}

#[cfg(test)]
mod tests;