Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lukem512 committed Apr 3, 2016
0 parents commit 1797c57
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 0 deletions.
8 changes: 8 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
The MIT License (MIT)
Copyright (c) 2016 Luke Mitchell

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# pronounceable

Pronounceable is a small module that allows you to test a word for pronounceability.

To use it, simple install via NPM and include it in your project file.

```
var pronounceable = require('pronounceable');
```

Then, to test a word for pronounceability, use the `test` method.

```
console.log(pronounceable.test('samosa')); // true
console.log(pronounceable.test('xghsii')); // false
```

You can also use the module to score a word on its pronounceability, using the `score` method. The higher the output value the more pronouncable the word.

```
console.log(pronouncable.score('peonies')); // 0.10176356810708122
console.log(pronouncable.score('sshh')); // 0.0008556941146173743
```

To generate your own dataset use the `train` method.

```
pronouncable.train('dictionary.txt', function(probabilities) {
// The data set has been returned
console.log(probabilities);
});
```
1 change: 1 addition & 0 deletions data/triplets.json

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "pronounceable",
"version": "1.0.0",
"description": "Test a word for pronounceability",
"main": "pronounceable.js",
"scripts": {
"test": "node ./test/test.js"
},
"repository": {
"type": "git",
"url": "https://github.com/lukem512/pronounceable.git"
},
"keywords": [
"pronounceable",
"pronounceability",
"grammar",
"language",
"speech"
],
"author": "Luke Mitchell",
"license": "MIT",
"bugs": {
"url": "https://github.com/lukem512/pronounceable/issues"
},
"homepage": "https://github.com/lukem512/pronounceable"
}
112 changes: 112 additions & 0 deletions pronounceable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Luke Mitchell, 2016
// https://github.com/lukem512/pronounceable

var fs = require('fs');
var path = require('path');

var threshold = 0.001;
var probs = {};

// Load probabilities from JSON file.
var probs = JSON.parse(fs.readFileSync(path.resolve(__dirname, 'data/triplets.json'), 'utf8'));

// Remove any non-alphabet characters
// and convert to lower case.
function clean(word) {
return word.replace(/[^a-zA-Z]/g,'').toLowerCase();
};

// Make a percentage.
function percent(score, count) {
return (score / count) * 100;
};

// Extract probabilities of word tuples
// from a large list of words.
module.exports.train = function(filename, callback) {
var probs = {};
var count = 0;

fs.readFile(filename, 'utf8', function read(err, data) {
if (err) throw err;

var words = data.trim().split(/\s+/);
words.forEach(function(w){
w = clean(w);

for (var i = 0; i < (w.length - 2); i++) {
if (!probs[w[i]]) probs[w[i]] = {};
if (!probs[w[i]][w[i+1]]) probs[w[i]][w[i+1]] = {};
if (!probs[w[i]][w[i+1]][w[i+2]]) probs[w[i]][w[i+1]][w[i+2]] = 1;
else probs[w[i]][w[i+1]][w[i+2]]++;
count++;
}
});

Object.keys(probs).forEach(function(first) {
Object.keys(probs[first]).forEach(function(second){
Object.keys(probs[first][second]).forEach(function(third){
probs[first][second][third] =
percent(probs[first][second][third], count);
});
});
});

callback(probs);
});
};

// Check whether a word is pronounceable using
// the word tuple probabilities.
module.exports.test = function(word) {
var w = clean(word);

switch (w.length) {
case 1:
break;

case 2:
// TODO - word pairs
if (typeof probs[w[0]][w[1]] === 'undefined') return false;

default:
for (var i = 0; i < (w.length - 2); i++) {
if (typeof probs[w[i]] === 'undefined' ||
typeof probs[w[i]][w[i+1]] === 'undefined' ||
typeof probs[w[i]][w[i+1]][w[i+2]] === 'undefined' ||
probs[w[i]][w[i+1]][w[i+2]] < threshold)
return false;
}
}

return true;
};

// Compute a normalised score for
// the pronounceability of the word.
module.exports.score = function(word) {
var w = clean(word);
var score = 0;

switch (w.length) {
case 1:
return 1;

case 2:
// TODO - word pairs
return NaN;

default:
for (var i = 0; i < (w.length - 2); i++) {
if (typeof probs[w[i]] === 'undefined' ||
typeof probs[w[i]][w[i+1]] === 'undefined' ||
typeof probs[w[i]][w[i+1]][w[i+2]] === 'undefined') {
score = score + 0;
} else {
score = score + probs[w[i]][w[i+1]][w[i+2]];
}
}
}

return (score / w.length);
};
45 changes: 45 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Luke Mitchell, 2016
// https://github.com/lukem512/pronounceable

var pronounceable = require('../pronounceable');

// Run tests to determine whether the
// metric is correct.
function test() {
var payload = {
a: true,
froggies: true,
frggies: false,
fry: true,
friy: false,
frrrr: false,
ax: true,
xa: true,
xax: false,
xaxa: false,
mom: true,
moom: true,
maam: false,
goo: true,
hxueb: false,
thth: false,
thump: true,
grumph: true,
samosa: true,
xghsii: false
};

var failed = false;
Object.keys(payload).some(function(w){
if (pronounceable.test(w) !== payload[w]) {
console.error('Test failed with \'' + w + '\'');
failed = true;
}
return failed;
});

return !failed;
};

// Run the test!
test();

0 comments on commit 1797c57

Please sign in to comment.