Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Arabic #40

Merged
merged 2 commits into from
May 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ default = ["languages"]
nightly = ["bench"]
bench = []

languages = ["da", "no", "de", "du", "es", "fi", "fr", "it", "pt", "ro", "ru", "sv", "tr", "zh", "ja"]
languages = ["da", "no", "de", "du", "es", "fi", "fr", "it", "pt", "ro", "ru", "sv", "tr", "zh", "ja", "ar"]
da = ["rust-stemmers"]
no = ["rust-stemmers"]
de = ["rust-stemmers"]
Expand All @@ -53,3 +53,4 @@ sv = ["rust-stemmers"]
tr = ["rust-stemmers"]
zh = ["jieba-rs"]
ja = ["lindera", "lindera-core"]
ar = []
27 changes: 27 additions & 0 deletions src/lang/ar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/// Simple Arabic stemmer based on lunr.ar.js from https://github.com/MihaiValentin/lunr-languages

use pipeline::Pipeline;
use regex::Regex;

pub fn make_pipeline() -> Pipeline {
Pipeline {
queue: vec![
("stemmer-ar".into(), stemmer),
],
}
}

fn stemmer(token: String) -> Option<String> {
lazy_static! {
static ref DIACRITICS: Regex = Regex::new("[\u{064b}-\u{065b}]").unwrap();
static ref ALEFS: Regex = Regex::new("[\u{0622}\u{0623}\u{0625}\u{0671}\u{0649}]").unwrap();
}
// remove elongating character
let token = token.replace('\u{0640}', "");
// remove diacritics
let token = DIACRITICS.replace(&token, "");
// replace all variations of alef (آأإٱى) to a plain alef (ا)
let token = ALEFS.replace(&token, "\u{0627}");

Some(token.into())
}
11 changes: 10 additions & 1 deletion src/lang/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ pub enum Language {
Chinese,
#[cfg(feature = "ja")]
Japanese,
#[cfg(feature = "ar")]
Arabic,
#[doc(hidden)]
#[strum(disabled)]
__NonExhaustive,
Expand Down Expand Up @@ -137,6 +139,8 @@ impl Language {
"zh" => Some(Language::Chinese),
#[cfg(feature = "ja")]
"ja" => Some(Language::Japanese),
#[cfg(feature = "ar")]
"ar" => Some(Language::Arabic),
_ => None,
}
}
Expand Down Expand Up @@ -182,6 +186,8 @@ impl Language {
Language::Chinese => "zh",
#[cfg(feature = "ja")]
Language::Japanese => "ja",
#[cfg(feature = "ar")]
Language::Arabic => "ar",
_ => panic!("Don't use the __NonExhaustive variant!"),
}
}
Expand Down Expand Up @@ -220,6 +226,8 @@ impl Language {
Language::Chinese => ::lang::zh::make_pipeline(),
#[cfg(feature = "ja")]
Language::Japanese => ::lang::ja::make_pipeline(),
#[cfg(feature = "ar")]
Language::Arabic => ::lang::ar::make_pipeline(),
_ => panic!("Dont use the `__NonExhaustive` variant!"),
}
}
Expand Down Expand Up @@ -257,4 +265,5 @@ pub mod sv;
pub mod tr;
#[cfg(feature = "zh")]
pub mod zh;

#[cfg(feature = "ar")]
pub mod ar;
7 changes: 7 additions & 0 deletions tests/data/ar.in.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
استعار جحا مرة آنية من جاره وعندما أعادها له أعاد معها آنية صغيرة
فسأله جاره لماذا أعدت مع أنيتي آنية صغيرة يا جحا؟
فقال له جحا: إنّ آنيتك ولدت في الأمس آنية صغيرة وإنّها الآن من حقك، فرح الرجل وأخذ الطنجرة ودخل بيته،
وبعد فترة من الزمان ذهب جحا إلى جاره وطلب منه أنية أخرى، فأعطاه جاره ما طلب، مرّ وقت طويل ولم يُعد جحا الآنية،
فذهب جاره إلى بيته ليطلبها منه، فاستقبله جحا باكياً منتحباً،
فقال له الرجل: مالي أراك باكياً يا جحا؟!! فقال له جحا وهو يبكي إنّ آنيتك توفيت بالأمس يا صاحبي،
فقال له جاره وهو غاضب: وكيف لآنيةٍ أن تموت يا رجل؟!! فقال جحا أتصدق أنّ إناء قد يلد ولا تصدق أنّه قد يموت؟!
118 changes: 118 additions & 0 deletions tests/data/ar.out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
استعار
جحا
مرة
انية
من
جاره
وعندما
اعادها
له
اعاد
معها
انية
صغيرة
فساله
جاره
لماذا
اعدت
مع
انيتي
انية
صغيرة
يا
جحا؟
فقال
له
جحا:
ان
انيتك
ولدت
في
الامس
انية
صغيرة
وانها
الان
من
حقك،
فرح
الرجل
واخذ
الطنجرة
ودخل
بيته،
وبعد
فترة
من
الزمان
ذهب
جحا
الى
جاره
وطلب
منه
انية
اخرى،
فاعطاه
جاره
ما
طلب،
مر
وقت
طويل
ولم
يعد
جحا
الانية،
فذهب
جاره
الى
بيته
ليطلبها
منه،
فاستقبله
جحا
باكيا
منتحبا،
فقال
له
الرجل:
مالي
اراك
باكيا
يا
جحا؟!!
فقال
له
جحا
وهو
يبكي
ان
انيتك
توفيت
بالامس
يا
صاحبي،
فقال
له
جاره
وهو
غاضب:
وكيف
لانية
ان
تموت
يا
رجل؟!!
فقال
جحا
اتصدق
ان
اناء
قد
يلد
ولا
تصدق
انه
قد
يموت؟!