textquery 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -9,6 +9,9 @@ Textquery is a simple PEG grammar with support for:
9
9
  - OR
10
10
  - NOT (- is an alias)
11
11
  - 'quoted strings'
12
+ - fuzzy matching
13
+ - case (in)sensitive
14
+ - custom delimeters
12
15
 
13
16
  == Example
14
17
 
@@ -25,6 +28,14 @@ Textquery is a simple PEG grammar with support for:
25
28
  q.match?("b") # => false
26
29
  q.match?("a b cdefg") # => true
27
30
 
31
+ TextQuery.new("a~").match?("adf") # => true
32
+ TextQuery.new("~a").match?("dfa") # => true
33
+ TextQuery.new("~a~").match?("daf") # => true
34
+ TextQuery.new("2~a~1").match?("edaf") # => true
35
+ TextQuery.new("2~a~2").match?("edaf") # => false
36
+
37
+ TextQuery.new("a", :ignorecase => true).match?("A b cD") # => true
38
+
28
39
  == License
29
40
 
30
41
  (The MIT License)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
@@ -8,7 +8,25 @@ end
8
8
 
9
9
  class WordMatch < Treetop::Runtime::SyntaxNode
10
10
  def eval(text, opt)
11
- not text.match("^#{query}#{opt[:delim]}|#{opt[:delim]}#{query}#{opt[:delim]}|#{opt[:delim]}#{query}$|^#{query}$").nil?
11
+ fuzzy = query.match(/(\d)*(~)?([^~]+)(~)?(\d)*$/)
12
+
13
+ q = []
14
+ q.push "." if fuzzy[2]
15
+ q.push fuzzy[1].nil? ? "*" : "{#{fuzzy[1]}}" if fuzzy[2]
16
+ q.push fuzzy[3]
17
+ q.push "." if fuzzy[4]
18
+ q.push fuzzy[5].nil? ? "*" : "{#{fuzzy[5]}}" if fuzzy[4]
19
+ q = q.join
20
+
21
+ regex = "^#{q}#{opt[:delim]}|#{opt[:delim]}#{q}#{opt[:delim]}|#{opt[:delim]}#{q}$|^#{q}$"
22
+
23
+ if opt[:ignorecase]
24
+ regex = Regexp.new(regex, Regexp::IGNORECASE, 'utf8')
25
+ else
26
+ regex = Regexp.new(regex, nil, 'utf8')
27
+ end
28
+
29
+ not text.match(regex).nil?
12
30
  end
13
31
 
14
32
  def query
@@ -160,14 +160,43 @@ describe TextQuery do
160
160
  q.eval("a b cdefg").should be_true
161
161
  end
162
162
 
163
+ it "should support fuzzy matching" do
164
+ parse("a~").eval("adf").should be_true
165
+ parse("~a").eval("dfa").should be_true
166
+ parse("~a~").eval("daf").should be_true
167
+
168
+ parse("1~a~1").eval("daf").should be_true
169
+ parse("2~a~1").eval("daf").should be_false
170
+ parse("1~a~2").eval("daf").should be_false
171
+
172
+ parse("~a~3").eval("daffy").should be_true
173
+ parse("a~1").eval("adf").should be_false
174
+
175
+ parse("a~1 AND b").eval("adf b").should be_false
176
+ parse("a~2 AND b").eval("adf b").should be_true
177
+ parse("a~3 AND b").eval("adf b").should be_false
178
+ end
179
+
163
180
  it "should work on CJK text" do
164
- JP = "に入れるわけにはいかないので、プラグインの出力が同一であることでもって同一性を判定する"
181
+ JP = "仕様変更は出し尽くしてしまいß"
165
182
 
166
- q = TextQuery.new("に入".mb_chars, :delim => '')
183
+ q = TextQuery.new("変更", :delim => '')
167
184
  q.eval(JP).should be_true
168
- q.eval("けにはい").should be_false
185
+ q.eval("変ま").should be_false
186
+ q.parse("は出").eval(JP).should be_true
187
+
188
+ q = TextQuery.new
189
+ q.parse("~出~").eval(JP).should be_true
190
+ q.parse("~出~ AND NOT ~尽~").eval(JP).should be_false
191
+ q.parse("~更は出~ OR ~尽く~").eval(JP).should be_true
192
+ end
193
+
194
+ it "should be case insensitive" do
195
+ TextQuery.new("a", :ignorecase => true).match?("A b cD").should be_true
196
+ TextQuery.new("a AND CD", :ignorecase => true).match?("A b cD").should be_true
169
197
 
170
- q.parse("れるわ AND が同".mb_chars).eval(JP).should be_true
171
- q.parse("れるわ AND NOT す".mb_chars).eval(JP).should be_false
198
+ TextQuery.new("a", :ignorecase => false).match?("A b cD").should be_false
199
+ TextQuery.new("a AND CD", :ignorecase => false).match?("A b cD").should be_false
172
200
  end
201
+
173
202
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: textquery
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ilya Grigorik
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-29 00:00:00 -05:00
12
+ date: 2009-12-30 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency