Sha256: 090e82d323d8c311f421a6b1f17699b131a627c43811653a5929470c18e7b438

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

require "open3"
require_relative "config"

Misspelling = Data.define(:word, :suggestions)

module Heckler
  class Aspell
    @process = nil

    def initialize(config)
      @config = config
    end

    def check(text)
      misspellings = get_misspellings(text)
      misspellings.reject { |misspelling| @config.word_ignored?(misspelling.word) }
    end

    private

    def get_misspellings(text)
      misspellings = run(text)
      misspellings
    end

    def take_suggestions(suggestions)
      suggestions = suggestions.select { |suggestion| suggestion.match(/[^a-zA-Z]/).nil? }
      suggestions.uniq
    end

    def run(text)
      stdin, stdout, stderr, wait_thr = Open3.popen3("aspell", "--encoding", "utf-8", "-a", "--ignore-case", "--lang=en_US", "--sug-mode=ultra")
      stdin.puts text
      stdin.close
      output = stdout.read
      stdout.close
      stderr.close

      output.lines.select { |line| line.start_with?("&") }.map do |line|
        word_metadata, suggestions = line.strip.split(":")
        word = word_metadata.split(" ")[1]
        suggestions = suggestions.strip.split(", ")
        Misspelling.new(word, take_suggestions(suggestions))
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
heckler-0.0.1 lib/heckler/aspell.rb