2019-12-11 17:02:10 +01:00
|
|
|
use std::cmp::Ordering;
|
2019-12-13 11:14:12 +01:00
|
|
|
use crate::{RawDocument, MResult};
|
2019-12-12 11:33:39 +01:00
|
|
|
use super::{Criterion, Context, ContextMut, prepare_query_distances};
|
2019-12-11 17:02:10 +01:00
|
|
|
|
|
|
|
pub struct Words;
|
|
|
|
|
|
|
|
impl Criterion for Words {
|
|
|
|
fn name(&self) -> &str { "words" }
|
|
|
|
|
2020-01-13 14:36:06 +01:00
|
|
|
fn prepare<'h, 'p, 'tag, 'txn, 'q, 'r>(
|
2019-12-11 17:02:10 +01:00
|
|
|
&self,
|
2020-01-13 14:36:06 +01:00
|
|
|
ctx: ContextMut<'h, 'p, 'tag, 'txn, 'q>,
|
2019-12-12 11:33:39 +01:00
|
|
|
documents: &mut [RawDocument<'r, 'tag>],
|
2019-12-13 11:14:12 +01:00
|
|
|
) -> MResult<()>
|
|
|
|
{
|
2020-01-13 14:36:06 +01:00
|
|
|
prepare_query_distances(documents, ctx.query_mapping, ctx.postings_lists);
|
2019-12-13 11:14:12 +01:00
|
|
|
Ok(())
|
2019-12-11 17:02:10 +01:00
|
|
|
}
|
|
|
|
|
2019-12-12 11:33:39 +01:00
|
|
|
fn evaluate(&self, _ctx: &Context, lhs: &RawDocument, rhs: &RawDocument) -> Ordering {
|
2019-12-11 17:02:10 +01:00
|
|
|
#[inline]
|
|
|
|
fn number_of_query_words(distances: &[Option<u8>]) -> usize {
|
|
|
|
distances.iter().cloned().filter(Option::is_some).count()
|
|
|
|
}
|
|
|
|
|
|
|
|
let lhs = number_of_query_words(&lhs.processed_distances);
|
|
|
|
let rhs = number_of_query_words(&rhs.processed_distances);
|
|
|
|
|
|
|
|
lhs.cmp(&rhs).reverse()
|
|
|
|
}
|
|
|
|
}
|