-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBigram.java
115 lines (92 loc) · 2.81 KB
/
Bigram.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package Entropy;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/*This class creates bigrams and calculates entropy*/
public class Bigram {
public static void main(String[] args) throws IOException {
try {
File f = new File("MidFile.txt");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
ArrayList < String > s = new ArrayList();
ArrayList < String > bigrams = new ArrayList();
HashMap < String, Integer > map = new HashMap < String, Integer > ();
String str = null;
while ((str = br.readLine()) != null) {
str = str.replaceAll("[^a-zA-Z ]", "");
str = str.replaceAll("\\[", "").replaceAll("\\]", "");
s.add(str);
}
StringBuilder build = new StringBuilder();
for (String sb: s) {
build.append(sb);
}
char p;
int count = 0;
for (int i = 0; i < build.length() - 1; i++) {
//if the previous or next character isnt space
if (build.charAt(i) != 32 && build.charAt(i + 1) != 32) {
String gram = Character.toString(build.charAt(i)) + Character.toString(build.charAt(i + 1));
bigrams.add(gram);
}
}
System.out.println(bigrams);
StringBuilder bd = new StringBuilder();
for (String sb: bigrams) {
build.append(sb);
}
//Store bigrams in map
for (String word: bigrams) {
Integer counti = map.get(word);
map.put(word, (counti == null) ? 1 : counti + 1);
}
int sum = 0;
for (int add: map.values()) {
sum += add;
}
//Iterate over map values and store frequencies in frequency array
int msize = map.size();
double fin[] = new double[msize];
Set entries = map.entrySet();
Iterator ei = entries.iterator();
int i = 0;
while (ei.hasNext()) {
Map.Entry mapping = (Map.Entry) ei.next();
fin[i] = (int) mapping.getValue();
i++;
}
//probability array
double pro[] = new double[msize];
//entropy array
double entrop[] = new double[msize];
//calculate probability for all bigrams
for (int m = 0; m < fin.length; m++) {
double prob = fin[m] / sum;
pro[m] = prob;
}
//calculate all entropies
for (int e = 0; e < pro.length; e++) {
double ent = (pro[e]) * (-Math.log(pro[e]) / Math.log(2.0));
entrop[e] = ent;
}
double entropy = 0.0;
for (double l: entrop) {
entropy += l;
}
System.out.println("Entropy for all bigrams is:" + entropy + " bits");
br.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}