Last active
August 31, 2018 09:37
-
-
Save bee-san/1c9cebb3d5ba1a75b692522dc5f384c2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function termFrequency(document){ | |
| // calculates term frequency of each sentence | |
| words_without_stopwords = prettify(document); | |
| // gets rid of trailing spaces | |
| const sentences = document.split(".").map(item => item.trim()); | |
| sentences[0] = sentences[0].substring(146); | |
| const TFVals = countWords(words_without_stopwords) | |
| const unique_words = uniqueWords(words_without_stopwords); | |
| // actually makes it TF values according to formula | |
| for (const [key, value] of Object.entries(TFVals)){ | |
| TFVals[key] = TFVals[key] / words_without_stopwords.length; | |
| } | |
| // splits it up into sentences now | |
| var TFSentences = {}; | |
| // for every sentence | |
| for (let i = 0; i <= sentences.length - 1; i ++){ | |
| // for every word in that sentence | |
| let sentence_split_words = sentences[i].split(" "); | |
| // get the assiocated TF values of each word | |
| // temp.add is the "TF" value of a sentence, we need to divide it at the end | |
| let temp_add = 0.0; | |
| let words_no_stop_words_length = prettify(sentences[i]).length; | |
| for (let x = 0; x <= sentence_split_words.length - 1; x++){ | |
| // get the assiocated TF value and add it to temp_add | |
| if (sentence_split_words[x].toLowerCase() in TFVals){ | |
| // adds all the TF values up | |
| temp_add = temp_add + TFVals[sentence_split_words[x].toLowerCase()]; | |
| } | |
| else{ | |
| // nothing, since it's a stop word. | |
| } | |
| } | |
| // TF sentences divide by X number of items on top | |
| TFSentences[sentences[i]] = temp_add / words_no_stop_words_length; | |
| } | |
| return TFSentences; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment