Skip to content

Commit

Permalink
When the suggestion distance is the same
Browse files Browse the repository at this point in the history
sort alphabetically for the cross ruby version tests
  • Loading branch information
robotdana committed Jan 26, 2025
1 parent f5be681 commit debb88d
Show file tree
Hide file tree
Showing 18 changed files with 285 additions and 227 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ jobs:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: 3.3
ruby-version: 3.4
bundler-cache: true
- run: COVERAGE=1 bundle exec rspec

- uses: actions/upload-artifact@v2
- uses: actions/upload-artifact@v4
with:
name: coverage-report
path: 'coverage/*'
Expand Down
51 changes: 11 additions & 40 deletions .simplecov
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,15 @@ SimpleCov.add_filter '/backports'
SimpleCov.add_filter '/spec/'
# because i have to skip and mock some of it for expediency reasons
SimpleCov.add_filter '/bin/generate'
require 'parallel'

# internals of Parallel i'm sure it's fine
# this is the only way that i can tell to coverage inside parallel blocks
# without modifying my code in ugly ways

module Parallel
def self.worker(job_factory, options, &block) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
child_read, parent_write = IO.pipe
parent_read, child_write = IO.pipe

pid = Process.fork do
# here begins my additions
SimpleCov.command_name "Parallel #{Process.pid}"
SimpleCov.formatter SimpleCov::Formatter::SimpleFormatter
SimpleCov.minimum_coverage 0
SimpleCov.print_error_status = false
SimpleCov.start
# here ends my additions

self.worker_number = options[:worker_number]

begin
options.delete(:started_workers).each(&:close_pipes)

parent_write.close
parent_read.close

process_incoming_jobs(child_read, child_write, job_factory, options, &block)
ensure
child_read.close
child_write.close
end
end

child_read.close
child_write.close

Worker.new(parent_read, parent_write, pid)
end
SimpleCov.enable_for_subprocesses true
SimpleCov.at_fork do |pid|
# This needs a unique name so it won't be overwritten
SimpleCov.command_name "#{SimpleCov.command_name} (subprocess: #{pid})"
# be quiet, the parent process will be in charge of output and checking coverage totals
SimpleCov.print_error_status = false
SimpleCov.formatter SimpleCov::Formatter::SimpleFormatter
SimpleCov.minimum_coverage 0
# start
SimpleCov.start
end
SimpleCov.start
3 changes: 0 additions & 3 deletions .spellr_wordlists/english.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ hardcoded
hashbang
hashbangs
hashbangs
hellooo
heuristically
hml
hostnames
Expand Down Expand Up @@ -154,8 +153,6 @@ sudo
superselector
svg
symlinks
thiswordisalsooutsidethethreshold
thiswordisoutsidethethreshould
thu
titlecase
tlds
Expand Down
12 changes: 12 additions & 0 deletions .spellr_wordlists/lorem.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
acn
amet
antoeuhealheuo
antoeuheapl
constx
dolar
dolara
dolares
dolor
donoore
elp
eplace
foa
hellooo
ipsum
lorem
thisisanimpossibleword
thiswordisalsooutsidethethreshold
thiswordisoutsidethethreshould
unword
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# v0.12.0

- Require stringio (thanks @alexdeng-mp)
- Require stringio in the tokenizer (thanks @alexdeng-mp)
- Remove implicit did_you_mean dependency, inline and specialize what of did_you_mean this was using.
- Update rubies this is tested with
- Replace readline dependency with reline
Expand Down
2 changes: 1 addition & 1 deletion lib/spellr/autocorrect_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
module Spellr
class AutocorrectReporter < BaseReporter
def finish
puts "\n"
warn "\n"
print_count(:checked, 'file')
print_value(total, 'error', 'found')
print_count(:total_fixed, 'error', 'fixed', hide_zero: true)
Expand Down
2 changes: 1 addition & 1 deletion lib/spellr/base_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def print_count(stat, noun, verb = stat, hide_zero: false)
end

def print_value(value, noun, verb, hide_zero: false)
puts "#{pluralize noun, value} #{verb}" if !hide_zero || value.positive?
warn "#{pluralize noun, value} #{verb}" if !hide_zero || value.positive?
end
end
end
4 changes: 2 additions & 2 deletions lib/spellr/interactive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
module Spellr
class Interactive < BaseReporter # rubocop:disable Metrics/ClassLength
def finish
puts "\n"
warn "\n"
print_count(:checked, 'file')
print_value(total, 'error', 'found')
print_count(:total_skipped, 'error', 'skipped', hide_zero: true)
Expand Down Expand Up @@ -165,7 +165,7 @@ def handle_response(token, suggestions) # rubocop:disable Metrics/CyclomaticComp
end

def handle_replace_with_suggestion(token, suggestions, letter)
replacement = suggestions[letter.to_i - 1].chomp
replacement = suggestions[letter.to_i - 1]

token.replace(replacement)
increment(:total_fixed)
Expand Down
2 changes: 1 addition & 1 deletion lib/spellr/rake_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def define_task # rubocop:disable Metrics/MethodLength
end

def write_cli_cmd(argv)
$stdout.puts("\e[2mspellr #{escaped_argv(argv)}\e[0m")
warn("\e[2mspellr #{escaped_argv(argv)}\e[0m")
end

def run(argv)
Expand Down
6 changes: 3 additions & 3 deletions lib/spellr/reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
module Spellr
class Reporter < Spellr::BaseReporter
def finish
puts "\n"
warn "\n"
print_count(:checked, 'file')
print_count(:total, 'error', 'found')

Expand All @@ -24,12 +24,12 @@ def call(token)
private

def interactive_command
puts "\nto add or replace words interactively, run:"
warn "\nto add or replace words interactively, run:"
command = ['spellr', '--interactive']
# sort is purely for repeatability for tests. so
command.concat(counts[:filenames].to_a.sort) unless counts[:filenames].length > 20

puts " #{Shellwords.join(command)}"
warn " #{Shellwords.join(command)}"
end

def filenames
Expand Down
11 changes: 5 additions & 6 deletions lib/spellr/suggester.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ def suggestions(token, limit = 5) # rubocop:disable Metrics/MethodLength, Metric
all_suggestions(term, jaro_winkler_similarity_threshold, wordlist)
end
suggestions.uniq!(&:word)
suggestions.sort_by!(&:jaro_winkler_similarity)
suggestions.reverse!
suggestions.sort_by! { |suggestion| [-suggestion.jaro_winkler_similarity, suggestion.word] }

suggestions = reduce_suggestions(suggestions, term, limit)

suggestions.map { |suggestion| suggestion.word.send(token.case_method).chomp }
suggestions.map { |suggestion| suggestion.word.send(token.case_method) }
end

def slow?
Expand All @@ -42,11 +41,11 @@ def fast_suggestions(token, limit = 5)
private

def all_suggestions(term, jaro_winkler_similarity_threshold, wordlist)
wordlist.filter_map do |word|
wordlist.reduce([]) do |acc, word|
similarity = ::JaroWinkler.similarity(word, term)
next unless similarity >= jaro_winkler_similarity_threshold
next acc unless similarity >= jaro_winkler_similarity_threshold

Suggestion.new(word, similarity)
acc << Suggestion.new(word, similarity)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/spellr/token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class String

def spellr_normalize
@@spellr_normalize.fetch(to_s) do |term|
@@spellr_normalize[term] = "#{term.strip.downcase.unicode_normalize.tr('’', "'")}\n"
@@spellr_normalize[term] = term.strip.downcase.unicode_normalize.tr('’', "'").to_s
end
end
end
Expand Down
19 changes: 12 additions & 7 deletions lib/spellr/wordlist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,29 @@ def <<(term)
touch
@include[term] = true
insert_sorted(term)
@path.write(words.join) # we don't need to clear the cache
write(words, clear_cache: false)
end

def words
@words ||= (exist? ? @path.readlines : [])
@words ||= if exist?
@path.readlines(chomp: true)
else
[]
end
end
alias_method :to_a, :words

def clean(file = @path)
require_relative 'tokenizer'
write(Spellr::Tokenizer.new(file, skip_key: false).normalized_terms.join)
write(Spellr::Tokenizer.new(file, skip_key: false).normalized_terms)
end

def write(content)
def write(words, clear_cache: true)
content = words.join("\n")
content << "\n" unless words.empty?
@path.write(content)

clear_cache
self.clear_cache if clear_cache
end

def exist?
Expand All @@ -70,8 +76,7 @@ def touch
return if exist?

@path.dirname.mkpath
@path.write('')
clear_cache
write([])
end

def length
Expand Down
2 changes: 1 addition & 1 deletion lib/spellr/wordlist_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
module Spellr
class WordlistReporter < Spellr::BaseReporter
def finish
puts words.sort.join unless words.empty?
puts words.sort.join("\n") unless words.empty?
end

def call(token)
Expand Down
Loading

0 comments on commit debb88d

Please sign in to comment.