rhymes 0.1.0 → 1.0.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.
@@ -1,34 +1,23 @@
1
+ {<img src="https://codeclimate.com/github/kalenkov/rhymes.png" />}[https://codeclimate.com/github/kalenkov/rhymes]
1
2
  = Rhymes
2
- Rhymes looks up perfect and identical rhymes using the Carnegie Mellon
3
- Pronouncing Dictionary, or any other pronounciation dictionary with
4
- compatible format.
5
-
6
- Dictionary can be downloaded from https://cmusphinx.svn.sourceforge.net/svnroot/cmusphinx/trunk/cmudict/
7
- (latest version - https://cmusphinx.svn.sourceforge.net/svnroot/cmusphinx/trunk/cmudict/cmudict.0.7a)
3
+ Rhymes looks up perfect and identical rhymes using precompiled version of the Carnegie Mellon
4
+ Pronouncing Dictionary.
8
5
 
9
6
  == Installation
10
7
 
11
8
  gem install rhymes
12
9
 
13
10
  == Usage
14
- Before first usage download and store the dictionary. Example below assumes
15
- dictionary stored by default at /tmp/cmudict.0.7a. Precompiled version will be stored
16
- as /tmp/rhymes.dat
17
11
 
18
12
  ruby > require 'rhymes'
19
13
  => true
20
14
  ruby > Rhymes.rhyme('ruby')
21
15
  => ["BOOBY", "DUBHI", "DUBY", "HRUBY", "KUBY", "LOOBY", "LUBY", "NEWBY", "RUBEY", "RUBI", "RUBIE", "SCOOBY", "TRUBEY", "TRUBY"]
22
16
 
23
- == Configuration
17
+ Class method will load dictionary on each call. Use instance method to front-load the expense
24
18
 
25
19
  ruby > require 'rhymes'
26
20
  => true
27
- ruby > Rhymes.setup
28
- => {:raw_dict=>"/tmp/cmudict.0.7a", :compiled=>"/tmp/rhymes.dat"}
29
- ruby > Rhymes.setup(:compiled => '/new/location/file')
30
- => {:raw_dict=>"/tmp/cmudict.0.7a", :compiled=>"/new/location/file"}
31
- ruby > Rhymes.setup{|c| c.raw_dict = '/new/raw/dict'}
32
- => {:raw_dict=>"/new/raw/dict", :compiled=>"/new/location/file"}
33
-
34
- Note that after the first successful call to Rhymes.rhyme all data is in memory and setup does not affect anything
21
+ ruby > rhymes = Rhymes.new; rhymes.rhyme('ruby')
22
+ => ["BOOBY", "DUBHI", "DUBY", "HRUBY", "KUBY", "LOOBY", "LUBY", "NEWBY", "RUBEY", "RUBI", "RUBIE", "SCOOBY", "TRUBEY", "TRUBY"]
23
+
@@ -0,0 +1,33 @@
1
+ Copyright (C) 1993-2008 Carnegie Mellon University. All rights reserved.
2
+
3
+ Redistribution and use in source and binary forms, with or without
4
+ modification, are permitted provided that the following conditions
5
+ are met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright
8
+ notice, this list of conditions and the following disclaimer.
9
+ The contents of this file are deemed to be source code.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright
12
+ notice, this list of conditions and the following disclaimer in
13
+ the documentation and/or other materials provided with the
14
+ distribution.
15
+
16
+ This work was supported in part by funding from the Defense Advanced
17
+ Research Projects Agency, the Office of Naval Research and the National
18
+ Science Foundation of the United States of America, and by member
19
+ companies of the Carnegie Mellon Sphinx Speech Consortium. We acknowledge
20
+ the contributions of many volunteers to the expansion and improvement of
21
+ this dictionary.
22
+
23
+ THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
24
+ ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25
+ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26
+ PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
27
+ NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Binary file
@@ -1,88 +1,31 @@
1
1
  require 'rhymes/version'
2
2
 
3
- module Rhymes
4
- # generic error
5
- class RhymesError < RuntimeError; end
3
+ class Rhymes
6
4
  # 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
-
11
- class << self
12
- ##
13
- # Sets up options.
14
- # - :raw_dict - location of raw dictionary file. Default: /tmp/cmudict.0.7a
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]
21
- end
22
-
23
- ##
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
28
- def rhyme(word)
29
- wup = word.upcase
30
- key = words[wup]
31
- raise UnknownWord unless key
32
- rhymes[key] - [wup]
33
- end
34
-
35
- private
36
- def options
37
- @options ||= Struct.new(:raw_dict, :compiled).new('/tmp/cmudict.0.7a', '/tmp/rhymes.dat')
38
- end
39
-
40
- def words
41
- init unless @words
42
- @words
43
- end
44
-
45
- def rhymes
46
- init unless @rhymes
47
- @rhymes
48
- end
49
-
50
-
51
- def perfect_key(pron)
52
- first = pron.rindex{|snd| snd =~ /1$/} || 0
53
- pron[first..-1]
54
- end
55
-
56
- def init
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
78
- end
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)
85
- end
5
+ class UnknownWord < RuntimeError; end
6
+
7
+ ##
8
+ # Class method (slow, reads data on each call)
9
+ # Returns the list of perfect and identical rhymes to provided word (in upper case)
10
+ # raises
11
+ # Rhymes::UnknownWord - word does not exist in the dictionary
12
+ def self.rhyme(word)
13
+ Rhymes.new.rhyme(word)
14
+ end
15
+
16
+ def initialize
17
+ @words, @rhymes = Marshal.load(File.open(File.expand_path('../data/rhymes.dat', File.dirname(__FILE__)), 'rb'){|f| f.read })
18
+ end
86
19
 
