Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function patternMaker(corpus) {
- // // We first normalize and split the corpus into an array of the words to easily traverse and build our pattern object
- let arrayOfWords = str.toLowerCase().split(' ');
- let corpusObj = {};
- for (let i = 0; i < arrayOfWords.length; i++) {
- // // We are checking for " " or "-" trying to pass themselves off as words and kicking them out.
- if (arrayOfWords[i] === ' ' || arrayOfWords[i] === '-') {
- arrayOfWords.splice(i, 1);
- } else {
- //continue until the last word, stop because last word has nothing following
- if (i !== arrayOfWords.length - 1) {
- // // We have found a repeated word! lets add it to our unique key's array!
- if (corpusObj.hasOwnProperty(arrayOfWords[i])) {
- corpusObj[arrayOfWords[i]].push(arrayOfWords[i + 1]);
- } else {
- // // Finally landing on a word we have not seen so we make it a new key w/ the next word as its value.
- corpusObj[arrayOfWords[i]] = [arrayOfWords[i + 1]];
- }
- }
- }
- }
- // // Our object has been filled so now lets return it!
- return corpusObj;
- }
- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement