我的程序的目的是在不使用任何pos标签的情况下消除介词,副词,连词和辅助动词。我需要在原句中突出显示左侧的单词。 浏览器应显示带有ids" stat'的段落元素。和' ans'。统计数据应显示整个段落,其中包含“' ans'如突出显示。可以以背景颜色或字体颜色等任何方式进行突出显示。 我无法做这个突出显示部分。
<body>
//Original Text
<p id="sentence" hidden>Particle physics (also high energy physics) is the branch of physics that studies the nature of the particles that constitute matter (particles with mass) and radiation (massless particles). Although the word "particle" can refer to various types of very small objects (e.g. protons, gas particles, or even household dust), "particle physics" usually investigates the irreducibly smallest detectable particles and the irreducibly fundamental force fields necessary to explain them. </p>
<p id="stat"> </p>
<p id="ans"> </p>
<style type="text/css">
.span {
background-color: lightgreen;
}
</style>
<script>
//WORDS TO BE ELIMINATED
var prep = ['With','with','At','at','From','from','Into','into','During','during','Including','including','Until','until','against','Against','Among','among','Throughout','throughout','Despite','despite','Towards','towards','Upon','upon','Concerning','concerning','Of','of','To','to','In','in','For','for','On','on','By','by','About','about','Like','like','Through','through','Over','over','Before','before','Between','between','after','Since','since','Without','without','Under','under','Within','within','Along','along','Following','following','Across','across','Behind','behind','Beyond','beyond','Plus','plus','Except','except','Up','up','Out','out','Around','around','Down','down','Off','off','Above','above','Near','near'];
var auxilVerb = ['do','does','did','has','have','had','is','am','are','was','were','be','being','been','may','must','might','should','could','would','shall','will','can'];
var conj = ['And','and','Or','or','But','but','Nor','nor','So','so','For','for','Yet','yet','After','after','Although','although','As','as','As if','as if','As Long As','as long as','Because','because','Before','before','Even if','even if','Even though','even though','Once','once','Since','since','So that','so that','Though','though','Till','till','Unless','unless','Until','until','What','what','When','Whenever','whenever','Wherever','wherever','Whether','Whether','While'];
var artc= ['A','a','An','an','The','the'];
var one = prep.concat(prep );
var two = one.concat(auxilVerb);
var three = two.concat(conj);
var elim = three.concat(artc);
//Split sentence into array of words for easier manipulation
var line1 = $('#sentence').html();
var line = line1.toString().split(" ");
var newStat = line.slice();
var emptyArr = [];
var words = line.slice();
//Eliminating the undesired words
aLoop:for(var k=0;k<line.length;k++){
bLoop:for (var t=0;t<elim.length;t++){
if(line[k]===elim[t]){
words[k] = ' ';
emptyArr.push(k);
continue aLoop;
} else {
//console.log(k);
words[k] = line[k];
//Expecting to add <span> tag to the words which should be highlighted
newStat.splice(k,0,"<span class='span'>");
newStat.splice(k+2,0,"</span>");
}
}
}
console.log(newStat);
console.log(words);
$('#stat').html(newStat.join(" ")); //DISPLAY the original sentence with highlighted words
$('#ans').html(words.join(" ")); //DISPLAY the left over words
</script>
</body>
答案 0 :(得分:1)
这是你的意思吗?
var eliminate = (function () {
var prep = ['With', 'with', 'At', 'at', 'From', 'from', 'Into', 'into', 'During', 'during', 'Including', 'including', 'Until', 'until', 'against', 'Against', 'Among', 'among', 'Throughout', 'throughout', 'Despite', 'despite', 'Towards', 'towards', 'Upon', 'upon', 'Concerning', 'concerning', 'Of', 'of', 'To', 'to', 'In', 'in', 'For', 'for', 'On', 'on', 'By', 'by', 'About', 'about', 'Like', 'like', 'Through', 'through', 'Over', 'over', 'Before', 'before', 'Between', 'between', 'after', 'Since', 'since', 'Without', 'without', 'Under', 'under', 'Within', 'within', 'Along', 'along', 'Following', 'following', 'Across', 'across', 'Behind', 'behind', 'Beyond', 'beyond', 'Plus', 'plus', 'Except', 'except', 'Up', 'up', 'Out', 'out', 'Around', 'around', 'Down', 'down', 'Off', 'off', 'Above', 'above', 'Near', 'near'];
var auxilVerb = ['do', 'does', 'did', 'has', 'have', 'had', 'is', 'am', 'are', 'was', 'were', 'be', 'being', 'been', 'may', 'must', 'might', 'should', 'could', 'would', 'shall', 'will', 'can'];
var conj = ['And', 'and', 'Or', 'or', 'But', 'but', 'Nor', 'nor', 'So', 'so', 'For', 'for', 'Yet', 'yet', 'After', 'after', 'Although', 'although', 'As', 'as', 'As if', 'as if', 'As Long As', 'as long as', 'Because', 'because', 'Before', 'before', 'Even if', 'even if', 'Even though', 'even though', 'Once', 'once', 'Since', 'since', 'So that', 'so that', 'Though', 'though', 'Till', 'till', 'Unless', 'unless', 'Until', 'until', 'What', 'what', 'When', 'Whenever', 'whenever', 'Wherever', 'wherever', 'Whether', 'Whether', 'While'];
var artc = ['A', 'a', 'An', 'an', 'The', 'the'];
return function testAgainst(str) {
for (var index = 0; index < prep.length; index++) {
var p = prep[index];
str = str.replace(new RegExp("( |$|^)" + p + "( |$|^)", "ig"), ' ');
}
for (var index = 0; index < auxilVerb.length; index++) {
var p = auxilVerb[index];
str = str.replace(new RegExp("( |$|^)" + p + "( |$|^)", "ig"), ' ');
}
for (var index = 0; index < conj.length; index++) {
var p = conj[index];
str = str.replace(new RegExp("( |$|^)" + p + "( |$|^)", "ig"), ' ');
}
for (var index = 0; index < artc.length; index++) {
var p = artc[index];
str = str.replace(new RegExp("( |$|^)" + p + "( |$|^)", "ig"), ' ');
}
return str.replace(new RegExp(" +", "ig"), " ").replace(new RegExp("( )*$( )*|( )*^( )*", "ig"), "");
};
})();
var input = document.getElementById("input");
var output = document.getElementById("output");
function run() {
var val = input.value.toString();
var goodWords = eliminate(val);
val = val.split(" ");
var html = "";
for (var i = 0; i < val.length; i++) {
if (goodWords.indexOf(val[i]) >= 0) {
html += " <b>" + val[i] + '</b> ';
}
else {
html += ' ' + val[i] + ' ';
}
output.innerHTML = html.replace(new RegExp(" +", "ig"), " ").replace(new RegExp("( )*$( )*|( )*^( )*", "ig"), "");
}
}
input.onchange = run;
input.onkeyup = run;
run();
<input value="The quick brown fox jumps over the lazy dog" id="input" />
<p id="output"></p>