20
+ ##
21
+ # Instance method. Use to front-load the expense of reading data file
22
+ # Returns the list of perfect and identical rhymes to provided word (in upper case)
23
+ # raises
24
+ # Rhymes::UnknownWord - word does not exist in the dictionary
25
+ def rhyme(word)
26
+ wup = word.upcase
27
+ key = @words[wup]
28
+ raise UnknownWord unless key
29
+ @rhymes[key] - [wup]
87
30
  end
88
31
  end
@@ -1,3 +1,3 @@
1
- module Rhymes
2
- VERSION = "0.1.0"
1
+ class Rhymes
2
+ VERSION = "1.0.0"
3
3
  end
@@ -11,8 +11,6 @@ Gem::Specification.new do |s|
11
11
  s.summary = %q{Lookup perfect and identical rhymes}
12
12
  s.description = %q{Lookup perfect and identical rhymes}
13
13
 
14
- s.rubyforge_project = "rhymes"
15
-
16
14
  s.files = `git ls-files`.split("\n")
17
15
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
16
  s.require_paths = ["lib"]
@@ -1,41 +1,17 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Rhymes do
4
- before do
5
- @sample = File.join(File.dirname(__FILE__), 'sample_raw.txt')
6
- Rhymes.setup(:raw_dict => @sample, :compiled => '')
7
- end
8
-
9
- it 'should save provided option' do
10
- Rhymes.setup(:compiled => '/tmp/sample').should == {:raw_dict => @sample, :compiled => '/tmp/sample'}
11
- end
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
-
20
4
  it 'should rhyme ruby with newby and scooby' do
21
- Rhymes.rhyme('ruby').should == ['NEWBY', 'SCOOBY']
22
- Rhymes.rhyme('Scooby').should == ['NEWBY', 'RUBY']
23
- Rhymes.rhyme('newby').should == ['RUBY', 'SCOOBY']
24
- end
25
-
26
- it 'should rhyme rube and cube' do
27
- Rhymes.rhyme('RubE').should == ['CUBE']
28
- Rhymes.rhyme('cUbE').should == ['RUBE']
5
+ Rhymes.rhyme('ruby').should include('NEWBY', 'SCOOBY')
6
+ rhymes = Rhymes.new
7
+ rhymes.rhyme('Scooby').should include('NEWBY', 'RUBY')
8
+ rhymes.rhyme('newby').should include('RUBY', 'SCOOBY')
29
9
  end
30
10
 
31
- it 'should rhyme lighter and scriptwriter' do
32
- Rhymes.rhyme('LIGHTER').should == ['SCRIPTWRITER']
33
- Rhymes.rhyme('SCRIPTWRITER').should == ['LIGHTER']
34
- end
35
-
36
- it 'should rhyme monterrey and usa' do
37
- Rhymes.rhyme('MONTERREY').should == ['USA']
38
- Rhymes.rhyme('USA').should == ['MONTERREY']
11
+ it 'should rhyme case-insensitive' do
12
+ rhymes = Rhymes.new
13
+ rhymes.rhyme('RubE').should include('CUBE')
14
+ rhymes.rhyme('cUbE').should include('RUBE')
39
15
  end
40
16
 
41
17
  it 'should raise with unknown word' do
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.1.0
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-14 00:00:00.000000000 Z
12
+ date: 2013-05-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -39,6 +39,8 @@ files:
39
39
  - LICENSE
40
40
  - README.rdoc
41
41
  - Rakefile
42
+ - data/cmudict_LICENSE
43
+ - data/rhymes.dat
42
44
  - lib/rhymes.rb
43
45
  - lib/rhymes/version.rb
44
46
  - rhymes.gemspec
@@ -64,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
66
  - !ruby/object:Gem::Version
65
67
  version: '0'
66
68
  requirements: []
67
- rubyforge_project: rhymes
69
+ rubyforge_project:
68
70
  rubygems_version: 1.8.24
69
71
  signing_key:
70
72
  specification_version: 3