chore: Clean up ranking functions internal names

This commit is contained in:
Clément Renault 2018-07-05 20:22:08 +02:00
parent 23134fee02
commit e4b1bb5d26

View File

@ -39,22 +39,21 @@ impl Document {
} }
fn sum_of_typos(lhs: &Document, rhs: &Document) -> Ordering { fn sum_of_typos(lhs: &Document, rhs: &Document) -> Ordering {
fn sum_of_typos(doc: &Document) -> u8 { let key = |doc: &Document| -> u8 {
GroupBy::new(&doc.matches, match_query_index).map(|m| m[0].distance).sum() GroupBy::new(&doc.matches, match_query_index).map(|m| m[0].distance).sum()
} };
sum_of_typos(lhs).cmp(&sum_of_typos(rhs))
key(lhs).cmp(&key(rhs))
} }
fn number_of_words(lhs: &Document, rhs: &Document) -> Ordering { fn number_of_words(lhs: &Document, rhs: &Document) -> Ordering {
fn number_of_words(doc: &Document) -> usize { let key = |doc: &Document| -> usize {
GroupBy::new(&doc.matches, match_query_index).count() GroupBy::new(&doc.matches, match_query_index).count()
} };
number_of_words(lhs).cmp(&number_of_words(rhs)).reverse()
key(lhs).cmp(&key(rhs)).reverse()
} }
fn words_proximity(lhs: &Document, rhs: &Document) -> Ordering {
fn word_proximity(doc: &Document) -> u32 {
fn attribute_proximity(lhs: &Match, rhs: &Match) -> u32 {
fn index_proximity(lhs: u32, rhs: u32) -> u32 { fn index_proximity(lhs: u32, rhs: u32) -> u32 {
if lhs < rhs { if lhs < rhs {
cmp::min(rhs - lhs, MAX_DISTANCE) cmp::min(rhs - lhs, MAX_DISTANCE)
@ -63,10 +62,13 @@ fn words_proximity(lhs: &Document, rhs: &Document) -> Ordering {
} }
} }
fn attribute_proximity(lhs: &Match, rhs: &Match) -> u32 {
if lhs.attribute != rhs.attribute { return MAX_DISTANCE } if lhs.attribute != rhs.attribute { return MAX_DISTANCE }
index_proximity(lhs.attribute_index, rhs.attribute_index) index_proximity(lhs.attribute_index, rhs.attribute_index)
} }
fn words_proximity(lhs: &Document, rhs: &Document) -> Ordering {
let key = |doc: &Document| -> u32 {
let mut proximity = 0; let mut proximity = 0;
let mut next_group_index = 0; let mut next_group_index = 0;
for group in GroupBy::new(&doc.matches, match_query_index) { for group in GroupBy::new(&doc.matches, match_query_index) {
@ -78,22 +80,25 @@ fn words_proximity(lhs: &Document, rhs: &Document) -> Ordering {
} }
} }
proximity proximity
} };
word_proximity(lhs).cmp(&word_proximity(rhs))
key(lhs).cmp(&key(rhs))
} }
fn sum_of_words_attribute(lhs: &Document, rhs: &Document) -> Ordering { fn sum_of_words_attribute(lhs: &Document, rhs: &Document) -> Ordering {
fn sum_attribute(doc: &Document) -> u8 { let key = |doc: &Document| -> u8 {
GroupBy::new(&doc.matches, match_query_index).map(|m| m[0].attribute).sum() GroupBy::new(&doc.matches, match_query_index).map(|m| m[0].attribute).sum()
} };
sum_attribute(lhs).cmp(&sum_attribute(rhs))
key(lhs).cmp(&key(rhs))
} }
fn sum_of_words_position(lhs: &Document, rhs: &Document) -> Ordering { fn sum_of_words_position(lhs: &Document, rhs: &Document) -> Ordering {
fn sum_attribute_index(doc: &Document) -> u32 { let key = |doc: &Document| -> u32 {
GroupBy::new(&doc.matches, match_query_index).map(|m| m[0].attribute_index).sum() GroupBy::new(&doc.matches, match_query_index).map(|m| m[0].attribute_index).sum()
} };
sum_attribute_index(lhs).cmp(&sum_attribute_index(rhs))
key(lhs).cmp(&key(rhs))
} }
fn exact(lhs: &Document, rhs: &Document) -> Ordering { fn exact(lhs: &Document, rhs: &Document) -> Ordering {