-
Notifications
You must be signed in to change notification settings - Fork 2
/
spellbreaker.rb
executable file
·107 lines (97 loc) · 2.25 KB
/
spellbreaker.rb
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env ruby
# this is pretty awful, it's just built to get the job done
class Misspell
attr_reader :dictionary
def initialize
@dictionary = []
@process = [
0, # :case,
1, # :repeat,
2, # :vowel,
3, # :case_repeat,
4, # :case_vowel,
5, # :repeat_case,
6, # :repeat_vowel,
7, # :vowel_case,
8, # :vowel_repeat,
9, # :case_repeat_vowel
]
@@vowels = ['a','e','i','o','u']
end
def build(dict)
if File.exists?(dict)
IO.foreach(dict) do |word|
@dictionary << word.strip.downcase
end
else
abort("Exit: Invalid dictionary #{dict}, is the path correct?")
end
end
def vowels(word)
(0..(word.length - 1)).each do |i|
if @@vowels.include?(word[i])
word[i] = (rand(100) >= 50) ? @@vowels.sample : word[i]
end
end
return word
end
def repeat(word)
(1..rand(1..3)).each do |i|
random = rand(0..word.length-1)
if word[random] != '\''
word.insert(random,word[random])
end
end
return word
end
def caps(word)
random = rand(0..word.length/2)
randomr = random+(rand(0..3))
word[random..randomr] = word[random..randomr].upcase
return word
end
def wrong(word)
case @process.sample
when 0
caps(word)
when 1
repeat(word)
when 2
vowels(word)
when 3
repeat(caps(word))
when 4
vowels(caps(word))
when 5
caps(repeat(word))
when 6
vowels(repeat(word))
when 7
caps(vowels(word))
when 8
repeat(vowels(word))
when 9
vowels(repeat(caps(word)))
end
return word
end
end
begin
if !ARGV[0] || ARGV[0].strip.empty?
# No argument, prompt the user for a dictionary path
print "Please enter the path to a dictionary file and press enter to continue\n( Default: '/usr/share/dict/words' )\n> "
dict = $stdin.gets.chomp.strip
else
# Argument passed, should use that as the dictionary path
dict = ARGV[0].strip
end
spell = Misspell.new
spell.build((dict.empty?) ? dict = '/usr/share/dict/words' : dict)
puts dict # Feed dictionary path to spellchecker
loop do
puts spell.wrong(spell.dictionary.sample)
end
rescue Interrupt
warn "\nBye!"
exit 1
end