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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 28

Thread: Output sentences using keywords in java

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    My Mood
    Confused
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Post Output sentences using keywords in java

    Hello,

    I am trying to write a program in Java that tags sentences from a text file. Each tag has an associated array of keywords. A tag is applied to a sentence if and only if the sentence contains one or more keywords belonging to the tag's array My aim is to output the sentences in a categorical manner, e.g. if a certain keyword is an a category, and that keyword is in the sentence, then that sentence should be able to go to into that category. This is just the basic of what I have done:

    For e.g. I have two tags: faults, and adaption. In those tags' arrays, I have words such as Bugs, Fail etc, for faults, and Polish, Clean up for adaption. One of the sentence will be 'BUG found and fixed', which would go into the faults category, hence it will input the sentence after the heading fault as it contains the word 'bug'.

    String[] fault = {
                "Misspelled",
                "Error",
                "Fixed",
                "Change",};
    And
    String[] adaption = {"Fixed comments",
            "Filters", "Polish","Adjust"};

    This is the code that I have used to read the text file, and split the words:
    {
      try{
              FileInputStream fstream = new FileInputStream("readthis.txt");
    	  DataInputStream in = new DataInputStream(fstream);
    	  BufferedReader br = new BufferedReader(new InputStreamReader(in));
    	  String newread;
     
    	  		while ((newread = br.readLine()) != null)   {
     
    	  	System.out.println( java.util.Arrays.toString( newread.split(" ")));
      }
    		in.close();
     
      }catch 		
        			(Exception e){//Catch exception if any
     
        		System.err.println("Error: " + e.getMessage());
      }
      }
    }

    Right now the output that I am getting is:
    ['This, bugs, needs, fixing']
    ['Cleaned, up, section, one']
    ['Fixed, typos']
    ['adjusted, the, code, to, make, it, look, simpler']
    as this were the words in my text file, without the split.

    My next step is to use the arrays that I have got, and use the sentences above to put them in their category, either in Faults, or Adaption, vie the keywords. However, I have looked at HashMaps, but I don't know how to use the arrays with it.
    Any help or suggestions is useful for me.

    Thank you


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Output sentences using keywords in java

    I'm not totally sure what your question is. What have you tried?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    My Mood
    Confused
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Output sentences using keywords in java

    At this moment, I have the arrays list, e.g. the keywords that goes with their category. And I have read the text file that I need, and split the words up. However, I'm trying to read the file, and output the sentences into an category, looking at the words that they have, from the array list, and output those sentence in the category. e.g. sentence: 'Fixed typos' would go into the faults category, because of the keyword 'fixed' in the faults array. So the output will be something like Faults: Fixed Typos. That is the part I'm stuck at, putting the sentence in the category.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Output sentences using keywords in java

    What about it is confusing you?

    I suggest writing out what you have to do, in plain English. Pretend you have a really dumb friend, and your job is to show that friend how to categorize a bunch of index cards that have these sentences written on them. Write out a list of steps your friend can follow to accomplish that goal.

    When you have that written out, you'll have an algorithm that you can then begin translating into code.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    My Mood
    Confused
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Output sentences using keywords in java

    I have all the steps, and someone suggested that I should use Hashmap to put the the sentences into their categories using ArrayList. I don't know how to actually do that. So I was asking for any suggestion that would help. I think my question is bit confusing.

  6. #6
    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: Output sentences using keywords in java

    A Map sounds like a useful tool. The key to the Map would be the category and the value associated with that key could be an ArrayList of all the sentences that matched.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    GameGirl91 (February 5th, 2013)

  8. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Output sentences using keywords in java

    The Map tutorial contains code for breaking up an array of words into the frequency of each. Might be useful: The Map Interface (The Java™ Tutorials > Collections > Interfaces)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. The Following User Says Thank You to KevinWorkman For This Useful Post:

    GameGirl91 (February 5th, 2013)

  10. #8
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    My Mood
    Confused
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Output sentences using keywords in java

    Can I use HashTable as well?

  11. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Output sentences using keywords in java

    Quote Originally Posted by GameGirl91 View Post
    Can I use HashTable as well?
    What happened when you tried?

    Notice that Hashtable *is a* Map.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. The Following User Says Thank You to KevinWorkman For This Useful Post:

    GameGirl91 (February 9th, 2013)

  13. #10
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    My Mood
    Confused
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Output sentences using keywords in java

    Quote Originally Posted by KevinWorkman View Post
    What happened when you tried?

    Notice that Hashtable *is a* Map.
    Ah, didn't know that. Do you know how I can get started? Because I don't really have clue. There are tutorials out there that helps you with just using hashmap by itself, but not with the arrays.

  14. #11
    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: Output sentences using keywords in java

    How do you want to use arrays with Maps?
    What is in the arrays?
    What in the Map?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #12
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    My Mood
    Confused
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Output sentences using keywords in java

    The array is going to be

    String[] faults = {
                "Misspelled",
                "Error",
                "Fixed",
                "Change",}
    And
    String[] adaption = {"Fixed comments",
            "Filters", "Polish","Adjust"};

    For the map, It's going to look for words from a text file, which is like this:
    ['This, bugs, needs, fixing']
    ['Cleaned, up, section, one']
    ['Fixed, typos']
    ['adjusted, the, code, to, make, it, look, simpler']
    I would love some help on how to get started.

  16. #13
    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: Output sentences using keywords in java

    Do some more research on how to use a Map. Your posting doesn't look like how a Map would be used.

    How are the two arrays: faults and adaption going to be used?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #14
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    My Mood
    Confused
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Output sentences using keywords in java

    Quote Originally Posted by Norm View Post
    Do some more research on how to use a Map. Your posting doesn't look like how a Map would be used.

    How are the two arrays: faults and adaption going to be used?
    Each of the two array's has keywords. The system will read those keywords from the text file and output those sentence's which will have the matching array keyword and print them, in the either one of the the two group, e.g. the sentence ' bug 1457454: new interface' will come in the faults, as the word 'bug' matches with the keyword in faults.

  18. #15
    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: Output sentences using keywords in java

    If you have arrays of words to search for in lines read from a file, as each line is read from the file loop through the arrays and use the array elements in a String class's method to see if the line contains one of the Strings from the arrays.
    If you don't understand my answer, don't ignore it, ask a question.

  19. The Following User Says Thank You to Norm For This Useful Post:

    GameGirl91 (February 19th, 2013)

  20. #16
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    My Mood
    Confused
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Output sentences using keywords in java

    Quote Originally Posted by Norm View Post
    If you have arrays of words to search for in lines read from a file, as each line is read from the file loop through the arrays and use the array elements in a String class's method to see if the line contains one of the Strings from the arrays.
    Can I than print out the results, e.g. all the sentences and print it out in another text file? If I can get any help in how to start the loop through the array, I would be great full.

    Thank you

  21. #17
    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: Output sentences using keywords in java

    See the tutorial about arrays:
    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Or look at the many code samples here on the forum.
    If you don't understand my answer, don't ignore it, ask a question.

  22. The Following User Says Thank You to Norm For This Useful Post:

    GameGirl91 (February 20th, 2013)

  23. #18
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    My Mood
    Confused
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Output sentences using keywords in java

    Thank you, I'll do that.

    I just wanted to know to make an For loop, I need a stop condition for the loop stop, and since I don't know how many sentences are there in the text file, how would I stop it? Really confused on this.

  24. #19
    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: Output sentences using keywords in java

    how would I stop it
    Use a while loop and exit the loop when there are no more lines to be read from the file.
    The code in post#1 does that already.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #20
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    My Mood
    Confused
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Output sentences using keywords in java

    Is there there anyway in which I can read from the array?

  26. #21
    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: Output sentences using keywords in java

    You can access elements of an array by using an index:
    theArray[theIndex] //<<<  access element in theArray at theIndex

    Can you explain what you mean by "read from the array"?
    If you don't understand my answer, don't ignore it, ask a question.

  27. #22
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    My Mood
    Confused
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Output sentences using keywords in java

    Because I already have two arrays I wondering how can I read the keywords from the array, and match them with the words in the sentences that I have in the text file.

  28. #23
    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: Output sentences using keywords in java

    how can I read the keywords from the array,
    See my post#21 for how to get/read elements in an array.

    match them with the words in the sentences
    Use the elements from the array in String class methods.
    If you don't understand my answer, don't ignore it, ask a question.

  29. The Following User Says Thank You to Norm For This Useful Post:

    GameGirl91 (February 20th, 2013)

  30. #24
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    My Mood
    Confused
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Output sentences using keywords in java

    I have a standard array right now, but I guess in order to read the elements from I need to convert the array into an arrayList. I have used this to do it:

    new ArrayList<String>(Arrays.asList(faults));
    ArrayList newarray = new ArrayList(100);
    newarray.addAll(Arrays.asList(faults));

    I was wondering if this was right?

  31. #25
    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: Output sentences using keywords in java

    if this was right?
    What happens to the object returned by the first line?

    Compile it and execute it to see. That's what I'd have to do.
    If you don't understand my answer, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. keywords
    By BLUEJ in forum Java Theory & Questions
    Replies: 5
    Last Post: January 14th, 2013, 08:05 PM
  2. Can you name the Java keywords?
    By KevinWorkman in forum The Cafe
    Replies: 7
    Last Post: November 19th, 2011, 06:13 PM
  3. java code to split text documents into paragraphs and sentences
    By draksha in forum Java Theory & Questions
    Replies: 5
    Last Post: August 17th, 2011, 05:06 AM
  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

Tags for this Thread