Solved

how to count the repeated words from the text file using java program

Posted on 2010-09-03
5
2,107 Views
Last Modified: 2012-05-10
Sir,
      I do my project in data mining, pls give the java program for count the repeated words from the  text file. For example, the file in ‘D’ colon, then we retrieve that text file and count the total no of repeated words and display the text area in java program.
      For ex:
            Hai – 6, good-2, world-5………………..
0
Comment
Question by:sunda2010
5 Comments
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 33595419
Read the file word by word

http://helpdesk.objects.com.au/java/using-scanner-to-read-words-from-text-file

And use a Map to store the word counts
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33595466
You have already accepted these solution, which are giving the same solution
http://www.experts-exchange.com/Programming/Languages/Java/Q_26449362.html
http://www.experts-exchange.com/Programming/Languages/Java/Q_26446914.html

Why can't you use them?
0
 
LVL 2

Expert Comment

by:chataros
ID: 33595513
you will find how you can read the file using google.
the algorithm is something like this:
import java.util.HashMap;

public class count
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        String fileContents = "asd aaa asd aaa asd";
        HashMap<String, Integer> wordCount = new HashMap<String, Integer>();
        String[] words = fileContents.split(" ");
        for (String word : words)
        {
            if (wordCount.containsKey(word))
            {
                Integer oldCount = wordCount.get(word);
                wordCount.put(word, oldCount + 1);
            }
            else
            {
                wordCount.put(word, 1);
            }
        }
    }

}
0
 

Expert Comment

by:ikraammomin
ID: 33595758
import java.util.HashMap;
import java.io.*;

public class WordCount
{

   public static void main(String[] args) throws Exception
   {
      FileReader fr = new FileReader("D:\abcd.text");
      String fileContents = "";
      String line = fr.readLine();
      while (line != null) {
            fileContents = fileContents + line;
            line = fr.readLine();
      }

       HashMap<String, Integer> wordCount = new HashMap<String, Integer>();
       String[] words = fileContents.split(" ");
       for (String word : words)
       {
           if (wordCount.containsKey(word))
           {
               Integer oldCount = wordCount.get(word);
               wordCount.put(word, oldCount + 1);
           }
           else
           {
               wordCount.put(word, 1);
           }
       }
   }

}
0
 
LVL 6

Expert Comment

by:__geof__
ID: 33597520
Quite the same answer as the other question: http://www.experts-exchange.com/Programming/Languages/Java/Q_26446916.html?cid=1131#a33594515
Just change the input to be a file instead of System.in
###
### CustomEntry class that implements comparable to be able to sort it afterwards and a method to change a Map to a List of CustomEntry
###

public class CustomEntry implements Comparable {

    private Map.Entry entry;

    public CustomEntry(Map.Entry entry) {
        this.entry = entry;
    }

    public Map.Entry getEntry() {
        return this.entry;
    }

    public int compareTo(CustomEntry anotherEntry) {
        Integer thisIntegerVal = (Integer) (this.getEntry().getValue());
        int thisVal = thisIntegerVal.intValue();
        Integer anotherIntegerVal = (Integer) (anotherEntry.getEntry().getValue());
        int anotherVal = anotherIntegerVal.intValue();
        return (thisVal < anotherVal ? 1 : (thisVal == anotherVal ? 0 : -1));
    }

    public int compareTo(Object o) {
        return compareTo((CustomEntry) o);
    }

    public static List<CustomEntry> convertMapToList(Map map) {
        List<CustomEntry> list = new ArrayList();
        Set entrySet = map.entrySet();
        Iterator iterator = entrySet.iterator();
        while (iterator.hasNext()) {
            Map.Entry entry = (Map.Entry) iterator.next();
            CustomEntry customEntry = new CustomEntry(entry);
            list.add(customEntry);
        }
        return list;
    }
}


###
### The main method that creates the Map from the string than get it sorted and printing them
###

    public static void main(String[] args) {
        HashMap<String, Integer> wordCount = new HashMap<String, Integer>();
        try {
            BufferedReader br = new BufferedReader(new FileReader(D:\file.txt));
            StringBuffer contents = new StringBuffer();
            String text = null;
 
            // repeat until all lines is read
            while ((text = reader.readLine()) != null)
            {
                contents.append(text)
                    .append(System.getProperty(
                        "line.separator"));
            }
            String str = contents.toString();
            String[] words = str.split(" ");
            for(String word:words){
                if(wordCount.containsKey(word)){
                    Integer i = wordCount.get(word) + 1;
                    wordCount.remove(word);
                    wordCount.put(word, i);
                }else{
                    wordCount.put(word, 1);
                }
            }
            List l = CustomEntry.convertMapToList(wordCount);
            Collections.sort(l);
            for(int i = 0; i < l.size(); i++){
                CustomEntry entry = (CustomEntry) l.get(i);
                System.out.println(entry.getEntry().getKey() + " occured " + entry.getEntry().getValue() + " times");
            }
        } catch (FileNotFoundException e) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question