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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//! Grouped representation and pretty printing of `DictQueryResult` in tabular form.
//!
//! The grouping is inspired by the [result page of dict.cc](https://www.dict.cc/?s=house).
//!
//! The grouping has two layers:
//! 1. By word count
//! 2. By word class group.
//!
//! # Example Output
//!
//! ```ignore
//! Verbs
//! --------------------
//!  Verb | verb | Verb
//!
//! Nouns
//! --------------------------
//!  DE         | EN   | Noun
//!  foo        | foo  | Noun
//!  Substantiv | noun | Noun
//!
//! Others
//! -------------------
//!  a | c | Adjective
//!  B | B | Adjective
//!  c | a | Adjective
//!
//! 2 Words: Verbs
//! ----------------------------
//!  foo Verb | foo verb | Verb
//! ```

use super::*;

use itertools::Itertools;
use itertools::GroupBy;
use std::vec::IntoIter;

/// Coarse grouping of `WordClass`.
#[allow(missing_docs)]
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Debug)]
pub enum WordClassesGroup {
    Verbs,
    Nouns,
    Others,
}

impl<'a> From<&'a [WordClass]> for WordClassesGroup {
    fn from(word_classes: &[WordClass]) -> Self {
        use self::WordClassesGroup::*;

        if word_classes.contains(&WordClass::Verb) {
            Verbs
        } else if word_classes.contains(&WordClass::Noun) {
            Nouns
        } else {
            Others
        }
    }
}

/// Grouped representation of `DictQueryResult`.
///
/// Implements Display using a formatted and aligned table.
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct DictQueryResultGrouped {
    word_count_groups: Vec<DictEntryWordCountGroup>
}

impl DictQueryResultGrouped {
    /// Returns a slice of `DictEntryWordCountGroup`.
    pub fn word_count_groups(&self) -> &[DictEntryWordCountGroup] {
        &self.word_count_groups
    }
}

impl Display for DictQueryResultGrouped {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        use prettytable::format::LinePosition::*;
        use prettytable::format::LineSeparator;
        use prettytable::format::consts::FORMAT_CLEAN;
        use prettytable::Table;

        let mut table = Table::init(
            self.word_count_groups.iter()
                .map(|word_count_group| row!(word_count_group.to_string()))
                .collect()
        );

        let mut format = *FORMAT_CLEAN;

        format.separator(Intern, LineSeparator::new(' ', ' ', ' ', ' '));
        format.padding(0, 0);

        table.set_format(format);

        f.write_str(&table.to_string())
    }
}


impl From<DictQueryResult> for DictQueryResultGrouped {
    fn from(query_result: DictQueryResult) -> Self {
        fn group_entries<G, I>(mut entries: Vec<I>, by: fn(&I) -> G)
                               -> GroupBy<G, IntoIter<I>, fn(&I) -> G>
            where G: Ord {
            entries.sort_unstable_by_key(by);

            entries.into_iter().group_by(by)
        }

        let query_direction = query_result.query_direction;
        let entries = query_result.entries;

        let get_word_count: fn(&DictEntry) -> u8 = match query_direction {
            QueryDirection::ToRight => |entry: &DictEntry| {
                entry.left_word.word_count
            },
            QueryDirection::Bidirectional => DictEntry::get_max_word_count,
            QueryDirection::ToLeft => |entry: &DictEntry| {
                entry.right_word.word_count
            },
        };

        let word_count_group_by =
            group_entries(entries, get_word_count);

        let grouped_entries: Vec<_> = word_count_group_by.into_iter().map(|(word_count, same_word_count_group)| {
            let same_word_count_pairs: Vec<(WordClassesGroup, DictEntry)> = same_word_count_group
                .map(|entry| {
                    let word_classes_group: WordClassesGroup = entry.word_classes.as_slice().into();

                    (word_classes_group, entry)
                })
                .collect();

            let word_class_group_by =
                group_entries(same_word_count_pairs, |&(word_class_group, _)| word_class_group);

            let vec_word_class_group: Vec<DictEntryWordClassGroup> =
                word_class_group_by.into_iter().map(|(word_class_group, entries_group)| {
                    let mut entries: Vec<DictEntry> = entries_group.map(|(_, entry)| entry).collect();

                    let cmp_left = |left_entry: &DictEntry, right_entry: &DictEntry| {
                        let left = &left_entry.left_word.indexed_word;
                        let right = &right_entry.left_word.indexed_word;

                        left.cmp(right)
                    };

                    let cmp_right = |left_entry: &DictEntry, right_entry: &DictEntry| {
                        let left = &left_entry.right_word.indexed_word;
                        let right = &right_entry.right_word.indexed_word;

                        left.cmp(right)
                    };

                    match query_direction {
                        QueryDirection::ToRight |
                        QueryDirection::Bidirectional => entries.sort_by(cmp_left),
                        QueryDirection::ToLeft => entries.sort_by(cmp_right),
                    };

                    DictEntryWordClassGroup {
                        word_count,
                        word_class_group,
                        entries,
                    }
                }).collect();

            DictEntryWordCountGroup {
                word_count,
                word_class_groups: vec_word_class_group,
            }
        }).collect();

        DictQueryResultGrouped {
            word_count_groups: grouped_entries,
        }
    }
}

/// A group of entries, which have the same word count and are coarsely grouped by word class.
///
/// Implements Display using a formatted and aligned table.
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct DictEntryWordCountGroup {
    word_count: u8,
    word_class_groups: Vec<DictEntryWordClassGroup>,
}

impl DictEntryWordCountGroup {
    /// Returns a slice of `DictEntryWordClassGroup`.
    pub fn word_class_groups(&self) -> &[DictEntryWordClassGroup] {
        &self.word_class_groups
    }

    /// The word count of this group.
    pub fn word_count(&self) -> u8 {
        self.word_count
    }
}

impl Display for DictEntryWordCountGroup {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        use prettytable::format::LinePosition::*;
        use prettytable::format::LineSeparator;
        use prettytable::format::consts::FORMAT_CLEAN;
        use prettytable::Table;

        let mut table = Table::init(
            self.word_class_groups.iter()
                .map(|word_class_group| row!(word_class_group.to_string()))
                .collect()
        );

        let mut format = *FORMAT_CLEAN;

        format.separator(Intern, LineSeparator::new(' ', ' ', ' ', ' '));
        format.padding(0, 0);

        table.set_format(format);

        f.write_str(&table.to_string())
    }
}

/// A group of entries, which have the same word count and word class group.
///
/// Implements Display using a formatted and aligned table.
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct DictEntryWordClassGroup {
    word_count: u8,
    word_class_group: WordClassesGroup,
    entries: Vec<DictEntry>,
}

impl DictEntryWordClassGroup {
    /// Returns a slice of entries in this group.
    pub fn entries(&self) -> &[DictEntry] {
        &self.entries
    }

    /// The word count of this group.
    pub fn word_count(&self) -> u8 {
        self.word_count
    }

    /// The word class group of this entry group.
    pub fn word_class_group(&self) -> WordClassesGroup {
        self.word_class_group
    }
}


impl Display for DictEntryWordClassGroup {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        use prettytable;
        use prettytable::Table;

        // TODO: word classes filter (redundant classes)
        let entry_rows: Vec<_> = self.entries.iter().map(|entry| {
            let left = &entry.left_word.to_colored_string();
            let right = &entry.right_word.to_colored_string();

            let word_classes = &entry.word_classes.iter().map(|word_class| format!("{:?}", word_class)).collect::<Vec<_>>().join(", ");

            row![left, right, word_classes]
        }).collect();

        let mut entry_table = Table::init(entry_rows);

        entry_table.set_format(*prettytable::format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);

        let entry_table_string = entry_table.to_string();

        let header_string = match self.word_count {
            0 | 1 => format!("{:?}", self.word_class_group),
            higher_word_count => format!("{} Words: {:?}", higher_word_count, self.word_class_group),
        };

        let mut complete_table = Table::init(vec![row![header_string], row![entry_table_string]]);


        let mut format = *prettytable::format::consts::FORMAT_NO_BORDER;

        format.padding(0, 0);

        complete_table.set_format(format);

        f.write_str(&complete_table.to_string())
    }
}