Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. package cirno.task1;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. /**
  7.  * Created by IntelliJ IDEA.
  8.  * User: cirno
  9.  * Date: 2/19/11
  10.  * Time: 7:18 PM
  11.  * To change this template use File | Settings | File Templates.
  12.  */
  13.  
  14.  
  15. class Cmp implements Comparator<Pair> {
  16.     public int compare(Pair a, Pair b) {
  17.         return b.getValue() - a.getValue();
  18.     }
  19. }
  20.  
  21. class Pair {
  22.     private String key;
  23.     private int value;
  24.  
  25.     public Pair(String a, int b) {
  26.         key = a;
  27.         value = b;
  28.     }
  29.  
  30.     public int getValue() {
  31.         return value;
  32.     }
  33.  
  34.     public String getKey() {
  35.         return key;
  36.     }
  37. }
  38.  
  39. public class WordCounter {
  40.     private static HashMap<String, Integer> map = new HashMap<String, Integer>();
  41.     private static int wNum = 0;
  42.  
  43.     public static void main(String args[]) {
  44.         if (args.length > 0) {
  45.             doAnything(args[0]);
  46.         } else {
  47.             System.out.println("Specify the file");
  48.         }
  49.     }
  50.  
  51.     static void printTable() {
  52.         List<Pair> lst = new ArrayList<Pair>();
  53.  
  54.         for(Map.Entry<String, Integer> entry : map.entrySet()) {
  55.             lst.add(new Pair(entry.getKey(), entry.getValue()));
  56.         }
  57.  
  58.         Collections.sort(lst, new Cmp());
  59.  
  60.         for(Pair entry : lst) {
  61.             System.out.println(entry.getKey() + ";" + entry.getValue() + ";" + (double)entry.getValue() / wNum);
  62.         }
  63.     }
  64.  
  65.     static void doAnything(String fileName) {
  66.         FileInputStream stream = null;
  67.  
  68.         try {
  69.             stream = new FileInputStream(fileName);
  70.             byte[] buf = new byte[stream.available()];
  71.             stream.read(buf);
  72.  
  73.             String str = new String(buf);
  74.  
  75.             String [] strArr = str.split("[^a-zA-Z0-9]");
  76.  
  77.             for(String s:strArr) {
  78.                 map.put(s, 1 + (map.containsKey(s) ? map.get(s) : 0));
  79.                 wNum++;
  80.             }
  81.  
  82.             printTable();
  83.         }
  84.         catch (IOException e) {
  85.             System.err.println("Error while reading file: " + e.getLocalizedMessage());
  86.         }
  87.         finally {
  88.             if (null != stream) {
  89.                 try {
  90.                     stream.close();
  91.                 }
  92.                 catch (IOException e) {
  93.                     e.printStackTrace(System.err);
  94.                 }
  95.             }
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement