-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from anyproto/tolya/go-4549-refactor-tantivy--…
…create_stemmer_lazy_static refactoring: use lazy_static for lang maps
- Loading branch information
Showing
2 changed files
with
32 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,35 @@ | ||
use tantivy::tokenizer::{Language, Stemmer}; | ||
use crate::tantivy_util::TantivyGoError; | ||
use lazy_static::lazy_static; | ||
use phf::phf_map; | ||
use tantivy::tokenizer::{Language, Stemmer}; | ||
|
||
lazy_static! { | ||
pub static ref LANGUAGES: phf::Map<&'static str, Language> = phf_map! { | ||
"ar" => Language::Arabic, | ||
"da" => Language::Danish, | ||
"nl" => Language::Dutch, | ||
"en" => Language::English, | ||
"fi" => Language::Finnish, | ||
"fr" => Language::French, | ||
"de" => Language::German, | ||
"el" => Language::Greek, | ||
"hu" => Language::Hungarian, | ||
"it" => Language::Italian, | ||
"no" => Language::Norwegian, | ||
"pt" => Language::Portuguese, | ||
"ro" => Language::Romanian, | ||
"ru" => Language::Russian, | ||
"es" => Language::Spanish, | ||
"sv" => Language::Swedish, | ||
"ta" => Language::Tamil, | ||
"tr" => Language::Turkish, | ||
}; | ||
} | ||
|
||
pub fn create_stemmer(lang: &str) -> Result<Stemmer, TantivyGoError> { | ||
let stemmer_language = match lang { | ||
"ar" => Ok(Language::Arabic), | ||
"da" => Ok(Language::Danish), | ||
"nl" => Ok(Language::Dutch), | ||
"en" => Ok(Language::English), | ||
"fi" => Ok(Language::Finnish), | ||
"fr" => Ok(Language::French), | ||
"de" => Ok(Language::German), | ||
"el" => Ok(Language::Greek), | ||
"hu" => Ok(Language::Hungarian), | ||
"it" => Ok(Language::Italian), | ||
"no" => Ok(Language::Norwegian), | ||
"pt" => Ok(Language::Portuguese), | ||
"ro" => Ok(Language::Romanian), | ||
"ru" => Ok(Language::Russian), | ||
"es" => Ok(Language::Spanish), | ||
"sv" => Ok(Language::Swedish), | ||
"ta" => Ok(Language::Tamil), | ||
"tr" => Ok(Language::Turkish), | ||
_ => Err(TantivyGoError(format!("{} is an unsupported language", lang))), | ||
}?; | ||
let stemmer_language = LANGUAGES | ||
.get(lang) | ||
.ok_or_else(|| TantivyGoError(format!("{lang} is an unsupported language")))?; | ||
|
||
Ok(Stemmer::new(stemmer_language)) | ||
Ok(Stemmer::new(stemmer_language.to_owned())) | ||
} |