Sha256: e1bdfcdbf3d122ff65d60dda836eced87d98554ba7733abf65cf967db1ac384f

Contents?: true

Size: 1.15 KB

Versions: 5

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

require 'nokogiri'

# Alfonso X module
module AlfonsoX
  # Spell checker
  module SpellChecker
    # Alfonso X dictionary
    module Dictionary
      # Rubymine dictionary loader
      class Rubymine
        # Default directory where the XML RubyMine dictionary file should be
        DEFAULT_PATH = '.idea/dictionaries'
        attr_reader :path

        def initialize(path = nil)
          @path = path || DEFAULT_PATH
          load_dictionaries
        end

        def self.from_config(yml_config)
          new(yml_config.fetch('path') { DEFAULT_PATH })
        end

        def word_present?(word)
          @words.include?(word.downcase)
        end

        def similar_words(_word)
          []
        end

        private

        def load_dictionaries
          @words = Set.new
          Dir.glob("#{@path}/*.xml") do |xml_file_path|
            xml_file_contents = ::File.open(xml_file_path).read
            xml_doc = ::Nokogiri::XML(xml_file_contents)
            xml_doc.css('w').each do |word|
              @words.add(word.content.downcase)
            end
          end
          @words
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
alfonsox-0.2.7 lib/alfonsox/spellchecker/dictionary/rubymine.rb
alfonsox-0.2.6 lib/alfonsox/spellchecker/dictionary/rubymine.rb
alfonsox-0.2.5 lib/alfonsox/spellchecker/dictionary/rubymine.rb
alfonsox-0.1.1 lib/alfonsox/spellchecker/dictionary/rubymine.rb
alfonsox-0.1.0 lib/alfonsox/spellchecker/dictionary/rubymine.rb