Solved

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

Posted on 2010-09-03
5
2,100 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 42
thymeleaf natural templating vs JSP 2 95
Java Jpanels and Jframe 8 33
Glassfish admin console not working 1 11
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

776 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