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

Class Method Summary collapse

Instance Attribute Details

#configObject (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_dictonariesArray<String>

Returns list of all dictionaries dir.

Returns:

  • (Array<String>)

    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_languagesArray<String>

Returns list of available dictonaries.

Returns:

  • (Array<String>)

    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

Parameters:

  • string (String)

    string to check

Returns:

  • (Array<Hash>)

    result of check



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_languagenil

Returns check if current language has dictonaries.

Returns:

  • (nil)

    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

Parameters:

  • word (String)

    to check

  • language (String)

    to check

Returns:

  • (Boolean)

    result of check



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

Parameters:

  • word (String)

    to check

Returns:

  • (Hash)

    word check result



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

.configConfig

Returns get current config.

Returns:

  • (Config)

    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

Yields:



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

Parameters:

  • extension (String)

    extension of dictionaries

  • language (String) (defaults to: config.expected_language)

    language to get

Returns:

  • (String)

    path



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_confignil

Returns Reset config to default.

Returns:

  • (nil)

    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

Parameters:

  • string (String)

    multi-word text

Returns:

  • (Array<String>)

    uniq 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