rhymes 0.0.4 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -3,3 +3,4 @@
3
3
  .rvmrc
4
4
  Gemfile.lock
5
5
  pkg/*
6
+ .idea
@@ -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
@@ -1,3 +1,5 @@
1
1
  require 'bundler/gem_tasks'
2
2
  require 'rspec/core/rake_task'
3
3
  RSpec::Core::RakeTask.new('spec')
4
+
5
+ task :default => :spec
@@ -1,25 +1,41 @@
1
1
  require 'rhymes/version'
2
2
 
3
3
  module Rhymes
4
- @@options = {:raw_dict => '/tmp/cmudict.0.7a', :compiled => '/tmp/rhymes.dat'}
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
- def setup(options)
12
- @@options.merge!(options)
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
- (rhymes[words[wup]] || []) - [wup]
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 File.exists?(@@options[:compiled])
42
- @words, @rhymes = Marshal.load(File.open(@@options[:compiled], 'rb'){|f| f.read })
43
- elsif File.exists?(@@options[:raw_dict])
44
- @words, @rhymes = {}, {}
45
- File.open(@@options[:raw_dict]) do |f|
46
- while l = f.gets do
47
- next if l =~ /^[^A-Z]/
48
- word, *pron = l.strip.split(' ')
49
- next if word.empty? || word =~ /\d/
50
- pron.map!{|snd| snd.sub(/2/, '1')}
51
- key = perfect_key(pron)
52
- @words[word] = key
53
- (@rhymes[key] ||= []) << word
54
- end
55
- end
56
- begin
57
- File.open(@@options[:compiled], 'wb'){|f| f.write(Marshal.dump([@words, @rhymes]))}
58
- rescue
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
@@ -1,3 +1,3 @@
1
1
  module Rhymes
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -1,16 +1,22 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Rhymes do
4
- before(:all) do
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 rhyme nothing with unknown word' do
36
- Rhymes.rhyme('dabadabadaba').should == []
41
+ it 'should raise with unknown word' do
42
+ expect {Rhymes.rhyme('dabadabadaba')}.to raise_error(Rhymes::UnknownWord)
37
43
  end
38
44
 
39
- end
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
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: 2012-06-17 00:00:00.000000000 Z
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: &79065380 !ruby/object:Gem::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: *79065380
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.15
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