Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. function patternMaker(corpus) {
  2. // // We first normalize and split the corpus into an array of the words to easily traverse and build our pattern object
  3. let arrayOfWords = str.toLowerCase().split(' ');
  4. let corpusObj = {};
  5.  
  6. for (let i = 0; i < arrayOfWords.length; i++) {
  7. // // We are checking for " " or "-" trying to pass themselves off as words and kicking them out.
  8. if (arrayOfWords[i] === ' ' || arrayOfWords[i] === '-') {
  9. arrayOfWords.splice(i, 1);
  10. } else {
  11. //continue until the last word, stop because last word has nothing following
  12. if (i !== arrayOfWords.length - 1) {
  13. // // We have found a repeated word! lets add it to our unique key's array!
  14. if (corpusObj.hasOwnProperty(arrayOfWords[i])) {
  15. corpusObj[arrayOfWords[i]].push(arrayOfWords[i + 1]);
  16. } else {
  17. // // Finally landing on a word we have not seen so we make it a new key w/ the next word as its value.
  18. corpusObj[arrayOfWords[i]] = [arrayOfWords[i + 1]];
  19. }
  20. }
  21. }
  22. }
  23. // // Our object has been filled so now lets return it!
  24. return corpusObj;
  25. }
  26.  
  27.  
  28.  
  29. //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement