File: lib.rs

package info (click to toggle)
rust-hyphenation 0.8.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,680 kB
  • sloc: makefile: 2
file content (205 lines) | stat: -rw-r--r-- 6,342 bytes parent folder | download
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
extern crate once_cell;
extern crate quickcheck;
extern crate unicode_segmentation;

use std::fs::File;
use std::io::BufReader;
use std::path::Path;

use once_cell::sync::Lazy;
use quickcheck::{quickcheck, TestResult};

extern crate hyphenation;
extern crate hyphenation_commons;
use hyphenation::extended::*;
use hyphenation::Language::*;
use hyphenation::*;

fn fiat_std(lang : Language) -> Standard {
    let filename = format!("{}.standard.bincode", lang.code());
    let file = File::open(Path::new("dictionaries").join(filename)).unwrap();
    Standard::from_reader(lang, &mut BufReader::new(file)).unwrap()
}

fn fiat_ext(lang : Language) -> Extended {
    let filename = format!("{}.extended.bincode", lang.code());
    let file = File::open(Path::new("dictionaries").join(filename)).unwrap();
    Extended::from_reader(lang, &mut BufReader::new(file)).unwrap()
}

static EN_US : Lazy<Standard> = Lazy::new(|| fiat_std(EnglishUS));
static HU : Lazy<Extended> = Lazy::new(|| fiat_ext(Hungarian));
static TR : Lazy<Standard> = Lazy::new(|| fiat_std(Turkish));


#[test]
fn collected_equals_original() {
    fn property(original : String) -> bool {
        let collected : String = EN_US.hyphenate(&original).iter().segments().collect();

        collected == original
    }

    quickcheck(property as fn(String) -> bool);
}

#[test]
fn opportunities_within_bounds() {
    fn property(s : String) -> TestResult {
        let ci : Vec<_> = s.char_indices().collect();
        let (l_min, r_min) = EnglishUS.minima();
        let s_len = ci.len();
        if s_len < l_min + r_min {
            return TestResult::discard();
        }

        let os : Vec<_> = EN_US.opportunities(&s);
        let ((l, _), (r, _)) = (ci[l_min], ci[s_len - r_min]);
        let within_bounds = |&i| i >= l && i <= r;

        TestResult::from_bool(os.iter().all(within_bounds))
    }

    quickcheck(property as fn(String) -> TestResult);
}

#[test]
fn basics_standard() {
    // Standard hyphenation
    let w0 = "anfractuous";
    let w1 = "hypha"; // minimum hyphenable length
                      // Exceptions
    let ex0 = "hyphenation";
    let ex1 = "bevies"; // unhyphenable (by exception)

    let h_w0 = EN_US.hyphenate(w0);
    let h_w1 = EN_US.hyphenate(w1);
    let h_ex0 = EN_US.hyphenate(ex0);
    let h_ex1 = EN_US.hyphenate(ex1);

    let seg0 = h_w0.iter().segments();
    let seg1 = h_ex0.iter().segments();
    let seg2 = h_ex1.iter().segments();
    let seg3 = h_w1.iter().segments();

    assert_eq!(seg0.size_hint(), (4, Some(4)));
    assert_eq!(seg1.size_hint(), (4, Some(4)));
    assert_eq!(seg2.size_hint(), (1, Some(1)));
    assert_eq!(seg3.size_hint(), (2, Some(2)));

    let v0 : Vec<&str> = seg0.clone().collect();
    let v1 : Vec<&str> = seg1.clone().collect();
    let v2 : Vec<&str> = seg2.clone().collect();
    let v3 : Vec<&str> = seg3.clone().collect();

    assert_eq!(v0, vec!["an", "frac", "tu", "ous"]);
    assert_eq!(v1, vec!["hy", "phen", "a", "tion"]);
    assert_eq!(v2, vec!["bevies"]);
    assert_eq!(v3, vec!["hy", "pha"]);

    // Additional size checks for partially consumed iterators.
    let mut seg2 = seg2;
    seg2.next();
    assert_eq!(seg2.size_hint(), (0, Some(0)));
    seg2.next();
    assert_eq!(seg2.size_hint(), (0, Some(0)));

    let mut seg3 = seg3;
    seg3.next();
    assert_eq!(seg3.size_hint(), (1, Some(1)));
    seg3.next();
    assert_eq!(seg3.size_hint(), (0, Some(0)));
}

