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
use std::path::Path;
use std::path::PathBuf;
use std::io::BufWriter;
use std::fs::File;
use std::cmp;
use std::collections::HashMap;
use std::error::Error;
use std::rc::Rc;
use byteorder::{WriteBytesExt, NativeEndian as NE};
use util::*;
use dictionary::build::*;
use dictionary::charcategory::{Category, SPACE_CHAR};
use trie::Searcher;

pub const KEY_PREFIX: &'static str = "\x02";

/// 文字カテゴリ定義を保持したバイナリデータを作成する
pub struct CharCategory {
    input_dir: PathBuf,
    encoding: String,
    output_dir: PathBuf
}

impl CharCategory {
    /// コンストラクタ
    /// # Arguments
    /// * `input_dir`  - テキスト単語辞書が配置されているディレクトリのパス
    /// * `encoding`   - テキスト単語辞書の文字列エンコーディング
    /// * `output_dir` - バイナリ単語辞書の保存先ディレクトリ
    pub fn new(input_dir: &Path, encoding: &str, output_dir: &Path) -> CharCategory {
        CharCategory {
            input_dir: input_dir.to_owned(),
            encoding: encoding.to_owned(),
            output_dir: output_dir.to_owned()
        }
    }

    /// 文字カテゴリ定義のバイナリデータを作成する
    pub fn build(self) -> AppResult<()> {
        // 文字カテゴリの定義を取得する
        let ccmap = self.parse_char_category_def()?;

        {
            // 文字カテゴリの定義を保存する
            let mut categories: Vec<&Category> = Vec::new();
            for e in ccmap.values() {
                categories.push(e);
            }
            self.save_char_category_map(categories)?;
        }

        // 文字とカテゴリのマッピングを取得/保存する
        self.build_code_category_map(ccmap)?;

        Ok(())
    }

    fn parse_char_category_def(&self) -> AppResult<HashMap<String, Category>> {
        let path = self.input_dir.join("char.def");
        let too_few_fields = |rl: &ReadLine| -> AppError {
            rl.parse_error("Invalid char category definition (too few fields).")
        };
        let parse_0or1 = |str: Option<&str>, rl: &ReadLine| -> AppResult<bool> {
            str.ok_or(too_few_fields(rl))
                .and_then(|s|
                    if s == "1" {
                        Ok(true)
                    } else if s == "0" {
                        Ok(false)
                    } else {
                        Err(rl.parse_error("Invalid char category definition (INVOKE must be '0' or '1')."))
                    }
                )
        };
        let mut rl = ReadLine::new(path.as_path(), &self.encoding)?;
        let srch = Searcher::new(self.output_dir.join("word2id").as_path())?;
        let mut map = HashMap::new();

        let mut s = String::new();
        loop {
            let len = rl.next(&mut s).map_err(|e| rl.convert_error(e))?;
            if len < 1 {
                break;
            }
            let line = s.trim_right();
            if line.len() < 1 || line.starts_with('#') || line.starts_with('0') {
                continue;
            }

            let mut ss = line.split_whitespace();
            let name = ss.next().ok_or(too_few_fields(&rl))?;
            let invoke: bool = parse_0or1(ss.next(), &rl)?;      // 0 or 1
            let group: bool = parse_0or1(ss.next(), &rl)?;      // 0 or 1
            // positive integer
            let length: i32 = ss.next()
                .ok_or(too_few_fields(&rl))
                .and_then(|s| s.parse().map_err(|e| AppError::from(e)))?;
            let key_utf16 = (KEY_PREFIX.to_string() + name).encode_utf16().collect::<Vec<_>>();
            let id = srch.search(&key_utf16);

            if length < 0 {
                return Err(rl.parse_error("Invalid char category definition (LENGTH must be 0 or positive integer)."));
            }
            if id < 0 {
                return Err(rl.parse_error(format!("Category '{}' is unregistered in trie", name)));
            }
            map.insert(name.to_string(), Category {
                id: id,
                length: length,
                invoke: invoke,
                group: group
            });
        }

        // "DEFAULT"と"SPACE"は必須カテゴリ
        if !map.contains_key("DEFAULT") {
            return Err(rl.parse_error("Missing mandatory category 'DEFAULT'."));
        }
        if !map.contains_key("SPACE") {
            return Err(rl.parse_error("Missing mandatory category 'SPACE'."));
        }
        Ok(map)
    }

    fn save_char_category_map(&self, mut categories: Vec<&Category>) -> AppResult<()> {
        let mut writer = BufWriter::new(File::create(self.output_dir.join("char.category").as_path())?);
        categories.sort();
        for e in categories {
            writer.write_i32::<NE>(e.id)?;
            writer.write_i32::<NE>(e.length)?;
            writer.write_i32::<NE>(if e.invoke { 1 } else { 0 })?;
            writer.write_i32::<NE>(if e.group { 1 } else { 0 })?;
        }
        Ok(())
    }

    fn build_code_category_map(&self, map: HashMap<String, Category>) -> AppResult<()> {
        let mut chars: Vec<Rc<CharId>> = Vec::with_capacity(0x10000);
        {
            let dft = Rc::new(CharId::new(map["DEFAULT"].id));
            for _ in 0..0x10000 {
                chars.push(dft.clone());
            }
        }

        {
            let path = self.input_dir.join("char.def");
            let mut rl = ReadLine::new(path.as_path(), &self.encoding)?;
            let mut s = String::new();
            loop {
                let len = rl.next(&mut s).map_err(|e| rl.parse_error(e.description()))?;
                if len < 1 {
                    break;
                }
                let line = s.trim_right();
                if line.len() < 1 || !line.starts_with('0') {
                    continue;
                }

                let mut ss = line.split_whitespace();
                let beg: i32;
                let end: i32;
                let ss0 = ss.next().ok_or(rl.parse_error("Too few fields"))?;
                if let Some(idx) = ss0.find("..") {
                    beg = i32::from_str_radix(&ss0[2..idx], 16)
                        .map_err(|e| rl.convert_error(e))?;
                    end = i32::from_str_radix(&ss0[(idx + 2 + 2)..], 16)
                        .map_err(|e| rl.convert_error(e))?;
                } else {
                    beg = i32::from_str_radix(&ss0[2..], 16)
                        .map_err(|e| rl.convert_error(e))?;
                    end = beg;
                }

                if !(0 <= beg && beg <= 0xFFFF &&
                    0 <= end && end <= 0xFFFF && beg <= end) {
                    return Err(rl.parse_error("Wrong UCS2 code specified."));
                }

                // 文字カテゴリ及び互換カテゴリの取得
                let category_name = ss.next().ok_or(rl.parse_error("Too few fields"))?;
                let category = map.get(category_name).ok_or_else(|| rl.parse_error(format!("Category '{}' is undefined.", category_name)))?;
                let ch = {
                    let mut ch = CharId::new(category.id);
                    while let Some(f) = ss.next() {
                        if f.starts_with('#') { break; }
                        let category = map.get(f).ok_or_else(|| rl.parse_error(format!("Category '{}' is undefined.", f)))?;
                        ch.add(category.id);
                    }
                    Rc::new(ch)
                };

                // カテゴリ登録
                for i in beg..(end + 1) {
                    chars[i as usize] = ch.clone();
                }
            }

            if chars[SPACE_CHAR as usize].id != map["SPACE"].id {
                return Err(rl.parse_error("0x0020 is reserved for 'SPACE' category"));
            }
        }

        let mut writer = BufWriter::new(File::create(self.output_dir.join("code2category").as_path())?);
        for c in &chars {
            writer.write_i32::<NE>(c.id)?;
        }
        for c in &chars {
            writer.write_i32::<NE>(c.mask)?;
        }

        Ok(())
    }
}


impl cmp::Ord for Category {
    fn cmp(&self, other: &Self) -> cmp::Ordering {
        self.id.cmp(&other.id)
    }
}

impl cmp::PartialOrd for Category {
    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl cmp::PartialEq for Category {
    fn eq(&self, other: &Self) -> bool {
        self.id.eq(&other.id)
    }
}

impl cmp::Eq for Category {}


struct CharId {
    id: i32,
    mask: i32
}

impl CharId {
    pub fn new(id: i32) -> CharId {
        let mut c = CharId { id: id, mask: 0 };
        c.add(id);
        c
    }

    pub fn add(&mut self, i: i32) {
        self.mask |= 1 << i;
    }
}