-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecreateDistributions.java
More file actions
58 lines (49 loc) · 1.81 KB
/
Copy pathRecreateDistributions.java
File metadata and controls
58 lines (49 loc) · 1.81 KB
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
/*
* File: RecreateDistributions.java
* Author: Jackson Davenport
*
* Takes as input two files which are the raw distributions as text files.
* This file will take those and rebuild the distributions passing back
* the full Distributions object.
*/
import java.io.BufferedReader;
import java.util.Hashtable;
import java.util.Enumeration;
public class RecreateDistributions{
public RecreateDistributions(){}
public Distributions rebuildDistribution(String subreddit, BufferedReader readerUni, BufferedReader readerBi){
String[] parsedLine;
String baseWord, prevWord, currentLine;
int count;
int totalUniCount = 0;
// Unigram Distribution
Hashtable<String, Integer> unigramDistribution = new Hashtable<String, Integer>();
// Bigram Distribution
Hashtable<String, BigramElement> bigramDistribution = new Hashtable<String, BigramElement>();
currentLine = Util.getNextLine(readerUni);
while(currentLine != null){
// Split it over the tab
parsedLine = currentLine.split("\t");
baseWord = parsedLine[0];
count = Integer.parseInt(parsedLine[1]);
unigramDistribution.put(baseWord, count);
totalUniCount += count;
currentLine = Util.getNextLine(readerUni);
}
currentLine = Util.getNextLine(readerBi);
while(currentLine != null){
// Split it over the tab
parsedLine = currentLine.split("\t");
baseWord = parsedLine[0];
prevWord = parsedLine[1];
count = Integer.parseInt(parsedLine[2]);
// Add the word | prevWord | count to the set
if(!bigramDistribution.containsKey(baseWord)){
bigramDistribution.put(baseWord, new BigramElement(baseWord));
}
bigramDistribution.get(baseWord).addWord(prevWord, count);
currentLine = Util.getNextLine(readerBi);
}
return new Distributions(subreddit, totalUniCount, unigramDistribution, bigramDistribution);
}
}