Skip to content

Instantly share code, notes, and snippets.

@son0fhobs
Last active August 29, 2015 13:57
Show Gist options
  • Save son0fhobs/9481866 to your computer and use it in GitHub Desktop.
Save son0fhobs/9481866 to your computer and use it in GitHub Desktop.
SERPs - Rating title by search term match
// stop words listed at end
// parse, then filter out empty ones. Remove apostrphes both from stop words and search term
// Value based on if term exists and how close it is to the beginning, based on linear weight.
var search_term = 'seo serp bookmarklet';
var serp_title = 'my seo serp bookmarklet for title value';
// remove stop words
search_term = remove_stop_words(search_term);
serp_title = remove_stop_words(serp_title);
// convert to arrays
search_term = search_term.split(' ');
serp_title = serp_title.split(' ');
// var serp_title = ['blog', 'post', 'title'];
var percentages = [];
var serch_term_length = search_term.length;
var title_length = serp_title.length;
var i = 0;
var max = serch_term_length;
for(i=0;i<max;i++){
// console.log(search_term[i]);
// console.log(serp_title);
if( ~serp_title.indexOf(search_term[i]) ){
keyword_pos = serp_title.indexOf(search_term[i]);
console.log(keyword_pos);
percentages[i] = get_keyword_percentage(keyword_pos, i, title_length);
percentages[i] = weight_percentage(percentages[i], 1);
}else{
percentages[i] = 0;
}
// console.log(percentages[i]);
}
console.log('sum = '+get_array_sum(percentages));
console.log('length = '+serch_term_length);
total_percent = get_array_sum(percentages)/serch_term_length;
console.log(total_percent);
function get_keyword_percentage(word_position, index, total_words){
var percent = 1 - ( (word_position-index) / (total_words-index) );
return percent;
}
function weight_percentage(percent, weight){
if(!percent){ return 0; }
// this gives extra value to offset if word doesn't appear. If 1 + 50%.
var weighted_percent = ( percent + weight )/weight+1;
return weighted_percent;
}
function get_array_sum(num_array){
var sum = 0;
var max = num_array.length;
var i=0;
for(i=0;i<max;i++){
sum += num_array[i];
}
return sum;
}
function remove_stop_words(string){
// check if array, if so remove from array instead
return string;
}
// json.parse()
stop_words = '["A","","a","able","about","across","after","all","almost","also","am","ago ","among","an","and","ahead ","any","always ","are","as","at"," ","B",""," ","be","because","been","best","between ","but","before ","by","becomes "," ","C",""," ","can","cannot","could","could\'nt"," "," ","D",""," ","dear","did","done","don\'t ","do","does","doing"," "," ","E",""," ","either","else","ever","every","each","elsewhere "," ","F",""," ","for","from","further","forward "," ","G",""," ","get","got","go","gone","goes","going "," ","H",""," ","had","hadn\'t ","has","have","haven\'t ","he","her","hers","him","his","how","hence ","however"," ","I",""," ","i","if","in","inc ","into","itself ","is","i\'m ","it","ie","i\'ll","inward ","its"," ","J",""," ","just "," ","L",""," ","like","likely","let","let\'s","likewise"," ","M",""," ","may","me","might","most","must","makes","make ","my","mainly","maybe "," ","N",""," ","neither","no","now ","nor","not","near","needs"," "," ","O",""," ","of","off","often","on","over ","oh ","only","overall ","or","other","our","own"," ","R",""," ","rather","regards","right","really"," "," ","S",""," ","said","say","self ","says","see ","she","should","selves ","seem ","since","so","seeming ","some"," ","T",""," ","than","that","the","their","thereby ","them","then","they\'d ","there","these","they","that\'s ","this","tis","to","they\'ll ","too","twas","think","thorough "," ","U",""," ","us","unless","unlike","up","upon"," "," ","W",""," ","wants","was","we","were","what","when","where","which","while","who","whom","will","we\'ll ","with","would","whose ","whenever "," ","Y",""," ","yet","you","your","yours"]';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment