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
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#![warn(clippy::all)]
#![warn(missing_docs)]
#![warn(missing_doc_code_examples)]

//! # About
//! Stop words are words that don't carry much meaning, and are typically removed as a preprocessing step before text
//! analysis or natural language processing. This crate contains common stop words for a variety of languages. This crate uses stop word
//! lists from [this resource](https://github.com/Alir3z4/stop-words/tree/bd8cc1434faeb3449735ed570a4a392ab5d35291) and also from [NLTK](https://www.nltk.org/).
//!
//! This crate currently includes the following languages:
//! - Arabic
//! - Azerbaijani
//! - Bulgarian
//! - Catalan
//! - Czech
//! - Danish
//! - Dutch
//! - English
//! - Finnish
//! - French
//! - German
//! - Greek
//! - Hebrew
//! - Hindi
//! - Hungarian
//! - Indonesian
//! - Italian
//! - Kazakh
//! - Nepali
//! - Norwegian
//! - Polish
//! - Portuguese
//! - Romanian
//! - Russian
//! - Slovak
//! - Slovenian
//! - Spanish
//! - Swedish
//! - Tajik
//! - Turkish
//! - Ukrainian
//! - Vietnamese

// Strum contains all the trait definitions
#[cfg(feature = "enum")]
use {std::string::ToString, strum_macros};

/// Enum containing available language names, spelled out
#[cfg(feature = "enum")]
#[non_exhaustive]
#[derive(strum_macros::ToString, Debug, Copy, Clone, PartialEq, strum_macros::EnumString)]
pub enum LANGUAGE {
    /// Arabic
    Arabic,

    /// Azerbaijani
    Azerbaijani,

    ///
    Catalan,

    ///
    Danish,

    ///
    English,

    ///
    French,

    ///
    Hindi,

    ///
    Indonesian,

    ///
    Norwegian,

    ///
    Portuguese,

    ///
    Russian,

    ///
    Spanish,

    ///
    Turkish,

    ///
    Vietnamese,

    ///
    Bulgarian,

    ///
    Czech,

    ///
    Dutch,

    ///
    Finnish,

    ///
    German,

    ///
    Hungarian,

    ///
    Italian,

    ///
    Polish,

    ///
    Romanian,

    ///
    Slovak,

    ///
    Swedish,

    ///
    Ukrainian,

    ///
    Hebrew,

    ///
    Greek,

    ///
    Kazakh,

    ///
    Nepali,

    ///
    Slovenian,

    ///
    Tajik,
}

/// Constant containing an array of available language names, spelled out
pub const LANGUAGES: [&str; 32] = [
    "arabic",
    "azerbaijani",
    "catalan",
    "danish",
    "english",
    "french",
    "hindi",
    "indonesian",
    "norwegian",
    "portuguese",
    "russian",
    "spanish",
    "turkish",
    "vietnamese",
    "bulgarian",
    "czech",
    "dutch",
    "finnish",
    "german",
    "hungarian",
    "italian",
    "polish",
    "romanian",
    "slovak",
    "swedish",
    "ukrainian",
    "hebrew",
    "greek",
    "kazakh",
    "nepali",
    "slovenian",
    "tajik",
];

/// Constant containing an array of available language names, using ISO-693-1 codes
pub const LANGUAGES_ISO_693_1: [&str; 32] = [
    "ar", "az", "ca", "da", "en", "fr", "hi", "in", "nn", "pt", "ru", "es", "tr", "vi", "bg", "cs",
    "nl", "fi", "de", "hu", "it", "pl", "ro", "sk", "sv", "uk", "he", "el", "kk", "ne", "sl", "tg",
];

/// Constant containing an array of available language names, using ISO-693-2T codes
pub const LANGUAGES_ISO_693_2T: [&str; 32] = [
    "ara", "aze", "cat", "dan", "eng", "fra", "hin", "ind", "nno", "por", "rus", "spa", "tur",
    "vie", "bul", "ces", "nld", "fin", "deu", "hun", "ita", "pol", "ron", "slk", "swe", "ukr",
    "heb", "ell", "kaz", "nep", "slv", "tgk",
];

