rust_persian_tools/half_space/
mod.rs

1/// removes half space & soft hyphon from text
2/// Example:
3/// ```
4/// use rust_persian_tools::half_space::remove_half_space;
5/// assert_eq!(
6///     remove_half_space("نمی‌خواهی درخت‌ها را ببینیم؟"),
7///     "نمی خواهی درخت ها را ببینیم؟".to_string()
8/// );
9/// ```
10pub fn remove_half_space(input: impl AsRef<str>) -> String {
11    let input = input.as_ref();
12
13    input
14        .replace('\u{00AD}', "")
15        .chars()
16        .map(|ch| if ch == '\u{200C}' { ' ' } else { ch })
17        .collect()
18}
19pub trait RemoveHalfSpace {
20    fn remove_half_space(&self) -> String;
21}
22
23impl RemoveHalfSpace for String {
24    fn remove_half_space(&self) -> String {
25        remove_half_space(self)
26    }
27}
28impl RemoveHalfSpace for str {
29    fn remove_half_space(&self) -> String {
30        remove_half_space(self)
31    }
32}
33use std::borrow::Cow;
34impl RemoveHalfSpace for Cow<'_, str> {
35    fn remove_half_space(&self) -> String {
36        remove_half_space(self)
37    }
38}
39
40/// add half space to input based on most useful
41/// Example:
42/// ```
43/// use rust_persian_tools::half_space::add_half_space;
44/// assert_eq!(
45///     add_half_space("نمی خواهی درخت ها را ببینیم؟"),
46///     "نمی‌خواهی درخت‌ها را ببینیم؟".to_string()
47/// );
48/// ```
49pub fn add_half_space(input: impl AsRef<str>) -> String {
50    let input = input.as_ref();
51    let result = remove_half_space(input.trim())
52        .replace("\u{0020}می\u{0020}", "\u{0020}می\u{200c}")
53        .replace("\u{0020}نمی\u{0020}", "\u{0020}نمی\u{200c}")
54        .replace("‌\u{0020}بی\u{0020}", "\u{0020}‌بی‌\u{200c}")
55        .replace("\u{0020}ام\u{0020}", "‌ام‌\u{200c}")
56        .replace("\u{0020}ات\u{0020}", "‌ات‌\u{200c}")
57        .replace("\u{0020}اش\u{0020}", "‌اش‌\u{200c}")
58        .replace("\u{0020}ای\u{0020}", "‌ای‌\u{200c}")
59        .replace("\u{0020}اید\u{0020}", "‌اید‌\u{200c}")
60        .replace("\u{0020}ایم\u{0020}", "‌ایم‌\u{200c}")
61        .replace("\u{0020}اند\u{0020}", "‌اند‌\u{200c}")
62        .replace("\u{0020}های\u{0020}", "‌های\u{0020}")
63        .replace("\u{0020}ها\u{0020}", "‌ها\u{0020}")
64        .replace("\u{0020}تر\u{0020}", "‌تر\u{0020}")
65        .replace("\u{0020}تری\u{0020}", "‌تری\u{0020}")
66        .replace("\u{0020}هایی\u{0020}", "‌هایی‌\u{200c}")
67        .replace("\u{0020}هایم\u{0020}", "‌هایم‌\u{200c}")
68        .replace("\u{0020}هایت\u{0020}", "‌هایت‌\u{200c}")
69        .replace("\u{0020}هایش\u{0020}", "‌هایش‌\u{200c}")
70        .replace("\u{0020}هایمان\u{0020}", "‌هایمان‌\u{200c}")
71        .replace("\u{0020}هایتان\u{0020}", "‌هایتان‌\u{200c}")
72        .replace("\u{0020}هایشان\u{0020}", "‌هایشان‌\u{200c}");
73
74    // these section fixes the words that are started with می | نمی |‌بی
75    if result.starts_with("می") {
76        if let Some((_, temp)) = result.split_once(' ') {
77            return format!("{}{}", "می\u{200c}", temp);
78        }
79    } else if result.starts_with("نمی") {
80        if let Some((_, temp)) = result.split_once(' ') {
81            return format!("{}{}", "نمی\u{200c}", temp);
82        }
83    } else if result.starts_with("بی") {
84        if let Some((_, temp)) = result.split_once(' ') {
85            return format!("{}{}", "‌بی‌\u{200c}", temp);
86        }
87    }
88
89    result
90}
91
92pub trait AddHalfSpace {
93    fn add_half_space(&self) -> String;
94}
95impl AddHalfSpace for String {
96    fn add_half_space(&self) -> String {
97        add_half_space(self)
98    }
99}
100impl AddHalfSpace for str {
101    fn add_half_space(&self) -> String {
102        add_half_space(self)
103    }
104}
105impl AddHalfSpace for Cow<'_, str> {
106    fn add_half_space(&self) -> String {
107        add_half_space(self)
108    }
109}
110
111#[cfg(test)]
112mod tests {
113    use super::*;
114
115    #[test]
116    fn test_adding_half_space() {
117        assert_eq!(
118            add_half_space("نمی خواهی درخت ها را ببینیم؟"),
119            "نمی‌خواهی درخت‌ها را ببینیم؟".to_string()
120        );
121
122        assert_eq!(
123            add_half_space("ای دوست سلام من به تو. نمی خواهمت درخت های چنار هاله صمیمی من"),
124            "ای دوست سلام من به تو. نمی‌خواهمت درخت‌های چنار هاله صمیمی من".to_string()
125        );
126        add_half_space("نمیخواهی"); // panic test
127    }
128
129    #[test]
130    fn test_removing_half_space() {
131        assert_eq!(
132            remove_half_space("نمی‌خواهی درخت‌ها را ببینیم؟"),
133            "نمی خواهی درخت ها را ببینیم؟".to_string()
134        );
135    }
136}