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
use std::io::{Read, Write};

use anyhow::Result;
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use fst::raw::Fst;

#[cfg(feature = "train")]
use crate::feature::FeatureContent;
#[cfg(feature = "train")]
use crate::sentence::BoundaryType;
#[cfg(feature = "train")]
use crate::utils::{FeatureIDManager, LazyIndexSort};
#[cfg(feature = "train")]
use liblinear::LibLinearModel;
#[cfg(feature = "train")]
const EPSILON: f64 = 1e-6;

#[cfg(not(feature = "model-quantize"))]
pub type WeightValue = f64;
#[cfg(feature = "model-quantize")]
pub type WeightValue = i16;
#[cfg(not(feature = "model-quantize"))]
pub type ScoreValue = f64;
#[cfg(feature = "model-quantize")]
pub type ScoreValue = i32;

/// Model data.
pub struct Model {
    pub(crate) word_fst: Fst<Vec<u8>>,
    pub(crate) type_fst: Fst<Vec<u8>>,
    pub(crate) dict_fst: Fst<Vec<u8>>,

    pub(crate) word_weights: Vec<Vec<WeightValue>>,
    pub(crate) type_weights: Vec<Vec<WeightValue>>,
    pub(crate) dict_weights: Vec<[ScoreValue; 3]>,

    #[cfg(feature = "model-quantize")]
    pub(crate) quantize_multiplier: f64,

    pub(crate) dict_word_wise: bool,

    pub(crate) bias: WeightValue,
    pub(crate) char_window_size: usize,
    pub(crate) type_window_size: usize,
}

impl Model {
    /// Exports the model data.
    ///
    /// # Arguments
    ///
    /// * `wtr` - Byte-oriented sink object.
    ///
    /// # Errors
    ///
    /// When `wtr` generates an error, it will be returned as is.
    pub fn write<W: Write>(&self, wtr: &mut W) -> Result<()> {
        wtr.write_u64::<BigEndian>(self.word_fst.as_bytes().len() as u64)?;
        wtr.write_all(self.word_fst.as_bytes())?;

        wtr.write_u64::<BigEndian>(self.type_fst.as_bytes().len() as u64)?;
        wtr.write_all(self.type_fst.as_bytes())?;

        wtr.write_u64::<BigEndian>(self.dict_fst.as_bytes().len() as u64)?;
        wtr.write_all(self.dict_fst.as_bytes())?;

        #[cfg(feature = "model-quantize")]
        wtr.write_f64::<BigEndian>(self.quantize_multiplier)?;

        wtr.write_u64::<BigEndian>(self.word_weights.len() as u64)?;
        for ws in &self.word_weights {
            wtr.write_u64::<BigEndian>(ws.len() as u64)?;
            for &w in ws {
                #[cfg(not(feature = "model-quantize"))]
                wtr.write_f64::<BigEndian>(w)?;
                #[cfg(feature = "model-quantize")]
                wtr.write_i16::<BigEndian>(w)?;
            }
        }

        wtr.write_u64::<BigEndian>(self.type_weights.len() as u64)?;
        for ws in &self.type_weights {
            wtr.write_u64::<BigEndian>(ws.len() as u64)?;
            for &w in ws {
                #[cfg(not(feature = "model-quantize"))]
                wtr.write_f64::<BigEndian>(w)?;
                #[cfg(feature = "model-quantize")]
                wtr.write_i16::<BigEndian>(w)?;
            }
        }

        wtr.write_u64::<BigEndian>(self.dict_weights.len() as u64)?;
        for ws in &self.dict_weights {
            for &w in ws {
                #[cfg(not(feature = "model-quantize"))]
                wtr.write_f64::<BigEndian>(w)?;
                #[cfg(feature = "model-quantize")]
                wtr.write_i32::<BigEndian>(w)?;
            }
        }

        wtr.write_u8(self.dict_word_wise as u8)?;

        #[cfg(not(feature = "model-quantize"))]
        wtr.write_f64::<BigEndian>(self.bias)?;
        #[cfg(feature = "model-quantize")]
        wtr.write_i16::<BigEndian>(self.bias)?;

        wtr.write_u64::<BigEndian>(self.char_window_size as u64)?;
        wtr.write_u64::<BigEndian>(self.type_window_size as u64)?;

        Ok(())
    }

