2011-08-16 12 views
9

टेक्स्ट के ब्लॉक से कीफ्रेज़ निकालने का सबसे अच्छा तरीका क्या है? मैं कीवर्ड निष्कर्षण करने के लिए एक उपकरण लिख रहा हूं: something like this। मुझे पाइथन और पर्ल के लिए एन-ग्राम निकालने के लिए कुछ पुस्तकालय मिल गए हैं, लेकिन मैं इसे नोड में लिख रहा हूं इसलिए मुझे जावास्क्रिप्ट समाधान की आवश्यकता है। यदि कोई मौजूदा जावास्क्रिप्ट पुस्तकालय नहीं है, तो क्या कोई यह बता सकता है कि ऐसा कैसे करें ताकि मैं इसे स्वयं लिख सकूं?पाठ से कीफ्रेज़ निकालें (1-4 शब्द ngrams)

उत्तर

15

मैं विचार पसंद है, इसलिए मैं इसे लागू किया गया है: नीचे देखें (वर्णनात्मक टिप्पणियां शामिल हैं)।
पूर्वावलोकन पर: 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> 
*/ 
+0

मैंने IE8 में एक बग को ठीक करने के लिए कोड अपडेट किया है। मेल के माध्यम से यह बग रिपोर्ट किया गया था, मैंने मेल और मेरी प्रतिक्रिया चिपका दी है (जो फिक्स ऑफ़र करता है और एक विस्तृत स्पष्टीकरण भी शामिल करता है): http://pastebin.com/7Edx88Gp। –

+0

सुंदर, कई सालों बाद आप अभी भी लोगों की मदद कर रहे हैं –

0

मैं जावास्क्रिप्ट में इस तरह के एक पुस्तकालय पता नहीं है लेकिन तर्क

  • तो तरह सरणी
  • में

    1. विभाजन पाठ और गिनती

    वैकल्पिक रूप से

    1. सरणी में विभाजन
    2. द्वितीयक सरणी बनाएं
    3. जांच करें कि क्या वर्तमान आइटम माध्यमिक सरणी में मौजूद है
    4. = एक आइटम की कुंजी
    5. बाकी वृद्धि मूल्य एक महत्वपूर्ण होने के रूप में धक्का यह मौजूद नहीं है, तो मांग की मद से 1 सरणी के प्रत्येक आइटम traversing। HTH

    इवो Stoykov

    +0

    यह does not नहीं कर क्या im चाहते b/c यह बहु शब्द ngrams ... यह एक शब्द के लिए काम करता निकालने नहीं है केवल –

    +1

    यहाँ देखो -> http: //valuetype.wordpress .com/2011/08/24/कीवर्ड-घनत्व-साथ-जावास्क्रिप्ट/यह एक शब्द गणना के साथ एक नमूना है लेकिन इसे आसानी से 3 या 4 शब्दों के लिए बढ़ाया जा सकता है – i100