/// Let's define a macro to help us out with string matching
macro_rules! string_match {
    (
        $($language:expr)*,
        $(
            $directory:literal, [$( $lang:literal ),*]
        ),*
    ) =>
    {
        match $( $language )? {
            $(
                $(
                    $lang => read_from_bytes(include_bytes!(concat!($directory, "/", $lang))),
                )*
            )*
            _ => panic!(concat!("Unfortunately, the '{}' language is not currently supported. Please make sure that the name of the language is spelled in English."), $( $language )? )
        }
    }
}

/// The only function you'll ever need! Given a language code or name it returns common stop words as a ``Vec<String>``
///
/// ```
/// #[cfg(not(feature = "enum"))]
/// let vec = stop_words::get("spanish");
/// #[cfg(feature = "enum")]
/// let vec = stop_words::get(stop_words::LANGUAGE::Spanish);
/// ```
pub fn get(
    #[cfg(feature = "enum")] input_language: LANGUAGE,
    #[cfg(not(feature = "enum"))] input_language: &'static str,
) -> Vec<String> {
    string_match!(
        parse(input_language),
        "savand",
        [
            "english",
            "hebrew",
            "arabic",
            "catalan",
            "danish",
            "french",
            "hindi",
            "indonesian",
            "norwegian",
            "portuguese",
            "russian",
            "spanish",
            "turkish",
            "vietnamese",
            "bulgarian",
            "czech",
            "dutch",
            "finnish",
            "german",
            "hungarian",
            "italian",
            "polish",
            "romanian",
            "slovak",
            "swedish",
            "ukrainian"
        ],
        "nltk",
        [
            "azerbaijani",
            "greek",
            "kazakh",
            "nepali",
            "slovenian",
            "tajik"
        ]
    )
}

/// Ok, you might need this function too. It fetches stop words specifically for NLTK.
///
/// ```
/// #[cfg(not(feature = "enum"))]
/// let vec = stop_words::get_nltk("spanish");
/// #[cfg(feature = "enum")]
/// let vec = stop_words::get_nltk(stop_words::LANGUAGE::Spanish);
/// ```
pub fn get_nltk(
    #[cfg(feature = "enum")] input_language: LANGUAGE,
    #[cfg(not(feature = "enum"))] input_language: &'static str,
) -> Vec<String> {
    // Match the full language name
    string_match!(
        parse(input_language),
        "nltk",
        [
            "english",
            "arabic",
            "danish",
            "french",
            "indonesian",
            "norwegian",
            "portuguese",
            "russian",
            "spanish",
            "turkish",
            "greek",
            "dutch",
            "finnish",
            "german",
            "hungarian",
            "italian",
            "romanian",
            "swedish",
            "azerbaijani",
            "kazakh",
            "nepali",
            "slovenian",
            "tajik"
        ]
    )
}

/// This is a helper function to resolve inputs when using different features
fn parse(
    #[cfg(feature = "enum")] input_language: LANGUAGE,
    #[cfg(not(feature = "enum"))] input_language: &'static str,
) -> &'static str {
    #[cfg(feature = "enum")]
    let target_string: &str = Box::leak(input_language.to_string().to_lowercase().into_boxed_str());
    #[cfg(not(feature = "enum"))]
    let target_string: &str = get_language_from_code(input_language);
    return target_string;
}

/// This function takes an arbitrary code and converts it as needed to a full language name
#[cfg(not(feature = "enum"))]
fn get_language_from_code(code: &str) -> &str {
    if code.len() == 2 {
        get_language_from_iso(code, LANGUAGES_ISO_693_1)
    } else if code.len() == 3 {
        get_language_from_iso(code, LANGUAGES_ISO_693_2T)
    } else {
        code
    }
}

/// This function converts a language code to a full language name
#[cfg(not(feature = "enum"))]
fn get_language_from_iso<'a>(code: &'a str, library: [&str; 32]) -> &'a str {
    let mut iter = library.iter();
    let idx = iter.position(|&x| x == code);
    match idx {
        Some(x) => LANGUAGES[x],
        None => panic!("It looks like you're trying to use an ISO 693 language code. Unfortunately, the {} language code is not currently supported.", code),
    }
}

/// This function converts the bytestring to a vector
fn read_from_bytes(bytes: &[u8]) -> Vec<String> {
    let contents = String::from_utf8_lossy(bytes);
    let split_contents = contents.split('\n');
    let mut output = vec![];
    for word in split_contents {
        output.push(String::from(word));
    }
    output
}