Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2010-09-03
5
Medium Priority
?
2,148 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 500 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:Gurvinder Pal Singh
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

609 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