syllables 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.
Files changed (2) hide show
  1. data/lib/syllables.rb +43 -0
  2. metadata +55 -0
data/lib/syllables.rb ADDED
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # file: syllables.rb
4
+ # original code: http://stackoverflow.com/questions/1271918/ruby-count-syllables
5
+
6
+ Tokenizer = /([aeiouy]{1,3})/
7
+
8
+ class Syllables
9
+
10
+ def initialize(text)
11
+ super()
12
+ @words = text.gsub(/\W+/, ' ').split\
13
+ .inject({}) {|r,word| r.merge({word.to_sym => count_syllables(word)})}
14
+ end
15
+
16
+ def to_h
17
+ @words
18
+ end
19
+
20
+ private
21
+
22
+ def count_syllables(word)
23
+
24
+ len = 0
25
+
26
+ if word[-3..-1] == 'ing' then
27
+ len += 1
28
+ word = word[0...-3]
29
+ end
30
+
31
+ got = word.scan(Tokenizer)
32
+ len += got.size()
33
+
34
+ if got.size() > 1 and got[-1] == ['e'] and
35
+ word[-1].chr() == 'e' and
36
+ word[-2].chr() != 'l' then
37
+ len -= 1
38
+ end
39
+
40
+ return len
41
+
42
+ end
43
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: syllables
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors: []
7
+
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-06-26 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email:
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/syllables.rb
26
+ has_rdoc: true
27
+ homepage:
28
+ licenses: []
29
+
30
+ post_install_message:
31
+ rdoc_options: []
32
+
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ version:
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ requirements: []
48
+
49
+ rubyforge_project:
50
+ rubygems_version: 1.3.5
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: syllables
54
+ test_files: []
55
+