2019-10-18 19:05:28 +08:00
|
|
|
mod document_id;
|
|
|
|
mod exact;
|
2019-10-02 23:34:32 +08:00
|
|
|
mod number_of_words;
|
2019-10-18 19:05:28 +08:00
|
|
|
mod sort_by_attr;
|
|
|
|
mod sum_of_typos;
|
2019-10-02 23:34:32 +08:00
|
|
|
mod sum_of_words_attribute;
|
|
|
|
mod sum_of_words_position;
|
2019-10-18 19:05:28 +08:00
|
|
|
mod words_proximity;
|
2019-10-02 23:34:32 +08:00
|
|
|
|
|
|
|
use crate::RawDocument;
|
2019-10-18 19:05:28 +08:00
|
|
|
use std::cmp::Ordering;
|
2019-10-02 23:34:32 +08:00
|
|
|
|
|
|
|
pub use self::{
|
2019-10-18 19:05:28 +08:00
|
|
|
document_id::DocumentId, exact::Exact, number_of_words::NumberOfWords,
|
|
|
|
sort_by_attr::SortByAttr, sum_of_typos::SumOfTypos,
|
|
|
|
sum_of_words_attribute::SumOfWordsAttribute, sum_of_words_position::SumOfWordsPosition,
|
2019-10-02 23:34:32 +08:00
|
|
|
words_proximity::WordsProximity,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub trait Criterion: Send + Sync {
|
|
|
|
fn evaluate(&self, lhs: &RawDocument, rhs: &RawDocument) -> Ordering;
|
|
|
|
|
2019-10-09 22:15:31 +08:00
|
|
|
fn name(&self) -> &str;
|
2019-10-02 23:34:32 +08:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn eq(&self, lhs: &RawDocument, rhs: &RawDocument) -> bool {
|
|
|
|
self.evaluate(lhs, rhs) == Ordering::Equal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: Criterion + ?Sized + Send + Sync> Criterion for &'a T {
|
|
|
|
fn evaluate(&self, lhs: &RawDocument, rhs: &RawDocument) -> Ordering {
|
|
|
|
(**self).evaluate(lhs, rhs)
|
|
|
|
}
|
|
|
|
|
2019-10-09 22:15:31 +08:00
|
|
|
fn name(&self) -> &str {
|
2019-10-02 23:34:32 +08:00
|
|
|
(**self).name()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn eq(&self, lhs: &RawDocument, rhs: &RawDocument) -> bool {
|
|
|
|
(**self).eq(lhs, rhs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Criterion + ?Sized> Criterion for Box<T> {
|
|
|
|
fn evaluate(&self, lhs: &RawDocument, rhs: &RawDocument) -> Ordering {
|
|
|
|
(**self).evaluate(lhs, rhs)
|
|
|
|
}
|
|
|
|
|
2019-10-09 22:15:31 +08:00
|
|
|
fn name(&self) -> &str {
|
2019-10-02 23:34:32 +08:00
|
|
|
(**self).name()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn eq(&self, lhs: &RawDocument, rhs: &RawDocument) -> bool {
|
|
|
|
(**self).eq(lhs, rhs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct CriteriaBuilder<'a> {
|
2019-10-18 19:05:28 +08:00
|
|
|
inner: Vec<Box<dyn Criterion + 'a>>,
|
2019-10-02 23:34:32 +08:00
|
|
|
}
|
|
|
|
|
2019-10-18 19:05:28 +08:00
|
|
|
impl<'a> CriteriaBuilder<'a> {
|
2019-10-02 23:34:32 +08:00
|
|
|
pub fn new() -> CriteriaBuilder<'a> {
|
|
|
|
CriteriaBuilder { inner: Vec::new() }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_capacity(capacity: usize) -> CriteriaBuilder<'a> {
|
2019-10-18 19:05:28 +08:00
|
|
|
CriteriaBuilder {
|
|
|
|
inner: Vec::with_capacity(capacity),
|
|
|
|
}
|
2019-10-02 23:34:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn reserve(&mut self, additional: usize) {
|
|
|
|
self.inner.reserve(additional)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add<C: 'a>(mut self, criterion: C) -> CriteriaBuilder<'a>
|
2019-10-18 19:05:28 +08:00
|
|
|
where
|
|
|
|
C: Criterion,
|
2019-10-02 23:34:32 +08:00
|
|
|
{
|
|
|
|
self.push(criterion);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn push<C: 'a>(&mut self, criterion: C)
|
2019-10-18 19:05:28 +08:00
|
|
|
where
|
|
|
|
C: Criterion,
|
2019-10-02 23:34:32 +08:00
|
|
|
{
|
|
|
|
self.inner.push(Box::new(criterion));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn build(self) -> Criteria<'a> {
|
|
|
|
Criteria { inner: self.inner }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Criteria<'a> {
|
|
|
|
inner: Vec<Box<dyn Criterion + 'a>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Default for Criteria<'a> {
|
|
|
|
fn default() -> Self {
|
|
|
|
CriteriaBuilder::with_capacity(7)
|
|
|
|
.add(SumOfTypos)
|
|
|
|
.add(NumberOfWords)
|
|
|
|
.add(WordsProximity)
|
|
|
|
.add(SumOfWordsAttribute)
|
|
|
|
.add(SumOfWordsPosition)
|
|
|
|
.add(Exact)
|
|
|
|
.add(DocumentId)
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> AsRef<[Box<dyn Criterion + 'a>]> for Criteria<'a> {
|
|
|
|
fn as_ref(&self) -> &[Box<dyn Criterion + 'a>] {
|
|
|
|
&self.inner
|
|
|
|
}
|
|
|
|
}
|