2018-07-07 20:43:29 +08:00
|
|
|
use std::cmp::{self, Ordering};
|
2018-12-07 21:41:06 +08:00
|
|
|
|
2019-01-30 23:30:27 +08:00
|
|
|
use slice_group_by::GroupBy;
|
2018-12-07 21:41:06 +08:00
|
|
|
|
2018-10-11 20:04:41 +08:00
|
|
|
use crate::rank::criterion::Criterion;
|
2019-02-02 21:22:31 +08:00
|
|
|
use crate::rank::RawDocument;
|
2018-07-07 20:43:29 +08:00
|
|
|
|
|
|
|
const MAX_DISTANCE: u32 = 8;
|
|
|
|
|
2019-02-02 21:22:31 +08:00
|
|
|
#[inline]
|
|
|
|
fn clone_tuple<T: Clone, U: Clone>((a, b): (&T, &U)) -> (T, U) {
|
|
|
|
(a.clone(), b.clone())
|
|
|
|
}
|
|
|
|
|
2018-07-07 20:43:29 +08:00
|
|
|
fn index_proximity(lhs: u32, rhs: u32) -> u32 {
|
|
|
|
if lhs < rhs {
|
|
|
|
cmp::min(rhs - lhs, MAX_DISTANCE)
|
|
|
|
} else {
|
|
|
|
cmp::min(lhs - rhs, MAX_DISTANCE) + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-02 21:22:31 +08:00
|
|
|
fn attribute_proximity((lattr, lwi): (u16, u32), (rattr, rwi): (u16, u32)) -> u32 {
|
|
|
|
if lattr != rattr { return MAX_DISTANCE }
|
|
|
|
index_proximity(lwi, rwi)
|
2018-07-07 20:43:29 +08:00
|
|
|
}
|
|
|
|
|
2019-02-02 21:22:31 +08:00
|
|
|
fn min_proximity((lattr, lwi): (&[u16], &[u32]), (rattr, rwi): (&[u16], &[u32])) -> u32 {
|
2018-07-07 20:43:29 +08:00
|
|
|
let mut min_prox = u32::max_value();
|
2019-02-12 22:42:17 +08:00
|
|
|
|
2019-02-02 21:22:31 +08:00
|
|
|
for a in lattr.iter().zip(lwi) {
|
|
|
|
for b in rattr.iter().zip(rwi) {
|
|
|
|
let a = clone_tuple(a);
|
|
|
|
let b = clone_tuple(b);
|
2018-07-07 20:43:29 +08:00
|
|
|
min_prox = cmp::min(min_prox, attribute_proximity(a, b));
|
|
|
|
}
|
|
|
|
}
|
2019-02-12 22:42:17 +08:00
|
|
|
|
2018-07-07 20:43:29 +08:00
|
|
|
min_prox
|
|
|
|
}
|
|
|
|
|
2019-02-12 22:42:17 +08:00
|
|
|
fn matches_proximity(
|
|
|
|
query_index: &[u32],
|
|
|
|
distance: &[u8],
|
|
|
|
attribute: &[u16],
|
|
|
|
word_index: &[u32],
|
|
|
|
) -> u32
|
|
|
|
{
|
|
|
|
let mut query_index_groups = query_index.linear_group();
|
2018-07-07 20:43:29 +08:00
|
|
|
let mut proximity = 0;
|
2019-02-02 21:22:31 +08:00
|
|
|
let mut index = 0;
|
2019-02-12 22:42:17 +08:00
|
|
|
|
|
|
|
let get_attr_wi = |index: usize, group_len: usize| {
|
|
|
|
// retrieve the first distance group (with the lowest values)
|
|
|
|
let len = distance[index..index + group_len].linear_group().next().unwrap().len();
|
2019-02-02 21:22:31 +08:00
|
|
|
|
|
|
|
let rattr = &attribute[index..index + len];
|
|
|
|
let rwi = &word_index[index..index + len];
|
|
|
|
|
|
|
|
(rattr, rwi)
|
2019-02-12 22:42:17 +08:00
|
|
|
};
|
2019-02-02 21:22:31 +08:00
|
|
|
|
2019-02-12 22:42:17 +08:00
|
|
|
let mut last = query_index_groups.next().map(|group| {
|
|
|
|
let attr_wi = get_attr_wi(index, group.len());
|
|
|
|
index += group.len();
|
|
|
|
attr_wi
|
|
|
|
});
|
2019-02-02 21:22:31 +08:00
|
|
|
|
2019-02-12 22:42:17 +08:00
|
|
|
// iter by windows of size 2
|
|
|
|
while let (Some(lhs), Some(rhs)) = (last, query_index_groups.next()) {
|
|
|
|
let attr_wi = get_attr_wi(index, rhs.len());
|
|
|
|
proximity += min_proximity(lhs, attr_wi);
|
|
|
|
last = Some(attr_wi);
|
|
|
|
index += rhs.len();
|
2018-07-07 20:43:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
proximity
|
|
|
|
}
|
|
|
|
|
2018-10-11 20:04:41 +08:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub struct WordsProximity;
|
|
|
|
|
2019-02-02 21:22:31 +08:00
|
|
|
impl Criterion for WordsProximity {
|
|
|
|
fn evaluate(&self, lhs: &RawDocument, rhs: &RawDocument) -> Ordering {
|
|
|
|
let lhs = {
|
|
|
|
let query_index = lhs.query_index();
|
2019-02-12 22:42:17 +08:00
|
|
|
let distance = lhs.distance();
|
2019-02-02 21:22:31 +08:00
|
|
|
let attribute = lhs.attribute();
|
|
|
|
let word_index = lhs.word_index();
|
2019-02-12 22:42:17 +08:00
|
|
|
matches_proximity(query_index, distance, attribute, word_index)
|
2019-02-02 21:22:31 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
let rhs = {
|
|
|
|
let query_index = rhs.query_index();
|
2019-02-12 22:42:17 +08:00
|
|
|
let distance = rhs.distance();
|
2019-02-02 21:22:31 +08:00
|
|
|
let attribute = rhs.attribute();
|
|
|
|
let word_index = rhs.word_index();
|
2019-02-12 22:42:17 +08:00
|
|
|
matches_proximity(query_index, distance, attribute, word_index)
|
2019-02-02 21:22:31 +08:00
|
|
|
};
|
2018-10-11 20:04:41 +08:00
|
|
|
|
|
|
|
lhs.cmp(&rhs)
|
|
|
|
}
|
2018-07-07 20:43:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn three_different_attributes() {
|
|
|
|
|
|
|
|
// "soup" "of the" "the day"
|
|
|
|
//
|
|
|
|
// { id: 0, attr: 0, attr_index: 0 }
|
|
|
|
// { id: 1, attr: 1, attr_index: 0 }
|
|
|
|
// { id: 2, attr: 1, attr_index: 1 }
|
|
|
|
// { id: 2, attr: 2, attr_index: 0 }
|
|
|
|
// { id: 3, attr: 3, attr_index: 1 }
|
|
|
|
|
2019-02-02 21:22:31 +08:00
|
|
|
let query_index = &[0, 1, 2, 2, 3];
|
2019-02-12 22:42:17 +08:00
|
|
|
let distance = &[0, 0, 0, 0, 0];
|
|
|
|
let attribute = &[0, 1, 1, 2, 3];
|
|
|
|
let word_index = &[0, 0, 1, 0, 1];
|
2018-07-07 20:43:29 +08:00
|
|
|
|
|
|
|
// soup -> of = 8
|
|
|
|
// + of -> the = 1
|
|
|
|
// + the -> day = 8 (not 1)
|
2019-02-12 22:42:17 +08:00
|
|
|
assert_eq!(matches_proximity(query_index, distance, attribute, word_index), 17);
|
2018-07-07 20:43:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn two_different_attributes() {
|
|
|
|
|
|
|
|
// "soup day" "soup of the day"
|
|
|
|
//
|
|
|
|
// { id: 0, attr: 0, attr_index: 0 }
|
|
|
|
// { id: 0, attr: 1, attr_index: 0 }
|
|
|
|
// { id: 1, attr: 1, attr_index: 1 }
|
|
|
|
// { id: 2, attr: 1, attr_index: 2 }
|
|
|
|
// { id: 3, attr: 0, attr_index: 1 }
|
|
|
|
// { id: 3, attr: 1, attr_index: 3 }
|
|
|
|
|
2019-02-02 21:22:31 +08:00
|
|
|
let query_index = &[0, 0, 1, 2, 3, 3];
|
2019-02-12 22:42:17 +08:00
|
|
|
let distance = &[0, 0, 0, 0, 0, 0];
|
|
|
|
let attribute = &[0, 1, 1, 1, 0, 1];
|
|
|
|
let word_index = &[0, 0, 1, 2, 1, 3];
|
2018-07-07 20:43:29 +08:00
|
|
|
|
|
|
|
// soup -> of = 1
|
|
|
|
// + of -> the = 1
|
|
|
|
// + the -> day = 1
|
2019-02-12 22:42:17 +08:00
|
|
|
assert_eq!(matches_proximity(query_index, distance, attribute, word_index), 3);
|
2018-07-07 20:43:29 +08:00
|
|
|
}
|
|
|
|
}
|