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
//! Data structures and methods for parsing and applying Knuth-Liang
//! hyphenation patterns.

use std::cmp::{max};
use std::collections::hash_map::{HashMap, Entry};
use std::hash::BuildHasherDefault;
use std::iter::{once};

use fnv::FnvHasher;
use unicode_normalization::{UnicodeNormalization};

use klpair::KLPair;


/// A basic trie, used to associate patterns to their hyphenation scores.
#[derive(Clone, Debug)]
pub struct Patterns {
    pub tally: Option<Vec<u32>>,
    pub descendants: HashMap<char, Patterns, BuildHasherDefault<FnvHasher>>
}

impl Patterns {
    /// Creates an empty `Patterns` trie.
    pub fn empty() -> Patterns {
        let fnv = BuildHasherDefault::<FnvHasher>::default();

        Patterns {
            tally: None,
            descendants: HashMap::with_hasher(fnv)
        }
    }

    /// Inserts a Knuth-Liang hyphenation pair into the trie.
    pub fn insert(&mut self, klpair: KLPair) {
        let (p, tally) = klpair;
        let p_norm = p.nfc();

        let node = p_norm.fold(self, |t, c| {
            match t.descendants.entry(c) {
                Entry::Vacant(e) => e.insert(Patterns::empty()),
                Entry::Occupied(e) => e.into_mut()
            }
        });

        node.tally = Some(tally);
    }

    /// Assigns a score to each potential hyphenation point.
    ///
    /// All patterns matching a substring of `word` are compounded, and for
    /// each hyphenation point, the highest competing value is selected.
    pub fn score(&self, word: &str) -> Vec<u32> {
        let w = word.to_lowercase();
        let cs = once('.').chain(w.chars()).chain(once('.'));
        let length = cs.clone().count();
        let mut points: Vec<u32> = vec![0; length + 1];

        for i in 0..length {
            let mut m = &self.descendants;
            for c in cs.clone().skip(i) {
                match m.get(&c) {
                    Some(&Patterns { tally: Some(ref t), descendants: ref m1 }) =>
                        for (j, &p) in t.iter().enumerate() {
                            let p1 = points[i + j];
                            m = m1;
                            points[i + j] = max(p, p1)
                    },
                    Some(patterns) => m = &patterns.descendants,
                    _ => break
                }
            }
        }

        points.truncate(length - 1);
        points.remove(0);
        points
    }
}