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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/*  voikko-rs - libvoikko bindings for the Rust programming language
    Copyright (C) 2019 Ronja Koistinen

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

mod libvoikko;
mod tests;

/// This module provides Rust bindings for libvoikko.
///
/// Libvoikko provides spell checking, hyphenation, grammar checking and
/// morphological analysis for the Finnish language.
///
/// voikko-rs requires libvoikko (version 4.1.1 or greater)
/// to be installed on your system.
///
/// # Example
///
/// ```
/// extern crate voikko_rs; // in Rust 2015
/// use voikko_rs::voikko;
///
/// fn main() {
///     let v = voikko::Voikko::new("fi-x-morphoid", None).unwrap();
///     println!("{}", v.hyphenate("kunnallispolitiikka", "-").unwrap());
/// }
/// ```
///
/// The above prints:
///
/// `kun-nal-lis-po-li-tiik-ka`
pub mod voikko {

    use crate::libvoikko;
    use std::collections::HashMap;
    use std::error;
    use unicode_segmentation::UnicodeSegmentation;

    /// Returns the version number of libvoikko.
    pub fn version<'a>() -> &'a str {
        libvoikko::version()
    }

    /// Information about an available dictionary
    ///
    /// Contains the language, script, variant and human readable description
    /// of the dictionary.
    #[derive(Debug, PartialEq, Eq)]
    pub struct Dictionary {
        pub language: String,
        pub script: String,
        pub variant: String,
        pub description: String,
    }

    impl Dictionary {
        /// Construct new Dictionary struct.
        ///
        /// # Arguments
        ///
        /// * `language`
        /// * `script`
        /// * `variant`
        /// * `description`
        pub fn new(language: &str, script: &str, variant: &str, description: &str) -> Dictionary {
            Dictionary {
                language: String::from(language),
                script: String::from(script),
                variant: String::from(variant),
                description: String::from(description),
            }
        }
    }

    /// A morphological analysis item
    pub type Analysis = HashMap<String, String>;

    /// Get a list of available dictionaries. Returns a vector of Dictionary structs.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to a directory from which dictionary files should be searched
    ///            first before looking into the standard dictionary locations.
    ///            Pass an empty string in order to only look in standard locations.
    pub fn list_dicts(path: &str) -> Vec<Dictionary> {
        libvoikko::list_dicts(path).unwrap_or_else(|_| vec![])
    }

    /// Return a list of language codes representing the languages for which at least one
    /// dictionary is available for spell checking. The codes conform to those specified
    /// in BCP 47. Typically the returned codes consist of only BCP 47 language subtags.
    /// They may also include tags in format Language-Script, Language-Region, or
    /// Language-Script-Region if such variants are widely used for a particular language.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to a directory from which dictionary files should be searched
    ///            first before looking into the standard dictionary locations.
    ///            Pass an empty string in order to only look in standard locations.
    pub fn list_supported_spelling_languages(path: &str) -> Vec<String> {
        libvoikko::list_supported_spelling_languages(path).unwrap_or_else(|_| vec![])
    }

    /// Same as list_supported_spelling_languages() but for hyphenation.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to a directory from which dictionary files should be searched
    ///            first before looking into the standard dictionary locations.
    ///            Pass an empty string in order to only look in standard locations.
    pub fn list_supported_hyphenation_languages(path: &str) -> Vec<String> {
        libvoikko::list_supported_hyphenation_languages(path).unwrap_or_else(|_| vec![])
    }

    /// Same as list_supported_spelling_languages() but for grammar checking.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to a directory from which dictionary files should be searched
    ///            first before looking into the standard dictionary locations.
    ///            Pass an empty string in order to only look in standard locations.
    pub fn list_supported_grammar_checking_languages(path: &str) -> Vec<String> {
        libvoikko::list_supported_grammar_checking_languages(path).unwrap_or_else(|_| vec![])
    }

    /// A Voikko instance
    pub struct Voikko {
        handle: *mut libvoikko::VoikkoHandle,
    }

    /// A spell check return value
    #[derive(Debug, PartialEq, Eq)]
    pub enum SpellReturn {
        /// Incorrect spelling
        SpellFailed,
        /// Correct spelling
        SpellOk,
        InternalError,
        CharsetConversionFailed,
    }

    #[derive(Debug, PartialEq, Eq)]
    pub enum TokenType {
        None,
        Word,
        Punctuation,
        Whitespace,
        Unknown,
    }

    /// Tokenization unit
    #[derive(Debug, PartialEq, Eq)]
    pub struct Token {
        /// Text of the token
        pub token_text: String,
        /// Type of the token
        pub token_type: TokenType,
    }

    impl Token {
        pub fn new(token_text: &str, token_type: TokenType) -> Token {
            Token {
                token_text: String::from(token_text),
                token_type,
            }
        }
    }

    /// Type of a following sentence
    #[derive(Debug, PartialEq, Eq, Clone, Copy)]
    pub enum SentenceType {
        /// There is no next sentence
        None,
        NoStart,
        /// There is probably a sentence following this one
        Probable,
        /// There is possibly a sentence following this one
        Possible,
    }

    /// A sentence
    #[derive(Debug, PartialEq, Eq)]
    pub struct Sentence {
        /// Text of the sentence
        text: String,
        /// The type of the next sentence
        next_start_type: SentenceType,
    }

    impl Sentence {
        pub fn new(sentence_text: &str, sentence_type: SentenceType) -> Sentence {
            Sentence {
                text: String::from(sentence_text),
                next_start_type: sentence_type,
            }
        }
    }

    #[derive(Debug, PartialEq, Eq)]
    /// Grammar error
    pub struct GrammarError {
        /// Error code
        pub code: i32,
        /// Start position of the error in characters
        pub start_pos: usize,
        /// Length of the error in characters
        pub length: usize,
        /// A list of suggestions for correcting the grammar error
        pub suggestions: Vec<String>,
        /// A localized short description of the grammar error
        pub description: String,
    }

    #[derive(Debug)]
    pub struct InitError {
        message: String,
    }

    impl InitError {
        pub fn new(message: &str) -> InitError {
            InitError {
                message: String::from(message),
            }
        }
    }

    impl std::fmt::Display for InitError {
        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
            write!(f, "{}", self.message)
        }
    }

    impl error::Error for InitError {
        fn description(&self) -> &str {
            self.message.as_str()
        }
    }

    impl std::convert::From<std::ffi::NulError> for InitError {
        fn from(error: std::ffi::NulError) -> Self {
            InitError {
                message: format!("{}", error)
            }
        }
    }

    impl Voikko {
        /// Initializes Voikko and returns a Result<Voikko, InitError>
        ///
        /// # Arguments
        ///
        /// * `language` - BCP 47 language tag for the language to be used.
        ///                Private use subtags can be used to specify the dictionary variant.
        /// * `path` - Path to a directory from which dictionary files should be searched first before
        ///            looking into the standard dictionary locations. If `None`, no additional search path
        ///            will be used.
        ///
        /// # Errors
        ///
        /// Returns an InitError result if init fails.
        pub fn new(language: &str, path: Option<&str>) -> Result<Voikko, InitError> {
            let v = libvoikko::init(language, path);

            match v {
                Ok(handle) => Ok(Voikko { handle }),
                Err(error) => Err(error),
            }
        }

        /// Check the spelling of a UTF-8 character string.
        ///
        /// # Arguments
        ///
        /// * `word` - word to check
        pub fn spell(&self, word: &str) -> SpellReturn {
            let ret = libvoikko::spell(self.handle, word);
            match ret {
                Ok(code) => match code {
                    0 => SpellReturn::SpellFailed,
                    1 => SpellReturn::SpellOk,
                    3 => SpellReturn::CharsetConversionFailed,
                    _ => SpellReturn::InternalError,
                },
                Err(_) => SpellReturn::SpellFailed,
            }

        }

        /// Finds suggested correct spellings for given UTF-8 encoded word.
        /// Returns a vector of strings - an empty vector, if no suggestions.
        ///
        /// # Arguments
        ///
        /// * `word` - word to find suggestions for
        pub fn suggest(&self, word: &str) -> Vec<String> {
            libvoikko::suggest(self.handle, word).unwrap_or_else(|_| vec![])
        }

        /// Hyphenates the given word in UTF-8 encoding.
        /// Returns a string containing the hyphenation using the following notation:
        /// * `' '` = no hyphenation at this character,
        /// * `'-'` = hyphenation point (character at this position
        ///        is preserved in the hyphenated form),
        /// * `'='` = hyphenation point (character at this position
        ///        is replaced by the hyphen.)
        ///
        /// # Arguments
        ///
        /// * `word` - word to hyphenate
        ///
        /// # Errors
        ///
        /// Returns an error result on error.
        pub fn hyphens(&self, word: &str) -> Result<String, bool> {
            libvoikko::hyphens(self.handle, word)
        }

        /// Hyphenates the given word in UTF-8 encoding.
        /// Returns a string where hyphens are inserted in all hyphenation points.
        ///
        /// # Arguments
        ///
        /// * `word` - word to hyphenate
        /// * `hyphen` - string to insert at hyphenation points
        ///
        /// # Errors
        ///
        /// Returns an error result on error.
        pub fn hyphenate(&self, word: &str, hyphen: &str) -> Result<String, bool> {
            let hyphens = self.hyphens(word);
            match hyphens {
                Err(_) => Err(false),
                Ok(hyph) => Ok(word
                    .graphemes(true)
                    .zip(hyph.graphemes(true))
                    .map(|(w, h)| match h {
                        " " => String::from(w),
                        "-" => format!("{}{}", hyphen, w),
                        "=" => String::from(hyphen),
                        _ => String::from(w),
                    })
                    .collect::<String>()),
            }
        }

        /// Tokenize a text string. Returns a vector of Token structs.
        ///
        /// # Arguments
        ///
        /// * `text` - Text to find tokens in.
        pub fn tokens(&self, text: &str) -> Vec<Token> {
            let mut tokenlist = Vec::new();
            let mut offset = 0;
            while offset < text.len() {
                let (raw_token, token_len) = libvoikko::next_token(self.handle, &text[offset..]);
                let token_type = match raw_token {
                    libvoikko::voikko_token_type::TOKEN_NONE => TokenType::None,
                    libvoikko::voikko_token_type::TOKEN_PUNCTUATION => TokenType::Punctuation,
                    libvoikko::voikko_token_type::TOKEN_WHITESPACE => TokenType::Whitespace,
                    libvoikko::voikko_token_type::TOKEN_WORD => TokenType::Word,
                    _ => TokenType::Unknown,
                };
                if token_type == TokenType::None {
                    break;
                }
                let token_text: String = text[offset..].chars().take(token_len).collect();
                let token = Token::new(&token_text, token_type);
                tokenlist.push(token);
                offset += token_text.as_bytes().len();
            }
            tokenlist
        }

        /// Find sentences in a text string. Returns a vector of Sentence structs.
        ///
        /// # Arguments
        ///
        /// * `text` - Text to find sentences in.
        pub fn sentences(&self, text: &str) -> Vec<Sentence> {
            let mut sentlist = Vec::new();
            let mut offset = 0;
            let mut next_start_type = SentenceType::NoStart;
            while offset < text.chars().count() && next_start_type != SentenceType::None {
                // sent_len is in UTF-8 characters, not bytes
                let next_text = text.chars().skip(offset).collect::<String>();
                let (raw_sent, sent_len) =
                    libvoikko::next_sentence(self.handle, next_text.as_str());
                next_start_type = match raw_sent {
                    libvoikko::voikko_sentence_type::SENTENCE_NO_START => SentenceType::NoStart,
                    libvoikko::voikko_sentence_type::SENTENCE_POSSIBLE => SentenceType::Possible,
                    libvoikko::voikko_sentence_type::SENTENCE_PROBABLE => SentenceType::Probable,
                    _ => SentenceType::None,
                };
                // construct new Sentence object with text slice and sentence type
                let token = Sentence::new(
                    text.chars()
                        .skip(offset)
                        .take(sent_len)
                        .collect::<String>()
                        .as_str(),
                    next_start_type,
                );
                sentlist.push(token);
                offset += sent_len;
            }
            sentlist
        }

        /// Analyzes the morphology of given word.
        ///
        /// Returns a vector of Analysis structs (std::collections::HashMap) or an empty vector if
        /// analysis fails.
        ///
        /// # Arguments
        ///
        /// * `word` - word to analyze
        // https://github.com/voikko/corevoikko/blob/rel-libvoikko-4.1.1/libvoikko/doc/morphological-analysis.txt
        pub fn analyze(&self, word: &str) -> Vec<Analysis> {
            libvoikko::analyze_word(self.handle, word).unwrap_or_else(|_| vec![])
        }

        /// Find all grammar errors in given text.
        ///
        /// Returns a vector of GrammarError structs or an empty vector if no errors found.
        ///
        /// # Arguments
        ///
        /// * `text` - Text to find grammar errors in. The text should usually begin at the start of
        ///            a paragraph or sentence.
        /// * `desc_lang` - ISO language code for the language in which to recieve error descriptions.
        pub fn grammar_errors(&self, text: &str, desc_lang: &str) -> Vec<GrammarError> {
            libvoikko::get_grammar_errors(self.handle, text, desc_lang).unwrap_or_else(|_| vec![])
        }

        // Values of option constants documented in
        // https://github.com/voikko/corevoikko/blob/rel-libvoikko-4.1.1/libvoikko/src/voikko_defines.h

        // Boolean options

        /// Ignore dot at the end of the word (needed for use in some word processors).
        /// If this option is set and input word ends with a dot, spell checking and
        /// hyphenation functions try to analyze the word without the dot if no results
        /// can be obtained for the original form. Also with this option, string tokenizer
        /// will consider trailing dot of a word to be a part of that word.
        ///
        /// Default: false
        pub fn set_opt_ignore_dot(&self, value: bool) -> bool {
            libvoikko::set_bool_option(self.handle, 0, value)
        }

        /// (Spell checking only) Ignore words containing numbers
        ///
        /// Default: false
        pub fn set_opt_ignore_numbers(&self, value: bool) -> bool {
            libvoikko::set_bool_option(self.handle, 1, value)
        }

        /// Accept words that are written completely in uppercase letters without checking
        /// them at all.
        ///
        /// Default: false
        pub fn set_opt_ignore_uppercase(&self, value: bool) -> bool {
            libvoikko::set_bool_option(self.handle, 3, value)
        }

        /// Accept words even when the first letter is in uppercase (start of sentence etc.)
        ///
        /// Default: true
        pub fn set_opt_accept_first_uppercase(&self, value: bool) -> bool {
            libvoikko::set_bool_option(self.handle, 6, value)
        }

        /// Accept words even when all of the letters are in uppercase. Note that this is
        /// not the same as `set_opt_ignore_uppercase(true)`: with this option the word is still
        /// checked, only case differences are ignored.
        ///
        /// Default: true
        pub fn set_opt_accept_all_uppercase(&self, value: bool) -> bool {
            libvoikko::set_bool_option(self.handle, 7, value)
        }

        /// Do not insert hyphenation positions that are considered to be ugly but correct
        ///
        /// Default: false
        pub fn set_opt_no_ugly_hyphenation(&self, value: bool) -> bool {
            libvoikko::set_bool_option(self.handle, 4, value)
        }

        /// Use suggestions optimized for optical character recognition software.
        /// By default suggestions are optimized for typing errors.
        ///
        /// Default: false
        pub fn set_opt_ocr_suggestions(&self, value: bool) -> bool {
            libvoikko::set_bool_option(self.handle, 8, value)
        }

        /// (Spell checking only): Ignore non-words such as URLs and email addresses.
        ///
        /// Default: true
        pub fn set_opt_ignore_nonwords(&self, value: bool) -> bool {
            libvoikko::set_bool_option(self.handle, 10, value)
        }

        /// (Spell checking only): Allow some extra hyphens in words. This option relaxes
        /// hyphen checking rules to work around some unresolved issues in the underlying
        /// morphology, but it may cause some incorrect words to be accepted. The exact
        /// behavior (if any) of this option is not specified.
        ///
        /// Default: false */
        pub fn set_opt_accept_extra_hyphens(&self, value: bool) -> bool {
            libvoikko::set_bool_option(self.handle, 11, value)
        }

        /// (Spell checking only): Accept missing hyphens at the start and end of the word.
        /// Some application programs do not consider hyphens to be word characters. This
        /// is a reasonable assumption for many languages but not for Finnish. If the
        /// application cannot be fixed to use a proper tokenisation algorithm for Finnish,
        /// this option may be used to tell libvoikko to work around this defect.
        ///
        /// Default: false
        pub fn set_opt_accept_missing_hyphens(&self, value: bool) -> bool {
            libvoikko::set_bool_option(self.handle, 12, value)
        }

        /// (Grammar checking only): Accept incomplete sentences that could occur in
        /// titles or headings. Set this option to true if your application is not able
        /// to differentiate titles from normal text paragraphs, or if you know that
        /// you are checking title text.
        ///
        /// Default: false
        pub fn set_opt_accept_titles_in_gc(&self, value: bool) -> bool {
            libvoikko::set_bool_option(self.handle, 13, value)
        }

        /// (Grammar checking only): Accept incomplete sentences at the end of the
        /// paragraph. These may exist when text is still being written.
        ///
        /// Default: false
        pub fn set_opt_accept_unfinished_paragraphs_in_gc(&self, value: bool) -> bool {
            libvoikko::set_bool_option(self.handle, 14, value)
        }

        /// (Hyphenation only): Hyphenate unknown words.
        ///
        /// Default: true
        pub fn set_opt_hyphenate_unknown_words(&self, value: bool) -> bool {
            libvoikko::set_bool_option(self.handle, 15, value)
        }

        /// (Grammar checking only): Accept paragraphs if they would be valid within
        /// bulleted lists.
        ///
        /// Default: false
        pub fn set_opt_accept_bulleted_lists_in_gc(&self, value: bool) -> bool {
            libvoikko::set_bool_option(self.handle, 16, value)
        }

        // Integer options

        /// The minimum length for words that may be hyphenated. This limit is also enforced on
        /// individual parts of compound words.
        ///
        /// Default: 2
        pub fn set_min_hyphenated_word_length(&self, value: isize) -> bool {
            libvoikko::set_int_option(self.handle, 9, value)
        }

        /// Size of the spell checker cache. This can be -1 (no cache) or
        /// >= 0 ( size in bytes = `2^cache_size * (6544*sizeof(wchar_t) + 1008)` ).
        ///
        /// Default: 0
        pub fn set_speller_cache_size(&self, value: isize) -> bool {
            libvoikko::set_int_option(self.handle, 17, value)
        }
    }

    impl Drop for Voikko {
        fn drop(&mut self) {
            libvoikko::terminate(self.handle);
        }
    }
}