1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use crate::{
TokenKind,
linting::{Suggestion, SuggestionCollectionExt},
};
#[cfg(feature = "thesaurus")]
use crate::spell::{Dictionary, FstDictionary};
/// Gets synonyms for a provided word.
///
/// If the `thesaurus` feature is not enabled, will always return [`None`].
#[allow(unreachable_code)]
pub fn get_synonyms(_word: &str) -> Option<Vec<&str>> {
#[cfg(feature = "thesaurus")]
{
return harper_thesaurus::thesaurus().get_synonyms(_word);
}
None
}
/// Gets synonyms for a provided word, sorted by the following means:
/// - The level of difference between the provided token and that of the synonym.
/// - How often the synonym is used.
///
/// If the `thesaurus` feature is not enabled, will always return [`None`].
#[allow(unreachable_code)]
pub fn get_synonyms_sorted(_word: &str, _token: &TokenKind) -> Option<Vec<&'static str>> {
#[cfg(feature = "thesaurus")]
{
// Sorting by frequency.
let mut syns = harper_thesaurus::thesaurus().get_synonyms_freq_sorted(_word)?;
// Sorting by TokenKind difference.
if let Some(Some(word_meta)) = _token.as_word() {
let dict = FstDictionary::curated();
syns.sort_by_key(|syn| {
if let Some(syn_meta) = dict.get_word_metadata_str(syn) {
word_meta.difference(&syn_meta)
} else {
u32::MAX
}
});
}
return Some(syns);
}
None
}
/// Helper method to provide synonym replacement suggestions for the provided word.
///
/// The output is sorted as in [`get_synonyms_sorted()`], which attempts to place more relevant
/// results first.
///
/// If the `thesaurus` feature isn't enabled or the word cannot be found in the thesaurus, will
/// return an empty iterator.
pub fn get_synonym_replacement_suggestions(
word: &str,
token: &TokenKind,
) -> impl Iterator<Item = Suggestion> {
get_synonyms_sorted(word, token)
.unwrap_or_default()
.to_replace_suggestions(word.chars())
}