rhymes 0.0.4 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README.rdoc +4 -2
- data/Rakefile +2 -0
- data/lib/rhymes.rb +50 -30
- data/lib/rhymes/version.rb +1 -1
- data/spec/rhymes_spec.rb +14 -8
- metadata +14 -6
data/.gitignore
CHANGED
data/README.rdoc
CHANGED
@@ -26,7 +26,9 @@ as /tmp/rhymes.dat
|
|
26
26
|
=> true
|
27
27
|
ruby > Rhymes.setup
|
28
28
|
=> {:raw_dict=>"/tmp/cmudict.0.7a", :compiled=>"/tmp/rhymes.dat"}
|
29
|
-
ruby > Rhymes.setup(:compiled=>'/new/location/file')
|
29
|
+
ruby > Rhymes.setup(:compiled => '/new/location/file')
|
30
30
|
=> {:raw_dict=>"/tmp/cmudict.0.7a", :compiled=>"/new/location/file"}
|
31
|
-
|
31
|
+
ruby > Rhymes.setup{|c| c.raw_dict = '/new/raw/dict'}
|
32
|
+
=> {:raw_dict=>"/new/raw/dict", :compiled=>"/new/location/file"}
|
32
33
|
|
34
|
+
Note that after the first successful call to Rhymes.rhyme all data is in memory and setup does not affect anything
|
data/Rakefile
CHANGED
data/lib/rhymes.rb
CHANGED
@@ -1,25 +1,41 @@
|
|
1
1
|
require 'rhymes/version'
|
2
2
|
|
3
3
|
module Rhymes
|
4
|
-
|
5
|
-
|
4
|
+
# generic error
|
5
|
+
class RhymesError < RuntimeError; end
|
6
|
+
# word does not exist in the dictionary
|
7
|
+
class UnknownWord < RhymesError; end
|
8
|
+
# dictionary file cannot be found at expected location
|
9
|
+
class DictionaryMissing < RhymesError; end
|
10
|
+
|
6
11
|
class << self
|
7
12
|
##
|
8
13
|
# Sets up options.
|
9
14
|
# - :raw_dict - location of raw dictionary file. Default: /tmp/cmudict.0.7a
|
10
|
-
# - :compiled - location to store/retrieve precompiled dictionary. Default: /tmp/rhymes.dat
|
11
|
-
|
12
|
-
|
15
|
+
# - :compiled - location to store/retrieve precompiled dictionary. Default: /tmp/rhymes.dat.
|
16
|
+
# Providing location to store precompiled dictionary speeds up consequent loads
|
17
|
+
def setup(opts = {})
|
18
|
+
opts.each{ |k, v| options[k] = v }
|
19
|
+
yield options if block_given?
|
20
|
+
Hash[options.each_pair.to_a]
|
13
21
|
end
|
14
22
|
|
15
23
|
##
|
16
|
-
# Return the list of perfect and identical rhymes to provided word
|
24
|
+
# Return the list of perfect and identical rhymes to provided word (in upper case)
|
25
|
+
# raises
|
26
|
+
# Rhymes::UnknownWord - word does not exist in the dictionary
|
27
|
+
# Rhymes::DictionaryMissing - dictionary file cannot be found at expected location
|
17
28
|
def rhyme(word)
|
18
29
|
wup = word.upcase
|
19
|
-
|
30
|
+
key = words[wup]
|
31
|
+
raise UnknownWord unless key
|
32
|
+
rhymes[key] - [wup]
|
20
33
|
end
|
21
34
|
|
22
35
|
private
|
36
|
+
def options
|
37
|
+
@options ||= Struct.new(:raw_dict, :compiled).new('/tmp/cmudict.0.7a', '/tmp/rhymes.dat')
|
38
|
+
end
|
23
39
|
|
24
40
|
def words
|
25
41
|
init unless @words
|
@@ -38,31 +54,35 @@ module Rhymes
|
|
38
54
|
end
|
39
55
|
|
40
56
|
def init
|
41
|
-
if
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
57
|
+
return if load_compiled
|
58
|
+
unless File.exists?(options.raw_dict)
|
59
|
+
msg = %W[File #{options.raw_dict} does not exist. You can download the latest dictionary
|
60
|
+
from https://cmusphinx.svn.sourceforge.net/svnroot/cmusphinx/trunk/cmudict and provide
|
61
|
+
file location with Rhymes.setup(:raw_dict => file_full_path) (/tmp/cmudict.0.7a by default)] * ' '
|
62
|
+
raise DictionaryMissing.new(msg)
|
63
|
+
end
|
64
|
+
load_dictionary
|
65
|
+
end
|
66
|
+
|
67
|
+
def load_dictionary
|
68
|
+
@words, @rhymes = {}, {}
|
69
|
+
File.open(options[:raw_dict]) do |f|
|
70
|
+
while l = f.gets do
|
71
|
+
next if l =~ /^[^A-Z]/
|
72
|
+
word, *pron = l.strip.split(' ')
|
73
|
+
next if word.empty? || word =~ /\d/
|
74
|
+
pron.map!{|snd| snd.sub(/2/, '1')}
|
75
|
+
key = perfect_key(pron)
|
76
|
+
@words[word] = key
|
77
|
+
(@rhymes[key] ||= []) << word
|
60
78
|
end
|
61
|
-
else
|
62
|
-
raise "File #{@@options[:raw_dict]} does not exist. You can download latest dictionary " +
|
63
|
-
"from https://cmusphinx.svn.sourceforge.net/svnroot/cmusphinx/trunk/cmudict and provide "
|
64
|
-
"file location with Rhymes.setup(:raw_dict => file_full_path) (/tmp/cmudict.0.7a by default)"
|
65
79
|
end
|
80
|
+
File.open(options.compiled, 'wb'){|f| f.write(Marshal.dump([@words, @rhymes]))} rescue nil
|
81
|
+
end
|
82
|
+
|
83
|
+
def load_compiled
|
84
|
+
@words, @rhymes = Marshal.load(File.open(options.compiled, 'rb'){|f| f.read }) if File.exists?(options.compiled)
|
66
85
|
end
|
86
|
+
|
67
87
|
end
|
68
88
|
end
|
data/lib/rhymes/version.rb
CHANGED
data/spec/rhymes_spec.rb
CHANGED
@@ -1,16 +1,22 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Rhymes do
|
4
|
-
before
|
4
|
+
before do
|
5
5
|
@sample = File.join(File.dirname(__FILE__), 'sample_raw.txt')
|
6
|
-
Rhymes.setup(:raw_dict => @sample)
|
6
|
+
Rhymes.setup(:raw_dict => @sample, :compiled => '')
|
7
7
|
end
|
8
8
|
|
9
9
|
it 'should save provided option' do
|
10
|
-
Rhymes.setup(:compiled => '/tmp/sample').should ==
|
11
|
-
{:raw_dict => @sample, :compiled => '/tmp/sample'}
|
10
|
+
Rhymes.setup(:compiled => '/tmp/sample').should == {:raw_dict => @sample, :compiled => '/tmp/sample'}
|
12
11
|
end
|
13
|
-
|
12
|
+
|
13
|
+
it 'should save block config' do
|
14
|
+
Rhymes.setup do |config|
|
15
|
+
config.raw_dict = 'foo'
|
16
|
+
config.compiled = 'bar'
|
17
|
+
end.should == {:raw_dict => 'foo', :compiled => 'bar'}
|
18
|
+
end
|
19
|
+
|
14
20
|
it 'should rhyme ruby with newby and scooby' do
|
15
21
|
Rhymes.rhyme('ruby').should == ['NEWBY', 'SCOOBY']
|
16
22
|
Rhymes.rhyme('Scooby').should == ['NEWBY', 'RUBY']
|
@@ -32,8 +38,8 @@ describe Rhymes do
|
|
32
38
|
Rhymes.rhyme('USA').should == ['MONTERREY']
|
33
39
|
end
|
34
40
|
|
35
|
-
it 'should
|
36
|
-
Rhymes.rhyme('dabadabadaba').
|
41
|
+
it 'should raise with unknown word' do
|
42
|
+
expect {Rhymes.rhyme('dabadabadaba')}.to raise_error(Rhymes::UnknownWord)
|
37
43
|
end
|
38
44
|
|
39
|
-
|
45
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rhymes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-03-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,12 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
25
30
|
description: Lookup perfect and identical rhymes
|
26
31
|
email:
|
27
32
|
- rubify@softover.com
|
@@ -60,8 +65,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
60
65
|
version: '0'
|
61
66
|
requirements: []
|
62
67
|
rubyforge_project: rhymes
|
63
|
-
rubygems_version: 1.8.
|
68
|
+
rubygems_version: 1.8.24
|
64
69
|
signing_key:
|
65
70
|
specification_version: 3
|
66
71
|
summary: Lookup perfect and identical rhymes
|
67
|
-
test_files:
|
72
|
+
test_files:
|
73
|
+
- spec/rhymes_spec.rb
|
74
|
+
- spec/sample_raw.txt
|
75
|
+
- spec/spec_helper.rb
|