onlyoffice_language_helper 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b7dacc49bd982030e1bbeb32cef0c6cfbe05c638cde4d8e78610b000f1a8c7be
4
- data.tar.gz: c9952a740dc42eedd48329498286e7b02777f2873b527ab782ca92c98443a531
3
+ metadata.gz: dd764ee21cd204cfb7d5a5043bc8dd77dd444a1155b061b121ba31c20d1070f6
4
+ data.tar.gz: 6740cc29afff12e6fcebbb3ec4e0ab22a715646c1f3a5ab79305d214f6d7bb5e
5
5
  SHA512:
6
- metadata.gz: 3e572ad5b79647ad9b26a8b9102ec4de89d077f9dae5a71aeb4c08798fdbba483ffc121d7deee57929e07fed55749e9f94f3d5a96df761792691c468cc5c25f1
7
- data.tar.gz: e7671a85fc20055c9bb782ef71bcb980ad8074543c9ef21a81ca8003a31dc273abb11c5fdc19e6707627b8d11be7e83c0a560297a6e52cd88320b46ffbef099d
6
+ metadata.gz: c658ea226f63814598546faf96c6e69bbcf75789b4e16140225eeeca581cd176bddf100e8023407d0224fc2d2c7a48603654ada5f649df53d2070cbe617760ab
7
+ data.tar.gz: 7125cacfd2ace60894d27cdf73562d59188eb6e206fafa7c314baf818d2d3ed78fba73bc6c271fe34dcacac8f333003d5b80a0db81fe61ad80ba6a6bb133f06e
@@ -1,12 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'hunspell-ffi'
3
+ require 'ffi/hunspell'
4
4
  require 'httparty'
5
5
  require 'json'
6
6
  require 'cgi'
7
7
  require 'whatlanguage'
8
8
  require_relative 'spell_checker/config'
9
- require_relative 'spell_checker/dictionaries_threads'
10
9
 
11
10
  # Spellchecker stuff
12
11
  module OnlyofficeLanguageHelper
@@ -27,8 +26,7 @@ module OnlyofficeLanguageHelper
27
26
  config
28
27
  yield(@config) if block_given?
29
28
  check_language
30
- @dictionary = Hunspell.new(path_to_dic_aff(:aff),
31
- path_to_dic_aff(:dic))
29
+ FFI::Hunspell.directories = all_languages_dictonaries
32
30
  OnlyofficeLoggerHelper.log('Configuring complete!')
33
31
  end
34
32
 
@@ -36,12 +34,41 @@ module OnlyofficeLanguageHelper
36
34
  # @param string [String] string to check
37
35
  # @return [Array<Hash>] result of check
38
36
  def self.check_in_all_dictionaries(string)
39
- check_configuration
40
- @dictionaries ||= DictionariesThreads.new
37
+ results = []
41
38
  split_text_by_words(string).map do |word|
42
- parse_spellcheck_result(word,
43
- @dictionaries.check_word(word))
39
+ word_hash = {}
40
+ word_hash[word] = check_word_in_all(word)
41
+ results << word_hash
44
42
  end
43
+ results
44
+ end
45
+
46
+ # Check word in all dictionaries
47
+ # @param [String] word to check
48
+ # @return [Hash] word check result
49
+ def self.check_word_in_all(word)
50
+ word_results = {}
51
+
52
+ @threads = []
53
+ available_languages.each do |language|
54
+ @threads << Thread.new do
55
+ word_results[language] = check_single_word(word, language)
56
+ end
57
+ end
58
+ @threads.each(&:join)
59
+ word_results
60
+ end
61
+
62
+ # Check if word correct in single language
63
+ # @param [String] word to check
64
+ # @param [String] language to check
65
+ # @return [Boolean] result of check
66
+ def self.check_single_word(word, language)
67
+ dict = FFI::Hunspell.dict(language)
68
+ result = dict.check?(word)
69
+ dict.close
70
+ OnlyofficeLoggerHelper.log("Word `#{word}` in `#{language}` correct: #{result}")
71
+ result
45
72
  end
46
73
 
47
74
  # Get path to dic aff
@@ -50,7 +77,7 @@ module OnlyofficeLanguageHelper
50
77
  # @return [String] path
51
78
  def self.path_to_dic_aff(extension, language = config.expected_language)
52
79
  "#{config.dictionaries_path}/dictionaries/"\
53
- "#{language}/#{language}.#{extension}"
80
+ "#{language}/#{language}.#{extension}"
54
81
  end
55
82
 
56
83
  # Split text by words
