pronouncing 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.
@@ -0,0 +1,117 @@
1
+ require "pronouncing/version"
2
+
3
+ module Pronouncing
4
+ class Dictionary
5
+ def initialize(dictionary = nil)
6
+ dictionary ||= File.join(File.dirname(__FILE__), "/dictionary/cmudict-0.7b.txt")
7
+
8
+ @rhymes = Hash.new{ |hash, key| hash[key] = [] }
9
+ @words = Hash.new do |hash, key|
10
+ hash[key] = {
11
+ phones: [], stresses: [], syllables: [], rhyming_parts: []
12
+ }
13
+ end
14
+
15
+ File.open(dictionary, 'r').each do |line|
16
+ unless is_comment?(line)
17
+ word, phones = line.strip.split(" ")
18
+ formatted_word = format_word(word)
19
+ add_to_rhymes_dict(formatted_word, phones)
20
+ add_to_words_dict(formatted_word, phones)
21
+ end
22
+ end
23
+ end
24
+
25
+ def phones(word)
26
+ word = clean_word(word)
27
+ @words[word][:phones]
28
+ end
29
+
30
+ def stresses(word)
31
+ word = clean_word(word)
32
+ @words[word][:stresses]
33
+ end
34
+
35
+ def syllables(word)
36
+ word = clean_word(word)
37
+ @words[word][:syllables]
38
+ end
39
+
40
+ def rhyming_parts(word)
41
+ all_pronunciations = phones(word)
42
+
43
+ all_pronunciations.map do |phones|
44
+ rhyming_part(phones)
45
+ end
46
+ end
47
+
48
+ def rhymes_for(word)
49
+ word = clean_word(word)
50
+ @words[word][:rhyming_parts].map{ |rp| @rhymes[rp] }.flatten
51
+ end
52
+
53
+ def rhymes?(word_1, word_2)
54
+ word_1 = clean_word(word_1)
55
+ word_2 = clean_word(word_2)
56
+ return true if word_1 == word_2
57
+
58
+ rhymes_for(word_1).include?(word_2)
59
+ end
60
+
61
+ private
62
+
63
+ def is_comment?(line)
64
+ line.start_with?(';;;')
65
+ end
66
+
67
+ def format_word(word)
68
+ return word if word[0] == '('
69
+
70
+ word.downcase.split("(")[0]
71
+ end
72
+
73
+ def clean_word(word)
74
+ word.strip.downcase
75
+ end
76
+
77
+ def add_to_words_dict(word, phones)
78
+ add_phones(word, phones)
79
+ add_stresses(word, phones)
80
+ add_syllable_count(word, phones)
81
+ add_rhyming_part(word, phones)
82
+ end
83
+
84
+ def add_phones(word, phones)
85
+ @words[word][:phones] << phones
86
+ end
87
+
88
+ def add_stresses(word, phones)
89
+ stresses = phones.gsub(/[^012]/, '')
90
+ @words[word][:stresses] << stresses
91
+ end
92
+
93
+ def add_syllable_count(word, phones)
94
+ syllable_count = phones.gsub(/[^012]/, '').length
95
+ @words[word][:syllables] << syllable_count
96
+ end
97
+
98
+ def add_rhyming_part(word, phones)
99
+ rhyming_part = rhyming_part(phones)
100
+ @words[word][:rhyming_parts] << rhyming_part
101
+ end
102
+
103
+ def rhyming_part(phones)
104
+ phones = phones.split().reverse
105
+ phones.each_with_index do |p, idx|
106
+ if "12".include?(p[-1])
107
+ return phones[0..idx].reverse.join(' ')
108
+ end
109
+ end
110
+ end
111
+
112
+ def add_to_rhymes_dict(word, phones)
113
+ rhyming_part = rhyming_part(phones)
114
+ @rhymes[rhyming_part] += [word]
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,3 @@
1
+ module Pronouncing
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,39 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "pronouncing/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pronouncing"
8
+ spec.version = Pronouncing::VERSION
9
+ spec.authors = ["contrepoint"]
10
+ spec.email = ["contrepoint21@gmail.com"]
11
+
12
+ spec.summary = "An interface for Carnegie Mellon University's CMU Pronouncing Dictionary"
13
+ spec.homepage = "https://github.com/contrepoint/pronouncing"
14
+
15
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
16
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
17
+ if spec.respond_to?(:metadata)
18
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
19
+
20
+ spec.metadata["homepage_uri"] = spec.homepage
21
+ spec.metadata["source_code_uri"] = "https://github.com/contrepoint/pronouncing"
22
+ else
23
+ raise "RubyGems 2.0 or newer is required to protect against " \
24
+ "public gem pushes."
25
+ end
26
+
27
+ # Specify which files should be added to the gem when it is released.
28
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
29
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
30
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
31
+ end
32
+ spec.bindir = "exe"
33
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
34
+ spec.require_paths = ["lib"]
35
+
36
+ spec.add_development_dependency "bundler", "~> 1.16"
37
+ spec.add_development_dependency "rake", "~> 10.0"
38
+ spec.add_development_dependency "rspec", "~> 3.0"
39
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pronouncing
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - contrepoint
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-10-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - contrepoint21@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - lib/dictionary/cmudict-0.7b.txt
72
+ - lib/pronouncing.rb
73
+ - lib/pronouncing/version.rb
74
+ - pronouncing.gemspec
75
+ homepage: https://github.com/contrepoint/pronouncing
76
+ licenses: []
77
+ metadata:
78
+ allowed_push_host: https://rubygems.org
79
+ homepage_uri: https://github.com/contrepoint/pronouncing
80
+ source_code_uri: https://github.com/contrepoint/pronouncing
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.6.11
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: An interface for Carnegie Mellon University's CMU Pronouncing Dictionary
101
+ test_files: []