summaryrefslogtreecommitdiff
path: root/test/did_you_mean/tree_spell/change_word.rb
blob: d34b1f38e624b14d3943388efe6e9d7267b2fd56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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