fix tests

This commit is contained in:
ad hoc 2022-02-02 18:45:11 +01:00
parent 7541ab99cd
commit 628c835a22
No known key found for this signature in database
GPG key ID: 4F00A782990CC643
5 changed files with 36 additions and 17 deletions

View file

@ -70,6 +70,7 @@ impl<'a> Search<'a> {
pub fn offset(&mut self, offset: usize) -> &mut Search<'a> {
self.offset = offset;
self
}
@ -301,23 +302,34 @@ pub fn word_derivations<'c>(
if max_typo == 1 {
let dfa = build_dfa(word, 1, is_prefix);
let starts = Str::new(get_first(word)).starts_with();
let mut stream = fst.search(starts.intersection(&dfa)).into_stream();
let mut stream = fst.search_with_state(starts.intersection(&dfa)).into_stream();
while let Some(word) = stream.next() {
while let Some((word, state)) = stream.next() {
let word = std::str::from_utf8(word)?;
derived_words.push((word.to_string(), 1));
let d = dfa.distance(state.1);
derived_words.push((word.to_string(), d.to_u8()));
}
} else {
let starts = Str::new(get_first(word)).starts_with();
let first = build_dfa(word, 1, is_prefix).intersection((&starts).complement());
let second = build_dfa(word, 2, is_prefix).intersection(&starts);
let automaton = first.union(second);
let second_dfa = build_dfa(word, 2, is_prefix);
let second = (&second_dfa).intersection(&starts);
let automaton = first.union(&second);
let mut stream = fst.search(automaton).into_stream();
let mut stream = fst.search_with_state(automaton).into_stream();
while let Some(word) = stream.next() {
let word = std::str::from_utf8(word)?;
derived_words.push((word.to_string(), 2));
while let Some((found_word, state)) = stream.next() {
let found_word = std::str::from_utf8(found_word)?;
// in the case the typo is on the first letter, we know the number of typo
// is two
if get_first(found_word) != get_first(word) {
derived_words.push((word.to_string(), 2));
} 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);
derived_words.push((word.to_string(), d.to_u8()));
}
}
}
}

View file

@ -365,7 +365,10 @@ fn create_query_tree(
.collect();
let mut operations = synonyms(ctx, &words)?.unwrap_or_default();
let concat = words.concat();
let query = Query { prefix: is_prefix, kind: typos(concat, true, 1) };
let query = Query {
prefix: is_prefix,
kind: typos(concat, authorize_typos, 1),
};
operations.push(Operation::Query(query));
and_op_children.push(Operation::or(false, operations));
}
@ -657,7 +660,7 @@ mod test {
]),
Operation::Query(Query {
prefix: true,
kind: QueryKind::tolerant(2, "heyfriends".to_string()),
kind: QueryKind::tolerant(1, "heyfriends".to_string()),
}),
],
);
@ -690,7 +693,7 @@ mod test {
]),
Operation::Query(Query {
prefix: false,
kind: QueryKind::tolerant(2, "heyfriends".to_string()),
kind: QueryKind::tolerant(1, "heyfriends".to_string()),
}),
],
);
@ -755,7 +758,7 @@ mod test {
]),
Operation::Query(Query {
prefix: false,
kind: QueryKind::tolerant(2, "helloworld".to_string()),
kind: QueryKind::tolerant(1, "helloworld".to_string()),
}),
],
);
@ -853,7 +856,7 @@ mod test {
]),
Operation::Query(Query {
prefix: false,
kind: QueryKind::tolerant(2, "newyorkcity".to_string()),
kind: QueryKind::tolerant(1, "newyorkcity".to_string()),
}),
],
),
@ -927,7 +930,7 @@ mod test {
]),
Operation::Query(Query {
prefix: false,
kind: QueryKind::tolerant(2, "wordsplitfish".to_string()),
kind: QueryKind::tolerant(1, "wordsplitfish".to_string()),
}),
],
);
@ -1047,7 +1050,7 @@ mod test {
]),
Operation::Query(Query {
prefix: false,
kind: QueryKind::tolerant(2, "heymyfriend".to_string()),
kind: QueryKind::tolerant(1, "heymyfriend".to_string()),
}),
],
),