-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTaggerApp.java
266 lines (230 loc) · 9.41 KB
/
TaggerApp.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* This app allows to build a graph from arrays of picture tags, and display
* the closely related ones for each tag.
* Data format: Map with keys equal to tags. Tag _a_ is mapped into a pair
* (int weight, List neighborhood). _a_.weight is the number of occurences of
* tag _a_ among the set of flickr photos. Each item in _a_.neighborhood is
* an instance of Edge(String label, int weight),
* where edge.label denotes the related tag, and edge.weight is the number of common
* occurences of tags _a_ and edge.label. Items in the list are sorted according
* to their label in the lexicographic order, to allow for efficient logarithmic
* updates.
*
* Methods of Tagger class:
*
* updateGraph(String[] tags):
* Updates the graph by plugging in a new set of tags.
*
* displaykNN(String targetFile, int k, int weightThreshold):
* Displays each tag with its nearest neighbors, and the corresponding weights.
*
* Efficiency:
* Good:
* Easy to access items to update at each step (TreeMap constant access).
* Easy to display k nearest tags for each tag (Sorted lists -> constant).
*/
import java.io.*;
import java.util.*;
import java.lang.*;
import java.nio.charset.Charset;
// class: Edge
// fields: String label, int weight
// For tag _a_, lists a related tag _b_ (label), and number of common
// occurences (weight).
class Edge {
public String label;
public int weight;
public Edge(String label) {
this.label = label;
this.weight = 1;
}
}
// class: NodeDescription
// fields: int nodeWeight, List neighborhood
// For tag _a_, lists number of occurences (nodeWeight), and list of edges
// (neighborhood).
class NodeDescription {
public int nodeWeight;
List<Edge> neighborhood;
public NodeDescription(List<Edge> neighborhood) {
this.nodeWeight = 1;
this.neighborhood = neighborhood;
}
}
// class LabelComparator: compares two edges by labels (lexicographically)
class LabelComparator implements Comparator<Edge> {
public int compare(Edge e1, Edge e2) {
return e1.label.compareTo(e2.label);
}
}
// class WeightComparator: compares two edges by weights (in descending order)
class WeightComparator implements Comparator<Edge> {
public int compare(Edge e1, Edge e2) {
return e2.weight - e1.weight;
}
}
// class Tagger:
// field graph: Maps tag -> (weight, neighborhood)
// method void updateGraph(String[] tags): updates graph with tags in the String argument
// method void displaykNN(String filename, int k, int weightThreshold): prints tags with
// at least weightThreshold occurences, and at most k of their neighbors with maximum
// number of common occurences
class Tagger {
// Map graph: Maps tag -> (weight, neighborhood)
private Map<String, NodeDescription> graph = new TreeMap<String, NodeDescription>();
// printToFile: Prints StringBuffer data into file filename, append if append=true.
private static void printToFile(StringBuffer data, String filename, boolean append) {
try {
FileOutputStream out = new FileOutputStream(filename, append);
PrintStream pPrint = new PrintStream(out);
pPrint.println(data);
pPrint.close();
}
catch (FileNotFoundException e) {
}
}
// updateGraph: Injects an array of tags into graph
// example graph created from [a, b], [a, c]:
// "a" -> nodeWeight = 2, neighborhood = ((label = "b", weight = 1), (label = "c", weight = 1))
// "b" -> nodeWeight = 1, neighborhood = ((label = "a", weight = 1))
// "c" -> nodeWeight = 1, neighborhood = ((label = "a", weight = 1))
public void updateGraph(String[] tags) {
List<Edge> neighborhood;
NodeDescription currentNode;
Edge currentEdge;
Comparator<Edge> c = new LabelComparator();
int index;
// for each artist
for (String tag : tags) {
index = 0;
// if graph doesn't contain key artist, initialize neighborhood,
// else get the neighborhood, and increase node weight
if (!graph.containsKey(tag)) {
neighborhood = new ArrayList<Edge>();
currentNode = new NodeDescription(neighborhood);
} else {
currentNode = graph.get(tag);
currentNode.nodeWeight++;
neighborhood = currentNode.neighborhood;
}
// for each neighbor other than artist
for (String neighbor : tags) {
if (!tag.equals(neighbor)) {
currentEdge = new Edge(neighbor);
// find neighbor in neigborhood
index = Collections.binarySearch(neighborhood, currentEdge, c);
// if neighbor not in neighborhood, set insert index, insert neighbor,
// else increase edge.weight
if (index < 0) {
index = -index - 1;
neighborhood.add(index, currentEdge);
} else {
currentEdge = neighborhood.get(index);
currentEdge.weight++;
neighborhood.set(index, currentEdge);
}
}
}
// if neighborhood non-empty, put artist to graph
if (!neighborhood.isEmpty()) {
currentNode.neighborhood = neighborhood;
graph.put(tag, currentNode);
}
}
}
// displaykNN: Displays (standard output or file) tags and at most k-tuple
// of related tags with max number of common occurences, to filter out noisy
// tags, weightThreshold can be set (minimum number of occurences)
//
// output example for graph build from ["a", "b"], ["a", "c"]:
// a: 2: (b, 1) (c, 1)
// b: 1: (a, 1)
// c: 1: (a, 1)
public void displaykNN(String targetFile, int k, int weightThreshold) {
Set<String> keys = graph.keySet();
Iterator iter = keys.iterator();
Comparator<Edge> c = new WeightComparator();
// clean-up file if it exists
if (targetFile != null)
printToFile(new StringBuffer(""), targetFile, false);
while (iter.hasNext()) {
String currentKey = (String)iter.next();
NodeDescription currentVal = graph.get(currentKey);
Collections.sort(currentVal.neighborhood, c);
StringBuffer output = new StringBuffer(200);
int count = 0;
if (currentVal.nodeWeight >= weightThreshold) {
output.append(String.format("%s: %d: ", currentKey, currentVal.nodeWeight));
for (Edge item : currentVal.neighborhood) {
if (count < k) {
output.append(String.format("(%s,%d) ", item.label, item.weight));
} else break;
count++;
}
output.append("\n");
if (targetFile != null) {
printToFile(output, targetFile, true);
} else {
System.out.println(output);
}
}
}
}
}
public class TaggerApp {
private static final int MAX_TAGS = 100;
public static void main(String[] args) throws IOException {
String[] status = { "LINK", "TITLE", "TAGS" };
int stateIndex;
String[] tagList = new String[MAX_TAGS];
int tagListIndex;
String[] toBuild;
int numberOfNeighbors = Integer.parseInt(args[0]);
int weightThreshold = Integer.parseInt(args[1]);
String path = args[2];
Tagger buildTags = new Tagger();
InputStream fis;
BufferedReader br;
String line;
File dir = new File(path);
String[] fileList;
if (dir.isDirectory()) {
fileList = dir.list();
} else {
fileList = new String[1];
path = "";
fileList[0] = dir.getPath();
}
for (String filename : fileList) {
stateIndex = 0;
tagListIndex = 0;
try {
fis = new FileInputStream(path + filename);
br = new BufferedReader(new InputStreamReader(fis, Charset.forName("UTF-8")));
while ((line = br.readLine()) != null) {
if (line.equals("")) {
if (stateIndex == 2) {
toBuild = Arrays.copyOf(tagList, tagListIndex);
buildTags.updateGraph(toBuild);
}
stateIndex = 0;
tagListIndex = 0;
} else if (stateIndex < 2) {
stateIndex++;
} else {
if (tagListIndex < MAX_TAGS) {
tagList[tagListIndex] = line;
tagListIndex++;
}
}
}
} catch (FileNotFoundException e) {
System.out.println("File not found.");
}
}
if (args.length == 3) {
buildTags.displaykNN(null, numberOfNeighbors, weightThreshold);
} else if (args.length > 3) {
buildTags.displaykNN(args[3], numberOfNeighbors, weightThreshold);
}
}
}