-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
132 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,83 @@ | ||
// A function that transforms words into their associated IPA format | ||
// i.e. mapping is English word --> IPA | ||
var getIPAForm = function(word) { | ||
/* Insert clever stuff here */ | ||
}; | ||
|
||
var mobytrie = new Trie(); | ||
|
||
// Populate the mobypron trie | ||
// Populate the mobypron trie [with pronunciations] | ||
for (var mobyword in mobypron) { | ||
if (mobypron.hasOwnProperty(mobyword)) { | ||
var fields = mobypron[mobyword].split(/\/+/); | ||
mobytrie.insert(fields, mobyword); | ||
} | ||
} | ||
|
||
// Populate a second trie with the words themselves | ||
var wordTrie = new Trie(); | ||
|
||
var words = Object.keys(mobypron); | ||
var numWords = words.length; | ||
|
||
for (var i = 0; i < numWords; i++) { | ||
var word = words[i]; | ||
wordTrie.insert(word.split(''), word); | ||
} | ||
|
||
/* | ||
* Returns a set of words from the mobypron trie that (hopefully) | ||
* rhyme with the word specified by PRON_FIELDS. | ||
*/ | ||
var fromTrie = function(pronFields) { | ||
if (pronFields.length > 4) { | ||
pronFields.splice(0, pronFields.length - 3); | ||
} else if (pronFields.length > 2) { | ||
pronFields.splice(0, pronFields.length - 2); | ||
} | ||
|
||
var results = mobytrie.autoComplete(pronFields); | ||
if (results.length == 0) { | ||
return ["grandfather clock"]; // hey, we've got to return something! | ||
} | ||
|
||
// Return a random word from results | ||
return [results[Math.floor(Math.random() * results.length)]]; | ||
} | ||
|
||
/* | ||
* Like fromTrie, except an "s" will automatically be appended to the resulting rhyme. | ||
*/ | ||
var pluralFromTrie = function(pronFields) { | ||
var rhyme = fromTrie(pronFields); | ||
rhyme[0] = rhyme[0].concat("s"); | ||
return rhyme; | ||
} | ||
|
||
var lookup = function(word) { | ||
if (mobypron.hasOwnProperty(word)) { | ||
var pronFields = mobypron[word].split(/\/+/); | ||
if (pronFields.length > 4) { | ||
pronFields.splice(0, pronFields.length - 3); | ||
} else if (pronFields.length > 2) { | ||
pronFields.splice(0, pronFields.length - 2); | ||
if (word.slice(-1) === "*") { | ||
switch (word) { | ||
case "n****": | ||
return ["gold digga"]; | ||
case "f***": | ||
return ["luck"]; | ||
case "s***": | ||
return ["hit"]; | ||
} | ||
} else if (mobypron.hasOwnProperty(word)) { | ||
return fromTrie(mobypron[word].split(/\/+/)); | ||
} else if (mobypron.hasOwnProperty(word.toLowerCase())) { | ||
return fromTrie(mobypron[word.toLowerCase()].split(/\/+/)); | ||
} else { | ||
var singular = word.substring(0, word.length - 1); | ||
if (word.slice(-1) === "s" && mobypron.hasOwnProperty(singular)) { | ||
return pluralFromTrie(mobypron[singular].split(/\/+/)); | ||
} | ||
|
||
var results = mobytrie.autoComplete(pronFields); | ||
if (results.length == 0) { return []; } | ||
var approximation = wordTrie.getApproximation(word.toLowerCase().split('')); | ||
if (approximation == null && word.slice(-1) === "s") { // plurality case | ||
approximation = wordTrie.getApproximation(singular.split('')); | ||
if (approximation == null) { | ||
return ["grandmother clock"]; | ||
} else { | ||
return pluralFromTrie(mobypron[approximation].split(/\/+/)); | ||
} | ||
} | ||
|
||
// Return a random word from results | ||
return [results[Math.floor(Math.random() * results.length)]]; | ||
} else { | ||
return []; | ||
return fromTrie(mobypron[approximation].split(/\/+/)); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters