Last active
April 18, 2020 07:58
-
-
Save bee-san/5651fb2b54e816c4b147b725507b2380 to your computer and use it in GitHub Desktop.
This file contains 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 TFIDF(documents){ | |
// calculates TF*IDF | |
const TFVals = termFrequency(documents); | |
const IDFVals = inverseDocumentFrequency(documents); | |
let TFidfDict = {}; | |
for (const [key, value] of Object.entries(TFVals)){ | |
if (key in IDFVals){ | |
TFidfDict[key] = TFVals[key] * IDFVals[key]; | |
} | |
} | |
let max = 0.0; | |
let max2 = 0.0; | |
let max3 = 0.0; | |
let max_sentence = ""; | |
let max2Sent = ""; | |
let max3Sent = ""; | |
// finds the top 3 sentences in TFidfDict | |
for (const [key, value] of Object.entries(TFidfDict)){ | |
if (TFidfDict[key] > max){ | |
max = TFidfDict[key]; | |
max_sentence = key; | |
} | |
else if (TFidfDict[key] > max2 && TFidfDict[key] < max){ | |
max2 = TFidfDict[key]; | |
max2Sent = key; | |
} | |
else if (TFidfDict[key] > max3 && TFidfDict[key] < max2 && TFidfDict[key] < max){ | |
max3 = TFidfDict[key]; | |
max3Sent = key; | |
} | |
} | |
return ("<br>" + "•" + max_sentence + "<br><br>" + "•" + max2Sent + "<br><br>" + "•" + max3Sent); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment