Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. public double getNgramsEntropy(String text, int n) throws FileNotFoundException {
  2.         PrintStream ps = new PrintStream("log" + n + ".txt");
  3.         int amount = 0;
  4.         double result = 0.0;
  5.         int length = text.length();
  6.         StringBuilder sb = new StringBuilder();
  7.         for(int i = 0; i < length - n + 1; i++) {
  8.             for(int j = 0; j < n; j++) {
  9.                 sb.append(text.charAt(i + j));
  10.             }
  11.             if(stringMap.containsKey(sb.toString())) {
  12.                 stringMap.put(sb.toString(), stringMap.get(sb.toString()) + 1.0);
  13.             }
  14.             else {
  15.                 stringMap.put(sb.toString(), 1.0);
  16.             }
  17.             sb.delete(0, sb.length());
  18.         }
  19.         for(Entry<String, Double> item: stringMap.entrySet()) {
  20.             amount += item.getValue();
  21.         }
  22.         for(Entry<String, Double> item: stringMap.entrySet()) {
  23.             stringMap.put(item.getKey(), (double)item.getValue() / amount);
  24.             result -= item.getValue() * (Math.log(item.getValue()) / Math.log(2));
  25.             ps.println(item.getKey() + " / " + item.getValue() + " / " + (Math.log(item.getValue()) / Math.log(2)) + " / " + item.getValue() * (Math.log(item.getValue()) / Math.log(2)));
  26.         }
  27.         stringMap.clear();
  28.         ps.println();
  29.         return result / n;
  30.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement