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
//! A simple Chinese Convert library (partially) compatible with
//! [OpenCC](https://github.com/BYVoid/OpenCC/)'s 
//! [dictionaries](https://github.com/BYVoid/OpenCC/tree/master/data/dictionary).
//! 
//! * Simple
//! 
//!   No complex configurations, all need is a text dictionary and input text.
//! 
//!   Built-in dictionaries included if `builtin_dicts` feature is on.
//! 
//! * Fast
//! 
//!   Use hashmap with tree structure for dictionary, faster than original
//!   OpenCC.
use std::{
    collections::HashMap,
    collections::hash_map::Entry,
    io::{Read, BufRead, BufReader},
    mem,
};

#[cfg(feature = "builtin_dicts")]
pub mod dicts;

#[cfg(test)]
mod tests;

/// Dictionary to convert text.
/// 
/// This library doesn't contain dictionary file. You may download them from
/// [OpenCC's repo](https://github.com/BYVoid/OpenCC/tree/master/data/dictionary).
/// 
/// For chaining multiple dicts, just concat all to one file (in any order).
/// 
/// # Built-in dictionaries
/// The library includes optional dictionaries when `builtin_dicts`
/// feature is on. Disabled by default.
/// 
///
/// # File Format
/// The format is the same as text format (not the compiled binary one)
/// in OpenCC project.
/// 
/// Specifically, one rule per line, two columns per rule splitted by a TAB
/// (`\t`). First column is original word/phrase, the other is converted one,
/// which may contains multiples word/phrase splitted by a space (` `), but
/// only the first one is used, others will be ignored. 
/// 
#[derive(Debug, Clone)]
pub struct Dict {
    roots: Vec<DictNode>,
}

#[derive(Debug, Clone)]
struct Leaf {
    key: Box<str>,
    value: Box<str>,
}

#[derive(Debug, Clone, Default)]
struct Node {
    value: Option<Box<str>>,
    tails: HashMap<char, DictNode>,
}

#[derive(Debug, Clone)]
enum DictNode {
    Leaf (Leaf),
    Node (Node),
}

impl DictNode {
    fn node() -> Self {
        DictNode::Node (
            Node::default()
        )
    }

    fn unwrap_node_mut(&mut self) -> &mut Node {
        match self {
            DictNode::Node (node) => node,
            DictNode::Leaf (_) => panic!("expect Node, found Leaf"),
        }

    }

    fn into_leaf(self) -> Leaf {
        match self {
            DictNode::Leaf (leaf) => leaf,
            DictNode::Node (_) => panic!("expect Leaf, found Node"),
        }
    }

    fn leaf(key: &str, value: Box<str>) -> Self {
        DictNode::Leaf (
            Leaf {
                key: key.into(),
                value,
            }
        )
    }

    fn add(&mut self, key: &str, value: Box<str>) {
        let self_node = match self {
            DictNode::Node (node) => node,
            DictNode::Leaf (_) => {
                let node = Node::default();
                let leaf = mem::replace(self, DictNode::Node(node));
                let Leaf { key, value } = leaf.into_leaf();
                let mut node = self.unwrap_node_mut();
                let mut key_chars = key.chars();
                node.value = if let Some(hash_key) = key_chars.next() {
                    let suffix = key_chars.as_str().into();
                    node.tails.insert(hash_key, DictNode::leaf(suffix, value));
                    None
                } else {
                    Some(value)
                };
                node
            }
        };

        let mut key_chars = key.chars();
        if let Some(hash_key) = key_chars.next() {
            let suffix = key_chars.as_str().into();
            match self_node.tails.entry(hash_key) {
                Entry::Occupied(mut entry) => {
                    entry.get_mut().add(suffix, value);
                }
                Entry::Vacant(entry) => {
                    entry.insert(DictNode::leaf(suffix, value));
                }
            };
        } else {
            self_node.value = Some(value);
        }
    }

    fn prefix_match<'a, 'b>(&'a self, query: &'b str)
            -> Option<(&'b str, &'a str)> {
        match self {
            &DictNode::Leaf ( Leaf { ref key, ref value } ) => {
                if query.starts_with(&**key) {
                    Some((&query[..key.len()], &value))
                } else {
                    None
                }
            },
            &DictNode::Node ( Node { ref value, ref tails } ) => {
                let mut query_chars = query.chars();
                let hash_key = query_chars.next();
                let suffix = query_chars.as_str();

                hash_key.and_then(|hash_key| {
                    tails.get(&hash_key)
                        .and_then(|node| node.prefix_match(suffix))
                        .map(|(prefix, value)| {
                            let n = query.len() - suffix.len() + prefix.len();
                            (&query[..n], value)
                        })
                }).or_else(||
                    value.as_ref().map(|v| ("", &**v))
                )
            }
        }
    }
}

impl Dict {
    /// Load dict from string
    pub fn load_str<T>(raw: T) -> Self
    where T: AsRef<str> {
        Dict::load_lines(raw.as_ref().lines())
    }

    /// Load dict from lines of string.
    pub fn load_lines<T, S>(lines: T) -> Self
    where T: Iterator<Item=S>,
          S: AsRef<str> {
        let mut root = DictNode::node();
        lines.filter_map(|line| {
            let mut cols = line.as_ref().splitn(2, '\t');
            let key = cols.next()?;
            let value = cols.next()?.splitn(2, ' ').next()?;
            Some((key.into(), value.into()))
        }).for_each(|(key, value): (String, Box<str>)| {
            root.add(&key, value)
        });
        Dict { roots: vec![root] }
    }

    /// Load dict file.
    /// Unrecognizable data will be silently ignored.
    pub fn load<T>(reader: T) -> Self
    where T: Read {
        let lines = BufReader::new(reader).lines().filter_map(|l| l.ok());
        Dict::load_lines(lines)
    }

    /// Return the new dict that chained together.
    pub fn chain(self, other: Dict) -> Self {
        let Dict { mut roots } = self;
        roots.extend(other.roots);
        Dict { roots }
    }

    fn replace(dict: &DictNode, mut text: &str) -> String {
        let mut output = String::with_capacity(text.len());
        while !text.is_empty() {
            match dict.prefix_match(text) {
                Some((prefix, value)) => {
                    output.push_str(value);
                    text = &text[prefix.len()..];
                },
                None => {
                    let mut chars = text.chars();
                    output.push(chars.next().unwrap());
                    text = chars.as_str();
                }
            }
        }
        output
    }

    /// Use this dict to convert string.
    /// Return converted text.
    pub fn replace_all(&self, text: &str) -> String {
        let mut buffer = Dict::replace(&self.roots[0], text);
        for dict in &self.roots[1..] {
            buffer = Dict::replace(dict, &buffer);
        }
        buffer
    }
}