-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhaiku.js
40 lines (29 loc) · 889 Bytes
/
haiku.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var fs = require("fs");
var cmudictFile = readCmudictFile('./cmudict.txt');
var library = formatData(cmudictFile);
function readCmudictFile(file){
return fs.readFileSync(file).toString();
}
function formatData(data){
var lines = data.toString().split("\n"),
lineSplit,
syllables;
var library = [];
lines = lines.splice(0,lines.length-1);
lines.forEach(function(line){
lineSplit = line.split(" ");
syllables = syllableCount(lineSplit[1]);
library = createLibrary(lineSplit[0], syllables, library);
});
return library;
}
function syllableCount(arr){
var numbers = arr.match(/\d+/g) || 0;
var count = numbers.length;
return count;
}
function createLibrary(word, syllable, library){
library[syllable] ? library[syllable].push(word) : library[syllable] = [];
return library;
}
module.exports = library;