-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathed.js
25 lines (21 loc) · 820 Bytes
/
ed.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
var input = "Gyozasix pot stickers.Tuna Pokifresh tuna with cucumber. spicy tuna seaweed and tuna flakes.Cherry Blossomtuna eel and fresh salmon topped with ikura";
var topLevelDictionary = {};
function insertWords(dictionary, words) {
if (words.length === 0) {
return;
}
var firstWord = words[0].toLowerCase();
var restOfWords = words.slice(1);
var entry = dictionary[firstWord];
if (!entry) {
dictionary[firstWord] = {};
entry = dictionary[firstWord];
}
insertWords(entry, restOfWords);
insertWords(topLevelDictionary, restOfWords);
}
var sentences = input.split(/\.\s*/g).map(function(sentence){return sentence.split(/\s+/g);});
for (var i =0; i < sentences.length; i++) {
insertWords(topLevelDictionary, sentences[i]);
}
console.log(JSON.stringify(topLevelDictionary, null, 2));