#[test]
fn basics_extended() {
    let w0 = "asszonnyal";
    let w1 = "esszé";

    let v0 : Vec<_> = HU.hyphenate(w0).into_iter().segments().collect();
    let v1 : Vec<_> = HU.hyphenate(w1).into_iter().segments().collect();

    assert_eq!(v0, vec!["asz", "szony", "nyal"]);
    assert_eq!(v1, vec!["esz", "szé"]);
}

#[test]
fn special_casing() {
    let w0 = "İbrahim";
    let v0 : Vec<_> = TR.hyphenate(&w0).into_iter().segments().collect();
    assert_eq!(v0, vec!["İb", "ra", "him"]);

    let w1 = "İLGİNÇ";
    let v1 : Vec<_> = TR.hyphenate(w1).into_iter().segments().collect();
    assert_eq!(v1, vec!["İL", "GİNÇ"]);

    let w2 = "MİCRO";
    let v2 : Vec<_> = EN_US.hyphenate(w2).into_iter().segments().collect();
    assert_eq!(v2, vec!["Mİ", "CRO"]);

    let w4 = "İDİOM";
    let v4 : Vec<_> = EN_US.hyphenate(w4).into_iter().segments().collect();
    assert_eq!(v4, vec!["İD", "İOM"]);

    let w3 = "MUCİLAGİNOUS";
    let v3 : Vec<_> = EN_US.hyphenate(w3).into_iter().segments().collect();
    assert_eq!(v3, vec!["MU", "CİLAGİ", "NOUS"]);
}

#[test]
fn language_mismatch_on_load() {
    let file = File::open("./dictionaries/mul-ethi.standard.bincode").unwrap();
    let mut reader = BufReader::new(file);
    assert!(Standard::from_reader(EnglishUS, &mut reader).is_err());
}

#[test]
fn text() {
    use unicode_segmentation::UnicodeSegmentation;

    let hyphenate_text = |text : &str| -> String {
        text.split_word_bounds()
            .flat_map(|word| EN_US.hyphenate(word).into_iter())
            .collect()
    };

    let t0 = "I know noble accents / And lucid, inescapable rhythms; […]";
    let expect0 = "I know no-ble ac-cents / And lu-cid, in-escapable rhythms; […]";
    let seg0 = hyphenate_text(t0);
    assert_eq!(seg0, expect0);

    let t1 = "ever-burning sulphur unconsumed";
    let expect1 = "ever-burn-ing sul-phur un-con-sumed";
    let seg1 = hyphenate_text(t1);
    assert_eq!(seg1, expect1);
}

#[test]
fn bounded_exception() {
    let e = "anisotropic"; // an-iso-trop-ic, by exception

    let bounded = EN_US.exception(e);
    let unbounded = EN_US.exception_within(e, (0, e.len()));

    assert_eq!(bounded, Some(vec![2, 5]));
    assert_eq!(unbounded, Some(vec![2, 5, 9]));
}

#[test]
fn readme_examples() {
    let hyphenated = EN_US.hyphenate("hyphenation");

    let break_indices = &hyphenated.breaks;
    assert_eq!(break_indices, &[2, 6, 7]);

    let marked = hyphenated.iter();
    let collected : Vec<String> = marked.collect();
    assert_eq!(collected, vec!["hy-", "phen-", "a-", "tion"]);

    let unmarked = hyphenated.iter().segments();
    let collected : Vec<&str> = unmarked.collect();
    assert_eq!(collected, vec!["hy", "phen", "a", "tion"]);

    let uppercase : Vec<_> = EN_US.hyphenate("CAPITAL").into_iter().segments().collect();
    assert_eq!(uppercase, vec!["CAP", "I", "TAL"]);
}