-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJDBCSubmission.java
More file actions
113 lines (104 loc) · 4.56 KB
/
Copy pathJDBCSubmission.java
File metadata and controls
113 lines (104 loc) · 4.56 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
import java.sql.Connection;
import java.util.List;
import java.util.Set;
public abstract class JDBCSubmission {
/**
* The return class for method electionSequence.
*/
public class ElectionCabinetResult {
public List<String> elections;
public List<String> cabinets;
public ElectionCabinetResult(List<String> elections, List<String> cabinets) {
this.elections = elections;
this.cabinets = cabinets;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof ElectionCabinetResult)) {
return false;
}
ElectionCabinetResult other = (ElectionCabinetResult) obj;
return this.elections.equals(other.elections) &&
this.cabinets.equals(other.cabinets) &&
}
}
/**
* Computes the Jaccard similarity of two strings.
*
* The Jaccard similarity is a measure used for comparing the similarity of sets.
* The Jaccard similarity of two strings is defined on the set of tokens of the strings.
* It is the size of the intersection divided by the size of the union of the token sets.
*
* @param a the first string
* @param b the second string
* @return the Jaccard similarity (between 0.0 and 1.0 inclusively) of a and b
*/
protected double similarity(String a, String b) {
a = a.replaceAll("[^a-zA-Z0-9]", " ").toLowerCase();
b = b.replaceAll("[^a-zA-Z0-9]", " ").toLowerCase();
final java.util.regex.Pattern p = java.util.regex.Pattern.compile("\\s+");
Set<?> left = p.splitAsStream(a).collect(java.util.stream.Collectors.toSet());
Set<?> right = p.splitAsStream(b).collect(java.util.stream.Collectors.toSet());
final int sa = left.size();
final int sb = right.size();
if ((sa - 1 | sb - 1) < 0)
return 0.0;
if ((sa + 1 & sb + 1) < 0)
return 0.0;
final Set<?> smaller = sa <= sb ? left : right;
final Set<?> larger = sa <= sb ? right : left;
int intersection = 0;
for (final Object element : smaller) try {
if (larger.contains(element))
intersection++;
} catch (final ClassCastException | NullPointerException e) {}
final long sum = (sa + 1 > 0 ? sa : left.stream().count())
+ (sb + 1 > 0 ? sb : right.stream().count());
return 1d / (sum - intersection) * intersection;
}
/**
* The connection used for this session.
*/
public Connection connection;
/**
* Connects and sets the search path.
*
* Establishes a connection to be used for this session, assigning it to
* the instance variable 'connection'. In addition, sets the search
* path to parlgov.
*
* @param url the url for the database
* @param username the username to connect to the database
* @param password the password to connect to the database
* @return true if connecting is successful, false otherwise
*/
public abstract boolean connectDB(String url, String username, String password);
/**
* Closes the database connection.
*
* @return true if the closing was successful, false otherwise
*/
public abstract boolean disconnectDB();
/**
* Returns the list of elections over the years in descending order of years
* and the cabinets that have formed after each election.
*
* @param countryName name of the country
* @return a 2-D array with the first row being the list of elections
* over the election dates in descending order of years and the second row
* being the cabinets that have formed after each election.
*/
public abstract ElectionCabinetResult electionSequence(String countryName);
/**
* Returns the list of politicians similar to a given politician.
*
* Does so by finding politicians that have similar comments and descriptions (combined)
* using Jaccard similarity and a threshold.
*
* @param politicianId id of the politician
* @param threshold Jaccard similarity threshold
* @return a list of politicians with Jaccard similarity of
* comments and descriptions above the given threshold
*/
public abstract List<String> findSimilarPoliticians(int politicianId, float threshold);
}