Skip to content

Commit

Permalink
Small bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tariqksoliman committed Apr 3, 2017
1 parent 389c5ae commit de82285
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 30 deletions.
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
helpers
nodeless
29 changes: 20 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# SyllaRhyme
An npm package (not yet) for counting syllables and rhyming words.
An npm package for counting syllables and rhyming words.

## Installation

```npm install syllarhyme```

### Nodeless
If you want it working straight in a browser:
Expand All @@ -18,19 +21,27 @@ var SyllaRhyme = require( 'syllarhyme' ); //if using Node

SyllaRhyme( function( sr ) {
console.log( sr.rhymes( 'rhymes' ) );
//["chimes", "dime's", "dimes", "heims", "himes", "hymes", "imes", "kimes", "climbs", "climes", "limes", "grimes", "crime's", "crimes", "crymes", "prime's", "primes", "rimes", "simes", "symes", "sometimes", "time's", "times", "times'"]
console.log( sr.rhymesWith( 'rhymes', 'with' ) );
//false
console.log( sr.pronunciation( 'pronunciation' ) );
//["P R OW0 N AH2 N S IY0 EY1 SH AH0 N", "P R AH0 N AH2 N S IY0 EY1 SH AH0 N"]
console.log( sr.pronunciation( 'pronunciation', 'ipa' ) );
//["p r oʊ n ʌ n s i eɪ ʃ ə n", "p r ə n ʌ n s i eɪ ʃ ə n"]
console.log( sr.syllables( 'syllables' ) );
//3
} );
```
## Functions

### rhymes( word )
* Returns a string array of all matching rhymes word (excluding itself).
### rhymesWith( word1, word2 )
* Returns a boolean of whether the words rhyme (true if words are the same).
### pronunciation( word, type *optional* )
* Returns a string array of possible pronunciations with space separated phonemes. By default it returns [Arpabet](https://en.wikipedia.org/wiki/Arpabet) transcriptions. If ```type``` is set to ```'ipa'```, it'll return [IPA](https://en.wikipedia.org/wiki/Help:IPA_for_English) transcriptions instead.
### syllables( word )
* Return an int indicating the number of syllables in word.
### rhymes( word )
* Returns a string array of all matching rhyming words (excluding itself).

### rhymesWith( word1, word2 )
* Returns a boolean of whether the words rhyme (true if words are the same).

### pronunciation( word, type *optional* )
* Returns a string array of possible pronunciations with space separated phonemes. By default it returns [Arpabet](https://en.wikipedia.org/wiki/Arpabet) transcriptions. If ```type``` is set to ```'ipa'```, it'll return [IPA](https://en.wikipedia.org/wiki/Help:IPA_for_English) transcriptions instead.

### syllables( str )
* Return an int indicating the number of syllables in word/sentence/string.
46 changes: 34 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
var fs = require( 'fs' );

module.exports = function( callback ) {

var dictPath = __dirname + '/dictionaries/cmudict-0.7b.txt';
var flipdictPath = __dirname + '/dictionaries/flipdict.txt';

syllarhyme = {
ipaMappings: {
Expand Down Expand Up @@ -197,8 +200,13 @@ module.exports = function( callback ) {
var phoneArr = [];
var num = 0;
var id = '';
// Scan dict linearly for the series:
// word
// word(1)
// ...
// word(n)
// not word
for( var i = 0; i < this.dict.length; i++ ) {

if( this.dict[i][0] == word + id ) {
phoneArr.push( this.dict[i].slice(2) );
num++;
Expand All @@ -212,12 +220,21 @@ module.exports = function( callback ) {
return phoneArr;
},
rhymes: function( word ) {
var rhymeArr = [];

word = word.toLowerCase();

var phonemesList = this._wordToPhonemes( word );
if( phonemesList.length == 0 ) return rhymeArr;

var match = phonesToMatch( phonemesList[0] );
var rhymeArr = [];
var matchesBegun = false;

//Scan flipdict linearly for the series:
// match
// ...
// match
// not match
for( var i = 0; i < this.flipdict.length; i++ ) {
for( var m = 0; m < match.length; m++ ) {
if( this.flipdict[i][m] != match[m] ) {
Expand Down Expand Up @@ -255,12 +272,20 @@ module.exports = function( callback ) {
return this.rhymes( word1 ).indexOf( word2.toLowerCase() ) > -1 ||
word1.toLowerCase() == word2.toLowerCase();
},
syllables: function( word ) {
var phonemes = this._wordToPhonemes( word )[0];
syllables: function( str ) {
var syllablesCount = 0;
for( var i = 0; i < phonemes.length; i++ ) {
if( (/^[aeiou]/i).test( phonemes[i] ) ) syllablesCount++;
}
str = str.replace(/[,.?!();]/g, "");
var words = str.split( ' ' );

for( var i = 0; i < words.length; i++ ) {
var phonemes = this._wordToPhonemes( words[i] )[0];
if( phonemes ) { //the word is found
//Count how many voweled phones
for( var j = 0; j < phonemes.length; j++ ) {
if( (/^[aeiou]/i).test( phonemes[j] ) ) syllablesCount++;
}
}
}
return syllablesCount;
},
//optional: type ('arpabet'(default) or 'ipa')
Expand All @@ -284,9 +309,6 @@ module.exports = function( callback ) {
};

//init
var dict = 'dictionaries/cmudict-0.7b.txt';
var flipdict = 'dictionaries/flipdict.txt';

var dictsLoaded = 0;
function dictLoaded() {
dictsLoaded++;
Expand All @@ -295,11 +317,11 @@ module.exports = function( callback ) {
}
}

readDictFile( dict, function( lines ) {
readDictFile( dictPath, function( lines ) {
syllarhyme.dict = lines;
dictLoaded();
} );
readDictFile( flipdict, function( lines ) {
readDictFile( flipdictPath, function( lines ) {
syllarhyme.flipdict = lines;
dictLoaded();
} );
Expand Down
39 changes: 31 additions & 8 deletions nodeless/SyllaRhyme/SyllaRhyme.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var SyllaRhyme = function( callback ) {
var dictPath = 'SyllaRhyme/dictionaries/cmudict-0.7b.txt';
var flipdictPath = 'SyllaRhyme/dictionaries/flipdict.txt';

syllarhyme = {
syllarhyme = {
ipaMappings: {
/*
Stress
Expand Down Expand Up @@ -199,8 +199,13 @@ var SyllaRhyme = function( callback ) {
var phoneArr = [];
var num = 0;
var id = '';
// Scan dict linearly for the series:
// word
// word(1)
// ...
// word(n)
// not word
for( var i = 0; i < this.dict.length; i++ ) {

if( this.dict[i][0] == word + id ) {
phoneArr.push( this.dict[i].slice(2) );
num++;
Expand All @@ -214,11 +219,21 @@ var SyllaRhyme = function( callback ) {
return phoneArr;
},
rhymes: function( word ) {
var rhymeArr = [];

word = word.toLowerCase();

var phonemesList = this._wordToPhonemes( word );
if( phonemesList.length == 0 ) return rhymeArr;

var match = phonesToMatch( phonemesList[0] );
var rhymeArr = [];
var matchesBegun = false;

//Scan flipdict linearly for the series:
// match
// ...
// match
// not match
for( var i = 0; i < this.flipdict.length; i++ ) {
for( var m = 0; m < match.length; m++ ) {
if( this.flipdict[i][m] != match[m] ) {
Expand Down Expand Up @@ -256,12 +271,20 @@ var SyllaRhyme = function( callback ) {
return this.rhymes( word1 ).indexOf( word2.toLowerCase() ) > -1 ||
word1.toLowerCase() == word2.toLowerCase();
},
syllables: function( word ) {
var phonemes = this._wordToPhonemes( word )[0];
syllables: function( str ) {
var syllablesCount = 0;
for( var i = 0; i < phonemes.length; i++ ) {
if( (/^[aeiou]/i).test( phonemes[i] ) ) syllablesCount++;
}
str = str.replace(/[,.?!();]/g, "");
var words = str.split( ' ' );

for( var i = 0; i < words.length; i++ ) {
var phonemes = this._wordToPhonemes( words[i] )[0];
if( phonemes ) { //the word is found
//Count how many voweled phones
for( var j = 0; j < phonemes.length; j++ ) {
if( (/^[aeiou]/i).test( phonemes[j] ) ) syllablesCount++;
}
}
}
return syllablesCount;
},
//optional: type ('arpabet'(default) or 'ipa')
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "syllarhyme",
"version": "1.0.0",
"version": "1.0.1",
"description": "Counts syllables and rhymes words.",
"main": "index.js",
"files": [
Expand Down
26 changes: 26 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ describe( 'SyllaRhyme', function() {
assert.equal( true, syllarhyme.rhymes( 'trunk' ).indexOf( 'shunk' ) > -1 );
});
});
describe( '#rhymes( "testnonsense" )', function() {
it( 'should return []', function() {
assert.deepEqual( [], syllarhyme.rhymes( 'testnonsense' ) );
});
});

//rhymesWith
describe( '#rhymesWith( "test", "best" )', function() {
Expand All @@ -72,6 +77,11 @@ describe( 'SyllaRhyme', function() {
assert.equal( false, syllarhyme.rhymesWith( 'test', '' ) );
});
});
describe( '#rhymesWith( "testnonsense", "morenonsense" )', function() {
it( 'should return false', function() {
assert.equal( false, syllarhyme._wordToPhonemes( 'testnonsense', 'morenonsense' ) );
});
});

//syllables
describe( '#syllables( "test" )', function() {
Expand All @@ -84,11 +94,21 @@ describe( 'SyllaRhyme', function() {
assert.equal( 3, syllarhyme.syllables( 'resplendent' ) );
});
});
describe( '#syllables( "How many syllables does this sentence have?" )', function() {
it( 'should return 11', function() {
assert.equal( 11, syllarhyme.syllables( 'How many syllables does this sentence have?' ) );
});
});
describe( '#syllables( "" )', function() {
it( 'should return 0', function() {
assert.equal( 0, syllarhyme.syllables( '' ) );
});
});
describe( '#syllables( "testnonsense" )', function() {
it( 'should return 0', function() {
assert.deepEqual( 0, syllarhyme.syllables( 'testnonsense' ) );
});
});

//pronunciation
describe( '#pronunciation( "test" )[0]', function() {
Expand All @@ -111,4 +131,10 @@ describe( 'SyllaRhyme', function() {
assert.equal( 'k j ʊ r i ə s', syllarhyme.pronunciation( 'curious', 'ipa' )[0] );
});
});
describe( '#pronunciation( "testnonsense" )', function() {
it( 'should return []', function() {
assert.deepEqual( [], syllarhyme.pronunciation( 'testnonsense' ) );
});
});

});

0 comments on commit de82285

Please sign in to comment.