Solved

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

Posted on 2010-09-03
5
2,122 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

726 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