Skip to content

Instantly share code, notes, and snippets.

@djfarrelly
Created August 12, 2014 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djfarrelly/eb060dc4f04a0a0a45a9 to your computer and use it in GitHub Desktop.
Save djfarrelly/eb060dc4f04a0a0a45a9 to your computer and use it in GitHub Desktop.
requirebin sketch
var Levenshtein = require('levenshtein');
var titles = ["15 Twitter Hacks That Will Turn You Into a Twitter Ninja", "The Big List of Twitter Tools: 59 Free Twitter Tools and Apps to Fit Any Need", "Introducing Groups: The Easiest Way to Manage All Your Social Media Accounts With One Click", "What Really Happens When Someone Clicks Your Facebook Like Button"];
var currentTitle = "59 Free Twitter Tools and Apps That Do Pretty Much Everything";
function matchingWordsPercentage(orig, test) {
var origWords = orig.toLowerCase().split(' ');
var testWords = test.toLowerCase().split(' ');
testWords = testWords.filter(function(word){
return ['the','to','a','and', 'of'].indexOf(word) === -1;
});
var matchingWords = origWords.filter(function(word){
return testWords.indexOf(word) !== -1;
});
console.log(matchingWords.length, testWords.length);
return matchingWords.length / testWords.length;
}
titles.forEach(function(title){
var l = new Levenshtein(currentTitle, title);
var m = matchingWordsPercentage(currentTitle, title);
var p = document.createElement('p');
p.textContent = title + ' l=' + l.distance + ' m=' + m;
document.body.appendChild(p)
});
require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({NXoJGc:[function(require,module,exports){(function(root,factory){if(typeof define=="function"&&typeof define.amd=="object"&&define.amd){define(function(){return factory(root)})}else if(typeof module=="object"&&module&&module.exports){module.exports=factory(root)}else{root.Levenshtein=factory(root)}})(this,function(root){function forEach(array,fn){var i,length;i=-1;length=array.length;while(++i<length)fn(array[i],i,array)}function map(array,fn){var result;result=Array(array.length);forEach(array,function(val,i,array){result.push(fn(val,i,array))});return result}function reduce(array,fn,accumulator){forEach(array,function(val,i,array){accumulator=fn(val,i,array)});return accumulator}function Levenshtein(str_m,str_n){var previous,current,matrix;matrix=this._matrix=[];if(str_m==str_n)return this.distance=0;else if(str_m=="")return this.distance=str_n.length;else if(str_n=="")return this.distance=str_m.length;else{previous=[0];forEach(str_m,function(v,i){i++,previous[i]=i});matrix[0]=previous;forEach(str_n,function(n_val,n_idx){current=[++n_idx];forEach(str_m,function(m_val,m_idx){m_idx++;if(str_m.charAt(m_idx-1)==str_n.charAt(n_idx-1))current[m_idx]=previous[m_idx-1];else current[m_idx]=Math.min(previous[m_idx]+1,current[m_idx-1]+1,previous[m_idx-1]+1)});previous=current;matrix[matrix.length]=previous});return this.distance=current[current.length-1]}}Levenshtein.prototype.toString=Levenshtein.prototype.inspect=function inspect(no_print){var matrix,max,buff,sep,rows;matrix=this.getMatrix();max=reduce(matrix,function(m,o){return Math.max(m,reduce(o,Math.max,0))},0);buff=Array((max+"").length).join(" ");sep=[];while(sep.length<(matrix[0]&&matrix[0].length||0))sep[sep.length]=Array(buff.length+1).join("-");sep=sep.join("-+")+"-";rows=map(matrix,function(row){var cells;cells=map(row,function(cell){return(buff+cell).slice(-buff.length)});return cells.join(" |")+" "});return rows.join("\n"+sep+"\n")};Levenshtein.prototype.getMatrix=function(){return this._matrix.slice()};Levenshtein.prototype.valueOf=function(){return this.distance};return Levenshtein})},{}],levenshtein:[function(require,module,exports){module.exports=require("NXoJGc")},{}]},{},[]);var Levenshtein=require("levenshtein");var titles=["15 Twitter Hacks That Will Turn You Into a Twitter Ninja","The Big List of Twitter Tools: 59 Free Twitter Tools and Apps to Fit Any Need","Introducing Groups: The Easiest Way to Manage All Your Social Media Accounts With One Click","What Really Happens When Someone Clicks Your Facebook Like Button"];var currentTitle="59 Free Twitter Tools and Apps That Do Pretty Much Everything";function matchingWordsPercentage(orig,test){var origWords=orig.toLowerCase().split(" ");var testWords=test.toLowerCase().split(" ");testWords=testWords.filter(function(word){return["the","to","a","and","of"].indexOf(word)===-1});var matchingWords=origWords.filter(function(word){return testWords.indexOf(word)!==-1});console.log(matchingWords.length,testWords.length);return matchingWords.length/testWords.length}titles.forEach(function(title){var l=new Levenshtein(currentTitle,title);var m=matchingWordsPercentage(currentTitle,title);var p=document.createElement("p");p.textContent=title+" l="+l.distance+" m="+m;document.body.appendChild(p)});
{
"name": "requirebin-sketch",
"version": "1.0.0",
"dependencies": {
"levenshtein": "1.0.4"
}
}
<style type='text/css'>html, body { margin: 0; padding: 0; border: 0; }
body, html { height: 100%; width: 100%; }</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment