Skip to content

Instantly share code, notes, and snippets.

@prykon prykon/tfidf.js
Last active Jun 14, 2019

What would you like to do?
TF-IDF SEO function for Google Sheets
* Return the time that the referenced cell or range was last changed.
* Initial use shows current time.
* @param {"keyword"} term La keyword a analizar
* @param {"texto"} flow El texto que contiene la keyword
* @param {"corpus"} document Todos los textos de tu sitio
* @param {"verbose"} verbose VERDADERO o FALSO: muestra un detalle del análisis
* @customfunction
function tfidf(term, flow, document, verbose) {
flow = flow.toLowerCase();
term = term.toLowerCase();
var flow_array = flow.trim().split(/\s+/);
var words_in_flow = flow_array.length;
//Contar veces que figura el term en el flow
var term_repetition_in_flow = 0;
for(var t=0; t<flow_array.length;t++){
if(flow_array[t] == term){term_repetition_in_flow++;}
//Encontrar el TF
var tf = term_repetition_in_flow / words_in_flow;
//Contar cantidad de docs con el término
var docs_with_term = 0;
for(var c = 0; c<document.length;c++){
var current_document = []
current_document[c] = JSON.stringify(document[c]);
current_document[c] = current_document[c].toLowerCase();
if(current_document[c].indexOf(term) > -1) {docs_with_term++;}
var cant_documentos = document.length;
terms_in_document = document.join().split(/\s+/).length+1;
var idf = Math.log(cant_documentos/docs_with_term)/Math.log(10);
var tfidf = tf*idf;
return "Term repetition in flow: "+term_repetition_in_flow+"\nWords in flow: "+words_in_flow+"\nDocs with term: "+docs_with_term+"\nTotal docs in corpus: "+cant_documentos+"\nTF: "+tf+"\nIDF: "+idf+"\nFormula: "+tf+" * log("+cant_documentos+"/"+docs_with_term+")\nTF-IDF: "+tfidf;}
if(!verbose){return tfidf;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.