Last active
August 31, 2018 10:17
-
-
Save bee-san/85eb15eba912da4612098db3419beb9e 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 inverseDocumentFrequency(document){ | |
| // calculates the inverse document frequency of every sentence | |
| const words_without_stopwords = prettify(document); | |
| const unique_words_set = uniqueWords(words_without_stopwords); | |
| const sentences = document.split(".").map(item => item.trim()); | |
| sentences[0] = sentences[0].substring(146); | |
| const lengthOfDocuments = sentences.length; | |
| // prettifys each sentence so it doesn't have stopwords | |
| const wordCountAll = countWords(words_without_stopwords); | |
| // counts words of each sentence | |
| // as each sentence is a document | |
| wordCountSentences = []; | |
| for (let i = 0; i <= lengthOfDocuments - 1; i ++){ | |
| wordCountSentences.push(countWords(prettify(sentences[i]))); | |
| } | |
| // calculate TF values of all documents | |
| let IDFVals = {}; | |
| // how many times that word appears in all sentences (documents) | |
| wordCountSentencesLength = wordCountSentences.length; | |
| // for every unique word | |
| for (let i = 0; i <= unique_words_set.length - 1; i++){ | |
| let temp_add = 0; | |
| // count how many times unique word appears in all sentences | |
| for (let x = 0; x <= wordCountSentencesLength - 1; x++){ | |
| if (unique_words_set[i] in wordCountSentences[x]){ | |
| temp_add =+ 1; | |
| } | |
| } | |
| IDFVals[unique_words_set[i]] = Math.log10(wordCountAll[unique_words_set[i]] / temp_add); | |
| } | |
| let IDFSentences = {}; | |
| // for every sentence | |
| for (let i = 0; i <= lengthOfDocuments - 1; i ++){ | |
| // for every word in that sentence | |
| let sentence_split_words = sentences[i].split(" "); | |
| // get the assiocated IDF values of each word | |
| // temp.add is the "IDF" 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++){ | |
| // if the word is not a stopword, get the assiocated IDF value and add it to temp_add | |
| if (sentence_split_words[x].toLowerCase() in IDFVals){ | |
| // adds all the IDF values up | |
| temp_add = temp_add + IDFVals[sentence_split_words[x].toLowerCase()]; | |
| } | |
| else{ | |
| // nothing, since it's a stop word. | |
| } | |
| } | |
| IDFSentences[sentences[i]] = temp_add / words_no_stop_words_length; | |
| } | |
| return IDFSentences; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment