-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrequencyCounterFile.java
61 lines (53 loc) · 1.84 KB
/
FrequencyCounterFile.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
/*
* Frequency counts from an external text file.
*
* Copyright (C) 2013 Lisa Vitolo <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Creative Commons
* Attribution-NonCommercial-ShareAlike 3.0 license.
* You should have received a copy of the license with this product.
* Otherwise, visit http://creativecommons.org/licenses/by-nc-sa/3.0/
*
*/
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.math.BigInteger;
/*
* The Takelab authors have grouped together the frequency counts for all the words found in the
* training files in one text file. Here we read from that file in order to speed up things.
*
* The frequency file doesn't use any POS tag.
*/
public class FrequencyCounterFile implements FrequencyCounter
{
private Map<String, BigInteger> frequencyCounts;
private BigInteger totalCount;
public FrequencyCounterFile(String file)
{
frequencyCounts = new HashMap<>();
List<String> entries = IOUtils.readlines(file);
for (String entry : entries) {
String[] fields = entry.split(" ");
if (fields.length > 1) {
frequencyCounts.put(fields[0], new BigInteger(fields[1]));
} else {
totalCount = new BigInteger(fields[0]); /* the first row is the total count */
}
}
}
public @Override BigInteger getFrequencyCount(String token, String tag)
{
BigInteger c = frequencyCounts.get(token);
/* This should not happen unless you use customized training files. */
if (c == null) {
return new BigInteger("0");
}
return c;
}
public @Override BigInteger getTotalCount()
{
return totalCount;
}
}