-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisualSumm.java
183 lines (162 loc) · 5.05 KB
/
VisualSumm.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
import java.io.File;
import java.util.Scanner;
import java.util.LinkedList;
import java.io.FileNotFoundException;
import java.net.*;
import java.io.*;
import org.json.*;
import java.util.LinkedList;
public class VisualSumm {
public static void main(String[] args) {
File doThisOne = new File("toSumm.txt");
VisualSumm vs = new VisualSumm();
String[] output = vs.generateUrlList(doThisOne);
for (String a : output) {
System.out.println(a);
}
}
public String[] generateUrlList(File toSummarize) {
// This is where we store the urls
LinkedList<String> urlList = new LinkedList<String>();
try {
// Read in word by word, using white space as delimiter.
// Then remove all punctuation and surrounding whitespace.
// Write results to file to ensure that we did it correctly.
Scanner scan = new Scanner(toSummarize);
LinkedList<WordWithCounts> words = new LinkedList<WordWithCounts>();
// Go through and do processing on word tokens, then add in
// the linked list
while (scan.hasNext()) {
String next = scan.next();
next = next.replaceAll("\\W+","");
next = next.replaceAll("\\s+","");
next = next.trim();
next = next.toLowerCase();
if (!next.equals("")) {
WordWithCounts wordInQuestion = new WordWithCounts(next);
boolean added = false;
for (int i=0; i<words.size(); i++) {
if (wordInQuestion.equals(words.get(i))) {
words.get(i).incrementCount();
added = true;
}
}
if (!added) {
words.add(wordInQuestion);
}
}
}
// Sort the list of words by their counts
WordWithCounts[] arr = words.toArray(new WordWithCounts[0]);
for (int i=0; i<words.size(); i++) {
int maxIndex = i;
for (int j=i; j<words.size(); j++) {
if (arr[j].getCount() > arr[maxIndex].getCount()) {
maxIndex = j;
}
}
// Swap the max element with the current element
WordWithCounts temp = arr[i];
arr[i] = arr[maxIndex];
arr[maxIndex] = temp;
}
for (WordWithCounts a:arr) {
System.out.println(a);
}
// Now we pull out the top ten most relevant words, if they aren't equal to certain common words.
LinkedList<String> notAllowed = new LinkedList<String>();
notAllowed.add("he");
notAllowed.add("she");
notAllowed.add("it");
notAllowed.add("a");
notAllowed.add("an");
notAllowed.add("the");
notAllowed.add("to");
notAllowed.add("of");
notAllowed.add("in");
notAllowed.add("was");
notAllowed.add("is");
notAllowed.add("and");
notAllowed.add("you");
notAllowed.add("i");
notAllowed.add("had");
notAllowed.add("that");
notAllowed.add("with");
notAllowed.add("as");
notAllowed.add("before");
notAllowed.add("this");
notAllowed.add("when");
notAllowed.add("but");
notAllowed.add("because");
notAllowed.add("this");
notAllowed.add("her");
notAllowed.add("for");
notAllowed.add("one");
notAllowed.add("at");
notAllowed.add("they");
notAllowed.add("on");
notAllowed.add("no");
notAllowed.add("were");
notAllowed.add("who");
notAllowed.add("from");
notAllowed.add("about");
notAllowed.add("up");
notAllowed.add("have");
notAllowed.add("been");
notAllowed.add("there");
notAllowed.add("said");
notAllowed.add("his");
notAllowed.add("not");
notAllowed.add("after");
notAllowed.add("be");
String[] wordsToLookUp = new String[10];
int indexToLookAt = 0;
boolean filled = false;
for (int i=0; i<10; i++) {
filled = false;
while (!filled) {
if (!notAllowed.contains(arr[indexToLookAt].getWord())) {
wordsToLookUp[i] = arr[indexToLookAt].getWord();
filled = true;
}
indexToLookAt++;
}
}
for (String a:wordsToLookUp) {
// Do a google image search, and output the first three results
try {
URL url = new URL("https://ajax.googleapis.com/ajax/services/search/images?" +
"v=1.0&q=" + a + "&userip=198.137.20.133");
URLConnection connection = url.openConnection();
connection.addRequestProperty("Referer", "http://visual-summary.appspot.com/");
String line;
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while((line = reader.readLine()) != null) {
builder.append(line);
}
JSONObject json = new JSONObject(builder.toString());
// now have some fun with the results...
//System.out.println(json.get("responseData").getClass().getName());
/*for (String a : JSONObject.getNames(json.get("responseData"))) {
System.out.println(a);
}*/
JSONArray jsonarr = json.getJSONObject("responseData").getJSONArray("results");
for (int i=0; i<jsonarr.length(); i++) {
urlList.add(jsonarr.getJSONObject(i).getString("url"));
}
}
catch (MalformedURLException e) {
System.out.println("Nooo");
}
catch (IOException e) {
System.out.println("NOOOOO");
}
}
}
catch (FileNotFoundException e) {
System.out.println("noooo");
}
return urlList.toArray(new String[0]);
}
}