Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Searching with keywords in a sentence

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Searching with keywords in a sentence

    Hi everyone...
    My question is related to deleting some unwanted words in a sentence like is,am,are,has,have,had,should,would and many more like these. Basically when user inputs a sentence in search box in order to search something, then program also shows the search related to above mentioned helping verbs and resultantly shows more results. So I want to delete all these words from a sentence to keep my search fast and accurate. If I use if-else or switch then I have to mention all helping verbs, which looks very odd in program and how many words I would have to handle. Is there any good and efficient way to handle this situation?
    For example:
    User inputs a sentence in search box "Is there any example of delete query"
    There are only 3 keywords in the sentence i.e example, delete and query.
    Thanks


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Searching with keywords in a sentence

    Yeah, there is an easier way to do it:

    1. Create a set of words you want to ignore
    2. Split the query by word
    3. For each word in the query, see if it is in the set of words to ignore. If it is, ignore it
    4. Run your search using any keywords which weren't ignored

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Searching with keywords in a sentence

    You mean like this.........
    But look at If Condition is too long. Is it a good approach?

    class removeIsAmAre{
    	public static void main(String args[]){
    		String sentence = "this is am are would should has have had a sentence completion";
    		String[] words = sentence.split(" ");
    		int lenArr=words.length;
    		for(int i=0;i<lenArr;i++){
    			//remove is, are, am, has, have, had, he, she, it,,him, her, his, they, would, should, a, this, that
    			if(words[i].equals("a") || words[i].equals("is") || words[i].equals("this") || words[i].equals("am") || words[i].equals("are")|| words[i].equals("her") || words[i].equals("this") || words[i].equals("that") || words[i].equals("had") || words[i].equals("would") || words[i].equals("should") || words[i].equals("has") || words[i].equals("have") || words[i].equals("he") || words[i].equals("she") || words[i].equals("it") || words[i].equals("they")){
    				words[i]="";
    			}
    		   System.out.println(words[i]);
    		}
    	}
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Searching with keywords in a sentence

    Put the words in a list or array and loop through that instead of that monstrous if statement.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: Searching with keywords in a sentence

    Your if-statement is insanely large and impractical, especially if you want to compare more words. Instead, use another array or an arraylist, such as:

    public class TestSentence {
        public static void main(String[] args) {
            String sentence = "this is am are would should has have had a sentence completion";
            String[] words = sentence.split(" ");
            String[] wordsToCompare = {"a", "is", "this", "am", "are", "her", "that", "had", "would", "should", "has", "have", "he", "she", "it", "they"};
            // outer loop that runs through the words array
            // nested loop that runs through the wordsToCompare array
            // compare if each element matches and do something, otherwise if they don't match, do something else
            // print
        }
    }

    import java.util.Arrays;
    import java.util.ArrayList;
     
    public class TestSentence {
        public static void main(String[] args) {
            String sentence = "this is am are would should has have had a sentence completion";
            String[] words = sentence.split(" ");
            ArrayList<String> wordsToCompareList = new ArrayList(Arrays.asList("a", "is", "this", "am", "are", "her", "that", "had", "would", "should", "has", "have", "he", "she", "it", "they"));
            // use the same logic for looping as above

Similar Threads

  1. [SOLVED] replacing few keywords with corresponding operators
    By shenaz in forum Java Theory & Questions
    Replies: 14
    Last Post: April 9th, 2013, 11:54 AM
  2. keywords
    By BLUEJ in forum Java Theory & Questions
    Replies: 5
    Last Post: January 14th, 2013, 08:05 PM
  3. Can you name the Java keywords?
    By KevinWorkman in forum The Cafe
    Replies: 7
    Last Post: November 19th, 2011, 06:13 PM
  4. keywords
    By crazyjava in forum Java Theory & Questions
    Replies: 2
    Last Post: September 29th, 2010, 12:13 AM
  5. how to extract variables,keywords,operator...
    By Nanda in forum Java Theory & Questions
    Replies: 1
    Last Post: November 12th, 2009, 10:19 PM