    /// Creates a model from a reader.
    ///
    /// # Arguments
    ///
    /// * `rdr` - A data source.
    ///
    /// # Returns
    ///
    /// A model data read from `rdr`.
    ///
    /// # Errors
    ///
    /// When `rdr` generates an error, it will be returned as is.
    pub fn read<R: Read>(rdr: &mut R) -> Result<Self> {
        let word_fst_size = rdr.read_u64::<BigEndian>()? as usize;
        let mut word_fst_bytes = vec![0; word_fst_size];
        rdr.read_exact(&mut word_fst_bytes)?;
        let word_fst = Fst::new(word_fst_bytes)?;

        let type_fst_size = rdr.read_u64::<BigEndian>()? as usize;
        let mut type_fst_bytes = vec![0; type_fst_size];
        rdr.read_exact(&mut type_fst_bytes)?;
        let type_fst = Fst::new(type_fst_bytes)?;

        let dict_fst_size = rdr.read_u64::<BigEndian>()? as usize;
        let mut dict_fst_bytes = vec![0; dict_fst_size];
        rdr.read_exact(&mut dict_fst_bytes)?;
        let dict_fst = Fst::new(dict_fst_bytes)?;

        #[cfg(feature = "model-quantize")]
        let quantize_multiplier = rdr.read_f64::<BigEndian>()?;

        let word_weights_size = rdr.read_u64::<BigEndian>()? as usize;
        let mut word_weights = Vec::with_capacity(word_weights_size);
        for _ in 0..word_weights_size {
            let weight_size = rdr.read_u64::<BigEndian>()? as usize;
            let mut weights = Vec::with_capacity(weight_size);
            for _ in 0..weight_size {
                #[cfg(not(feature = "model-quantize"))]
                let weight = rdr.read_f64::<BigEndian>()?;
                #[cfg(feature = "model-quantize")]
                let weight = rdr.read_i16::<BigEndian>()?;
                weights.push(weight);
            }
            word_weights.push(weights);
        }

        let type_weights_size = rdr.read_u64::<BigEndian>()? as usize;
        let mut type_weights = Vec::with_capacity(type_weights_size);
        for _ in 0..type_weights_size {
            let weight_size = rdr.read_u64::<BigEndian>()? as usize;
            let mut weights = Vec::with_capacity(weight_size);
            for _ in 0..weight_size {
                #[cfg(not(feature = "model-quantize"))]
                let weight = rdr.read_f64::<BigEndian>()?;
                #[cfg(feature = "model-quantize")]
                let weight = rdr.read_i16::<BigEndian>()?;
                weights.push(weight);
            }
            type_weights.push(weights);
        }

        let dict_weights_size = rdr.read_u64::<BigEndian>()? as usize;
        let mut dict_weights = Vec::with_capacity(dict_weights_size);
        for _ in 0..dict_weights_size {
            #[cfg(not(feature = "model-quantize"))]
            let mut weights = [0.; 3];
            #[cfg(feature = "model-quantize")]
            let mut weights = [0; 3];
            #[cfg(not(feature = "model-quantize"))]
            for weight in &mut weights {
                *weight = rdr.read_f64::<BigEndian>()?;
            }
            #[cfg(feature = "model-quantize")]
            for weight in &mut weights {
                *weight = rdr.read_i32::<BigEndian>()?;
            }
            dict_weights.push(weights);
        }

        let dict_word_wise = rdr.read_u8()? != 0;

        #[cfg(not(feature = "model-quantize"))]
        let bias = rdr.read_f64::<BigEndian>()?;
        #[cfg(feature = "model-quantize")]
        let bias = rdr.read_i16::<BigEndian>()?;

        let char_window_size = rdr.read_u64::<BigEndian>()? as usize;
        let type_window_size = rdr.read_u64::<BigEndian>()? as usize;

        Ok(Self {
            word_fst,
            type_fst,
            dict_fst,

            #[cfg(feature = "model-quantize")]
            quantize_multiplier,

            word_weights,
            type_weights,
            dict_weights,
            dict_word_wise,
            bias,
            char_window_size,
            type_window_size,
        })
    }

