Jui
Jui

Reputation: 11

Eliminate some words from an array and highlight the remaining words

The purpose of my program is to eliminate prepositions, adverbs, conjunctions and auxillary verbs without making use of any pos tagger. I need to highlight the left over words in the original sentence. The browser should display the paragraph elements with ids 'stat' and 'ans'. Stat should display the entire paragraph with the words which are present in 'ans' as highlighted. Highlighting can be done in any way like background-color or font-color. I am not able to do this highlighting part.

<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>

Upvotes: 0

Views: 63

Answers (1)

Emil S. J&#248;rgensen
Emil S. J&#248;rgensen

Reputation: 6366

Is this what you mean?

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>

Upvotes: 1

Related Questions