-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscregex.rb
executable file
·138 lines (129 loc) · 3.62 KB
/
scregex.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/ruby
class String
SCRABBLE_POINTS = {
'a' => 1,
'b' => 3,
'c' => 3,
'd' => 2,
'e' => 1,
'f' => 4,
'g' => 2,
'h' => 4,
'i' => 1,
'j' => 8,
'k' => 5,
'l' => 1,
'm' => 3,
'n' => 1,
'o' => 1,
'p' => 3,
'q' => 10,
'r' => 1,
's' => 1,
't' => 1,
'u' => 1,
'v' => 4,
'w' => 4,
'x' => 8,
'y' => 4,
'z' => 10,
}
WORDS_WITH_FRIENDS_POINTS = {
'a' => 1,
'b' => 3,
'c' => 4,
'd' => 2,
'e' => 1,
'f' => 4,
'g' => 3,
'h' => 3,
'i' => 1,
'j' => 8,
'k' => 5,
'l' => 2,
'm' => 3,
'n' => 1,
'o' => 1,
'p' => 4,
'q' => 10,
'r' => 1,
's' => 1,
't' => 1,
'u' => 1,
'v' => 4,
'w' => 4,
'x' => 8,
'y' => 4,
'z' => 10,
}
def score
p = 0
self.each_char do |c|
p += (WORDS_WITH_FRIENDS_POINTS[c] rescue -100)
end
p
end
end
def regex(rack_letters, wildcard_count, board_letters = '')
rack_letters_uniq = rack_letters.split(//).uniq.to_s
if wildcard_count > 0
if board_letters.length > 0
if rack_letters.length > 0
regex = /^[#{rack_letters_uniq}]{0,#{rack_letters.length}}(\w([#{rack_letters_uniq}]){0,#{rack_letters.length}}){0,#{wildcard_count}}#{board_letters}[#{rack_letters_uniq}]{0,#{rack_letters.length}}(\w[#{rack_letters_uniq}]{0,#{rack_letters.length}}){0,#{wildcard_count}}$/
else
regex = /^\w{0,#{wildcard_count}}#{board_letters}\w{0,#{wildcard_count}}$/
end
else
regex = /^[#{rack_letters_uniq}]{0,#{rack_letters.length}}(\w[#{rack_letters_uniq}]{0,#{rack_letters.length}}){0,#{wildcard_count}}$/
end
else
regex = board_letters.length > 0 ?
/^[#{rack_letters_uniq}]{0,#{rack_letters.length}}#{board_letters}[#{rack_letters_uniq}]{0,#{rack_letters.length}}$/ :
/^[#{rack_letters_uniq}]{0,#{rack_letters.length}}$/
end
puts "Regex: #{regex}"
regex
end
def matching_words(rack_letters, wildcard_count, board_letters = '')
regex = regex(rack_letters, wildcard_count, board_letters)
file = File.open('/usr/share/dict/words', 'r')
all_letters = (rack_letters + board_letters)
min_length = [2, board_letters.length + 1].max
max_length = (all_letters.length + wildcard_count)
min_length = [min_length, max_length].min
puts "length: #{min_length} - #{max_length}"
words = file.select do |word|
w = word.strip
if (
w.length >= min_length &&
w.length <= max_length &&
w.match(regex)
)
remaining_letters = all_letters
wildcard_usages = 0
w.split(//).each do |letter_in_word|
if remaining_letters.include?(letter_in_word)
remaining_letters = remaining_letters.sub(letter_in_word, '')
else
wildcard_usages += 1
end
end
if wildcard_usages <= wildcard_count
true
else
false
end
else
false
end
end.collect {|w| w.strip.downcase}.sort.sort_by { |w| -(w.score + w.length * 100) }
end
rack_letters = ARGV[0].strip.downcase
board_letter_array = ARGV.slice(1, 99).collect {|s| s.strip.downcase}
wildcard_count = rack_letters.count("?")
rack_letters = rack_letters.gsub(/\?/, '')
puts "#{wildcard_count} wildcard(s) detected"
for board_letters in ([''] + board_letter_array)
puts "Searching for: #{[(rack_letters.length > 0 ? rack_letters : nil), (wildcard_count > 0 ? (wildcard_count.to_s + ' wildcard(s)') : nil)].select {|e| !e.nil?}.join(' plus ')}#{board_letters.length > 0 ? ' incorporating ' + board_letters : nil}"
matching_words(rack_letters, wildcard_count, board_letters).each {|w| puts " #{w} (#{w.score})"}
end