Results 1 to 3 of 3
  1. #1
    Pleenen is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Default Read in words from a text file that contains a story

    I need to read in multiple text files which contain stories. I need to break each sentence up into six word sequence and then place it into a hash map. I've started writing the code but am not sure how to read the words in to make the six word sequence. (I was thinking of using a tokenizer but don't know how to implement it) Then, after getting a six word sequence, get rid of the first word and add on a new word. Any help would be appreciated.

    public class Main {

    static String wordList;
    static String[] readingInWords = new String[6];
    static int wordCount = 0;

    static Map<String, Integer> docs = new HashMap<String, Integer>();

    public static void main(String[] args) throws FileNotFoundException
    {
    File folder = new File("Docs to be tested");
    File[] listOfFiles = folder.listFiles();

    for(int i = 0; i < listOfFiles.length; i++)
    {
    FileInputStream in = new FileInputStream(listOfFiles[i]);
    System.setIn(in);



    if(wordCount == 6)
    {
    wordList = readingInWords[0] + " " + readingInWords[1] + " " + readingInWords[2] +
    " " + readingInWords[3] + " " + readingInWords[4] + " " + readingInWords[5];
    Integer freq = docs.get(wordList);
    docs.put(wordList, (freq == null) ? 1 : freq + 1);
    wordCount = 0;

    readingInWords[0] = readingInWords[1];
    readingInWords[1] = readingInWords[2];
    readingInWords[2] = readingInWords[3];
    readingInWords[3] = readingInWords[4];
    readingInWords[4] = readingInWords[5];
    }
    else
    {

    }

    wordCount++;
    }
    }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Break the problem up into small steps.
    One approach:
    First write a program to read all the files and display their lines.
    Then figure out how to find a sentence. I assume it would end with a . and a space or end of line. Can a sentence span a line of text?
    Then work out how to get words in groups of 6. Is there short group at the end of a sentence?
    Another approach would be to use the Scanner class which can read one word at a time. Then you'd need to look for the ending period.

    You've written a lot of code without thinking the problem thru. Do the design first, then write the code.

  3. #3
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Then, after getting a six word sequence, get rid of the first word and add on a new word. Any help would be appreciated.
    What you need is a Queue, and in Java the simplest Collection that implements the Queue interface is LinkedList. Specifically, you'll want a LinkedList<String>.

    Queue is an interface, like List or Map. You should declare it as an instance variable, then initialize it in your constructor. (Don't make it static unless your instructor is specifically teaching you that bad practice, and give your class a better name than Main.)
    Java Code:
    import java.util.*;
    
    public class StoryParser {
    
            // you don't need those other instance variables
            private Map<String, Integer> phrases;
            private Queue<String> wordQueue;
    
            // initialize your instance variables in a constructor
            public StoryParser() {
                    phrases = new HashMap<String, Integer>();
                    wordQueue = new LinkedList<String>();
            }
    
            // put the code you had in main() into this method instead
            private void run() throws IOException {
                    File folder = new File("Docs to be tested");
                    // etc.
            }
    
            public static void main(String[] args) throws IOException {
                    new StoryParser().run();
            }
    }
    The way I would do it is to read the file a line at a time, pass the line to a second parseWords(String line) method to break it up into words, and have that method call a processWord(String word) method for each word it finds. The only tricky part of parsing words is identifying what all of your delimiters should be. You start by opening an empty StringBuilder, then you scan through the characters of your line one at a time. If the character is not a delimiter (space, tab, punctuation, etc.) then you just append it to the StringBuilder object. If it is a delimiter, you call toString() on the StringBuilder, send it off to processWord(), then setLength(0) your StringBuilder to empty it out again. When you get to the end of the line, you'll need to send the current StringBuilder to processWord() one more time.

    Your processWord() method will add the incoming word to the end of your Queue. If the Queue has six words in it, you need to concatenate them together into a String (separated by spaces, I guess?), putting all but the first word back on the Queue as you go. You pull items off the front of a Queue with remove() and put them on the end with add().

    Once you have your concatenated String, you check if your Map has that String yet. If so, you read the Integer value, and if not, you just create a new Integer 0. Then you increment the Integer, and write it out to your Map.

    Hope that helps.

    -Gary-

Similar Threads

  1. Can't read from a text file
    By kevin8160 in forum New To Java
    Replies: 2
    Last Post: 02-07-2010, 07:45 PM
  2. Read from a text file?
    By aaronfsimons in forum New To Java
    Replies: 3
    Last Post: 05-01-2009, 05:42 AM
  3. read from text file
    By rayda in forum New To Java
    Replies: 5
    Last Post: 04-10-2009, 04:51 AM
  4. Read and modify text file
    By heartysnowy in forum New To Java
    Replies: 27
    Last Post: 11-19-2008, 11:03 AM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 10:13 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •