-2

I have a string s1(contains a lot of english sentences),a list of string l1(which contains elements like bus, house, train, train engine, train engine's life(no. of words are never greater than 3), etc) and a list of string l2 (similar to l1) and an integer n.

For each element of l1, I have to find other elements of l1 or that of l2 within s(l1's elements can occur multiple times in s and we have to find it for every occurrence) such that their distance(distance is in no. of words,eg.: I have xyz.In this case, distance between I and xyz is 2) from that l1's element is less than or equal to n on either side of l1's element. Those words/phrases(elements of l1 or l2) are to be stored in a list in java.

e.g.:
Input:
s= i am asd fgh and studying in zxc vbn bnmkl. i am 100 years old.(may contain other symbols too)
l1= i
l2=asd, fgh, zxc, vbn
n=3.

Output:
asd,fgh,zxc,vbn



               for(int j=0;j<l1.size();j++){
                    String s1=l1.get(j);
                    int index = s.indexOf(s1);
                    while(index >= 0) {
                       System.out.println(index);
                       for(int k=0;k<n;){
                           // how to detect the words before and after that element of l1 such that distance is <= n on either side in a passage and they are contained in either l1 or l2.
                       }
                   index = s.indexOf(s1, index+1);
                   }
               }

Can anybody help me how to implement this?

Sam
  • 11
  • 6
  • We will not generate code for you. Start coding. Come back, when you have some code and ask specific question about the parts you do not understand. – Turing85 Jun 25 '15 at 19:54
  • You should take a look at regular expressions. – Hauke S Jun 25 '15 at 19:55
  • I have added the code and added the section(as a comment) where I need help. – Sam Jun 25 '15 at 20:13
  • Instead of asking "can you help me implement this?" which is just a cordial way of saying "can you do that for me?", we advice you to try and experiment with your code and to present a specific question, directly related to a doubt. It's not that we're not helpful, but this way your question and their answers will be helpful for other people as well. – Filipe Fedalto Jun 25 '15 at 20:31

1 Answers1

0

Google is your best friend for something like this. It's also a very simple function that can be interpreted in different ways.

using loops (for, foreach, while) could be of use, the following links are examples of this. Find specific word in text file and count it && Count the number of Occurrences of a Word in a String

Now that you have a search function, the question is how to set it up with multiple filters.


Here's a general example

if s2 was to become a string array for example

 string[] table = s2.split(' '); // this would do that

then you could use a for loop to loop through each filter like so:

 for (int i = 0; i < table.length(); i++) 
{
  string filter = table[i]; //this would get the filter of the current loop
  // search function here as well as your counters (refer to examples provided)
}

Here's another example

  String table_filter = l2.split(" "); //this would split your filter string into an array (at every space) String table_s = s.split(" ");
 //this would split your string into an array (at every space)

 for (int i = 0; i < table_filter.size(); i++){     
 for (int j = 0; j <
 table_s.size(); j++)   
  {     
     // do stuff (like comparing if this = that then do this or using a switch )}
Community
  • 1
  • 1
Viralwarrior012
  • 185
  • 1
  • 14
  • I realize that in java, the split is different than c#, you will have to search on how to split a string into a table, just type "Java split()" in Google and you'll have hundreds of examples – Viralwarrior012 Jun 25 '15 at 20:12
  • I have added the code and added the section where I need help. The problem in your solution is that s is a passage which can contain symbols like '.', ',' etc also which act as a separator. – Sam Jun 25 '15 at 20:12
  • to split a string in java: http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java – Viralwarrior012 Jun 25 '15 at 20:14
  • The problem with the above approach that I think would be what if the element is in the next or the next to next array element. This can be checked but this would make it too complex. Instead, the better approach is to check for the index of that element and detect n number of words before and after that element and check whether l1 or l2's elements are contained in it. However, I am not able to code this statement in java. – Sam Jun 25 '15 at 20:20