    #[cfg(feature = "train")]
    pub(crate) fn from_liblinear_model(
        model: impl LibLinearModel,
        fid_manager: FeatureIDManager,
        dict_fst: Fst<Vec<u8>>,
        char_window_size: usize,
        type_window_size: usize,
        dict_word_max_size: usize,
    ) -> Self {
        let bias = model.label_bias(BoundaryType::WordBoundary as i32);
        let mut word_sorter = LazyIndexSort::new();
        let mut type_sorter = LazyIndexSort::new();
        let mut word_weights_tmp = vec![];
        let mut type_weights_tmp = vec![];

        let mut dict_weights: Vec<[_; 3]> = (0..dict_word_max_size)
            .map(|_| [ScoreValue::default(); 3])
            .collect();

        #[cfg(feature = "model-quantize")]
        let mut weight_max = bias.abs();
        #[cfg(feature = "model-quantize")]
        for fid in 0..model.num_features() {
            let weight = model
                .feature_coefficient(fid as i32, BoundaryType::WordBoundary as i32)
                .abs();
            if weight > weight_max {
                weight_max = weight;
            }
        }
        #[cfg(feature = "model-quantize")]
        let quantize_multiplier = weight_max / 32767.;

        #[cfg(feature = "model-quantize")]
        let bias = (bias / quantize_multiplier) as i16;

        for (feature, fid) in fid_manager.map {
            let weight =
                model.feature_coefficient(fid as i32 + 1, BoundaryType::WordBoundary as i32);
            if weight > -EPSILON && weight < EPSILON {
                continue;
            }
            #[cfg(not(feature = "model-quantize"))]
            match feature.feature {
                FeatureContent::CharacterNgram(word) => {
                    let id = word_sorter.get_id(word.as_bytes()) as usize;
                    if id == word_weights_tmp.len() {
                        word_weights_tmp
                            .push(vec![0.; char_window_size * 2 - word.chars().count() + 1]);
                    }
                    word_weights_tmp[id][feature.rel_position] = weight;
                }
                FeatureContent::CharacterTypeNgram(types) => {
                    let types_u8: Vec<u8> = types.iter().map(|&t| t as u8).collect();
                    let id = type_sorter.get_id(&types_u8) as usize;
                    if id == type_weights_tmp.len() {
                        type_weights_tmp.push(vec![0.; type_window_size * 2 - types.len() + 1]);
                    }
                    type_weights_tmp[id][feature.rel_position] = weight;
                }
                FeatureContent::DictionaryWord(size) => {
                    dict_weights[size][feature.rel_position] = weight;
                }
            };
            #[cfg(feature = "model-quantize")]
            match feature.feature {
                FeatureContent::CharacterNgram(word) => {
                    let id = word_sorter.get_id(word.as_bytes()) as usize;
                    if id == word_weights_tmp.len() {
                        word_weights_tmp
                            .push(vec![0; char_window_size * 2 - word.chars().count() + 1]);
                    }
                    word_weights_tmp[id][feature.rel_position] =
                        (weight / quantize_multiplier) as i16;
                }
                FeatureContent::CharacterTypeNgram(types) => {
                    let types_u8: Vec<u8> = types.iter().map(|&t| t as u8).collect();
                    let id = type_sorter.get_id(&types_u8) as usize;
                    if id == type_weights_tmp.len() {
                        type_weights_tmp.push(vec![0; type_window_size * 2 - types.len() + 1]);
                    }
                    type_weights_tmp[id][feature.rel_position] =
                        (weight / quantize_multiplier) as i16;
                }
                FeatureContent::DictionaryWord(size) => {
                    dict_weights[size - 1][feature.rel_position] =
                        (weight / quantize_multiplier) as i32;
                }
            };
        }
        let word_id_sort_map = word_sorter.sort();
        let type_id_sort_map = type_sorter.sort();
        let mut word_weights = vec![];
        let mut type_weights = vec![];
        for id in word_id_sort_map {
            word_weights.push(word_weights_tmp[id].clone());
        }
        for id in type_id_sort_map {
            type_weights.push(type_weights_tmp[id].clone());
        }
        let word_fst = Fst::from_iter_map(word_sorter.map).unwrap();
        let type_fst = Fst::from_iter_map(type_sorter.map).unwrap();
        Self {
            word_fst,
            type_fst,
            dict_fst,

            #[cfg(feature = "model-quantize")]
            quantize_multiplier,

            word_weights,
            type_weights,
            dict_weights,
            dict_word_wise: false,
            bias,
            char_window_size,
            type_window_size,
        }
    }
}