Skip to content

Instantly share code, notes, and snippets.

@bee-san
Last active April 18, 2020 07:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bee-san/5651fb2b54e816c4b147b725507b2380 to your computer and use it in GitHub Desktop.
Save bee-san/5651fb2b54e816c4b147b725507b2380 to your computer and use it in GitHub Desktop.
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