// remove new lines and spaces
const splitStr = str => {
  let arr = str.split('\n').join(' ');
  arr = arr.split(' ');
  return arr;
};

const removeAbbr = arr => {
  return arr.filter(word => {
    return word // convert string to array of words; only include words (no empty strings)
        && word !== 'Mr.'
        && word !== 'Ms.'
        && word !== 'Mrs.'
        && (word.length > 2 || word[word.length - 1] !== '.'); // removes middle initials as to not confuse them with the end of a sentence
  });
};

const filterWords = arr => {
  return arr.filter((word, i, a) => {
    let prevWord = a[i - 1];
    return i !== 0 // exclude the first word (will always be capitalized)
      && /^[a-zA-Z]/.test(arr[i]) // exclude anything that does not start with a letter
      && !word.includes('—') // excludes any two words that have been joined by an em-dash
      && !word.includes('-') // excludes and two words that have been joined by a hyphen
      && prevWord[prevWord.length - 1] !== '.' // excludes first word of a sentence
      && prevWord[prevWord.length - 1] !== '!' // excludes first word of a sentence
      && prevWord[prevWord.length - 1] !== '?' // excludes first word of a sentence
      && prevWord[prevWord.length - 2] !== '.' // excludes first word of a sentence if previous sentence ended in close quote
      && prevWord[prevWord.length - 1] !== ':';
  });
};

const removePunctuation = arr => {
  return arr.map(word => word.replace(/'s/g, '')) // remove 's from end of words
            .map(word =>  word.replace(/\W+/g, '')); // remove any non letter characters (i.e. extraneous quotes)
};
// remove any date words
const removeWords = arr => {
  const dateWords = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', 'Jan', 'Feb', 'Mar', 'Apr', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Mon', 'Tue', 'Wed', 'Thurs', 'Fri',  'Sat', 'Sun'];
  return arr.filter(word => !dateWords.includes(word));
};

const arrToObj = arr => {
  let pronounObj = {};
  let i = 0;
  while (i < arr.length) {
    if (/^[A-Z]/.test(arr[i])) { // check if word is capitalized
      let newWord = `${arr[i]}`;
      while (/^[A-Z]/.test(arr[i + 1])) { // if next word is capitalized too, combine them
        newWord += ` ${arr[i + 1]}`;
        i++;
      }
      pronounObj[newWord] ? pronounObj[newWord]++ : pronounObj[newWord] = 1; // count the number of occurences
    }
    i++;
  }
  return pronounObj;
};

// convert obj of words to array by rate of occurrence
const sortObjByOccurrence = obj => {
  const hash = {};
  for (const word in obj) {
    if (hash[obj[word]]) {
      hash[obj[word]].push({
        title: word,
        description: '',
        image: '',
        link: ''
      });
    } else {
      hash[obj[word]] = [{
        title: word,
        description: '',
        image: '',
        link: ''
      }];
    }
  }
  return hash;
};

const convertHashToOrderedArr = hash => {
  let arrayOfOccurrences = [];
  let arrayOfWords = [];
  for (const num in hash) {
    arrayOfOccurrences.push(+num);
  }
  arrayOfOccurrences = arrayOfOccurrences.sort((a, b) => b - a);
  arrayOfOccurrences.forEach(key => {
    arrayOfWords = arrayOfWords.concat(hash[key]);
  })
  return arrayOfWords;
};

export default function findProperNouns(text) {
  return convertHashToOrderedArr(sortObjByOccurrence(arrToObj(removeWords(removePunctuation(filterWords(removeAbbr(splitStr(text))))))));
}


@@ -862,97 +862,8 @@
 ash%0A
-      && !word.includes('-') // excludes and two words that have been joined by a hyphen%0A
     
@@ -1306,27 +1306,27 @@
 t remove
-Punctuation
+Apostrophes
  = arr =
@@ -1414,122 +1414,8 @@
 rds%0A
-            .map(word =%3E  word.replace(/%5CW+/g, '')); // remove any non letter characters (i.e. extraneous quotes)%0A
 %7D;%0A/
@@ -2104,16 +2104,55 @@
 %5Bi + 1%5D)
+ && newWord%5BnewWord.length - 1%5D !== ','
 ) %7B // i
@@ -2195,16 +2195,68 @@
 ine them
+. Will not combine proper nouns separated by a comma
 %0A       
@@ -3260,16 +3260,240 @@
 ds;%0A%7D;%0A%0A
+const removePunctuation = arr =%3E %7B%0A  const newArr = arr.map(obj =%3E %7B%0A    const title = obj.title.replace(/%5B%22,.!?*%5D+/g, '');%0A    obj.title = title;%0A    return obj;%0A  %7D); // remove punctuation from strings%0A  return newArr;%0A%7D%0A%0A
 export d
@@ -3541,16 +3541,34 @@
  return 
+removePunctuation(
 convertH
@@ -3630,27 +3630,27 @@
 s(remove
-Punctuation
+Apostrophes
 (filterW
@@ -3686,13 +3686,14 @@
 ))))))))
+)
 ;%0A%7D%0A%0A

fix punctuation bugs with parser