-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask4.java
370 lines (349 loc) · 11.5 KB
/
task4.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Stack;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.Queue;
import java.util.LinkedList;
import java.util.Scanner;
import org.apache.commons.io.IOUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.JSONParser;
import org.jsoup.Jsoup;
import org.jsoup.select.Elements;
import org.jsoup.nodes.*;
import com.dropbox.core.DbxException;
import com.dropbox.core.DbxRequestConfig;
import com.dropbox.core.DbxDownloader;
import com.dropbox.core.v2.DbxClientV2;
import com.dropbox.core.v2.files.WriteMode;
import com.dropbox.core.v2.files.UploadErrorException;
import com.dropbox.core.v2.files.DownloadErrorException;
import com.dropbox.core.v2.files.ListFolderResult;
import com.dropbox.core.v2.files.Metadata;
import com.dropbox.core.v2.files.FileMetadata;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
public class task4{
public void t4(){
try{
File dogNames = new File("../dataset/t1_output03.txt");
Scanner listing = new Scanner(dogNames);
boolean ending = true;
while(listing.hasNextLine()){
String index = listing.nextLine();
Scanner scan = new Scanner(index).useDelimiter(": ");
String arrayNames [] = new String[2];
arrayNames[0] = scan.next();
arrayNames[1] = scan.next();
File dogHtml = getHTML(arrayNames[1]);
String BCR = GenerateBCR(dogHtml);
String TBCR = GenerateTextualData(dogHtml);
String writing = (arrayNames[0] + "\n" + BCR + TBCR + "\n");
writeToFile("t4_output01.txt", writing, true);
}
}catch(Exception e){
e.printStackTrace();
}
}
public void writeToFile(String fileName, String data, boolean append){
BufferedWriter bw = null;
FileWriter fw = null;
try {
File file = new File("../dataset/"+fileName);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
// true = append file
fw = new FileWriter(file.getAbsoluteFile(), append);
bw = new BufferedWriter(fw);
bw.write(data);
//System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public String GenerateBCR(File file) {
// System.out.println(path);
Stack<Double> stack = new Stack<Double>();
Queue<Double> queue = new LinkedList();
String listing = "";
try{
Document doc = Jsoup.parse(file, null);
Elements dogName = doc.select("header h1");
Elements resultLinks = doc.select(("div[class=group-list-item]"));
Elements check = doc.select(("span[class=star-block stars-column]"));
Elements group = doc.select("div[class=star-by-breed clearfix default-border-bottom default-margin-bottom default-padding-bottom parent-characteristic] span[class=characteristic item-trigger-title]");
Elements name = doc.select (("div[class=js-list-item item-expandable-content default-border-bottom default-margin-bottom default-padding-bottom star-by-breed child-characteristic] span[class=characteristic item-trigger-title]"));
String dog = (dogName.eq(0).text());
//System.out.println(dog);
int count1 = 0;
int count2 = 0;
//String cool = name.indexOf(name.text(),3);
for(Element g: group){
listing += "\""+g.text()+"\" : [ \"";
if(check.eq(count1).text().length() == 0){
count1++;
while(check.eq(count1).text().length() != 0){
listing += (name.eq(count2).text()+ "\" : ");
if(check.eq(count1+1).text().length() != 0){
listing += (check.eq(count1).text()+",\n\"");
stack.add(Double.valueOf(check.eq(count1).text()));
}else{
listing += (check.eq(count1).text()+"]\n");
stack.add(Double.valueOf(check.eq(count1).text()));
}
count1++;
count2++;
}
}
int first = 0;
stack = getAverage(stack);
listing+="Grouping total: " + stack.pop()+"\n\n";
// while(!stack.isEmpty()){
//
// if(first == 0){
// listing += (stack.peek()+"\n");
// queue.add(stack.pop());
//
// first++;
// }
// else{
// listing += (stack.peek()+"\n");
// queue.add(stack.pop());
// }
// }
//listing += "The average of this grouping is: "+getAverage(stack)+"\n\n";
}
return listing;
//System.out.println(listing);
//System.out.println(listing);
//System.out.println(listing);
}catch(Exception e){
System.exit(0);
}
//file.delete();
return listing;
}
public String GenerateTextualData(File file){
Queue<String> queue = new LinkedList();
String listing = "";
try{
Document doc = Jsoup.parse(file, null);
String [] titles = {"","Highlights", "History", "Size", "Personality", "Health", "Care", "Feeding", "Coat Color And Grooming", "Children And Other Pets", "Breed Organizations"};
Elements dogName = doc.select("header h1");
Elements sections = doc.select("h2[class=js-section-heading description-title]");
Elements textual = doc.select("div[class = breed-data-item-content js-breed-data-section]");
Elements stats = doc.select("span[class = characteristic]");
// Elements statData = doc.select();
int count = 0;
boolean run = true;
String vital = "";
String dog = (dogName.eq(0).text());
// listing += (dog+"\n");
// System.out.println(dog);
for(Element dogs : dogName){
for(Element stat: stats){
vital += (stat.text() + " ");
vital += (stat.nextSibling());
count ++;
}
listing += (vital+"\n");
queue.add(vital);
count = 0;
int count2 = 0;
for(String title : titles){
//System.out.println(section.text());
run = true;
for(Element section : sections){
//System.out.println(section.text());
if(section.text().equals(title)){
//System.out.println(section.text());
//System.out.println("I equaled a title");
//System.out.println(section.text());
String fix = textual.eq(count).text();
fix = fix.replace('\'', '\"');
queue.add(fix);
listing += (fix+"\n");
run = false;
}else{
//System.out.println("I am in the else statement");
}
//else if(section.text().equals("Highlights")){
// System.out.println(textual.eq(count).text());
// }else if(section.text().equals("History")){
// System.out.println(textual.eq(count).text());
// }
}if(run){
//System.out.println("I am in run");
//count2++;
queue.add("No information found");
listing += ("No information found\n");
}
count++;
//System.out.println(section.text());
}
//System.out.println(count2);
}
//writeToFile(dog, listing);
return listing;
}catch(Exception e){
e.printStackTrace();
}
return listing;
}
public void GenerateName(File file){
Queue<String> queue = new LinkedList();
try{
Document doc = Jsoup.parse(file, null);
Elements dogName = doc.select("header h1");
String dog = (dogName.eq(0).text());
}catch(Exception e){
e.printStackTrace();
}
}
public Stack<Double> getAverage(Stack<Double> stack){
double avg = 0;
Stack<Double> ret = new Stack<Double>();
int size = stack.size();
double var = 0;
DecimalFormat df = new DecimalFormat("###.##");
while(!stack.isEmpty()){
avg += stack.peek();
var = Double.valueOf(df.format(stack.pop()));
ret.add(var);
}
avg = Double.valueOf(df.format(avg/size));
ret.add(avg);
return ret;
}
public File getHTML(String path){
URL url;
File skip = new File("../dataset/test.html");
try {
// get URL content
url = new URL(path);
URLConnection conn = url.openConnection();
// open the stream and put it into BufferedReader
BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
//save to this filename
String fileName = "../dataset/test.html";
File file = new File(fileName);
if (!file.exists()) {
file.createNewFile();
}
//use FileWriter to write file
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
while ((inputLine = br.readLine()) != null) {
bw.write(inputLine);
}
bw.close();
br.close();
return file;
}catch (Exception e) {
e.printStackTrace();
}
return skip;
}
public void ShowAllDogBreeds(File file){
try{
Document doc = Jsoup.parse(file, null);
Elements resultLinks = doc.select(("div[class=group-list-item]"));
char[] array = new char[26];
int index = 0;
for (char c = 'A'; c <= 'Z'; c++)
array[index++] = c;
String listOfDogs = "";
for (int i = 0; i < array.length; i++){
for (Element link : resultLinks){
if (link.text().startsWith(Character.toString(array[i])))
listOfDogs += link.text() + ",";
}
if (listOfDogs.indexOf(",") > 0)
listOfDogs = listOfDogs.substring(0, listOfDogs.lastIndexOf(","));
listOfDogs += "\n\n";
}
System.out.println(listOfDogs);
}catch(Exception e){
System.out.println("There was an error in the something");
}
}
public void getDogHTML(File file, String arg){
System.out.println(arg);
try{
Document doc = Jsoup.parse(file, null);
Elements dogLinks = doc.select(("div[class=group-list-item] h2 a[href]"));
for(Element link : dogLinks){
//System.out.println(link.text().charAt(0));
int num = (int)link.text().charAt(0);
//System.out.println(num);
if(arg.equals("class01")&&(num >=(int)'A' && num <= (int)'F')){
String linkDog = link.attr("href");
File gotten = getHTML(linkDog);
GenerateBCR(gotten);
GenerateTextualData(gotten);
GenerateName(gotten);
//System.out.println(link.attr("href"));
}
if(arg.equals("class02")&&(num >(int)'F' && num <= (int)'N')){
String linkDog = link.attr("href");
File gotten = getHTML(linkDog);
GenerateBCR(gotten);
GenerateTextualData(gotten);
GenerateName(gotten);
//System.out.println(link.attr("href"));
}
if(arg.equals("class03")&&(num >(int)'N' && num <= (int)'T')){
String linkDog = link.attr("href");
File gotten = getHTML(linkDog);
GenerateBCR(gotten);
GenerateTextualData(gotten);
GenerateName(gotten);
//System.out.println(link.attr("href"));
}
if(arg.equals("class04")&&(num >(int)'T' && num <= (int)'Z')){
String linkDog = link.attr("href");
File gotten = getHTML(linkDog);
GenerateBCR(gotten);
GenerateTextualData(gotten);
GenerateName(gotten);
//System.out.println(link.attr("href"));
}
}
}catch(Exception e){
System.out.println("Error in getDogHTML");
}
}
}