wordfinder 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +35 -0
- data/Rakefile +2 -0
- data/lib/word_finder.rb +60 -0
- data/lib/word_finder/version.rb +10 -0
- data/lib/wordfinder.rb +8 -0
- data/spec/spec_helper.rb +5 -0
- data/spec/wordfinder_spec.rb +71 -0
- data/wordfinder.gemspec +21 -0
- metadata +108 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 d11wtq
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# WordFinder
|
2
|
+
|
3
|
+
A high-level wrapper around libaspell, to find words in an ordered stream of
|
4
|
+
characters.
|
5
|
+
|
6
|
+
## Requirements & Installation
|
7
|
+
|
8
|
+
This depends on libaspell, via [ffi-aspell](https://github.com/YorickPeterse/ffi-aspell).
|
9
|
+
In order to install it, you'll need to make sure you have aspell installed, with
|
10
|
+
the development package.
|
11
|
+
|
12
|
+
On Ubuntu:
|
13
|
+
|
14
|
+
sudo apt-get install libaspell-dev
|
15
|
+
gem install wordfinder
|
16
|
+
|
17
|
+
You may also add a line to your Gemfile:
|
18
|
+
|
19
|
+
gem "wordfinder"
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
There is only one method in WordFinder.
|
24
|
+
|
25
|
+
``` ruby
|
26
|
+
require "wordfinder"
|
27
|
+
|
28
|
+
WordFinder.words_in("threelittlepigswenttomarket")
|
29
|
+
#=> ["three", "little", "pigs", "went", "to", "market"]
|
30
|
+
```
|
31
|
+
|
32
|
+
## Copyright & Licensing
|
33
|
+
|
34
|
+
Copyright © 2012 Chris Corbyn
|
35
|
+
Refer to the LICENSE file for details.
|
data/Rakefile
ADDED
data/lib/word_finder.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
##
|
2
|
+
# WordFinder.
|
3
|
+
# Copyright © 2012 Chris Corbyn.
|
4
|
+
#
|
5
|
+
# See LICENSE file for details.
|
6
|
+
##
|
7
|
+
|
8
|
+
require "ffi"
|
9
|
+
require "ffi/aspell"
|
10
|
+
require "word_finder/version"
|
11
|
+
|
12
|
+
module WordFinder
|
13
|
+
class << self
|
14
|
+
# Find all the words in the given phrase, ignoring unrecognized characters.
|
15
|
+
#
|
16
|
+
# Words will be accumulated to use the longest possible matches.
|
17
|
+
#
|
18
|
+
# @param [String] phrase
|
19
|
+
# a sentence, which need not contain whitespace or punctuation
|
20
|
+
#
|
21
|
+
# @option [String] :lang
|
22
|
+
# the language of the aspell dictionary (default "en_US")
|
23
|
+
#
|
24
|
+
# @return [Array<String>]
|
25
|
+
# an Array containing all words found, in order
|
26
|
+
def words_in(phrase, options = {})
|
27
|
+
speller = FFI::Aspell::Speller.new(options[:lang] || "en_US")
|
28
|
+
|
29
|
+
matched = []
|
30
|
+
unmatched = ""
|
31
|
+
|
32
|
+
phrase.each_char do |c|
|
33
|
+
unmatched << c
|
34
|
+
|
35
|
+
tmp, matched = matched, []
|
36
|
+
|
37
|
+
if tmp.empty?
|
38
|
+
if speller.correct?(unmatched)
|
39
|
+
matched.push(unmatched)
|
40
|
+
unmatched = ""
|
41
|
+
end
|
42
|
+
else
|
43
|
+
(0..tmp.length).each do |i|
|
44
|
+
word = tmp[i..-1].join + unmatched
|
45
|
+
|
46
|
+
if speller.correct?(word)
|
47
|
+
matched.push(word)
|
48
|
+
unmatched = ""
|
49
|
+
break
|
50
|
+
elsif tmp[i]
|
51
|
+
matched << tmp[i]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
matched.reject{|m| m !~ /\w/} # remove punctuation marks
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/lib/wordfinder.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe WordFinder do
|
4
|
+
let(:words) { WordFinder.words_in(phrase) }
|
5
|
+
|
6
|
+
describe ".words_in" do
|
7
|
+
context "with an unstemmable word" do
|
8
|
+
let(:phrase) { "run" }
|
9
|
+
|
10
|
+
it "returns an Array containing the word" do
|
11
|
+
words.should == %w[run]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context "with a stemmable word" do
|
16
|
+
let(:phrase) { "running" }
|
17
|
+
|
18
|
+
it "returns an Array containing the unstemmed word" do
|
19
|
+
words.should == %w[running]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context "with multiple unstemmable words" do
|
24
|
+
let(:phrase) { "runfast" }
|
25
|
+
|
26
|
+
it "returns an Array containing the words" do
|
27
|
+
words.should == %w[run fast]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context "with multiple stemmable words" do
|
32
|
+
let(:phrase) { "runningfaster" }
|
33
|
+
|
34
|
+
it "returns an Array containing the words" do
|
35
|
+
words.should == %w[running faster]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context "with a word ending in junk" do
|
40
|
+
let(:phrase) { "runfh" }
|
41
|
+
|
42
|
+
it "returns an Array containing the valid word" do
|
43
|
+
words.should == %w[run f h]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context "with a word staring in junk" do
|
48
|
+
let(:phrase) { "qlrun" }
|
49
|
+
|
50
|
+
it "returns an Array containing the valid word" do
|
51
|
+
words.should == %w[q l run]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context "with word separated by junk" do
|
56
|
+
let(:phrase) { "gracefulqlrun" }
|
57
|
+
|
58
|
+
it "returns an Array containing the valid words" do
|
59
|
+
words.should == %w[graceful q l run]
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context "with words separated by punctuation" do
|
64
|
+
let(:phrase) { "I need it, now!" }
|
65
|
+
|
66
|
+
it "returns an Array containing all words without punctuation" do
|
67
|
+
words.should == %w[I need it now]
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/wordfinder.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path("../lib/word_finder/version", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["d11wtq"]
|
6
|
+
gem.email = ["chris@w3style.co.uk"]
|
7
|
+
gem.description = "Finds words in a stream of characters"
|
8
|
+
gem.summary = "Finds words in a stream of characters"
|
9
|
+
gem.homepage = "https://github.com/d11wtq/wordfinder"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "wordfinder"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = WordFinder::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency "rspec"
|
19
|
+
gem.add_runtime_dependency "ffi"
|
20
|
+
gem.add_runtime_dependency "ffi-aspell"
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wordfinder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- d11wtq
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: ffi
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: ffi-aspell
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: Finds words in a stream of characters
|
63
|
+
email:
|
64
|
+
- chris@w3style.co.uk
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- .rspec
|
71
|
+
- Gemfile
|
72
|
+
- LICENSE
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- lib/word_finder.rb
|
76
|
+
- lib/word_finder/version.rb
|
77
|
+
- lib/wordfinder.rb
|
78
|
+
- spec/spec_helper.rb
|
79
|
+
- spec/wordfinder_spec.rb
|
80
|
+
- wordfinder.gemspec
|
81
|
+
homepage: https://github.com/d11wtq/wordfinder
|
82
|
+
licenses: []
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ! '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 1.8.24
|
102
|
+
signing_key:
|
103
|
+
specification_version: 3
|
104
|
+
summary: Finds words in a stream of characters
|
105
|
+
test_files:
|
106
|
+
- spec/spec_helper.rb
|
107
|
+
- spec/wordfinder_spec.rb
|
108
|
+
has_rdoc:
|