Module: OnlyofficeLanguageHelper::SpellChecker
- Includes:
- HTTParty
- Defined in:
- lib/onlyoffice_language_helper/spell_checker.rb,
lib/onlyoffice_language_helper/spell_checker/config.rb
Overview
USAGE: SpellChecker.configure do |config|
config.expected_language = 'lv_LV'
end
SpellChecker.check_in_all_dictionaries(“viens no veidiem,
iespieddarbiem: nav periodisks izdevums")
Defined Under Namespace
Classes: Config
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Class Method Summary collapse
-
.all_languages_dictonaries ⇒ Array<String>
List of all dictionaries dir.
-
.available_languages ⇒ Array<String>
List of available dictonaries.
-
.check_in_all_dictionaries(string) ⇒ Array<Hash>
Check in all known dictionaries.
-
.check_language ⇒ nil
Check if current language has dictonaries.
-
.check_single_word(word, language) ⇒ Boolean
Check if word correct in single language.
-
.check_word_in_all(word) ⇒ Hash
Check word in all dictionaries.
-
.config ⇒ Config
Get current config.
-
.configure {|@config| ... } ⇒ Object
Configure Spellchecker.
-
.path_to_dic_aff(extension, language = config.expected_language) ⇒ String
Get path to dic aff.
-
.reset_config ⇒ nil
Reset config to default.
-
.split_text_by_words(string) ⇒ Array<String>
Split text by words.
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
21 22 23 |
# File 'lib/onlyoffice_language_helper/spell_checker.rb', line 21 def config @config end |
Class Method Details
.all_languages_dictonaries ⇒ Array<String>
Returns list of all dictionaries dir.
114 115 116 117 118 |
# File 'lib/onlyoffice_language_helper/spell_checker.rb', line 114 def self.all_languages_dictonaries Dir.glob("#{Dir.pwd}/lib/onlyoffice_language_helper/dictionaries/*").select do |fn| File.directory?(fn) end end |
.available_languages ⇒ Array<String>
Returns list of available dictonaries.
109 110 111 |
# File 'lib/onlyoffice_language_helper/spell_checker.rb', line 109 def self.available_languages all_languages_dictonaries.map { |dir| File.basename(dir) } end |
.check_in_all_dictionaries(string) ⇒ Array<Hash>
Check in all known dictionaries
36 37 38 39 40 41 42 43 44 |
# File 'lib/onlyoffice_language_helper/spell_checker.rb', line 36 def self.check_in_all_dictionaries(string) results = [] split_text_by_words(string).map do |word| word_hash = {} word_hash[word] = check_word_in_all(word) results << word_hash end results end |
.check_language ⇒ nil
Returns check if current language has dictonaries.
101 102 103 104 105 106 |
# File 'lib/onlyoffice_language_helper/spell_checker.rb', line 101 def self.check_language unless File.exist?(path_to_dic_aff(:dic)) || File.exist?(path_to_dic_aff(:aff)) raise 'Incorrect language' end end |
.check_single_word(word, language) ⇒ Boolean
Check if word correct in single language
66 67 68 69 70 71 72 |
# File 'lib/onlyoffice_language_helper/spell_checker.rb', line 66 def self.check_single_word(word, language) dict = FFI::Hunspell.dict(language) result = dict.check?(word) dict.close OnlyofficeLoggerHelper.log("Word `#{word}` in `#{language}` correct: #{result}") result end |
.check_word_in_all(word) ⇒ Hash
Check word in all dictionaries
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/onlyoffice_language_helper/spell_checker.rb', line 49 def self.check_word_in_all(word) word_results = {} @threads = [] available_languages.each do |language| @threads << Thread.new do word_results[language] = check_single_word(word, language) end end @threads.each(&:join) word_results end |
.config ⇒ Config
Returns get current config.
91 92 93 |
# File 'lib/onlyoffice_language_helper/spell_checker.rb', line 91 def self.config @config ||= Config.new end |
.configure {|@config| ... } ⇒ Object
Configure Spellchecker
24 25 26 27 28 29 30 31 |
# File 'lib/onlyoffice_language_helper/spell_checker.rb', line 24 def self.configure OnlyofficeLoggerHelper.log('Begin configuring SpellChecker') config yield(@config) if block_given? check_language FFI::Hunspell.directories = all_languages_dictonaries OnlyofficeLoggerHelper.log('Configuring complete!') end |
.path_to_dic_aff(extension, language = config.expected_language) ⇒ String
Get path to dic aff
78 79 80 81 |
# File 'lib/onlyoffice_language_helper/spell_checker.rb', line 78 def self.path_to_dic_aff(extension, language = config.expected_language) "#{config.dictionaries_path}/dictionaries/" \ "#{language}/#{language}.#{extension}" end |
.reset_config ⇒ nil
Returns Reset config to default.
96 97 98 |
# File 'lib/onlyoffice_language_helper/spell_checker.rb', line 96 def self.reset_config @config = Config.new end |
.split_text_by_words(string) ⇒ Array<String>
Split text by words
86 87 88 |
# File 'lib/onlyoffice_language_helper/spell_checker.rb', line 86 def self.split_text_by_words(string) string.to_s.scan(/\b[[:word:]['-]]+\b/u).uniq end |