LittleWeasel 1.1.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.idea/LittleWeasel.iml +144 -0
- data/.idea/workspace.xml +378 -266
- data/Gemfile +5 -3
- data/README.md +2 -1
- data/lib/LittleWeasel/version.rb +1 -1
- data/lib/LittleWeasel.rb +65 -9
- data/spec/checker/checker_spec.rb +89 -2
- metadata +2 -2
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -22,6 +22,7 @@ Or install it yourself as:
|
|
22
22
|
require 'LittleWeasel'
|
23
23
|
|
24
24
|
LittleWeasel::Checker.instance.exists?('word', options|nil) # true if exists in the dictionary, false otherwise.
|
25
|
+
LittleWeasel::Checker.instance.exists?('Multiple words', options|nil) # true if exists in the dictionary, false otherwise.
|
25
26
|
|
26
27
|
## Contributing
|
27
28
|
|
@@ -31,7 +32,7 @@ Not taking contributions just yet.
|
|
31
32
|
|
32
33
|
(The MIT License)
|
33
34
|
|
34
|
-
Copyright © 2013 Gene M. Angelo, Jr.
|
35
|
+
Copyright © 2013-2014 Gene M. Angelo, Jr.
|
35
36
|
|
36
37
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
37
38
|
|
data/lib/LittleWeasel/version.rb
CHANGED
data/lib/LittleWeasel.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'singleton'
|
2
2
|
require "LittleWeasel/version"
|
3
|
+
require 'active_support/inflector'
|
3
4
|
|
4
5
|
module LittleWeasel
|
5
6
|
|
@@ -14,25 +15,31 @@ module LittleWeasel
|
|
14
15
|
|
15
16
|
private
|
16
17
|
|
17
|
-
attr_reader :
|
18
|
+
attr_reader :alphabet_exclusion_list
|
19
|
+
|
20
|
+
# Keep these private...will expose as options later.
|
21
|
+
attr_accessor :word_regex, :numeric_regex, :non_wordchar_regex
|
18
22
|
|
19
23
|
public
|
20
24
|
|
21
25
|
# The constructor
|
22
26
|
def initialize
|
23
|
-
@options = {exclude_alphabet: false, strip_whitespace: false}
|
27
|
+
@options = {exclude_alphabet: false, strip_whitespace: false, ignore_numeric: true}
|
24
28
|
@alphabet_exclusion_list = %w{ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z }
|
29
|
+
@numeric_regex = /^[-+]?[0-9]?(\.[0-9]+)?$+/
|
30
|
+
@word_regex = /\s+(?=(?:[^"]*"[^"]*")*[^"]*$)/
|
31
|
+
@non_wordchar_regex = /\W+/
|
25
32
|
@dictionary = Hash.new(1)
|
26
33
|
load
|
27
34
|
end
|
28
35
|
|
29
36
|
# Interrogates the dictionary to determine whether or not [word] exists.
|
30
37
|
#
|
31
|
-
# @param [String] word the word to interrogate
|
38
|
+
# @param [String] word the word or words to interrogate
|
32
39
|
# @param [Hash] options options to apply to this query (see #options=). Options passed to this
|
33
40
|
# method are applied for this query only.
|
34
41
|
#
|
35
|
-
# @return [Boolean] true if *word* exists, false otherwise.
|
42
|
+
# @return [Boolean] true if the word/words in *word* exists, false otherwise.
|
36
43
|
#
|
37
44
|
# @example
|
38
45
|
#
|
@@ -40,12 +47,17 @@ module LittleWeasel
|
|
40
47
|
# LittleWeasel::Checker.instance.exists?('A', {exclude_alphabet:true}) # false
|
41
48
|
# LittleWeasel::Checker.instance.exists?('X', {exclude_alphabet:false}) # true
|
42
49
|
# LittleWeasel::Checker.instance.exists?('Hello') # true
|
43
|
-
# LittleWeasel::Checker.instance.exists?('Hello World') # false, two words does not a single word make :)
|
44
50
|
#
|
45
51
|
# LittleWeasel::Checker.instance.exists?(' Hello ') # false (default options, :strip_whitespace => false)
|
46
52
|
# LittleWeasel::Checker.instance.exists?(' Yes ', {strip_whitespace:true}) # true
|
47
53
|
# LittleWeasel::Checker.instance.exists?('No ', {strip_whitespace:false}) # false
|
48
|
-
# LittleWeasel::Checker.instance.exists?('
|
54
|
+
# LittleWeasel::Checker.instance.exists?('How dy', {strip_whitespace:true}) # false, strip_whitespace only removes leading and trailing spaces
|
55
|
+
#
|
56
|
+
# LittleWeasel::Checker.instance.exists?('90210') # true (default options, ignore_numeric => true)
|
57
|
+
# LittleWeasel::Checker.instance.exists?('90210', {ignore_numeric:false}) # false
|
58
|
+
#
|
59
|
+
# LittleWeasel::Checker.instance.exists?('Hello World') # true, we're accepting multiple words now :)}
|
60
|
+
# LittleWeasel::Checker.instance.exists?("hello, mister; did I \'mention\'' that lemon cake is \"great?\" It's just wonderful!") # true
|
49
61
|
#
|
50
62
|
def exists?(word, options=nil)
|
51
63
|
options = options || @options
|
@@ -55,10 +67,11 @@ module LittleWeasel
|
|
55
67
|
word.strip! if options[:strip_whitespace]
|
56
68
|
|
57
69
|
return false if word.empty?
|
58
|
-
|
70
|
+
return block_exists? word if block?(word)
|
71
|
+
return true if options[:ignore_numeric] && number?(word)
|
59
72
|
return false if options[:exclude_alphabet] && word.length == 1 && @alphabet_exclusion_list.include?(word.upcase)
|
60
73
|
|
61
|
-
|
74
|
+
valid_word? word
|
62
75
|
end
|
63
76
|
|
64
77
|
# Sets the global options for this gem.
|
@@ -86,7 +99,12 @@ module LittleWeasel
|
|
86
99
|
#
|
87
100
|
# LittleWeasel::Checker.instance.options({strip_whitespace:true})
|
88
101
|
# LittleWeasel::Checker.instance.exists?(' Yes ') # true
|
89
|
-
# LittleWeasel::Checker.instance.exists?('
|
102
|
+
# LittleWeasel::Checker.instance.exists?('How dy') # false, strip_whitespace only removes leading and trailing spaces
|
103
|
+
#
|
104
|
+
# LittleWeasel::Checker.instance.exists?('90210') # true (default options, ignore_numeric => true)
|
105
|
+
# LittleWeasel::Checker.instance.exists?('90210', {ignore_numeric:false}) # false
|
106
|
+
# LittleWeasel::Checker.instance.exists?('I watch Beverly Hills 90210') # true (default options, ignore_numeric => true)
|
107
|
+
# LittleWeasel::Checker.instance.exists?('I watch Beverly Hills 90210', {ignore_numeric:false}) # false
|
90
108
|
#
|
91
109
|
def options=(options)
|
92
110
|
@options = options
|
@@ -99,6 +117,43 @@ module LittleWeasel
|
|
99
117
|
@options
|
100
118
|
end
|
101
119
|
|
120
|
+
protected
|
121
|
+
|
122
|
+
def number?(word)
|
123
|
+
word.strip.gsub(@numeric_regex).count > 0
|
124
|
+
end
|
125
|
+
|
126
|
+
def block?(string)
|
127
|
+
tmp = string
|
128
|
+
return false unless tmp.is_a?(String)
|
129
|
+
tmp.gsub!(@numeric_regex, "")#.squeeze(" ").strip
|
130
|
+
return false unless tmp.length > 1
|
131
|
+
tmp.strip.scan(/[\w'-]+/).length > 1
|
132
|
+
end
|
133
|
+
|
134
|
+
def block_exists?(block)
|
135
|
+
tmp = block
|
136
|
+
tmp.gsub!(@numeric_regex, "").squeeze(" ").strip if options[:ignore_numeric]
|
137
|
+
tmp.gsub!(@non_wordchar_regex, " ")
|
138
|
+
tmp.split(@word_regex).uniq.each { |word|
|
139
|
+
return false unless valid_block_word?(word)
|
140
|
+
}
|
141
|
+
return true
|
142
|
+
end
|
143
|
+
|
144
|
+
def valid_word?(word)
|
145
|
+
tmp = word.downcase
|
146
|
+
exists = dictionary.has_key?(tmp)
|
147
|
+
exists = dictionary.has_key?(tmp.singularize) unless exists
|
148
|
+
puts "=> exists?(#{word}=>#{tmp}) # => #{exists}"
|
149
|
+
exists
|
150
|
+
end
|
151
|
+
|
152
|
+
def valid_block_word?(word)
|
153
|
+
return true if word.length == 1
|
154
|
+
valid_word? word.strip
|
155
|
+
end
|
156
|
+
|
102
157
|
private
|
103
158
|
|
104
159
|
def dictionary_path
|
@@ -110,6 +165,7 @@ module LittleWeasel
|
|
110
165
|
io.each { |line| line.chomp!; @dictionary[line] = line }
|
111
166
|
end
|
112
167
|
end
|
168
|
+
|
113
169
|
end
|
114
170
|
|
115
171
|
end
|
@@ -93,13 +93,29 @@ describe 'LittleWeasel Inline Options Tests' do
|
|
93
93
|
words.each { |word| @spell.exists?(word).should == true }
|
94
94
|
end
|
95
95
|
|
96
|
+
it 'should return true if option ignore_numeric is true, and word is a number' do
|
97
|
+
@spell.options = {ignore_numeric: true}
|
98
|
+
@spell.exists?('1').should == true
|
99
|
+
@spell.exists?('-1').should == true
|
100
|
+
@spell.exists?('1.0').should == true
|
101
|
+
@spell.exists?('-1.0').should == true
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'should return false if option ignore_numeric is false, and word is a number' do
|
105
|
+
@spell.options = {ignore_numeric: false}
|
106
|
+
@spell.exists?('1').should == false
|
107
|
+
@spell.exists?('-1').should == false
|
108
|
+
@spell.exists?('1.0').should == false
|
109
|
+
@spell.exists?('-1.0').should == false
|
110
|
+
end
|
111
|
+
|
96
112
|
end
|
97
113
|
|
98
114
|
describe 'LittleWeasel Global Options Tests' do
|
99
115
|
|
100
|
-
before (:
|
116
|
+
before (:each) do
|
101
117
|
@spell = LittleWeasel::Checker.instance
|
102
|
-
@spell.options = {exclude_alphabet: true, strip_whitespace: true}
|
118
|
+
@spell.options = {exclude_alphabet: true, strip_whitespace: true, ignore_numeric: true}
|
103
119
|
end
|
104
120
|
|
105
121
|
it 'should create a LittleWeasel object' do
|
@@ -133,6 +149,9 @@ describe 'LittleWeasel Global Options Tests' do
|
|
133
149
|
|
134
150
|
@spell.exists?(' h ', {exclude_alphabet: false, strip_whitespace: true}).should == true
|
135
151
|
@spell.exists?(' h ').should == false
|
152
|
+
|
153
|
+
@spell.exists?('1', {ignore_numeric: false}).should == false
|
154
|
+
@spell.exists?('1').should == true
|
136
155
|
end
|
137
156
|
|
138
157
|
it 'should return true for valid word with leading spaces' do
|
@@ -147,4 +166,72 @@ describe 'LittleWeasel Global Options Tests' do
|
|
147
166
|
@spell.exists?(' apple ').should == true
|
148
167
|
end
|
149
168
|
|
169
|
+
it 'should return true if word is a number' do
|
170
|
+
@spell.exists?('1').should == true
|
171
|
+
@spell.exists?('-1').should == true
|
172
|
+
@spell.exists?('1.0').should == true
|
173
|
+
@spell.exists?('-1.0').should == true
|
174
|
+
end
|
175
|
+
|
176
|
+
it 'should return false if word is a number' do
|
177
|
+
@spell.options = {ignore_numeric: false}
|
178
|
+
@spell.exists?('1').should == false
|
179
|
+
@spell.exists?('-1').should == false
|
180
|
+
@spell.exists?('1.0').should == false
|
181
|
+
@spell.exists?('-1.0').should == false
|
182
|
+
end
|
183
|
+
|
184
|
+
end
|
185
|
+
|
186
|
+
describe 'LittleWeasel Block Tests' do
|
187
|
+
|
188
|
+
before (:all) do
|
189
|
+
@spell = LittleWeasel::Checker.instance
|
190
|
+
@spell.options = {exclude_alphabet: true, strip_whitespace: true}
|
191
|
+
end
|
192
|
+
|
193
|
+
it 'should return true for valid words' do
|
194
|
+
@spell.exists?('apple sauce').should == true
|
195
|
+
end
|
196
|
+
|
197
|
+
it 'should return false for invalid words' do
|
198
|
+
@spell.exists?('appel sauce').should == false
|
199
|
+
end
|
200
|
+
|
201
|
+
it 'should return true for valid words with leading spaces' do
|
202
|
+
@spell.exists?(' apple sauce').should == true
|
203
|
+
end
|
204
|
+
|
205
|
+
it 'should return true for valid words with trailing spaces' do
|
206
|
+
@spell.exists?('apple sauce ').should == true
|
207
|
+
end
|
208
|
+
|
209
|
+
it 'should return true for valid words with leading and trailing spaces' do
|
210
|
+
@spell.exists?(' apple sauce ').should == true
|
211
|
+
end
|
212
|
+
|
213
|
+
it 'should return true for valid words split across \n and \r\n' do
|
214
|
+
@spell.exists?("apple\r\n\n\nsauce is great\r\n").should == true
|
215
|
+
end
|
216
|
+
|
217
|
+
it 'should return true for valid words separated by \t' do
|
218
|
+
@spell.exists?("\tapple\t\t\tsauce is great\t").should == true
|
219
|
+
end
|
220
|
+
|
221
|
+
it 'should return true for valid words that are repeated' do
|
222
|
+
@spell.exists?(' are you bad are you bad are you bad ').should == true
|
223
|
+
end
|
224
|
+
|
225
|
+
it 'should return true for valid words separated by non-alpha chars' do
|
226
|
+
@spell.exists?("hello, mister; did I \'mention\'' that lemon cake is \"great?\" It's just wonderful!").should == true
|
227
|
+
end
|
228
|
+
|
229
|
+
it 'should return true for valid pluralized words' do
|
230
|
+
@spell.exists?('Apples and peaches are great! Some people are good, each person is a people.').should == true
|
231
|
+
end
|
232
|
+
|
233
|
+
it 'should return true for valid words' do
|
234
|
+
@spell.exists?('Hell o').should == true
|
235
|
+
end
|
236
|
+
|
150
237
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: LittleWeasel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2014-06-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|