2023-02-21 09:42:54 +01:00
|
|
|
use std::mem;
|
|
|
|
use std::ops::RangeInclusive;
|
|
|
|
|
|
|
|
use charabia::normalizer::NormalizedTokenIter;
|
|
|
|
use charabia::{SeparatorKind, TokenKind};
|
|
|
|
use fst::automaton::Str;
|
|
|
|
use fst::{Automaton, IntoStreamer, Streamer};
|
|
|
|
use heed::types::DecodeIgnore;
|
|
|
|
use heed::RoTxn;
|
2023-03-02 21:27:57 +01:00
|
|
|
use itertools::Itertools;
|
2023-02-21 09:42:54 +01:00
|
|
|
|
2023-03-08 09:55:53 +01:00
|
|
|
use super::interner::{Interned, Interner};
|
|
|
|
use super::SearchContext;
|
2023-02-21 09:42:54 +01:00
|
|
|
use crate::search::fst_utils::{Complement, Intersection, StartsWith, Union};
|
|
|
|
use crate::search::{build_dfa, get_first};
|
2023-03-02 21:27:57 +01:00
|
|
|
use crate::{CboRoaringBitmapLenCodec, Index, Result};
|
|
|
|
|
2023-03-08 15:04:25 +01:00
|
|
|
/// A phrase in the user's search query, consisting of several words
|
|
|
|
/// that must appear side-by-side in the search results.
|
2023-03-06 19:21:55 +01:00
|
|
|
#[derive(Default, Clone, PartialEq, Eq, Hash)]
|
2023-03-02 21:27:57 +01:00
|
|
|
pub struct Phrase {
|
2023-03-06 19:21:55 +01:00
|
|
|
pub words: Vec<Option<Interned<String>>>,
|
2023-03-02 21:27:57 +01:00
|
|
|
}
|
|
|
|
impl Phrase {
|
2023-03-06 19:21:55 +01:00
|
|
|
pub fn description(&self, interner: &Interner<String>) -> String {
|
|
|
|
self.words.iter().flatten().map(|w| interner.get(*w)).join(" ")
|
2023-03-02 21:27:57 +01:00
|
|
|
}
|
|
|
|
}
|
2023-02-21 09:42:54 +01:00
|
|
|
|
2023-03-08 15:04:25 +01:00
|
|
|
/// A structure storing all the different ways to match
|
|
|
|
/// a term in the user's search query.
|
2023-03-09 11:12:31 +01:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
2023-02-21 09:42:54 +01:00
|
|
|
pub struct WordDerivations {
|
2023-03-08 15:04:25 +01:00
|
|
|
/// The original word
|
2023-03-06 19:21:55 +01:00
|
|
|
pub original: Interned<String>,
|
2023-03-08 15:04:25 +01:00
|
|
|
// TODO: original should only be used for debugging purposes?
|
|
|
|
// TODO: pub zero_typo: Option<Interned<String>>,
|
|
|
|
// TODO: pub prefix_of: Box<[Interned<String>]>,
|
|
|
|
/// All the synonyms of the original word
|
2023-03-06 19:21:55 +01:00
|
|
|
pub synonyms: Box<[Interned<Phrase>]>,
|
2023-03-08 15:04:25 +01:00
|
|
|
|
|
|
|
/// The original word split into multiple consecutive words
|
2023-03-06 19:21:55 +01:00
|
|
|
pub split_words: Option<Interned<Phrase>>,
|
2023-03-08 15:04:25 +01:00
|
|
|
|
|
|
|
/// The original words and words which are prefixed by it
|
2023-03-06 19:21:55 +01:00
|
|
|
pub zero_typo: Box<[Interned<String>]>,
|
2023-03-08 15:04:25 +01:00
|
|
|
|
|
|
|
/// Words that are 1 typo away from the original word
|
2023-03-06 19:21:55 +01:00
|
|
|
pub one_typo: Box<[Interned<String>]>,
|
2023-03-08 15:04:25 +01:00
|
|
|
|
|
|
|
/// Words that are 2 typos away from the original word
|
2023-03-06 19:21:55 +01:00
|
|
|
pub two_typos: Box<[Interned<String>]>,
|
2023-03-08 15:04:25 +01:00
|
|
|
|
|
|
|
/// True if the prefix databases must be used to retrieve
|
|
|
|
/// the words which are prefixed by the original word.
|
2023-02-21 09:42:54 +01:00
|
|
|
pub use_prefix_db: bool,
|
|
|
|
}
|
|
|
|
impl WordDerivations {
|
2023-03-08 15:04:25 +01:00
|
|
|
/// Return an iterator over all the single words derived from the original word.
|
|
|
|
///
|
|
|
|
/// This excludes synonyms, split words, and words stored in the prefix databases.
|
2023-03-09 11:12:31 +01:00
|
|
|
pub fn all_single_word_derivations_except_prefix_db(
|
2023-03-06 19:21:55 +01:00
|
|
|
&'_ self,
|
|
|
|
) -> impl Iterator<Item = Interned<String>> + Clone + '_ {
|
|
|
|
self.zero_typo.iter().chain(self.one_typo.iter()).chain(self.two_typos.iter()).copied()
|
2023-02-21 09:42:54 +01:00
|
|
|
}
|
2023-03-09 11:12:31 +01:00
|
|
|
pub fn is_empty(&self) -> bool {
|
2023-02-21 09:42:54 +01:00
|
|
|
self.zero_typo.is_empty()
|
|
|
|
&& self.one_typo.is_empty()
|
|
|
|
&& self.two_typos.is_empty()
|
2023-03-08 15:04:25 +01:00
|
|
|
&& self.synonyms.is_empty()
|
|
|
|
&& self.split_words.is_none()
|
2023-02-21 09:42:54 +01:00
|
|
|
&& !self.use_prefix_db
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 15:04:25 +01:00
|
|
|
/// Compute the word derivations for the given word
|
2023-02-21 09:42:54 +01:00
|
|
|
pub fn word_derivations(
|
2023-03-06 19:21:55 +01:00
|
|
|
ctx: &mut SearchContext,
|
2023-02-21 09:42:54 +01:00
|
|
|
word: &str,
|
|
|
|
max_typo: u8,
|
|
|
|
is_prefix: bool,
|
2023-03-09 15:53:59 +01:00
|
|
|
) -> Result<Interned<WordDerivations>> {
|
2023-03-08 15:04:25 +01:00
|
|
|
let fst = ctx.index.words_fst(ctx.txn)?;
|
2023-03-06 19:21:55 +01:00
|
|
|
let word_interned = ctx.word_interner.insert(word.to_owned());
|
|
|
|
|
2023-02-21 09:42:54 +01:00
|
|
|
let use_prefix_db = is_prefix
|
2023-03-06 19:21:55 +01:00
|
|
|
&& ctx
|
|
|
|
.index
|
|
|
|
.word_prefix_docids
|
|
|
|
.remap_data_type::<DecodeIgnore>()
|
|
|
|
.get(ctx.txn, word)?
|
|
|
|
.is_some();
|
2023-02-21 09:42:54 +01:00
|
|
|
|
|
|
|
let mut zero_typo = vec![];
|
|
|
|
let mut one_typo = vec![];
|
|
|
|
let mut two_typos = vec![];
|
|
|
|
|
|
|
|
if max_typo == 0 {
|
2023-02-23 13:13:19 +01:00
|
|
|
if is_prefix && !use_prefix_db {
|
2023-02-21 09:42:54 +01:00
|
|
|
let prefix = Str::new(word).starts_with();
|
|
|
|
let mut stream = fst.search(prefix).into_stream();
|
|
|
|
|
2023-03-09 11:12:31 +01:00
|
|
|
while let Some(derived_word) = stream.next() {
|
|
|
|
let derived_word = std::str::from_utf8(derived_word)?.to_owned();
|
|
|
|
let derived_word_interned = ctx.word_interner.insert(derived_word);
|
|
|
|
zero_typo.push(derived_word_interned);
|
2023-02-21 09:42:54 +01:00
|
|
|
}
|
|
|
|
} else if fst.contains(word) {
|
2023-03-06 19:21:55 +01:00
|
|
|
zero_typo.push(word_interned);
|
2023-02-21 09:42:54 +01:00
|
|
|
}
|
|
|
|
} else if max_typo == 1 {
|
|
|
|
let dfa = build_dfa(word, 1, is_prefix);
|
|
|
|
let starts = StartsWith(Str::new(get_first(word)));
|
|
|
|
let mut stream = fst.search_with_state(Intersection(starts, &dfa)).into_stream();
|
2023-03-09 11:12:31 +01:00
|
|
|
// TODO: There may be wayyy too many matches (e.g. in the thousands), how to reduce them?
|
|
|
|
|
|
|
|
while let Some((derived_word, state)) = stream.next() {
|
|
|
|
let derived_word = std::str::from_utf8(derived_word)?;
|
2023-02-21 09:42:54 +01:00
|
|
|
|
|
|
|
let d = dfa.distance(state.1);
|
2023-03-09 11:12:31 +01:00
|
|
|
let derived_word_interned = ctx.word_interner.insert(derived_word.to_owned());
|
2023-02-21 09:42:54 +01:00
|
|
|
match d.to_u8() {
|
|
|
|
0 => {
|
2023-03-09 11:12:31 +01:00
|
|
|
zero_typo.push(derived_word_interned);
|
2023-02-21 09:42:54 +01:00
|
|
|
}
|
|
|
|
1 => {
|
2023-03-09 11:12:31 +01:00
|
|
|
one_typo.push(derived_word_interned);
|
2023-02-21 09:42:54 +01:00
|
|
|
}
|
|
|
|
_ => panic!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let starts = StartsWith(Str::new(get_first(word)));
|
|
|
|
let first = Intersection(build_dfa(word, 1, is_prefix), Complement(&starts));
|
|
|
|
let second_dfa = build_dfa(word, 2, is_prefix);
|
|
|
|
let second = Intersection(&second_dfa, &starts);
|
|
|
|
let automaton = Union(first, &second);
|
|
|
|
|
|
|
|
let mut stream = fst.search_with_state(automaton).into_stream();
|
2023-03-09 11:12:31 +01:00
|
|
|
// TODO: There may be wayyy too many matches (e.g. in the thousands), how to reduce them?
|
2023-02-21 09:42:54 +01:00
|
|
|
|
2023-03-09 11:12:31 +01:00
|
|
|
while let Some((derived_word, state)) = stream.next() {
|
|
|
|
let derived_word = std::str::from_utf8(derived_word)?;
|
|
|
|
let derived_word_interned = ctx.word_interner.insert(derived_word.to_owned());
|
2023-02-21 09:42:54 +01:00
|
|
|
// in the case the typo is on the first letter, we know the number of typo
|
|
|
|
// is two
|
2023-03-09 11:12:31 +01:00
|
|
|
if get_first(derived_word) != get_first(word) {
|
|
|
|
two_typos.push(derived_word_interned);
|
2023-02-21 09:42:54 +01:00
|
|
|
} else {
|
|
|
|
// Else, we know that it is the second dfa that matched and compute the
|
|
|
|
// correct distance
|
|
|
|
let d = second_dfa.distance((state.1).0);
|
|
|
|
match d.to_u8() {
|
|
|
|
0 => {
|
2023-03-09 11:12:31 +01:00
|
|
|
zero_typo.push(derived_word_interned);
|
2023-02-21 09:42:54 +01:00
|
|
|
}
|
|
|
|
1 => {
|
2023-03-09 11:12:31 +01:00
|
|
|
one_typo.push(derived_word_interned);
|
2023-02-21 09:42:54 +01:00
|
|
|
}
|
|
|
|
2 => {
|
2023-03-09 11:12:31 +01:00
|
|
|
two_typos.push(derived_word_interned);
|
2023-02-21 09:42:54 +01:00
|
|
|
}
|
|
|
|
_ => panic!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-06 19:21:55 +01:00
|
|
|
let split_words = split_best_frequency(ctx.index, ctx.txn, word)?.map(|(l, r)| {
|
|
|
|
ctx.phrase_interner.insert(Phrase {
|
|
|
|
words: vec![Some(ctx.word_interner.insert(l)), Some(ctx.word_interner.insert(r))],
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
let synonyms = ctx.index.synonyms(ctx.txn)?;
|
2023-03-02 21:27:57 +01:00
|
|
|
|
|
|
|
let synonyms = synonyms
|
|
|
|
.get(&vec![word.to_owned()])
|
|
|
|
.cloned()
|
|
|
|
.unwrap_or_default()
|
|
|
|
.into_iter()
|
2023-03-06 19:21:55 +01:00
|
|
|
.map(|words| {
|
|
|
|
let words = words.into_iter().map(|w| Some(ctx.word_interner.insert(w))).collect();
|
|
|
|
ctx.phrase_interner.insert(Phrase { words })
|
|
|
|
})
|
2023-03-02 21:27:57 +01:00
|
|
|
.collect();
|
|
|
|
|
2023-03-09 15:53:59 +01:00
|
|
|
let interned = ctx.derivations_interner.insert(WordDerivations {
|
2023-03-06 19:21:55 +01:00
|
|
|
original: ctx.word_interner.insert(word.to_owned()),
|
2023-03-02 21:27:57 +01:00
|
|
|
synonyms,
|
|
|
|
split_words,
|
2023-03-06 19:21:55 +01:00
|
|
|
zero_typo: zero_typo.into_boxed_slice(),
|
|
|
|
one_typo: one_typo.into_boxed_slice(),
|
|
|
|
two_typos: two_typos.into_boxed_slice(),
|
2023-03-02 21:27:57 +01:00
|
|
|
use_prefix_db,
|
2023-03-09 15:53:59 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
Ok(interned)
|
2023-03-02 21:27:57 +01:00
|
|
|
}
|
|
|
|
|
2023-03-08 15:04:25 +01:00
|
|
|
/// Split the original word into the two words that appear the
|
|
|
|
/// most next to each other in the index.
|
|
|
|
///
|
|
|
|
/// Return `None` if the original word cannot be split.
|
2023-03-02 21:27:57 +01:00
|
|
|
fn split_best_frequency(
|
|
|
|
index: &Index,
|
|
|
|
txn: &RoTxn,
|
|
|
|
original: &str,
|
|
|
|
) -> Result<Option<(String, String)>> {
|
|
|
|
let chars = original.char_indices().skip(1);
|
|
|
|
let mut best = None;
|
|
|
|
|
|
|
|
for (i, _) in chars {
|
|
|
|
let (left, right) = original.split_at(i);
|
|
|
|
|
|
|
|
let key = (1, left, right);
|
|
|
|
let frequency = index
|
|
|
|
.word_pair_proximity_docids
|
|
|
|
.remap_data_type::<CboRoaringBitmapLenCodec>()
|
|
|
|
.get(txn, &key)?
|
|
|
|
.unwrap_or(0);
|
|
|
|
|
|
|
|
if frequency != 0 && best.map_or(true, |(old, _, _)| frequency > old) {
|
|
|
|
best = Some((frequency, left, right));
|
|
|
|
}
|
|
|
|
}
|
2023-02-21 09:42:54 +01:00
|
|
|
|
2023-03-02 21:27:57 +01:00
|
|
|
Ok(best.map(|(_, left, right)| (left.to_owned(), right.to_owned())))
|
2023-02-21 09:42:54 +01:00
|
|
|
}
|
|
|
|
|
2023-03-09 11:12:31 +01:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash)]
|
2023-02-21 09:42:54 +01:00
|
|
|
pub enum QueryTerm {
|
2023-03-06 19:21:55 +01:00
|
|
|
Phrase { phrase: Interned<Phrase> },
|
2023-03-09 11:12:31 +01:00
|
|
|
// TODO: change to `Interned<WordDerivations>`?
|
|
|
|
Word { derivations: Interned<WordDerivations> },
|
2023-02-21 09:42:54 +01:00
|
|
|
}
|
2023-03-06 08:35:01 +01:00
|
|
|
|
2023-02-21 09:42:54 +01:00
|
|
|
impl QueryTerm {
|
2023-03-08 15:04:25 +01:00
|
|
|
/// Return the original word from the given query term
|
2023-03-06 19:21:55 +01:00
|
|
|
pub fn original_single_word<'interner>(
|
|
|
|
&self,
|
|
|
|
word_interner: &'interner Interner<String>,
|
2023-03-09 11:12:31 +01:00
|
|
|
derivations_interner: &'interner Interner<WordDerivations>,
|
2023-03-06 19:21:55 +01:00
|
|
|
) -> Option<&'interner str> {
|
2023-02-21 09:42:54 +01:00
|
|
|
match self {
|
2023-03-02 21:27:57 +01:00
|
|
|
QueryTerm::Phrase { phrase: _ } => None,
|
2023-02-21 09:42:54 +01:00
|
|
|
QueryTerm::Word { derivations } => {
|
2023-03-09 11:12:31 +01:00
|
|
|
let derivations = derivations_interner.get(*derivations);
|
2023-02-21 09:42:54 +01:00
|
|
|
if derivations.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
2023-03-06 19:21:55 +01:00
|
|
|
Some(word_interner.get(derivations.original))
|
2023-02-21 09:42:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 15:04:25 +01:00
|
|
|
/// A query term term coupled with its position in the user's search query.
|
2023-03-06 19:21:55 +01:00
|
|
|
#[derive(Clone)]
|
2023-02-21 09:42:54 +01:00
|
|
|
pub struct LocatedQueryTerm {
|
2023-03-02 21:27:57 +01:00
|
|
|
pub value: QueryTerm,
|
2023-02-21 09:42:54 +01:00
|
|
|
pub positions: RangeInclusive<i8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LocatedQueryTerm {
|
2023-03-08 15:04:25 +01:00
|
|
|
/// Return `true` iff the word derivations within the query term are empty
|
2023-03-09 11:12:31 +01:00
|
|
|
pub fn is_empty(&self, interner: &Interner<WordDerivations>) -> bool {
|
|
|
|
match self.value {
|
2023-03-08 15:04:25 +01:00
|
|
|
// TODO: phrases should be greedily computed, so that they can be excluded from
|
|
|
|
// the query graph right from the start?
|
2023-03-02 21:27:57 +01:00
|
|
|
QueryTerm::Phrase { phrase: _ } => false,
|
2023-03-09 11:12:31 +01:00
|
|
|
QueryTerm::Word { derivations, .. } => interner.get(derivations).is_empty(),
|
2023-02-21 09:42:54 +01:00
|
|
|
}
|
|
|
|
}
|
2023-03-06 08:35:01 +01:00
|
|
|
}
|
2023-02-21 09:42:54 +01:00
|
|
|
|
2023-03-08 15:04:25 +01:00
|
|
|
/// Convert the tokenised search query into a list of located query terms.
|
2023-03-13 14:03:48 +01:00
|
|
|
pub fn located_query_terms_from_string<'ctx>(
|
|
|
|
ctx: &mut SearchContext<'ctx>,
|
2023-03-06 08:35:01 +01:00
|
|
|
query: NormalizedTokenIter<Vec<u8>>,
|
|
|
|
words_limit: Option<usize>,
|
|
|
|
) -> Result<Vec<LocatedQueryTerm>> {
|
2023-03-06 19:21:55 +01:00
|
|
|
let authorize_typos = ctx.index.authorize_typos(ctx.txn)?;
|
|
|
|
let min_len_one_typo = ctx.index.min_word_len_one_typo(ctx.txn)?;
|
|
|
|
let min_len_two_typos = ctx.index.min_word_len_two_typos(ctx.txn)?;
|
2023-03-06 08:35:01 +01:00
|
|
|
|
2023-03-08 15:04:25 +01:00
|
|
|
// TODO: should `exact_words` also disable prefix search, ngrams, split words, or synonyms?
|
2023-03-06 19:21:55 +01:00
|
|
|
let exact_words = ctx.index.exact_words(ctx.txn)?;
|
2023-03-06 08:35:01 +01:00
|
|
|
|
|
|
|
let nbr_typos = |word: &str| {
|
|
|
|
if !authorize_typos
|
|
|
|
|| word.len() < min_len_one_typo as usize
|
|
|
|
|| exact_words.as_ref().map_or(false, |fst| fst.contains(word))
|
|
|
|
{
|
|
|
|
0
|
|
|
|
} else if word.len() < min_len_two_typos as usize {
|
|
|
|
1
|
|
|
|
} else {
|
|
|
|
2
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-03-08 15:04:25 +01:00
|
|
|
let mut located_terms = Vec::new();
|
2023-03-06 08:35:01 +01:00
|
|
|
|
2023-03-08 15:04:25 +01:00
|
|
|
let mut phrase = Vec::new();
|
2023-03-06 08:35:01 +01:00
|
|
|
let mut quoted = false;
|
|
|
|
|
|
|
|
let parts_limit = words_limit.unwrap_or(usize::MAX);
|
|
|
|
|
|
|
|
let mut position = -1i8;
|
|
|
|
let mut phrase_start = -1i8;
|
|
|
|
let mut phrase_end = -1i8;
|
|
|
|
|
|
|
|
let mut peekable = query.peekable();
|
|
|
|
while let Some(token) = peekable.next() {
|
|
|
|
// early return if word limit is exceeded
|
2023-03-08 15:04:25 +01:00
|
|
|
if located_terms.len() >= parts_limit {
|
|
|
|
return Ok(located_terms);
|
2023-03-06 08:35:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
match token.kind {
|
|
|
|
TokenKind::Word | TokenKind::StopWord => {
|
|
|
|
position += 1;
|
|
|
|
// 1. if the word is quoted we push it in a phrase-buffer waiting for the ending quote,
|
|
|
|
// 2. if the word is not the last token of the query and is not a stop_word we push it as a non-prefix word,
|
|
|
|
// 3. if the word is the last token of the query we push it as a prefix word.
|
|
|
|
if quoted {
|
|
|
|
phrase_end = position;
|
|
|
|
if phrase.is_empty() {
|
|
|
|
phrase_start = position;
|
|
|
|
}
|
|
|
|
if let TokenKind::StopWord = token.kind {
|
|
|
|
phrase.push(None);
|
|
|
|
} else {
|
2023-03-06 19:21:55 +01:00
|
|
|
let word = ctx.word_interner.insert(token.lemma().to_string());
|
2023-03-06 08:35:01 +01:00
|
|
|
// TODO: in a phrase, check that every word exists
|
|
|
|
// otherwise return WordDerivations::Empty
|
2023-03-06 19:21:55 +01:00
|
|
|
phrase.push(Some(word));
|
2023-03-06 08:35:01 +01:00
|
|
|
}
|
|
|
|
} else if peekable.peek().is_some() {
|
|
|
|
match token.kind {
|
|
|
|
TokenKind::Word => {
|
2023-03-06 19:21:55 +01:00
|
|
|
let word = token.lemma();
|
2023-03-08 15:04:25 +01:00
|
|
|
let derivations = word_derivations(ctx, word, nbr_typos(word), false)?;
|
2023-02-21 09:42:54 +01:00
|
|
|
let located_term = LocatedQueryTerm {
|
2023-03-09 15:53:59 +01:00
|
|
|
value: QueryTerm::Word { derivations },
|
2023-02-21 09:42:54 +01:00
|
|
|
positions: position..=position,
|
|
|
|
};
|
2023-03-08 15:04:25 +01:00
|
|
|
located_terms.push(located_term);
|
2023-02-21 09:42:54 +01:00
|
|
|
}
|
2023-03-06 08:35:01 +01:00
|
|
|
TokenKind::StopWord | TokenKind::Separator(_) | TokenKind::Unknown => {}
|
2023-02-21 09:42:54 +01:00
|
|
|
}
|
2023-03-06 08:35:01 +01:00
|
|
|
} else {
|
2023-03-06 19:21:55 +01:00
|
|
|
let word = token.lemma();
|
2023-03-08 15:04:25 +01:00
|
|
|
let derivations = word_derivations(ctx, word, nbr_typos(word), true)?;
|
2023-03-06 08:35:01 +01:00
|
|
|
let located_term = LocatedQueryTerm {
|
2023-03-09 15:53:59 +01:00
|
|
|
value: QueryTerm::Word { derivations },
|
2023-03-06 08:35:01 +01:00
|
|
|
positions: position..=position,
|
|
|
|
};
|
2023-03-08 15:04:25 +01:00
|
|
|
located_terms.push(located_term);
|
2023-02-21 09:42:54 +01:00
|
|
|
}
|
2023-03-06 08:35:01 +01:00
|
|
|
}
|
|
|
|
TokenKind::Separator(separator_kind) => {
|
|
|
|
match separator_kind {
|
|
|
|
SeparatorKind::Hard => {
|
|
|
|
position += 1;
|
2023-02-21 09:42:54 +01:00
|
|
|
}
|
2023-03-06 08:35:01 +01:00
|
|
|
SeparatorKind::Soft => {
|
|
|
|
position += 0;
|
2023-02-21 09:42:54 +01:00
|
|
|
}
|
|
|
|
}
|
2023-03-06 08:35:01 +01:00
|
|
|
let quote_count = token.lemma().chars().filter(|&s| s == '"').count();
|
|
|
|
// swap quoted state if we encounter a double quote
|
|
|
|
if quote_count % 2 != 0 {
|
|
|
|
quoted = !quoted;
|
|
|
|
}
|
|
|
|
// if there is a quote or a hard separator we close the phrase.
|
|
|
|
if !phrase.is_empty() && (quote_count > 0 || separator_kind == SeparatorKind::Hard)
|
|
|
|
{
|
|
|
|
let located_query_term = LocatedQueryTerm {
|
|
|
|
value: QueryTerm::Phrase {
|
2023-03-06 19:21:55 +01:00
|
|
|
phrase: ctx
|
|
|
|
.phrase_interner
|
|
|
|
.insert(Phrase { words: mem::take(&mut phrase) }),
|
2023-03-06 08:35:01 +01:00
|
|
|
},
|
|
|
|
positions: phrase_start..=phrase_end,
|
|
|
|
};
|
2023-03-08 15:04:25 +01:00
|
|
|
located_terms.push(located_query_term);
|
2023-03-06 08:35:01 +01:00
|
|
|
}
|
2023-02-21 09:42:54 +01:00
|
|
|
}
|
2023-03-06 08:35:01 +01:00
|
|
|
_ => (),
|
2023-02-21 09:42:54 +01:00
|
|
|
}
|
2023-03-06 08:35:01 +01:00
|
|
|
}
|
2023-02-21 09:42:54 +01:00
|
|
|
|
2023-03-06 08:35:01 +01:00
|
|
|
// If a quote is never closed, we consider all of the end of the query as a phrase.
|
|
|
|
if !phrase.is_empty() {
|
|
|
|
let located_query_term = LocatedQueryTerm {
|
2023-03-06 19:21:55 +01:00
|
|
|
value: QueryTerm::Phrase {
|
|
|
|
phrase: ctx.phrase_interner.insert(Phrase { words: mem::take(&mut phrase) }),
|
|
|
|
},
|
2023-03-06 08:35:01 +01:00
|
|
|
positions: phrase_start..=phrase_end,
|
|
|
|
};
|
2023-03-08 15:04:25 +01:00
|
|
|
located_terms.push(located_query_term);
|
2023-02-21 09:42:54 +01:00
|
|
|
}
|
2023-03-06 08:35:01 +01:00
|
|
|
|
2023-03-08 15:04:25 +01:00
|
|
|
Ok(located_terms)
|
2023-02-21 09:42:54 +01:00
|
|
|
}
|
|
|
|
|
2023-03-06 08:35:01 +01:00
|
|
|
// TODO: return a word derivations instead?
|
2023-03-06 19:21:55 +01:00
|
|
|
pub fn ngram2(
|
|
|
|
ctx: &mut SearchContext,
|
|
|
|
x: &LocatedQueryTerm,
|
|
|
|
y: &LocatedQueryTerm,
|
|
|
|
) -> Option<(Interned<String>, RangeInclusive<i8>)> {
|
2023-03-06 08:35:01 +01:00
|
|
|
if *x.positions.end() != y.positions.start() - 1 {
|
|
|
|
return None;
|
2023-02-21 09:42:54 +01:00
|
|
|
}
|
2023-03-06 19:21:55 +01:00
|
|
|
match (
|
2023-03-09 11:12:31 +01:00
|
|
|
&x.value.original_single_word(&ctx.word_interner, &ctx.derivations_interner),
|
|
|
|
&y.value.original_single_word(&ctx.word_interner, &ctx.derivations_interner),
|
2023-03-06 19:21:55 +01:00
|
|
|
) {
|
2023-03-06 08:35:01 +01:00
|
|
|
(Some(w1), Some(w2)) => {
|
2023-03-06 19:21:55 +01:00
|
|
|
let term = (
|
|
|
|
ctx.word_interner.insert(format!("{w1}{w2}")),
|
|
|
|
*x.positions.start()..=*y.positions.end(),
|
|
|
|
);
|
2023-03-06 08:35:01 +01:00
|
|
|
Some(term)
|
2023-02-21 09:42:54 +01:00
|
|
|
}
|
2023-03-06 08:35:01 +01:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2023-03-08 15:04:25 +01:00
|
|
|
|
|
|
|
// TODO: return a word derivations instead?
|
2023-03-06 08:35:01 +01:00
|
|
|
pub fn ngram3(
|
2023-03-06 19:21:55 +01:00
|
|
|
ctx: &mut SearchContext,
|
2023-03-06 08:35:01 +01:00
|
|
|
x: &LocatedQueryTerm,
|
|
|
|
y: &LocatedQueryTerm,
|
|
|
|
z: &LocatedQueryTerm,
|
2023-03-06 19:21:55 +01:00
|
|
|
) -> Option<(Interned<String>, RangeInclusive<i8>)> {
|
2023-03-06 08:35:01 +01:00
|
|
|
if *x.positions.end() != y.positions.start() - 1
|
|
|
|
|| *y.positions.end() != z.positions.start() - 1
|
|
|
|
{
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
match (
|
2023-03-09 11:12:31 +01:00
|
|
|
&x.value.original_single_word(&ctx.word_interner, &ctx.derivations_interner),
|
|
|
|
&y.value.original_single_word(&ctx.word_interner, &ctx.derivations_interner),
|
|
|
|
&z.value.original_single_word(&ctx.word_interner, &ctx.derivations_interner),
|
2023-03-06 08:35:01 +01:00
|
|
|
) {
|
|
|
|
(Some(w1), Some(w2), Some(w3)) => {
|
2023-03-06 19:21:55 +01:00
|
|
|
let term = (
|
|
|
|
ctx.word_interner.insert(format!("{w1}{w2}{w3}")),
|
|
|
|
*x.positions.start()..=*z.positions.end(),
|
|
|
|
);
|
2023-03-06 08:35:01 +01:00
|
|
|
Some(term)
|
2023-02-21 09:42:54 +01:00
|
|
|
}
|
2023-03-06 08:35:01 +01:00
|
|
|
_ => None,
|
2023-02-21 09:42:54 +01:00
|
|
|
}
|
|
|
|
}
|