word 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +17 -0
- data/MIT-LICENSE +21 -0
- data/README.rdoc +20 -0
- data/Rakefile +25 -0
- data/lib/word/extract.rb +10 -0
- data/lib/word/match.rb +29 -0
- data/lib/word/regexp.rb +24 -0
- data/lib/word/version.rb +3 -0
- data/lib/word/wildcard.rb +3 -0
- data/lib/word.rb +8 -0
- data/spec/spec_helper.rb +4 -0
- data/spec/word/extract_spec.rb +23 -0
- data/spec/word/match_spec.rb +61 -0
- data/spec/word/regexp_spec.rb +103 -0
- data/word.gemspec +25 -0
- metadata +92 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2011 Mohammad Satrio http://tyok.org/
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
data/README.rdoc
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
== Word
|
2
|
+
Extends String with word matching capabilities
|
3
|
+
|
4
|
+
== Installation
|
5
|
+
Install the gem:
|
6
|
+
gem install word
|
7
|
+
Add it to your Gemfile:
|
8
|
+
gem "word"
|
9
|
+
|
10
|
+
== Usage
|
11
|
+
You can use `has_none?`, `has_any?`, `has_all?` to check if a string contain some word
|
12
|
+
"penguin guy".has_any? "bear" # false
|
13
|
+
"penguin guy".has_any? "bear", "penguin" # true
|
14
|
+
|
15
|
+
You can also use array of string as input
|
16
|
+
"penguin guy".has_none? ["bear", "penguin"] # false
|
17
|
+
"penguin guy".has_all? ["guy", "penguin"] # true
|
18
|
+
|
19
|
+
== License
|
20
|
+
MIT License. Copyright 2011 Mohammad Satrio.
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "bundler"
|
4
|
+
Bundler::GemHelper.install_tasks
|
5
|
+
|
6
|
+
require "rake"
|
7
|
+
require "rake/testtask"
|
8
|
+
Rake::TestTask.new(:test) do |test|
|
9
|
+
test.libs << "lib"
|
10
|
+
test.libs << "spec"
|
11
|
+
test.pattern = "spec/**/*_spec.rb"
|
12
|
+
test.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
task :default => :test
|
16
|
+
|
17
|
+
require "rake/rdoctask"
|
18
|
+
Rake::RDocTask.new do |rdoc|
|
19
|
+
require "word/version"
|
20
|
+
|
21
|
+
rdoc.rdoc_dir = "rdoc"
|
22
|
+
rdoc.title = "Word #{Word::VERSION}"
|
23
|
+
rdoc.rdoc_files.include("README*")
|
24
|
+
rdoc.rdoc_files.include("lib/**/*.rb")
|
25
|
+
end
|
data/lib/word/extract.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require File.expand_path("../wildcard", __FILE__)
|
2
|
+
module Word
|
3
|
+
module Extract
|
4
|
+
# "a b 'c d'" => ["a", "b", "\"c d\""]
|
5
|
+
def extract_words(wildcard = Word::WILDCARD)
|
6
|
+
word = /".*?"|'.*?'|#{wildcard}?\w+#{wildcard}?/ # "word1 word2" | 'word1 word2' | word
|
7
|
+
scan(word).flatten.map{|x| x.gsub(/"|'/, '"')}.uniq
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
data/lib/word/match.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require File.expand_path("../regexp", __FILE__)
|
2
|
+
module Word
|
3
|
+
module Match
|
4
|
+
include Regexp
|
5
|
+
|
6
|
+
def has_none?(*args)
|
7
|
+
words = args.first.respond_to?(:first) ? args.first : args
|
8
|
+
return nil == (self =~ Regexp.has_any_regexp(words))
|
9
|
+
end
|
10
|
+
|
11
|
+
def has_any?(*args)
|
12
|
+
words = args.first.respond_to?(:first) ? args.first : args
|
13
|
+
return nil != (self =~ Regexp.has_any_regexp(words))
|
14
|
+
end
|
15
|
+
|
16
|
+
def has_all?(*args)
|
17
|
+
words = args.first.respond_to?(:first) ? args.first : args
|
18
|
+
matching = match(Regexp.has_any_regexp(words)).to_s.downcase
|
19
|
+
return false if matching.empty?
|
20
|
+
remain = words - [matching]
|
21
|
+
if remain.empty?
|
22
|
+
true
|
23
|
+
else
|
24
|
+
has_all?(remain)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
data/lib/word/regexp.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require File.expand_path("../wildcard", __FILE__)
|
2
|
+
module Word
|
3
|
+
module Regexp
|
4
|
+
|
5
|
+
def word_regexp(wildcard = Word::WILDCARD, options = true)
|
6
|
+
wildcard = wildcard || Word::WILDCARD
|
7
|
+
prefixed = /^(#{wildcard})/
|
8
|
+
suffixed = /(#{wildcard})$/
|
9
|
+
|
10
|
+
prefix = (wildcard and self =~ prefixed) ? "" : "\\b"
|
11
|
+
suffix = (wildcard and self =~ suffixed) ? "" : "\\b"
|
12
|
+
|
13
|
+
w = self.gsub(/#{prefixed}|#{suffixed}/, "")
|
14
|
+
w = prefix + ::Regexp.escape(w) + suffix
|
15
|
+
|
16
|
+
::Regexp.new(w, options)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.has_any_regexp(words, wildcard = Word::WILDCARD, options = true)
|
20
|
+
/#{words.map{|w| w.word_regexp(wildcard, options)}.join('|')}/
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
data/lib/word/version.rb
ADDED
data/lib/word.rb
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
require File.expand_path("../word/extract", __FILE__)
|
2
|
+
require File.expand_path("../word/match", __FILE__)
|
3
|
+
require File.expand_path("../word/regexp", __FILE__)
|
4
|
+
require File.expand_path("../word/version", __FILE__)
|
5
|
+
require File.expand_path("../word/wildcard", __FILE__)
|
6
|
+
|
7
|
+
String.send :include, Word::Match
|
8
|
+
String.send :include, Word::Extract
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "word/extract"
|
3
|
+
|
4
|
+
String.send :include, Word::Extract
|
5
|
+
|
6
|
+
describe Word::Extract do
|
7
|
+
|
8
|
+
describe "extract_words" do
|
9
|
+
it "returns an array" do
|
10
|
+
"penguin guy".extract_words.must_be_kind_of Array
|
11
|
+
end
|
12
|
+
|
13
|
+
it "extracts words from a string" do
|
14
|
+
"penguin guy".extract_words.must_equal %w[ penguin guy ]
|
15
|
+
"PenguIn guy".extract_words.must_equal %w[ PenguIn guy ]
|
16
|
+
end
|
17
|
+
|
18
|
+
it "treats a pair of quote and anything between as a word" do
|
19
|
+
"'awesome penguin' guy".extract_words.must_equal %w[ "awesome\ penguin" guy ]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "word/match"
|
3
|
+
|
4
|
+
String.send :include, Word::Match
|
5
|
+
|
6
|
+
describe Word::Match do
|
7
|
+
|
8
|
+
describe "has_none?" do
|
9
|
+
it "returns true if none of its arguments is in the string" do
|
10
|
+
"penguin guy".has_none?( "bear" ).must_equal true
|
11
|
+
"penguin guy".has_none?(["bear"]).must_equal true
|
12
|
+
end
|
13
|
+
|
14
|
+
it "returns false if any of its arguments is in the string" do
|
15
|
+
"penguin guy".has_none?( "bear", "guy" ).must_equal false
|
16
|
+
"penguin guy".has_none?(["bear", "guy"]).must_equal false
|
17
|
+
end
|
18
|
+
|
19
|
+
it "returns false if all of its arguments is in the string" do
|
20
|
+
"penguin guy".has_none?( "penguin", "guy" ).must_equal false
|
21
|
+
"penguin guy".has_none?(["penguin", "guy"]).must_equal false
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "has_any?" do
|
26
|
+
it "returns false if none of its arguments is in the string" do
|
27
|
+
"penguin guy".has_any?( "bug", "bear" ).must_equal false
|
28
|
+
"penguin guy".has_any?(["bug", "bear"]).must_equal false
|
29
|
+
end
|
30
|
+
|
31
|
+
it "returns true if any of its arguments is in the string" do
|
32
|
+
"penguin guy".has_any?( "penguin", "lover" ).must_equal true
|
33
|
+
"penguin guy".has_any?(["penguin", "lover"]).must_equal true
|
34
|
+
end
|
35
|
+
|
36
|
+
it "returns true if all of its arguments is in the string" do
|
37
|
+
"penguin guy".has_any?( "penguin", "guy" ).must_equal true
|
38
|
+
"penguin guy".has_any?(["penguin", "guy"]).must_equal true
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "has_all?" do
|
43
|
+
it "returns false if none of its arguments is in the string" do
|
44
|
+
"penguin guy".has_all?( "bug", "bear" ).must_equal false
|
45
|
+
"penguin guy".has_all?(["bug", "bear"]).must_equal false
|
46
|
+
end
|
47
|
+
|
48
|
+
it "returns false if any of its arguments is in the string" do
|
49
|
+
"penguin guy".has_all?( "penguin", "lover" ).must_equal false
|
50
|
+
"penguin guy".has_all?(["penguin", "lover"]).must_equal false
|
51
|
+
end
|
52
|
+
|
53
|
+
it "returns true if all of its arguments is in the string" do
|
54
|
+
"penguin guy".has_all?( "penguin", "guy" ).must_equal true
|
55
|
+
"penguin guy".has_all?(["penguin", "guy"]).must_equal true
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "word/regexp"
|
3
|
+
|
4
|
+
String.send :include, Word::Regexp
|
5
|
+
|
6
|
+
describe Word::Regexp do
|
7
|
+
|
8
|
+
describe "word_regexp" do
|
9
|
+
before :each do
|
10
|
+
@word = "penguin"
|
11
|
+
@normal_matches = ["penguin", "Ima penguin!", "Me da'penguin"]
|
12
|
+
@insensitive_matches = ["PenGuin", "Ima PengUin!", "Me da'PENGUIN"]
|
13
|
+
@normal_not_matches = ["kitten", "dog", "pengu", "guin"]
|
14
|
+
@suffixed_matches = ["real penguinterminator", "penguinhuh, yeah"]
|
15
|
+
@prefixed_matches = ["I'm dapenguin y'know?"]
|
16
|
+
@wildcard_matches = ["oh my supercalifragilisticexpenguindociou OMIGOD!!!"]
|
17
|
+
end
|
18
|
+
|
19
|
+
it "returns a valid regexp" do
|
20
|
+
@word.word_regexp.must_be_kind_of ::Regexp
|
21
|
+
end
|
22
|
+
|
23
|
+
it "returns a regexp to match a word" do
|
24
|
+
penguin = @word.word_regexp
|
25
|
+
(@normal_matches + @insensitive_matches).each do |string|
|
26
|
+
penguin.must_match string
|
27
|
+
end
|
28
|
+
(@normal_not_matches + @suffixed_matches).each do |string|
|
29
|
+
penguin.wont_match string
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
it "accepts prefix wildcard" do
|
34
|
+
penguin_pref = "*#{@word}".word_regexp nil
|
35
|
+
(@normal_matches + @insensitive_matches + @prefixed_matches).each do |string|
|
36
|
+
penguin_pref.must_match string
|
37
|
+
end
|
38
|
+
(@normal_not_matches + @suffixed_matches).each do |string|
|
39
|
+
penguin_pref.wont_match string
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
it "accepts sufix wildcard" do
|
44
|
+
penguin_suf = "#{@word}*".word_regexp nil
|
45
|
+
(@normal_matches + @insensitive_matches + @suffixed_matches).each do |string|
|
46
|
+
penguin_suf.must_match string
|
47
|
+
end
|
48
|
+
(@normal_not_matches + @prefixed_matches).each do |string|
|
49
|
+
penguin_suf.wont_match string
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
it "accepts prefix and sufix wildcard" do
|
54
|
+
penguin_wild = "*#{@word}*".word_regexp nil
|
55
|
+
(@normal_matches + @insensitive_matches + @suffixed_matches + @prefixed_matches).each do |string|
|
56
|
+
penguin_wild.must_match string
|
57
|
+
end
|
58
|
+
(@normal_not_matches).each do |string|
|
59
|
+
penguin_wild.wont_match string
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
it "accepts custom wildcard" do
|
64
|
+
penguin_wild = "%#{@word}%".word_regexp /%/
|
65
|
+
(@normal_matches + @insensitive_matches + @suffixed_matches + @prefixed_matches).each do |string|
|
66
|
+
penguin_wild.must_match string
|
67
|
+
end
|
68
|
+
(@normal_not_matches).each do |string|
|
69
|
+
penguin_wild.wont_match string
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
it "accepts regexp options" do
|
74
|
+
penguin = @word.word_regexp nil, nil
|
75
|
+
(@insensitive_matches).each do |string|
|
76
|
+
penguin.wont_match string
|
77
|
+
end
|
78
|
+
|
79
|
+
penguin_wild = "*#{@word}*".word_regexp nil, ::Regexp::IGNORECASE
|
80
|
+
(@normal_matches + @insensitive_matches + @suffixed_matches + @prefixed_matches).each do |string|
|
81
|
+
penguin_wild.must_match string
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
describe "self.has_any_regexp" do
|
87
|
+
before :each do
|
88
|
+
@penguin_or_guy = Word::Regexp.has_any_regexp(%w[ penguin guy])
|
89
|
+
end
|
90
|
+
|
91
|
+
it "returns a Regexp" do
|
92
|
+
@penguin_or_guy.must_be_kind_of ::Regexp
|
93
|
+
end
|
94
|
+
|
95
|
+
it "matches any of the words provided" do
|
96
|
+
@penguin_or_guy.must_match "a quick brown penguin jumps over lazy polar bear"
|
97
|
+
@penguin_or_guy.must_match "a quick brown guy jumps over lazy polar bear"
|
98
|
+
@penguin_or_guy.must_match "a quick brown guy jumps over lazy penguin"
|
99
|
+
@penguin_or_guy.wont_match "a quick brown fox jumps over lazy dog"
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
data/word.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "word/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "word"
|
7
|
+
s.version = Word::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Mohammad Satrio"]
|
10
|
+
s.email = ["wolfaeon@gmail.com"]
|
11
|
+
s.homepage = "https://github.com/tyok/word"
|
12
|
+
s.summary = "Extends String with word matching capabilities"
|
13
|
+
s.description = "Extends String with word matching capabilities"
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.extra_rdoc_files = ["README.rdoc"]
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.licenses = ["MIT"]
|
21
|
+
|
22
|
+
s.add_development_dependency "bundler", [">= 1.0.0"]
|
23
|
+
s.add_development_dependency "minitest", [">= 0"]
|
24
|
+
end
|
25
|
+
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: word
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: "0.1"
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Mohammad Satrio
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-05-05 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: bundler
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.0.0
|
24
|
+
type: :development
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: minitest
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: "0"
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id002
|
37
|
+
description: Extends String with word matching capabilities
|
38
|
+
email:
|
39
|
+
- wolfaeon@gmail.com
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files:
|
45
|
+
- README.rdoc
|
46
|
+
files:
|
47
|
+
- .gitignore
|
48
|
+
- Gemfile
|
49
|
+
- Gemfile.lock
|
50
|
+
- MIT-LICENSE
|
51
|
+
- README.rdoc
|
52
|
+
- Rakefile
|
53
|
+
- lib/word.rb
|
54
|
+
- lib/word/extract.rb
|
55
|
+
- lib/word/match.rb
|
56
|
+
- lib/word/regexp.rb
|
57
|
+
- lib/word/version.rb
|
58
|
+
- lib/word/wildcard.rb
|
59
|
+
- spec/spec_helper.rb
|
60
|
+
- spec/word/extract_spec.rb
|
61
|
+
- spec/word/match_spec.rb
|
62
|
+
- spec/word/regexp_spec.rb
|
63
|
+
- word.gemspec
|
64
|
+
homepage: https://github.com/tyok/word
|
65
|
+
licenses:
|
66
|
+
- MIT
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: "0"
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: "0"
|
84
|
+
requirements: []
|
85
|
+
|
86
|
+
rubyforge_project:
|
87
|
+
rubygems_version: 1.8.0
|
88
|
+
signing_key:
|
89
|
+
specification_version: 3
|
90
|
+
summary: Extends String with word matching capabilities
|
91
|
+
test_files: []
|
92
|
+
|