Last active
August 29, 2015 14:25
-
-
Save LeonardoCiaccio/fb75afe22f48667edcbf to your computer and use it in GitHub Desktop.
Conta la presenza di parole in una stringa
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
/* | |
Density 1.0.0 | |
Misura la densità delle parole, ovvero conta la presenza di ogni parola | |
// Default RegExp // | |
([a-zA-Z0-9]{2,100}[a-zA-Z0-9\-]{0,100}[a-zA-Z0-9]{3,100}){1,100} | |
// Restituisce un array ordinato dal valore più grande al più piccolo | |
D( "Testo in cui cercare, unico parametro obbligatorio", RegExp, Integer ); | |
// Esempi | |
var words = D( "Questa è una frase" ); | |
// [["questa",1],["frase",1]] | |
var words = D( "Queste sono parole, dove scelgo di restituire solo parole con quantità di almeno 2 prenze", 2 ); | |
// [["parole",2]] | |
var words = D( "Scelgo di trovare parole con numeri tipo 123456 ", /[0-9]{3,100}/gi ); | |
// [["123456",1]] | |
Minify : | |
!function(){function r(r,n){var t=[],e=r.length,o=0;for(o;e>o;o++)n(r[o])&&t.push(r[o]);return t}function n(r,n){var t=r.match(n);if(!t)return[];for(var e=0;e<t.length;e++){var a=t[e].toLowerCase();o[a]=o[a]?o[a]+1:1}var i=[];for(var u in o)i.push([u,o[u]]);return i.sort(function(r,n){return n[1]-r[1]}),i}function t(r){return r%1===0}var e=/([a-zA-Z0-9]{2,100}[a-zA-Z0-9\-]{0,100}[a-zA-Z0-9]{3,100}){1,100}/gi,o={};window.D=function(o,a,i){if(!o||"string"!=typeof o||a&&!t(a)&&a instanceof RegExp==!1)throw new Error("Density require 'string', Regexp 2° parameters optional, Int 3° parameters optional ");i=t(a)?a:i&&t(i)?i:null,a=a instanceof RegExp==!0?a:e;var u=n(o,a);return t(i)&&i>0?r(u,function(r){return r[1]>=i}):u}}(); | |
Bookmarklet con un risultato minimo di 3 presenze : | |
javascript:!function(){function r(r,n){var t=[],e=r.length,o=0;for(o;e>o;o++)n(r[o])&&t.push(r[o]);return t}function n(r,n){var t=r.match(n);if(!t)return[];for(var e=0;e<t.length;e++){var a=t[e].toLowerCase();o[a]=o[a]?o[a]+1:1}var i=[];for(var u in o)i.push([u,o[u]]);return i.sort(function(r,n){return n[1]-r[1]}),i}function t(r){return r%1===0}var e=/([a-zA-Z0-9]{2,100}[a-zA-Z0-9\-]{0,100}[a-zA-Z0-9]{3,100}){1,100}/gi,o={};window.D=function(o,a,i){if(!o||"string"!=typeof o||a&&!t(a)&&a instanceof RegExp==!1)throw new Error("Density require 'string', Regexp 2° parameters optional, Int 3° parameters optional ");i=t(a)?a:i&&t(i)?i:null,a=a instanceof RegExp==!0?a:e;var u=n(o,a);return t(i)&&i>0?r(u,function(r){return r[1]>=i}):u}}(),alert(JSON.stringify(D(document.body.innerText,3))); | |
*/ | |
( function(){ | |
var reWords = /([a-zA-Z0-9]{2,100}[a-zA-Z0-9\-]{0,100}[a-zA-Z0-9]{3,100}){1,100}/gi, | |
words = {}; | |
// Filtra un array | |
function __grep( items, callback ){ | |
var filtered = [], | |
len = items.length, | |
i = 0; | |
for( i; i < len; i++ ){ | |
if( callback( items[ i ] ) )filtered.push( items[ i ] ); | |
} | |
return filtered; | |
}; | |
// Conta le parole e le restituisce in un array ordinato [ parola, quantità ] | |
function __countWords( text, search ){ | |
// Controllo e aggiungo le singole parole | |
var w, | |
all = text.match( search ); | |
if( !all )return []; | |
// Contiamo le parole | |
for( var i = 0; i < all.length; i++ ){ | |
var ident = all[ i ].toLowerCase(); | |
words[ ident ] = ( !words[ ident ] ) ? 1 : words[ ident ] + 1 ; | |
} | |
// Le ordiniamo in ordine decrescente | |
var sortable = []; | |
for( var word in words ){ | |
sortable.push( [ word, words[ word ] ] ); | |
} | |
sortable.sort( function( a, b ){ return b[ 1 ] - a[ 1 ] } ); | |
return sortable; | |
}; | |
// Un intero ? | |
function __isInt( n ){ | |
return n % 1 === 0; | |
}; | |
window.D = function( text, search, candidate ){ | |
// Controllo i parametri, tutto ok ? | |
if( !text || typeof text !== "string" || | |
( search && !__isInt( search ) && search instanceof RegExp === false ) )throw new Error( "Density require 'string', Regexp 2° parameters optional, Int 3° parameters optional " ); | |
// Controllo che i valori siano in fase | |
candidate = ( __isInt( search ) ) ? search : ( candidate && __isInt( candidate ) ) ? candidate : null; | |
search = ( search instanceof RegExp === true ) ? search : reWords; | |
// Contiamo le parole | |
var results = __countWords( text, search ); | |
// Filtriamo per quantità ? | |
return ( __isInt( candidate ) && candidate > 0 ) ? __grep( results, function( word ){ | |
return ( word[ 1 ] >= candidate ); | |
} ) : results; | |
}; | |
} )(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment