-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClaimBusterAPI.java
82 lines (69 loc) · 3.04 KB
/
ClaimBusterAPI.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
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class ClaimBusterAPI {
public static void main(String[] args) {
String input = "Donald Duck is the current president of the United States.";
double score = getClaimBusterScore(input);
System.out.println("ClaimBuster score for \"" + input + "\": " + score);
}
public static double getClaimBusterScore(String input) {
String apiKey = "f69e1f1a98ee44ef8b586c31df77ecea";
double score = -1.0; // Default value if score extraction fails
String encodedInput;
String apiEndpoint;
try {
encodedInput = URLEncoder.encode(input, "UTF-8");
apiEndpoint = "https://idir.uta.edu/claimbuster/api/v2/score/text/" + encodedInput;
} catch (IOException e) {
System.out.println("Error encoding input: " + e.getMessage());
return score;
}
try {
URL url = new URL(apiEndpoint);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// Set request method and headers
connection.setRequestMethod("GET");
connection.setRequestProperty("x-api-key", apiKey);
// Get the API response
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
// Parse the JSON response
String jsonResponse = response.toString();
JSONParser parser = new JSONParser();
try {
JSONObject json = (JSONObject) parser.parse(jsonResponse);
JSONArray results = (JSONArray) json.get("results");
if (results != null && results.size() > 0) {
JSONObject result = (JSONObject) results.get(0);
score = (Double) result.get("score");
} else {
System.out.println("No results found in the JSON response.");
}
} catch (ParseException e) {
System.out.println("Error parsing JSON response: " + e.getMessage());
}
} else {
System.out.println("API request failed. Response Code: " + responseCode);
}
connection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
return score;
}
}