-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
135 lines (106 loc) · 3.48 KB
/
Graph.java
File metadata and controls
135 lines (106 loc) · 3.48 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
/* Thanks for teaching me this semester Profressor David,
* I really enjoyed your class and you are an amazing person!:)
*/
/*
* The purpose of this class is to retrieve the names from file(tmdb_5000_credits.csv)
* and add edges between two actors
* The source code of json was download from
* hclttps://github.com/stleary/JSON-java
* @author Chris Li
*/
import java.io.File;
import org.json.JSONObject;
import java.io.FileReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.util.*;
public class Graph {
//Data members
private HashMap<String, Set<String>> hashMap;
/** Constructor creating a hashmap for name of actors and
* their connected actors
*/
public Graph(){
hashMap = new HashMap<>(1000);
}
/** This function parses the input file and get the substring of each line
* by checking '[' as the start of a string and "}]" as the end of a string,
* and then split the string by checking "},".
* @param file: the file to be parsed
*/
public void readNames(String file) {
File inFile = new File(file);
String inString = "";
if(!inFile.isFile()){
System.exit(1);
}
try {
BufferedReader reader = new BufferedReader(new FileReader(inFile));
inString = reader.readLine();
while ((inString = reader.readLine()) != null) {
try{
String nameList = inString.substring(inString.indexOf("[") + 1, inString.indexOf("}]"));
if(!nameList.isEmpty()){
String[] actorsNames = nameList.split("},");
findConnections(actorsNames);
}
}catch(Exception e){
continue;
}
}
reader.close();
} catch (IOException ex) {
System.out.println("No such file..");
}
}
public HashMap<String, Set<String>> getMap() {
return hashMap;
}
/** Function for determining the connections between to
* actors, it loops through the splited array and try to find
* actors' name each by each and add a connection between
* two different names
* @param actorsNames: an splited array that contains infromation of different actors
*/
private void findConnections(String[] actorsNames){
for (int i = 0; i < actorsNames.length; i++) {
String newString = actorsNames[i];
newString = (newString + '}').trim().replaceAll("}}", "}");
newString = newString.replaceAll("\"\"", "\"");
if (newString.startsWith("{")) {
JSONObject dataJson = new JSONObject(newString);
String name1 = dataJson.getString("name");
for (int j = 0; j < actorsNames.length; j++) {
String newString2 = actorsNames[j];
newString2 = (newString2 + '}').trim().replaceAll("}}", "}");
newString2 = newString2.replaceAll("\"\"", "\"");
if (newString2.startsWith("{")) {
// checking the name are different
if (i != j) {
JSONObject data = new JSONObject(newString2);
String name = data.getString("name");
addConnections(name, name1);
}
}
}
}
}
}
/** Function for adding connections between two actors
* it cheack if the hashmap already has the actor's name
* and if it is, then just add the second actor to its connections
* otherwise create a new to hold the connection and update the hashmap
* @param name1: the name of first actor
* @param name2: the name of second actor
*/
private void addConnections(String name1, String name2) {
if (hashMap.containsKey(name1)) {
Set<String> set = hashMap.get(name1);
set.add(name2);
} else {
Set<String> set = new HashSet<>(10);
set.add(name2);
hashMap.put(name1, set);
}
}
}