-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWikiAPI.java
More file actions
242 lines (210 loc) · 11.4 KB
/
WikiAPI.java
File metadata and controls
242 lines (210 loc) · 11.4 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
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
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.stream.Collectors;
import java.util.*;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.URI;
import java.io.IOException;
import java.io.File;
import java.io.FileNotFoundException;
public class WikiAPI {
private static final String base = "https://en.wikipedia.org/w/api.php?action=query&format=json";
HttpClient client = HttpClient.newBuilder().build();
public List<String> APIgetLinks(String url, String pr, String stopUrl) {
url = url.replace("/wiki/", "");
List<String> links = new ArrayList<>();
if (url.equals("Demonym")) {
File f = new File("Demonym_" + pr + ".txt");
try {
Scanner sc = new Scanner(f);
String[] linksArray = sc.nextLine().split(", ");
Collections.addAll(links, linksArray);
sc.close();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
} else {
JSONObject responseObj;
String cont = null;
try {
while (links.size() < 10000) {
String re = "";
if (url.contains("Category:") || url.contains("category:")) {
re = base + "&list=categorymembers&cmtitle=" + url
+ "&cmprop=title&cmtype=page%7Csubcat&cmlimit=500";
} else {
re = base + "&titles=" + url + "&prop=" + pr + "&";
if (pr.equals("links")) {
re += "pllimit=500";
} else if (pr.equals("linkshere")) {
re += "lhprop=title&lhlimit=500";
} else if (pr.equals("categories")) {
re += "clshow=!hidden&cllimit=500";
}
}
URI reqUri = null;
if (cont == null) {
reqUri = URI.create(re);
} else {
// make the specific type of continue request
cont = cont.replace("|", "%7C");
if (url.contains("Category:") || url.contains("category:")) {
reqUri = URI.create(re + "&cmcontinue=" + cont);
} else {
if (pr.equals("links")) {
reqUri = URI.create(re + "&plcontinue=" + cont);
} else if (pr.equals("linkshere")) {
reqUri = URI.create(re + "&lhcontinue=" + cont);
} else if (pr.equals("categories")) {
reqUri = URI.create(re + "&clcontinue=" + cont);
}
}
}
// send the request
HttpRequest request = HttpRequest.newBuilder()
.GET()
.uri(reqUri)
.build();
System.out.println(request);
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
responseObj = new JSONObject(response.body());
JSONArray lList = new JSONArray();
// add contents to list
if (url.contains("Category:") || url.contains("category:")) {
lList = responseObj.getJSONObject("query").getJSONArray("categorymembers");
} else {
JSONObject lObj = responseObj.getJSONObject("query").getJSONObject("pages");
lList = lObj.getJSONObject(lObj.keys().next()).getJSONArray(pr);
}
for (Object j : lList) {
JSONObject jO = new JSONObject(j.toString());
if (!jO.getString("title").contains("User:") && !jO.getString("title").contains("Wikipedia:")
&& !jO
.getString("title").contains("Talk:")
&& !jO.getString("title").contains("User_talk:"))
links.add("/wiki/" + jO.getString("title").replace(" ", "_"));
}
// if there is no continue element or if the stopURL has been found, break the
// loop and return the links, otherwise
if (!responseObj.keySet().contains("continue")) {
break;
} else if (links.contains(stopUrl)) {
break;
}
// otherwise get the specific type of continue and set it's contents equal to
// the cont string
else {
if (url.contains("Category:") || url.contains("category")) {
cont = responseObj.getJSONObject("continue").getString("cmcontinue");
} else {
if (pr.equals("links")) {
cont = responseObj.getJSONObject("continue").getString("plcontinue");
} else if (pr.equals("linkshere")) {
cont = responseObj.getJSONObject("continue").getString("lhcontinue");
// System.out.println(responseObj.getJSONObject("continue"));
} else if (pr.equals("categories")) {
cont = responseObj.getJSONObject("continue").getString("clcontinue");
}
}
}
}
} catch (IOException | InterruptedException ex) {
throw new RuntimeException(ex);
}
}
return links;
}
public List<String> findWikiPath(String start, String end, String method) {
// add list of visited links to avoid loops
List<String> path = new ArrayList<>();
List<String> visitedUrls = new ArrayList<>();
List<String> targetLinks = new ArrayList<>();
urlLink target = new urlLink(end);
if (method.equals("down")) {
targetLinks = APIgetLinks(end, "linkshere", "PLACEHOLDER");
} else if (method.equals("up")) {
targetLinks = APIgetLinks(end, "links", "PLACEHOLDER");
}
// select the first link found on the target page to compare to
// current opened link
urlLink currentLink = new urlLink(start);
if (method.equals("down")) {
currentLink.setSimilarity(APIgetLinks(start, "links", end), targetLinks);
} else if (method.equals("up")) {
currentLink.setSimilarity(APIgetLinks(start, "linkshere", end), targetLinks);
}
double currentBestMatch = 0;
List<String> currentChildren = new ArrayList<>();
while (true) {
currentBestMatch = currentLink.getSimilarity();
System.out.println("CurrentBestMatch: " + currentBestMatch);
System.out.println(currentLink.getUrl() + " " + currentLink.getSimilarity());
if (method.equals("down")) {
currentChildren = APIgetLinks(currentLink.getUrl(), "links", end);
} else if (method.equals("up")) {
if (currentLink.getUrl().contains("List")) {
currentChildren = APIgetLinks(currentLink.getUrl(), "links", end);
} else {
currentChildren = APIgetLinks(currentLink.getUrl(), "linkshere", "/wiki/Demonym");
}
}
if (currentChildren.contains(end)) { // if the target is on the current page return path
currentLink = new urlLink(end, currentLink);
while (currentLink.getParent() != null) {
path.add(currentLink.getParent().getUrl());
currentLink = currentLink.getParent();
}
// reverse the path so it is ordered from start to finish
Collections.reverse(path);
path.add(target.getUrl());
// break out of
break;
}
List<String> tempChildren = new ArrayList<String>(currentChildren);
currentChildren.retainAll(targetLinks); // get all similar links
// if no similar links, examine all
if (currentChildren.size() == 0) {
currentChildren = tempChildren;
}
urlLink curlLink = currentLink;
// map from list of urls to list of urlLinks
List<urlLink> currentChildrenURL = currentChildren.stream().distinct()
.map(l -> new urlLink(l, curlLink))
.collect(Collectors.toList());
currentChildrenURL.removeIf(s -> s.getUrl().contains("User")); // never go into User links
currentChildrenURL.removeIf(s -> visitedUrls.contains(s.getUrl())); // remove already visited links to
// avoid loops
currentChildrenURL.removeIf(s -> s.getUrl().contains("Wikipedia") || s.getUrl().contains("talk"));
currentChildrenURL.removeIf(s -> s.getUrl().contains("January") ||
s.getUrl().contains("February") || s.getUrl().contains("March") ||
s.getUrl().contains("April") || s.getUrl().contains("May") ||
s.getUrl().contains("June") || s.getUrl().contains("July") ||
s.getUrl().contains("August") || s.getUrl().contains("September") ||
s.getUrl().contains("October") || s.getUrl().contains("November") ||
s.getUrl().contains("December")); // avoid getting stuck in
// dates;
// make pQueue, find similarities, add to pqueue, select most common link
Comparator<urlLink> similarityCompare = Comparator.comparing(urlLink::getSimilarity);
PriorityQueue<urlLink> pQueue = new PriorityQueue<urlLink>(similarityCompare.reversed());
for (urlLink link : currentChildrenURL) {
// set the similarity
link.setSimilarity(APIgetLinks(link.getUrl(), "links", "PLACEHOLDER"), targetLinks);
System.out.println(" examining: " + link.getUrl() + " " +
link.getSimilarity());
pQueue.add(link);
// if a new better link is found, break without examining the rest, results in
// this alogirthm being fast but not finding the shortest possible path
if (link.getSimilarity() > currentBestMatch) {
break;
}
}
// set the new link, add it to visitedURLs, clear the priority queue
currentLink = pQueue.poll();
visitedUrls.add(currentLink.getUrl());
pQueue.clear();
}
return path;
}
}