我有一个表格,它是使用 jQuery 插件 Datatables 从粘贴到 textarea 中的任何文本生成的。我想忽略数字以及大量常用词,例如“and, or, for, the, but, etc.” 我该怎么办?
这是我当前的 JavaScript:
<script>
$( document ).ready( function() {
$('form').submit(function(event){
event.preventDefault();
var
keyword_list = $('#searchtext').val().split(" "),
word_list = $('#searchtext').val().split(" "),
nwords = word_list.length;
$('#result').html(nwords + " Total Words in Article");
keyword_dict = {};
for (var i = 0, w; w = keyword_list[i]; i++) {
var w = w.replace(/\W/g,'');
keyword_dict[w] = 0;
}
for (var i = 0, w; w = word_list[i]; i++) {
var w = w.replace(/\W/g,'');
for(var keyword in keyword_dict){
if (keyword == w){
keyword_dict[w] += 1;
}
}
}
items = '<thead><th>unique keywords</th><th>occurance</th><th>percent of text</th></thead>';
for (keyword in keyword_dict){
var occ = keyword_dict[keyword]*100/nwords;
var c=''
items += '<tr class="'+c+'"><td>'+keyword+'</td><td>'+keyword_dict[keyword]+'</td><td>'+occ.toPrecision(2)+'</td></tr>';
}
console.log(keyword_dict);
$('#result').append('<table>'+items+'<table>');
$('#result table').dataTable();
});
});
</script>
这是HTML:
<form>
<label>Article:</label><br>
<textarea rows="25" cols="100" id="searchtext"></textarea><br>
<input class="btn btn-success" id="go" type="submit" value="Submit"></input>
</form>
<div id="result">
</div>