summaryrefslogtreecommitdiff
path: root/test/did_you_mean/tree_spell/human_typo.rb
diff options
context:
space:
mode:
authorKevin Deisz <kevin.deisz@gmail.com>2019-10-29 10:08:37 -0400
committerYuki Nishijima <yk.nishijima@gmail.com>2019-11-30 21:08:19 -0500
commit171803d5d34feb1b4244ca81b9db0a7bc2171c85 (patch)
tree664ee644da144f28152097fbe5ea43329bfc0576 /test/did_you_mean/tree_spell/human_typo.rb
parenta2fc6a51dd2e1a153559038795e1e2509f9c6a94 (diff)
Promote did_you_mean to default gem
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.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/2689
Diffstat (limited to 'test/did_you_mean/tree_spell/human_typo.rb')
-rw-r--r--test/did_you_mean/tree_spell/human_typo.rb89
1 files changed, 89 insertions, 0 deletions
diff --git a/test/did_you_mean/tree_spell/human_typo.rb b/test/did_you_mean/tree_spell/human_typo.rb
new file mode 100644
index 0000000000..302d4d6902
--- /dev/null
+++ b/test/did_you_mean/tree_spell/human_typo.rb
@@ -0,0 +1,89 @@
+# module for classes needed to test TreeSpellChecker
+module TreeSpell
+ require_relative 'change_word'
+ # Simulate an error prone human typist
+ # see doc/human_typo_api.md for the api description
+ class HumanTypo
+ def initialize(input, lambda: 0.05)
+ @input = input
+ check_input
+ @len = input.length
+ @lambda = lambda
+ end
+
+ def call
+ @word = input.dup
+ i_place = initialize_i_place
+ loop do
+ action = action_type
+ @word = make_change action, i_place
+ @len = word.length
+ i_place += exponential
+ break if i_place >= len
+ end
+ word
+ end
+
+ private
+
+ attr_accessor :input, :word, :len, :lambda
+
+ def initialize_i_place
+ i_place = nil
+ loop do
+ i_place = exponential
+ break if i_place < len
+ end
+ i_place
+ end
+
+ def exponential
+ (rand / (lambda / 2)).to_i
+ end
+
+ def rand_char
+ popular_chars = alphabetic_characters + special_characters
+ n = popular_chars.length
+ popular_chars[rand(n)]
+ end
+
+ def alphabetic_characters
+ ('a'..'z').to_a.join + ('A'..'Z').to_a.join
+ end
+
+ def special_characters
+ '?<>,.!`+=-_":;@#$%^&*()'
+ end
+
+ def toss
+ return +1 if rand >= 0.5
+ -1
+ end
+
+ def action_type
+ [:insert, :transpose, :delete, :substitute][rand(4)]
+ end
+
+ def make_change(action, i_place)
+ cw = ChangeWord.new(word)
+ case action
+ when :delete
+ cw.deletion(i_place)
+ when :insert
+ cw.insertion(i_place, rand_char)
+ when :substitute
+ cw.substitution(i_place, rand_char)
+ when :transpose
+ cw.transposition(i_place, toss)
+ end
+ end
+
+ def check_input
+ fail check_input_message if input.nil? || input.length < 5
+ end
+
+ def check_input_message
+ "input length must be greater than 5 characters: #{input}"
+ end
+ end
+end