Qual è il modo migliore per estrarre i keyphras da un blocco di testo? Sto scrivendo uno strumento per fare l'estrazione di parole chiave: something like this. Ho trovato alcune librerie per Python e Perl per estrarre n-grammi, ma sto scrivendo questo in Nodo quindi ho bisogno di una soluzione JavaScript. Se non ci sono librerie JavaScript esistenti, qualcuno potrebbe spiegare come farlo, quindi posso semplicemente scriverlo da solo?Estrarre i keyphrase dal testo (1-4 word ngrams)
risposta
mi piace l'idea, così ho implementato: vedi qui sotto (commenti descrittivi sono inclusi).
Anteprima: http://fiddle.jshell.net/WsKMx/
/*@author Rob W, created on 16-17 September 2011, on request for Stackoverflow (http://stackoverflow.com/q/7085454/938089)
* Modified on 17 juli 2012, fixed IE bug by replacing [,] with [null]
* This script will calculate words. For the simplicity and efficiency,
* there's only one loop through a block of text.
* A 100% accuracy requires much more computing power, which is usually unnecessary
**/
var text = "A quick brown fox jumps over the lazy old bartender who said 'Hi!' as a response to the visitor who presumably assaulted the maid's brother, because he didn't pay his debts in time. In time in time does really mean in time. Too late is too early? Nonsense! 'Too late is too early' does not make any sense.";
var atLeast = 2; // Show results with at least .. occurrences
var numWords = 5; // Show statistics for one to .. words
var ignoreCase = true; // Case-sensitivity
var REallowedChars = /[^a-zA-Z'\-]+/g;
// RE pattern to select valid characters. Invalid characters are replaced with a whitespace
var i, j, k, textlen, len, s;
// Prepare key hash
var keys = [null]; //"keys[0] = null", a word boundary with length zero is empty
var results = [];
numWords++; //for human logic, we start counting at 1 instead of 0
for (i=1; i<=numWords; i++) {
keys.push({});
}
// Remove all irrelevant characters
text = text.replace(REallowedChars, " ").replace(/^\s+/,"").replace(/\s+$/,"");
// Create a hash
if (ignoreCase) text = text.toLowerCase();
text = text.split(/\s+/);
for (i=0, textlen=text.length; i<textlen; i++) {
s = text[i];
keys[1][s] = (keys[1][s] || 0) + 1;
for (j=2; j<=numWords; j++) {
if(i+j <= textlen) {
s += " " + text[i+j-1];
keys[j][s] = (keys[j][s] || 0) + 1;
} else break;
}
}
// Prepares results for advanced analysis
for (var k=1; k<=numWords; k++) {
results[k] = [];
var key = keys[k];
for (var i in key) {
if(key[i] >= atLeast) results[k].push({"word":i, "count":key[i]});
}
}
// Result parsing
var outputHTML = []; // Buffer data. This data is used to create a table using `.innerHTML`
var f_sortAscending = function(x,y) {return y.count - x.count;};
for (k=1; k<numWords; k++) {
results[k].sort(f_sortAscending);//sorts results
// Customize your output. For example:
var words = results[k];
if (words.length) outputHTML.push('<td colSpan="3" class="num-words-header">'+k+' word'+(k==1?"":"s")+'</td>');
for (i=0,len=words.length; i<len; i++) {
//Characters have been validated. No fear for XSS
outputHTML.push("<td>" + words[i].word + "</td><td>" +
words[i].count + "</td><td>" +
Math.round(words[i].count/textlen*10000)/100 + "%</td>");
// textlen defined at the top
// The relative occurence has a precision of 2 digits.
}
}
outputHTML = '<table id="wordAnalysis"><thead><tr>' +
'<td>Phrase</td><td>Count</td><td>Relativity</td></tr>' +
'</thead><tbody><tr>' +outputHTML.join("</tr><tr>")+
"</tr></tbody></table>";
document.getElementById("RobW-sample").innerHTML = outputHTML;
/*
CSS:
#wordAnalysis td{padding:1px 3px 1px 5px}
.num-words-header{font-weight:bold;border-top:1px solid #000}
HTML:
<div id="#RobW-sample"></div>
*/
non so un tale libreria in JavaScript, ma la logica è di testo
- divisa in serie
- quindi selezionare e contare
alternativa
- suddivisa in serie
- creare un array secondario
- attraversa ogni elemento della prima matrice
- controllo se l'articolo corrente esiste in matrice secondaria
- se non esiste spinta come chiave
- altro incremento di valore di un elemento avente una chiave = all'elemento ricercato. HTH
Ivo Stoykov
questo doesnt non fare ciò che vogliono im b/c non estrae multi-ngrams parola ... funziona per le singole parole solo –
guarda qui -> http: //valuetype.wordpress .com/2011/08/24/keyword-density-with-javascript/questo è un esempio con un numero di parole ma potrebbe essere facilmente esteso per 3 o 4 parole – i100
- 1. Estrarre nazionalità e paesi dal testo
- 2. Estrarre le parole nei rettangoli dal testo
- 3. Come estrarre gli URL dal testo
- 4. Come estrarre i dati dal grafico matplotlib
- 5. Estrarre la cella di tabella MS Word come immagine?
- 6. Word-Wrap Android Testo del testo EditText
- 7. Estrai testo da file pdf e word
- 8. Come posso estrarre datetime dal testo a mano libera?
- 9. Estrarre la prima e l'ultima colonna dal file di testo
- 10. Ottieni testo sopra la tabella MS Word
- 11. Variazione backgroundcolor testo in Word
- 12. come recuperare i dati dal database SQL in Word 2010?
- 13. Perché la funzione ngrams() fornisce distinti bigram?
- 14. Estrai JSON dal testo
- 15. Come estrarre AST dal codice Objective-C?
- 16. Come estrarre i vettori di velocità dal flusso ottico denso?
- 17. libgps per estrarre i dati dal demone gpsd
- 18. Estrarre i valori dell'intervallo di confidenza dal correlogramma ACF
- 19. Come estrarre i metadati dal file mp3 nello sviluppo ios
- 20. come estrarre i dati dal file xml usando notepad ++?
- 21. U-SQL Impossibile estrarre i dati dal file JSON
- 22. Estrazione dati casella di testo da più file Microsoft Word
- 23. Estrarre nomi da testo (Java)
- 24. Estrarre i dati EXIF come testo utilizzando ImageMagick
- 25. estrarre il testo da tex, rimuovere i tag in lattice
- 26. Come rimuovere i segni diacritici dal testo?
- 27. Colorazione multi-word di testo Javafx
- 28. Come estrarre la frazione diadica dal galleggiante
- 29. Come estrarre le immagini dal file swf?
- 30. ffmpeg per estrarre l'audio dal video
Ho aggiornato il codice per correggere un bug in IE8. Questo bug è stato segnalato via mail, ho incollato la mail e la mia risposta (che offre la correzione e include una spiegazione dettagliata) qui: http://pastebin.com/7Edx88Gp. –
bellissimo, molti anni dopo stai ancora aiutando le persone –