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
use alloc::string::String;
use alloc::vec::Vec;
#[cfg(feature = "std")]
use std::io::{Read, Write};
use bincode::{Decode, Encode};
use crate::dict_model::{DictModel, WordWeightRecord};
use crate::errors::{Result, VaporettoError};
use crate::ngram_model::{NgramModel, TagNgramModel};
use crate::utils::VecWriter;
const MODEL_MAGIC: &[u8] = b"VaporettoTokenizer 0.5.0\n";
#[derive(Debug, Decode, Encode)]
pub struct TagModel {
pub(crate) token: String,
pub(crate) tags: Vec<Vec<String>>,
pub(crate) char_ngram_model: TagNgramModel<String>,
pub(crate) type_ngram_model: TagNgramModel<Vec<u8>>,
pub(crate) bias: Vec<i32>,
}
impl TagModel {
pub fn token(&self) -> &str {
&self.token
}
}
#[derive(Debug)]
pub struct Model(pub(crate) ModelData);
#[derive(Debug, Decode, Encode)]
pub struct ModelData {
pub(crate) char_ngram_model: NgramModel<String>,
pub(crate) type_ngram_model: NgramModel<Vec<u8>>,
pub(crate) dict_model: DictModel,
pub(crate) bias: i32,
pub(crate) char_window_size: u8,
pub(crate) type_window_size: u8,
pub(crate) tag_models: Vec<TagModel>,
}
impl Model {
#[cfg(any(feature = "train", feature = "kytea", test))]
pub(crate) const fn new(
char_ngram_model: NgramModel<String>,
type_ngram_model: NgramModel<Vec<u8>>,
dict_model: DictModel,
bias: i32,
char_window_size: u8,
type_window_size: u8,
tag_models: Vec<TagModel>,
) -> Self {
Self(ModelData {
char_ngram_model,
type_ngram_model,
dict_model,
bias,
char_window_size,
type_window_size,
tag_models,
})
}
pub fn to_vec(&self) -> Result<Vec<u8>> {
let mut wtr = VecWriter(MODEL_MAGIC.to_vec());
let config = bincode::config::standard();
bincode::encode_into_writer(&self.0, &mut wtr, config)?;
Ok(wtr.0)
}
#[cfg(feature = "std")]
pub fn write<W>(&self, mut wtr: W) -> Result<()>
where
W: Write,
{
wtr.write_all(MODEL_MAGIC)?;
let config = bincode::config::standard();
bincode::encode_into_std_write(&self.0, &mut wtr, config)?;
Ok(())
}
pub fn read_slice(slice: &[u8]) -> Result<(Self, &[u8])> {
if &slice[..MODEL_MAGIC.len()] != MODEL_MAGIC {
return Err(VaporettoError::invalid_model("model version mismatch"));
}
let config = bincode::config::standard();
let (data, size) = bincode::decode_from_slice(&slice[MODEL_MAGIC.len()..], config)?;
Ok((Self(data), &slice[MODEL_MAGIC.len() + size..]))
}
#[cfg(feature = "std")]
pub fn read<R>(mut rdr: R) -> Result<Self>
where
R: Read,
{
let mut magic = [0; MODEL_MAGIC.len()];
rdr.read_exact(&mut magic)?;
if magic != MODEL_MAGIC {
return Err(VaporettoError::invalid_model("model version mismatch"));
}
let config = bincode::config::standard();
Ok(Self(bincode::decode_from_std_read(&mut rdr, config)?))
}
pub fn dictionary(&self) -> &[WordWeightRecord] {
self.0.dict_model.dictionary()
}
pub fn replace_dictionary(&mut self, dict: Vec<WordWeightRecord>) {
self.0.dict_model = DictModel::new(dict);
}
pub fn tag_models(&self) -> &[TagModel] {
&self.0.tag_models
}
}