0

hi I have a few problems:

  1. What might I get the word around the word selected, if the word before and after the selected word given limits only 20 words that surround the selected word?
  2. how to get the correct position if the word in a paragraph have the same word, for example I have a sentence like this: foo laa foo doo then I choose the word "foo" whose position is in between the words laa and doo?
  3. how to get word from a different paragraph?


    for example:
    p1 : I like the red shirt
    p2: my mother did not like the red shirt


    the word I selected is "mother", and I have to take 10 words around the word "mother" that is "I like the red dress" and "I do not like the red shirt."

notes: question No. 2 is able to use the nextSibling and previousSibling?

this is my code i try :

<html>
<head>
<script type="text/javascript">
function getElements(){
  var x = document.getElementsByTagName("body");
  x = x[0].innerHTML;

  x = x.replace(/&(lt|gt);/g, function (strMatch, p1){
            return (p1 == "lt")? "<" : ">";});
  var str  = x.replace(/<\/?[^>]+(>|$)/g, "");



  var emailPattern = /[_a-zA-Z0-9\.]+@[\.a-zA-Z0-9]+\.[a-zA-Z]+/gi;
  var urlPattern = /[a-z]+:\/\/[^\s]+/gi;
  var numberOrSymbolPattern = /[0-9\.,!@#\$%\^\&*\(\)`~_\-=\+|\\{}\[\]\s:;<>\?\/]+/gi;

  //////alert(str);

  var str  = str.replace(emailPattern , " ");
  var str  = str.replace(urlPattern , " ");
  var str  = str.replace(numberOrSymbolPattern , " ");
  //////alert(str);
  var str  = str.replace(/[\n\f\r\t]/g, " ");

   var hilangtandabaca = str.replace(/[.!:;'",?]/g," ");

   var kataptg = hilangtandabaca;
   //alert(kataptg);
   var kata = new Array();
    kata[0] = " is ";
    kata[1] = " the ";
    kata[3] = " of ";
    kata[4] = " a ";
    kata[5] = " or ";
    kata[6] = " for ";
    kata[7] = " from ";
    kata[8] = " in ";
    kata[9] = " this ";
    kata[10] = " and ";
    kata[11] = " on ";
    kata[12] = " with ";
    kata[13] = " my ";

 for(var i=0,regex; i<kata.length; i++){
        var regex = new RegExp(kata[i],"gi");
        kataptg = kataptg.replace(regex," ");
    }

   var select = getSelected()+ "";
   alert(select);
   var index = kataptg.indexOf(select);
   //alert("indeks select text:" + index);
   if (index >= 0) {
    var strBefore = "";
    var strAfter = "";
    //var strOri ="";
    //if (index = -1)
    //strOri = kataptg.substr(index);
    //alert(strOri);
    if (index > 0)
    strBefore = kataptg.substr(0, index);
    //alert(strBefore);
    if (index < kataptg.length - 1)
    strAfter = kataptg.substr(index + select.length, kataptg.length - (index + select.length));
    //alert(strAfter);
    alert("Before: " + strBefore + "\nAfter: " + strAfter);
   }

}


function getSelected() {
   var userSelection;
   if (window.getSelection) {
      userSelection = window.getSelection();
   } else if (document.selection) {
       userSelection = document.selection.createRange();
   }
   return userSelection;
}


</script>
</head>                        
<body>
<h2>About</h2>
<p> my email : a@a.a
<p> my url http://id.wikipedia.org/wiki/URL
<p> my telepon number = 081330782
<p>okey In agriculture, the harvest is the process of gathering mature crops from the fields. Reaping is the cutting of grain or pulse for harvest, typically using a scythe, sickle, or reaper.[1] The harvest marks the end of the growing season, or the growing cycle for a particular crop, and this is the focus of seasonal celebrations of on many religions. On smaller farms with minimal mechanization, harvesting is the most labor-intensive activity of the growing season great yeah. !:;'",?</p>

<p>
<input type="button" onclick="getElements()" value="ambil select text" />
</p>

</body>
</html>
user495688
  • 973
  • 2
  • 15
  • 25
  • I've given you a sensible answer to a question very similar to this before (http://stackoverflow.com/questions/4332753/how-to-get-selected-text-but-can-i-get-surrounding-context-in-javascript/4333439#4333439), which you seem to have ignored. Why did you ignore it? If you didn't understand it, you can ask for clarification in a comment. – Tim Down Dec 05 '10 at 11:59

1 Answers1

1

This is a perfect example of JavaScript's innerHTML and split() methods. You can loop through the content of all of the p elements. Here's an example of searching in the first paragraph:

contentArray = document.getElementByTagName('p')[0].innerHTML.split(' ')

split(' ') splits the content of the element into an array, separating by the spaces. innerHTML is self explanatory.

Now, to find your words. indexOf() is your friend in this case:

foodex = contentArray.indexOf('foo');
alert('The first occurrence of the string \'foo\' in the text is at word number ' + foodex);

Finally, to get surrounding words, just play with the array (this won't work if the occurrence of the string is close to the start or end of the paragraph, namely less than 10 words away:

alert('I am the 10th word after \'foo\'' + contentArray[foodex + 10 - 1]);

Good luck (no guarantees this code works out of the box)!

Blender
  • 289,723
  • 53
  • 439
  • 496
  • @ blender : may i give you and example, i mean my code because i have try it before i make this question,but it isn't help me.. – user495688 Dec 05 '10 at 04:47
  • Sorry, I can barely understand you. You need to use more grammar, as that helps people understand you clearly (sorry if that sounds insulting). What's your *exact* question? What's wrong with the code? – Blender Dec 05 '10 at 04:52
  • Well, sorry if my grammar bad ..:) I've made this code as above, I've tried as you suggested to me long before I made this question. but I am still struggling to get the position of the same word when using the index, because surely the result will be the same although the word I select is actually located in different positions – user495688 Dec 05 '10 at 05:05
  • Have you tried `indexOf('asd', 5)`? It looks for the sixth occurrence. I'm not getting what the problem is. Could you tell us what's wrong, explicitly? – Blender Dec 05 '10 at 05:14
  • sorry I do not understand what you mean by indexOf (asd, 5) for example I have a sentence "Reaping is the cutting of grain or pulse for harvest, typically using a Scythe, sickle, or reaper.The harvest marks the end of the growing season ....", then I choose the word" harvest "which position lies between "the" and "marks" should I get the index of 40 (for example), but when using var index = hilangtandabaca.indexOf (strSelectedText);i get the index of "harvest" a position between "for" and "typically" – user495688 Dec 05 '10 at 05:41
  • @Blender: I think this about the selection, i.e. the portion in the page highlighted by the user. The OP has asked similar questions before, for example here: http://stackoverflow.com/questions/4332753/how-to-get-selected-text-but-can-i-get-surrounding-context-in-javascript – Tim Down Dec 05 '10 at 12:01
  • @ Tim Down : do you mean like this var test_value = range.focusNode.parentNode.id; – user495688 Dec 05 '10 at 12:38
  • @user495688: I'm not sure. A `Range` object doesn't have a `focusNode` property, so I imagine you're thinking of `Selection` objects which do. To be honest, this whole question is quite unclear to me. You might be better off spending a little time asking just one part of the question as clearly as you can. – Tim Down Dec 05 '10 at 15:53
  • @ Tim Down : i have try to implement it in my code,but it doesn't work automatically when user selected text and system want to get surrounding text. – user495688 Dec 06 '10 at 05:24