Skip to content

Instantly share code, notes, and snippets.

@zavan
Last active September 4, 2020 11:48
Show Gist options
  • Save zavan/a4598da44e5904c843eff0a8567cf9f9 to your computer and use it in GitHub Desktop.
Save zavan/a4598da44e5904c843eff0a8567cf9f9 to your computer and use it in GitHub Desktop.
Parsing aspell CLI output with Ruby for TinyMCE
# See https://stackoverflow.com/a/63740582/1415262
class TinymceController < ApplicationController
skip_forgery_protection
respond_to :json
def spellcheck
suggestions = check_spelling_new(
spellcheck_params[:text],
spellcheck_params[:lang]
)
render json: {words: suggestions}
end
private
def spellcheck_params
params.permit(:method, :lang, :text)
end
def check_spelling_new(text, lang)
suggestions = {}
spell_check_response = `echo "#{text}" | aspell -a -l #{lang}`
if spell_check_response.present?
spelling_errors = spell_check_response.split(' ').slice(1..-1)
spelling_errors.length.times do |i|
spelling_errors[i].strip!
if spelling_errors[i].to_s.start_with?('&')
match_data = spelling_errors[i + 1]
suggestion_count = spelling_errors[i + 2].to_i
suggestions[match_data] ||= []
suggestion_count.times do |k|
suggestions[match_data] << spelling_errors[i + k + 4].gsub(',', '')
end
end
end
end
suggestions
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment