Results 1 to 4 of 4
  1. #1
    kashifjoseph is offline Member
    Join Date
    Aug 2011
    Posts
    1
    Rep Power
    0

    Default display top ten occuring most words in a file

    i have a program which displays no of occurances of all words in a file and also sorts it according to the number of occurances

    How can I display only the top 10 of them?

    Here is my program:
    Java Code:
    import java.io.*; 
    import java.util.*; 
    
    public class HashMapEx {
    
        public static void main(String[] args) {         
            LinkedHashMap<String, Integer> wordcount =
                    new LinkedHashMap<String, Integer>();
            try { 
                BufferedReader in = new BufferedReader(
                                          new FileReader("c:\\test\\sample.txt"));
                String str;
    
                while ((str = in.readLine()) != null) { 
                    str = str.toLowerCase(); // convert to lower case 
                    int idx1 = -1;
    
                    for (int i = 0; i < str.length(); i++) { 
    
                        if  ((!Character.isLetter(str.charAt(i))) ||
                             (i + 1 == str.length())) { 
    
                            if (i - idx1 > 1) { 
                                if (Character.isLetter(str.charAt(i))) 
                                    i++;
                                String word = str.substring(idx1 + 1, i);
                                if  (wordcount.containsKey(word)) { 
                                    wordcount.put(word, wordcount.get(word) + 1);
                                } else { 
                                    wordcount.put(word, 1);
                                } 
                            }          
                            idx1 = i;
                        } 
                    } 
                } 
                in.close();
            } catch (Exception e) { 
                e.printStackTrace();
                System.exit(1);
            } 
    
            ArrayList<Integer> values = new ArrayList<Integer>();
            values.addAll(wordcount.values());
    
            Collections.sort(values, Collections.reverseOrder());
    
            int last_i = -1;
    
            for (Integer i : values) { 
                if (last_i == i) // without duplicates
                    continue;
                last_i = i;
    
                for (String s : wordcount.keySet()) { 
                    if (wordcount.get(s) == i) // which have this value  
                        System.out.println(s + ":" + i);
                } 
            } 
        } 
    }
    Last edited by sunde887; 08-16-2011 at 08:32 PM. Reason: Added code tags, [code]...[/code]

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Well if you sorted it, then you would display just the highest 10 results. If you sort it in reverse order(highest-lowest), the first 10 entries would be the items you want, otherwise it should be the last 10 entries.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    Quote Originally Posted by Junky View Post
    Duplicate post.
    No, that's a cross post.

    db

Similar Threads

  1. display random words from a string
    By Kenken in forum New To Java
    Replies: 4
    Last Post: 04-02-2011, 01:38 PM
  2. windowClosed(WindowEvent e) occuring twice
    By AndyV in forum AWT / Swing
    Replies: 3
    Last Post: 02-27-2011, 12:13 PM
  3. Replies: 1
    Last Post: 12-01-2010, 06:35 PM
  4. Finding words (and more) in another file
    By Lund01 in forum Java Applets
    Replies: 5
    Last Post: 09-30-2010, 01:03 PM
  5. HELP ON errors occuring in the code
    By jaiminparikh in forum Advanced Java
    Replies: 6
    Last Post: 02-12-2009, 02:02 PM

Tags for this Thread

Posting Permissions

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