@@ -60,19 +87,6 @@ module OnlyofficeLanguageHelper
60
87
  string.to_s.scan(/\b[[:word:]['-]]+\b/u).uniq
61
88
  end
62
89
 
63
- # Parse spellchecker result
64
- # @param word [String] word to check
65
- # @param spellcheck_result [Hash] param to set result
66
- # @return [Hash] value of result
67
- def self.parse_spellcheck_result(word, spellcheck_result)
68
- unless spellcheck_result[config.expected_language]
69
- warn("Word '#{word}' was not found in "\
70
- "'#{config.expected_language}' dictionary!")
71
- spellcheck_result['suggestions'] = @dictionary.suggest(word)
72
- end
73
- { word => spellcheck_result }
74
- end
75
-
76
90
  # @return [Config] get current config
77
91
  def self.config
78
92
  @config ||= Config.new
@@ -83,13 +97,6 @@ module OnlyofficeLanguageHelper
83
97
  @config = Config.new
84
98
  end
85
99
 
86
- # @return [nil] check if current config is right
87
- def self.check_configuration
88
- return if @dictionary
89
-
90
- raise 'Call SpellChecker.configure method before using it!'
91
- end
92
-
93
100
  # @return [nil] check if current language has dictonaries
94
101
  def self.check_language
95
102
  unless File.exist?(path_to_dic_aff(:dic)) ||
@@ -97,5 +104,17 @@ module OnlyofficeLanguageHelper
97
104
  raise 'Incorrect language'
98
105
  end
99
106
  end
107
+
108
+ # @return [Array<String>] list of available dictonaries
109
+ def self.available_languages
110
+ all_languages_dictonaries.map { |dir| File.basename(dir) }
111
+ end
112
+
113
+ # @return [Array<String>] list of all dictionaries dir
114
+ def self.all_languages_dictonaries
115
+ Dir.glob("#{Dir.pwd}/lib/onlyoffice_language_helper/dictionaries/*").select do |fn|
116
+ File.directory?(fn)
117
+ end
118
+ end
100
119
  end
101
120
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module OnlyofficeLanguageHelper
4
4
  # @return [String] version of gem
5
- VERSION = '0.3.0'
5
+ VERSION = '0.4.0'
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: onlyoffice_language_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ONLYOFFICE
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-11-21 00:00:00.000000000 Z
12
+ date: 2021-07-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: detect_language
@@ -26,33 +26,33 @@ dependencies:
26
26
  - !ruby/object:Gem::Version
27
27
  version: '1'
28
28
  - !ruby/object:Gem::Dependency
29
- name: httparty
29
+ name: ffi-hunspell
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - ">="
32
+ - - "~>"
33
33
  - !ruby/object:Gem::Version
34
- version: 0.10.0
34
+ version: '0'
35
35
  type: :runtime
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - ">="
39
+ - - "~>"
40
40
  - !ruby/object:Gem::Version
41
- version: 0.10.0
41
+ version: '0'
42
42
  - !ruby/object:Gem::Dependency
43
- name: hunspell-ffi
43
+ name: httparty
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
- - - "~>"
46
+ - - ">="
47
47
  - !ruby/object:Gem::Version
48
- version: '0'
48
+ version: 0.10.0
49
49
  type: :runtime
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
- - - "~>"
53
+ - - ">="
54
54
  - !ruby/object:Gem::Version
55
- version: '0'
55
+ version: 0.10.0
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: onlyoffice_file_helper
58
58
  requirement: !ruby/object:Gem::Requirement
@@ -179,6 +179,20 @@ dependencies:
179
179
  - - "~>"
180
180
  - !ruby/object:Gem::Version
181
181
  version: '1'
182
+ - !ruby/object:Gem::Dependency
183
+ name: rubocop-rake
184
+ requirement: !ruby/object:Gem::Requirement
185
+ requirements:
186
+ - - "~>"
187
+ - !ruby/object:Gem::Version
188
+ version: '0'
189
+ type: :development
190
+ prerelease: false
191
+ version_requirements: !ruby/object:Gem::Requirement
192
+ requirements:
193
+ - - "~>"
194
+ - !ruby/object:Gem::Version
195
+ version: '0'
182
196
  - !ruby/object:Gem::Dependency
183
197
  name: rubocop-rspec
184
198
  requirement: !ruby/object:Gem::Requirement
@@ -321,7 +335,6 @@ files:
321
335
  - lib/onlyoffice_language_helper/name.rb
322
336
  - lib/onlyoffice_language_helper/spell_checker.rb
323
337
  - lib/onlyoffice_language_helper/spell_checker/config.rb
324
- - lib/onlyoffice_language_helper/spell_checker/dictionaries_threads.rb
325
338
  - lib/onlyoffice_language_helper/version.rb
326
339
  homepage: https://github.com/ONLYOFFICE-QA/onlyoffice_language_helper
327
340
  licenses:
@@ -347,7 +360,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
347
360
  - !ruby/object:Gem::Version
348
361
  version: '0'
349
362
  requirements: []
350
- rubygems_version: 3.1.4
363
+ rubygems_version: 3.2.21
351
364
  signing_key:
352
365
  specification_version: 4
353
366
  summary: ONLYOFFICE Helper Gem for language operation
@@ -1,67 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module OnlyofficeLanguageHelper
4
- module SpellChecker
5
- # Threads for spellchecker
6
- class DictionariesThreads
7
- attr_accessor :word
8
-
9
- Thread.abort_on_exception = true
10
-
11
- def initialize
12
- @dictionaries = {}
13
- Dir.glob("#{SpellChecker.config.dictionaries_path}/dictionaries/*")
14
- .select { |f| File.directory?(f) }.each do |lang|
15
- @dictionaries[File.basename(lang)] =
16
- Hunspell.new(SpellChecker.path_to_dic_aff(:aff,
17
- File.basename(lang)),
18
- SpellChecker.path_to_dic_aff(:dic,
19
- File.basename(lang)))
20
- end
21
- init_threads_data
22
- end
23
-
24
- # @return [Array<Thread>] list of checker threads
25
- def threads
26
- @threads ||= init_threads
27
- end
28
-
29
- # Check current word
30
- # @param word [String] word to check
31
- # @return [Hash] result of check
32
- def check_word(word)
33
- @result = {}
34
- @word = word.to_s
35
- start_threads
36
- end
37
-
38
- # @return [Hash] start threads and return results
39
- def start_threads
40
- threads.each(&:run)
41
- until @result.length == @dictionaries.length
42
- end
43
- @result
44
- end
45
-
46
- private
47
-
48
- def init_threads_data
49
- @result = {}
50
- threads
51
- end
52
-
53
- def init_threads
54
- @threads = []
55
- @dictionaries.each do |key, value|
56
- @threads << Thread.new do
57
- loop do
58
- @result[key] = value.check(@word.to_s)
59
- Thread.stop
60
- end
61
- end
62
- end
63
- @threads
64
- end
65
- end
66
- end
67
- end