Results 1 to 4 of 4

Thread: Code Run Help

  1. #1
    coder09 is offline Member
    Join Date
    Jan 2009
    Posts
    20
    Rep Power
    0

    Default Code Run Help

    Hi,

    What do I need to do to run the the following code on a news article text file named, for example, "article.txt"? I tried calling the class from another Java file, but I'm not doing something correctly. Thanks

    Java Code:
    package preProcessing;
    
    import java.util.StringTokenizer;
    import fileOperations.importExport.ListWords;
    
    /**
     * <p>Description: Uses lists of most common not usable words for seach purposes to remove those from string</p>
    
     */
    public class RemoveStopwords {
    
            /**
             * Removes the stop words not usable for search purposes
             * @param text String input text
             * @param language String language code [en,fr,de]
             * @return String output text
             */
            public static String removeStopwords(String text){
            ListWords stopWordsEN = new ListWords("mystopwords.txt");
            //ListWords stopWordsFR = new ListWords("frenchST.txt");
            //ListWords stopWordsDE = new ListWords("germanST.txt");
    
            StringTokenizer st2 = new StringTokenizer(text);
            String resultOutput = new String();
    
                while (st2.hasMoreTokens()) {
                    String s = st2.nextToken();
                    if (stopWordsEN.contains(s)) {
                    } else {
                        resultOutput += ' ';
                        resultOutput += s;
                    }
                }
            return resultOutput;
        }
    }
    Last edited by coder09; 02-03-2009 at 11:35 PM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    25

    Default

    Create an instance of the class and call this public method with a string argument. It returns a string. What you have done so far, and what happen when you try to do something.

    I hope you have an idea what's going on with this code.

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    11

    Default

    Here's how to do what you described, assuming you've got all the necessary imports and try {} catch ...

    Java Code:
    File file = new File("article.txt");
    
    // open the file and create an input stream
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(file)); 
    
    int numToRead = file.length();  //only works for < 2GB files
    
    // creat a buffer to read into
    byte[] bytes = new byte[numToRead];
    
    // read fully.  The loop is necessary because in.read is not guaranteed to read fully
    while(numToRead > 0) {
      int numRead = in.read(bytes, bytes.length-numToRead, numToRead);
    
      if(numRead < 0) break;
    
      numToRead -= numRead;
    }
    
    // Create a String from the text
    String string = new String(bytes,"utf-8"); // plain text assumption
    
    //Call your guy.  The method is static and public, so no need to instantiate
    
    String newString = RemoveStopWords.removeStopWords(string);
    
    // tada!

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    25

    Default

    Ya, no need to instantiate for static methods referring. But it's better to give some hints/tips to the thread starter before posting a code. Make him/her to think something first and have a try. So they can learn new things.

Similar Threads

  1. Why doesn't this code accept my code?
    By PeterFeng in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 02:39 PM
  2. HELP ME with tis code ?????
    By sheckoo in forum New To Java
    Replies: 4
    Last Post: 11-27-2008, 06:19 AM
  3. log in code
    By kevN in forum New To Java
    Replies: 0
    Last Post: 11-17-2008, 11:05 AM
  4. I need help fixing my code.. or non code?
    By MrHuggykins in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 11:12 PM
  5. Replies: 1
    Last Post: 04-26-2007, 04:52 PM

Posting Permissions

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