-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwentyNewsgroupsCorpus.java
executable file
·165 lines (140 loc) · 5.51 KB
/
TwentyNewsgroupsCorpus.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import com.aliasi.classify.Classification;
import com.aliasi.classify.Classified;
import com.aliasi.corpus.ObjectHandler;
import com.aliasi.corpus.Corpus;
import com.aliasi.io.FileLineReader;
import com.aliasi.tokenizer.Tokenizer;
import com.aliasi.util.Arrays;
import com.aliasi.util.ObjectToSet;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TwentyNewsgroupsCorpus
extends Corpus<ObjectHandler<Classified<CharSequence>>> {
final Map<String,String[]> mGoldCatToTexts;
final Map<String,String[]> mTrainingCatToTexts;
final Map<String,String[]> mTestCatToTexts;
int mMaxSupervisedInstancesPerCategory = 1;
public TwentyNewsgroupsCorpus(File path) throws IOException {
File goldDir = new File(path,"gold");
File trainDir = new File(path,"train");
File testDir = new File(path,"test");
mGoldCatToTexts = read(goldDir);
mTrainingCatToTexts = read(trainDir);
mTestCatToTexts = read(testDir);
}
public Set<String> categorySet() {
return mTrainingCatToTexts.keySet();
}
public void permuteInstances(Random random) {
for (String[] xs : mTrainingCatToTexts.values())
Arrays.permute(xs,random);
for (String[] xs : mGoldCatToTexts.values())
Arrays.permute(xs,random);
}
public void setMaxSupervisedInstancesPerCategory(int max) {
mMaxSupervisedInstancesPerCategory = max;
}
public void visitTrain(ObjectHandler<Classified<CharSequence>> handler) {
visit(mTrainingCatToTexts,handler,mMaxSupervisedInstancesPerCategory);
}
public void visitGold(ObjectHandler<Classified<CharSequence>> handler) {
visit(mGoldCatToTexts,handler,Integer.MAX_VALUE);
}
public void visitTest(ObjectHandler<Classified<CharSequence>> handler) {
visit(mTestCatToTexts,handler,Integer.MAX_VALUE);
}
public Corpus<ObjectHandler<CharSequence>> unlabeledCorpus() {
return new Corpus<ObjectHandler<CharSequence>>() {
public void visitTest(ObjectHandler<CharSequence> handler) {
throw new UnsupportedOperationException();
}
public void visitTrain(ObjectHandler<CharSequence> handler) {
for (String[] texts : mTrainingCatToTexts.values())
for (int i = mMaxSupervisedInstancesPerCategory;
i < texts.length;
++i)
handler.handle(texts[i]);
}
};
}
public String toString() {
StringBuilder sb = new StringBuilder();
int totalTrain = 0;
int totalGold = 0;
int totalTest = 0;
for (String cat : new TreeSet<String>(mTrainingCatToTexts.keySet())) {
sb.append(cat);
int gold = mGoldCatToTexts.get(cat).length;
int train = mTrainingCatToTexts.get(cat).length;
int test = 0;
try {
test = mTestCatToTexts.get(cat).length;
} catch (Exception e) {
//Nothing
}
totalGold += gold;
totalTrain += train;
totalTest += test;
sb.append(" #gold=" + gold);
sb.append(" #train=" + train);
sb.append(" #test=" + test);
sb.append('\n');
}
sb.append("TOTALS: #gold=" + totalGold
+ " #train=" + totalTrain
+ " #test=" + totalTest
+ " #combined=" + (totalGold + totalTrain + totalTest));
sb.append('\n');
return sb.toString();
}
public int getTestCount(String cat){
int test = 0;
try {
test = mTestCatToTexts.get(cat).length;
} catch (Exception e) {
//Nothing
}
return test;
}
private static Map<String,String[]> read(File dir)
throws IOException {
ObjectToSet<String,String> catToTexts
= new ObjectToSet<String,String>();
for (File catDir : dir.listFiles()) {
String cat = catDir.getName();
for (File file : catDir.listFiles()) {
String[] lines
= FileLineReader.readLineArray(file,"ISO-8859-1");
Set<String> texts = new HashSet<String>(java.util.Arrays.asList(lines));
catToTexts.addMembers(cat,texts);
}
}
Map<String,String[]> map = new HashMap<String,String[]>();
for (Map.Entry<String,Set<String>> entry : catToTexts.entrySet())
map.put(entry.getKey(),
entry.getValue().toArray(new String[0]));
return map;
}
private static void visit(Map<String,String[]> catToItems,
ObjectHandler<Classified<CharSequence>> handler,
int maxItems) {
for (Map.Entry<String,String[]> entry : catToItems.entrySet()) {
String cat = entry.getKey();
Classification c = new Classification(cat);
String[] texts = entry.getValue();
for (int i = 0; i < maxItems && i < texts.length; ++i) {
Classified<CharSequence> classifiedText
= new Classified<CharSequence>(texts[i],c);
handler.handle(classifiedText);
}
}
}
}