use super::VowelClassifier;
#[derive(Debug, Clone, Copy, Default)]
pub struct ArabicClassifier {
pub long_vowels_as_vowels: bool,
}
impl ArabicClassifier {
pub fn new() -> Self {
Self::default()
}
pub fn with_long_vowels() -> Self {
Self {
long_vowels_as_vowels: true,
}
}
}
static ARABIC_VOWELS: &[char] = &[
'\u{064E}', '\u{064F}', '\u{0650}', '\u{064B}', '\u{064C}', '\u{064D}', '\u{0670}', ];
impl VowelClassifier for ArabicClassifier {
fn is_vowel(&self, c: char) -> bool {
let code = c as u32;
match code {
0x064E => true, 0x064F => true, 0x0650 => true,
0x064B => true, 0x064C => true, 0x064D => true,
0x0670 => true,
0x0627 if self.long_vowels_as_vowels => true, 0x0648 if self.long_vowels_as_vowels => true, 0x064A if self.long_vowels_as_vowels => true, 0x0649 if self.long_vowels_as_vowels => true,
_ => false,
}
}
fn script_name(&self) -> &'static str {
"Arabic"
}
fn vowels(&self) -> &[char] {
ARABIC_VOWELS
}
fn is_consonant(&self, c: char) -> bool {
let code = c as u32;
match code {
0x0621 => true, 0x0622 => true, 0x0623 => true, 0x0624 => true, 0x0625 => true, 0x0626 => true, 0x0628 => true, 0x062A => true, 0x062B => true, 0x062C => true, 0x062D => true, 0x062E => true, 0x062F => true, 0x0630 => true, 0x0631 => true, 0x0632 => true, 0x0633 => true, 0x0634 => true, 0x0635 => true, 0x0636 => true, 0x0637 => true, 0x0638 => true, 0x0639 => true, 0x063A => true, 0x0641 => true, 0x0642 => true, 0x0643 => true, 0x0644 => true, 0x0645 => true, 0x0646 => true, 0x0647 => true, 0x0629 => true,
0x0627 if !self.long_vowels_as_vowels => true, 0x0648 if !self.long_vowels_as_vowels => true, 0x064A if !self.long_vowels_as_vowels => true,
0x067E => true, 0x0686 => true, 0x0698 => true, 0x06AF => true, 0x0679 => true, 0x0688 => true, 0x0691 => true, 0x06BA => true,
_ => false,
}
}
fn normalize(&self, input: &str) -> String {
use unicode_normalization::UnicodeNormalization;
input.nfd().collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_harakat_vowels() {
let c = ArabicClassifier::new();
assert!(c.is_vowel('\u{064E}')); assert!(c.is_vowel('\u{064F}')); assert!(c.is_vowel('\u{0650}')); }
#[test]
fn test_tanwin() {
let c = ArabicClassifier::new();
assert!(c.is_vowel('\u{064B}')); assert!(c.is_vowel('\u{064C}')); assert!(c.is_vowel('\u{064D}')); }
#[test]
fn test_consonant_letters() {
let c = ArabicClassifier::new();
assert!(!c.is_vowel('ب')); assert!(!c.is_vowel('ت')); assert!(!c.is_vowel('ث')); assert!(!c.is_vowel('ج')); assert!(!c.is_vowel('ح')); assert!(!c.is_vowel('خ')); }
#[test]
fn test_long_vowel_letters_default() {
let c = ArabicClassifier::new();
assert!(!c.is_vowel('ا')); assert!(!c.is_vowel('و')); assert!(!c.is_vowel('ي')); }
#[test]
fn test_long_vowel_letters_enabled() {
let c = ArabicClassifier::with_long_vowels();
assert!(c.is_vowel('ا')); assert!(c.is_vowel('و')); assert!(c.is_vowel('ي')); }
#[test]
fn test_is_consonant() {
let c = ArabicClassifier::new();
assert!(c.is_consonant('ب')); assert!(c.is_consonant('ك')); assert!(c.is_consonant('ل')); assert!(c.is_consonant('م')); assert!(c.is_consonant('ن')); }
#[test]
fn test_urdu_consonants() {
let c = ArabicClassifier::new();
assert!(c.is_consonant('پ')); assert!(c.is_consonant('چ')); assert!(c.is_consonant('ژ')); assert!(c.is_consonant('گ')); }
}