mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
171803d5d3
At the moment, there are some problems with regard to bundler + did_you_mean because of did_you_mean being a bundled gem. Since the vendored version of thor inside bundler and ruby itself explicitly requires did_you_mean, it can become difficult to load it when using Bundler.setup. See this issue: https://github.com/yuki24/did_you_mean/issues/117#issuecomment-482733159 for more details.
61 lines
1.5 KiB
Ruby
61 lines
1.5 KiB
Ruby
module TreeSpell
|
|
# Changes a word with one of four actions:
|
|
# insertion, substitution, deletion and transposition.
|
|
class ChangeWord
|
|
# initialize with input string
|
|
def initialize(input)
|
|
@input = input
|
|
@len = input.length
|
|
end
|
|
|
|
# insert char after index of i_place
|
|
def insertion(i_place, char)
|
|
@word = input.dup
|
|
return char + word if i_place == 0
|
|
return word + char if i_place == len - 1
|
|
word.insert(i_place + 1, char)
|
|
end
|
|
|
|
# substitute char at index of i_place
|
|
def substitution(i_place, char)
|
|
@word = input.dup
|
|
word[i_place] = char
|
|
word
|
|
end
|
|
|
|
# delete character at index of i_place
|
|
def deletion(i_place)
|
|
@word = input.dup
|
|
word.slice!(i_place)
|
|
word
|
|
end
|
|
|
|
# transpose char at i_place with char at i_place + direction
|
|
# if i_place + direction is out of bounds just swap in other direction
|
|
def transposition(i_place, direction)
|
|
@word = input.dup
|
|
w = word.dup
|
|
return swap_first_two(w) if i_place + direction < 0
|
|
return swap_last_two(w) if i_place + direction >= len
|
|
swap_two(w, i_place, direction)
|
|
w
|
|
end
|
|
|
|
private
|
|
|
|
attr_accessor :word, :input, :len
|
|
|
|
def swap_first_two(w)
|
|
w[1] + w[0] + word[2..-1]
|
|
end
|
|
|
|
def swap_last_two(w)
|
|
w[0...(len - 2)] + word[len - 1] + word[len - 2]
|
|
end
|
|
|
|
def swap_two(w, i_place, direction)
|
|
w[i_place] = word[i_place + direction]
|
|
w[i_place + direction] = word[i_place]
|
|
end
|
|
end
|
|
end
|