Skip to content

Commit

Permalink
Fix bug: sort trigrams by number of occurences and alphabetic order
Browse files Browse the repository at this point in the history
  • Loading branch information
greyblake committed Feb 5, 2017
1 parent 0aa0532 commit 30d142d
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/trigrams.rs
Expand Up @@ -4,14 +4,14 @@ use std::collections::HashMap;
pub fn get_trigrams_with_positions(text : &str) -> HashMap<String, u32> {
let counter_hash = count(text);

let mut count_vec: Vec<_> = counter_hash.iter().collect();
count_vec.sort_by_key(|key| key.1 );
count_vec.reverse();
// Sort in descending order by number of occurrences and trigrams
let mut count_vec: Vec<_> = counter_hash.iter().map(|(trigram, count)| (count, trigram)).collect();
count_vec.sort_by(|a, b| b.cmp(a));

let mut result: HashMap<String, u32> = HashMap::new();

// TODO: extract 600 as LANG_PROFILE_LENGTH * 2
for (i, trigram) in count_vec.iter().take(600).map(|x| x.0).enumerate() {
for (i, trigram) in count_vec.iter().take(600).map(|x| x.1).enumerate() {
// TODO: find a way not to clone it
result.insert((*trigram).clone(), i as u32);
}
Expand Down

0 comments on commit 30d142d

Please sign in to comment.