From de82285f3bea72fddc1e5425712578f1f0de77c4 Mon Sep 17 00:00:00 2001 From: tariqksoliman Date: Sun, 2 Apr 2017 18:41:33 -0700 Subject: [PATCH] Small bug fixes --- .npmignore | 2 ++ README.md | 29 +++++++++++++------ index.js | 46 +++++++++++++++++++++++-------- nodeless/SyllaRhyme/SyllaRhyme.js | 39 ++++++++++++++++++++------ package.json | 2 +- test/test.js | 26 +++++++++++++++++ 6 files changed, 114 insertions(+), 30 deletions(-) create mode 100644 .npmignore diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..1e3565b --- /dev/null +++ b/.npmignore @@ -0,0 +1,2 @@ +helpers +nodeless \ No newline at end of file diff --git a/README.md b/README.md index 7829b64..e368c66 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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. \ No newline at end of file +### 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. \ No newline at end of file diff --git a/index.js b/index.js index 4657f58..9774b9f 100644 --- a/index.js +++ b/index.js @@ -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: { @@ -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++; @@ -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] ) { @@ -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') @@ -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++; @@ -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(); } ); diff --git a/nodeless/SyllaRhyme/SyllaRhyme.js b/nodeless/SyllaRhyme/SyllaRhyme.js index 5f57ba5..28e401a 100644 --- a/nodeless/SyllaRhyme/SyllaRhyme.js +++ b/nodeless/SyllaRhyme/SyllaRhyme.js @@ -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 @@ -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++; @@ -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] ) { @@ -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') diff --git a/package.json b/package.json index e7b399c..0cb9379 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "syllarhyme", - "version": "1.0.0", + "version": "1.0.1", "description": "Counts syllables and rhymes words.", "main": "index.js", "files": [ diff --git a/test/test.js b/test/test.js index fdebca0..c88de35 100644 --- a/test/test.js +++ b/test/test.js @@ -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() { @@ -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() { @@ -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() { @@ -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' ) ); + }); + }); + }); \ No newline at end of file