syllabize 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +3 -3
- data/lib/syllabize.rb +36 -20
- data/lib/syllabize/version.rb +1 -1
- data/spec/lib/syllabize_spec.rb +56 -21
- data/syllabize.gemspec +1 -0
- metadata +18 -2
data/README.md
CHANGED
@@ -20,9 +20,9 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
First, require `syllabize`. Then, you can call the `count_syllables` method on a string.
|
22
22
|
|
23
|
-
```
|
24
|
-
require '
|
25
|
-
=> true
|
23
|
+
```bash
|
24
|
+
require 'syllabize'
|
25
|
+
# => true
|
26
26
|
'Ruby'.count_syllables
|
27
27
|
# => 2
|
28
28
|
```
|
data/lib/syllabize.rb
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
require 'yaml'
|
2
|
+
require 'numbers_and_words'
|
2
3
|
|
3
4
|
module Syllabize
|
4
5
|
|
5
6
|
class Counter
|
6
|
-
attr_accessor :
|
7
|
+
attr_accessor :str, :exceptions_file
|
7
8
|
|
8
|
-
def initialize(
|
9
|
-
@
|
9
|
+
def initialize(str)
|
10
|
+
@str = strip_punctuation(str)
|
10
11
|
handle_non_string_input
|
11
12
|
load_exceptions
|
12
13
|
end
|
@@ -14,12 +15,14 @@ module Syllabize
|
|
14
15
|
CONSONANTS = /[bcdfghjklmnpqrstvwxz]/i
|
15
16
|
VOWELS = /[aeiou]/i
|
16
17
|
LE_VOWEL_SOUND = /((le)\z)|((le(d|r|s))|(ling)\z)/i
|
17
|
-
DIPHTHONGS = /ou|ie|oo|oi|ea|ee|ai|ae|ay/i
|
18
|
+
DIPHTHONGS = /ou|ie|io|oa|oo|oi|ea|ee|ai|ae|ay/i
|
18
19
|
Y_AS_VOWEL = /[^yY][yY]/
|
19
20
|
RE_VOWEL = /(^re[aeiou])/i
|
20
21
|
|
21
22
|
def count_syllables
|
22
|
-
|
23
|
+
@str = str.to_i.to_words if is_int_in_string_form?
|
24
|
+
return break_into_words if str.split(' ').length > 1
|
25
|
+
return handle_exceptions if exceptions.keys.include?(str)
|
23
26
|
@syllables = count_vowels
|
24
27
|
handle_additions
|
25
28
|
handle_subtractions
|
@@ -28,18 +31,25 @@ module Syllabize
|
|
28
31
|
|
29
32
|
private
|
30
33
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
end unless respond_to?(:__dir__, true)
|
34
|
+
def break_into_words
|
35
|
+
str.split(' ').collect(&:count_syllables).reduce(:+)
|
36
|
+
end
|
35
37
|
|
36
38
|
def strip_punctuation(string)
|
37
39
|
string.gsub(/\W/,'')
|
38
40
|
end
|
39
41
|
|
42
|
+
def is_int_in_string_form?
|
43
|
+
str_as_int = str.to_i
|
44
|
+
if str_as_int.zero?
|
45
|
+
return str == '0' ? true : false
|
46
|
+
end
|
47
|
+
(str_as_int * str_as_int) > 0 ? true : false
|
48
|
+
end
|
49
|
+
|
40
50
|
def handle_non_string_input
|
41
|
-
if !(
|
42
|
-
raise ArgumentError.new "#{
|
51
|
+
if !(str.is_a?(String))
|
52
|
+
raise ArgumentError.new "#{str} must be a string"
|
43
53
|
end
|
44
54
|
end
|
45
55
|
|
@@ -52,7 +62,7 @@ module Syllabize
|
|
52
62
|
end
|
53
63
|
|
54
64
|
def handle_exceptions
|
55
|
-
exceptions[
|
65
|
+
exceptions[str.to_s]
|
56
66
|
end
|
57
67
|
|
58
68
|
def handle_additions
|
@@ -68,15 +78,15 @@ module Syllabize
|
|
68
78
|
end
|
69
79
|
|
70
80
|
def count_vowels
|
71
|
-
|
81
|
+
str.scan(VOWELS).count
|
72
82
|
end
|
73
83
|
|
74
84
|
def ends_in_silent_e?
|
75
|
-
|
85
|
+
str.downcase.each_char.to_a.last == 'e'
|
76
86
|
end
|
77
87
|
|
78
88
|
def contains_le_vowel_sound?
|
79
|
-
|
89
|
+
str.scan(LE_VOWEL_SOUND).any?
|
80
90
|
end
|
81
91
|
|
82
92
|
def contains_non_initial_y?
|
@@ -84,24 +94,30 @@ module Syllabize
|
|
84
94
|
end
|
85
95
|
|
86
96
|
def count_ys_in_vowel_role
|
87
|
-
|
97
|
+
str.scan(Y_AS_VOWEL).size
|
88
98
|
end
|
89
99
|
|
90
100
|
def begins_with_re_vowel?
|
91
|
-
|
101
|
+
str.scan(RE_VOWEL).any?
|
92
102
|
end
|
93
103
|
|
94
104
|
def contains_diphthongs?
|
95
|
-
|
105
|
+
str.downcase.scan(DIPHTHONGS).any?
|
96
106
|
end
|
97
107
|
|
98
108
|
def count_diphthongs
|
99
|
-
|
109
|
+
str.downcase.scan(DIPHTHONGS).count
|
100
110
|
end
|
101
111
|
|
102
112
|
def ends_in_sm?
|
103
|
-
|
113
|
+
str.end_with?('sm')
|
104
114
|
end
|
115
|
+
|
116
|
+
|
117
|
+
# for Ruby 1.9
|
118
|
+
def __dir__
|
119
|
+
File.dirname(__FILE__)
|
120
|
+
end unless respond_to?(:__dir__, true)
|
105
121
|
end
|
106
122
|
|
107
123
|
end
|
data/lib/syllabize/version.rb
CHANGED
data/spec/lib/syllabize_spec.rb
CHANGED
@@ -4,7 +4,7 @@ class Syllabize::Counter
|
|
4
4
|
public :count_vowels, :ends_in_silent_e?, :count_diphthongs,
|
5
5
|
:contains_diphthongs?, :ends_in_sm?, :contains_non_initial_y?,
|
6
6
|
:count_ys_in_vowel_role, :contains_le_vowel_sound?,
|
7
|
-
:begins_with_re_vowel?
|
7
|
+
:begins_with_re_vowel?, :is_int_in_string_form?
|
8
8
|
end
|
9
9
|
|
10
10
|
describe Syllabize::Counter do
|
@@ -16,6 +16,36 @@ describe Syllabize::Counter do
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
+
describe '#is_int_in_string_form?' do
|
20
|
+
subject { Syllabize::Counter.new(word).is_int_in_string_form? }
|
21
|
+
context 'is an int' do
|
22
|
+
context 'zero' do
|
23
|
+
let(:word) { '0' }
|
24
|
+
it 'returns true' do
|
25
|
+
expect(subject).to be_true
|
26
|
+
end
|
27
|
+
end
|
28
|
+
context 'positive' do
|
29
|
+
let(:word) { '500' }
|
30
|
+
it 'returns true' do
|
31
|
+
expect(subject).to be_true
|
32
|
+
end
|
33
|
+
end
|
34
|
+
context 'negative' do
|
35
|
+
let(:word) { '-10' }
|
36
|
+
it 'returns true' do
|
37
|
+
expect(subject).to be_true
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
context 'not an int' do
|
42
|
+
let(:word) { 'pizza' }
|
43
|
+
it 'returns false' do
|
44
|
+
expect(subject).to be_false
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
19
49
|
describe '#ends_in_silent_e?' do
|
20
50
|
context 'silent e' do
|
21
51
|
it 'returns true if the word ends in a silent e' do
|
@@ -84,8 +114,8 @@ describe Syllabize::Counter do
|
|
84
114
|
|
85
115
|
describe '#count_diphthongs' do
|
86
116
|
it 'counts the diphthongs in a word' do
|
87
|
-
expect(Syllabize::Counter.new('air').count_diphthongs).to eq(1)
|
88
|
-
expect(Syllabize::Counter.new('aerie').count_diphthongs).to eq(2)
|
117
|
+
expect(Syllabize::Counter.new('air').count_diphthongs).to eq(1)
|
118
|
+
expect(Syllabize::Counter.new('aerie').count_diphthongs).to eq(2)
|
89
119
|
end
|
90
120
|
end
|
91
121
|
|
@@ -99,26 +129,31 @@ describe Syllabize::Counter do
|
|
99
129
|
context 'given a string' do
|
100
130
|
it 'counts the syllables in a word' do
|
101
131
|
{
|
102
|
-
'blizzard'
|
103
|
-
'why'
|
104
|
-
'plain'
|
105
|
-
'sticky'
|
106
|
-
'syzygy'
|
107
|
-
'yeses'
|
108
|
-
'communism'
|
109
|
-
'please'
|
110
|
-
'candle'
|
111
|
-
'handling'
|
112
|
-
'realize'
|
113
|
-
'really'
|
114
|
-
'cooperate'
|
115
|
-
'ways'
|
116
|
-
"Wayne's"
|
117
|
-
'basement'
|
118
|
-
'
|
132
|
+
'blizzard' => 2,
|
133
|
+
'why' => 1,
|
134
|
+
'plain' => 1,
|
135
|
+
'sticky' => 2,
|
136
|
+
'syzygy' => 3,
|
137
|
+
'yeses' => 2,
|
138
|
+
'communism' => 4,
|
139
|
+
'please' => 1,
|
140
|
+
'candle' => 2,
|
141
|
+
'handling' => 3,
|
142
|
+
'realize' => 3,
|
143
|
+
'really' => 2,
|
144
|
+
'cooperate' => 4,
|
145
|
+
'ways' => 1,
|
146
|
+
"Wayne's" => 1,
|
147
|
+
'basement' => 2,
|
148
|
+
'coach' => 1,
|
149
|
+
'five' => 1,
|
150
|
+
'hundred' => 2,
|
151
|
+
'I really think so' => 5,
|
152
|
+
'500' => 3,
|
153
|
+
'1,000,000' => 3,
|
119
154
|
}.each do |word, actual|
|
120
155
|
count = Syllabize::Counter.new(word).count_syllables
|
121
|
-
expect(count).to eq(actual), "#{word} was not the correct number of syllables; expected #{actual}, was #{count}"
|
156
|
+
expect(count).to eq(actual), "'#{word}' was not the correct number of syllables; expected #{actual}, was #{count}"
|
122
157
|
end
|
123
158
|
end
|
124
159
|
end
|
data/syllabize.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: syllabize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.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: 2014-
|
12
|
+
date: 2014-05-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -59,6 +59,22 @@ dependencies:
|
|
59
59
|
- - ! '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: numbers_and_words
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
62
78
|
description: A syllable counter written in Ruby
|
63
79
|
email:
|
64
80
|
- nick@nickcox.me
|