Skip to content

Commit 7cc6ee4

Browse files
author
Ben McLean
committed
Initial commit
0 parents  commit 7cc6ee4

15 files changed

Lines changed: 695 additions & 0 deletions

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.settings/
2+
.classpath
3+
.project
4+
target/

pom.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>au.com.origma</groupId>
5+
<artifactId>PerspectiveAPI</artifactId>
6+
<version>0.0.1</version>
7+
<build>
8+
<sourceDirectory>src</sourceDirectory>
9+
<plugins>
10+
<plugin>
11+
<artifactId>maven-compiler-plugin</artifactId>
12+
<version>3.3</version>
13+
<configuration>
14+
<source>1.3</source>
15+
<target>1.1</target>
16+
</configuration>
17+
</plugin>
18+
</plugins>
19+
</build>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>com.konghq</groupId>
24+
<artifactId>unirest-java</artifactId>
25+
<version>3.1.02</version>
26+
</dependency>
27+
</dependencies>
28+
</project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (C) Origma Pty Ltd (ACN 629 381 184). All Rights Reserved
3+
* Unauthorized copying of this file, via any medium is strictly prohibited
4+
* Proprietary and confidential
5+
* Written by Ben McLean <ben@origma.com.au>, November 2019
6+
*/
7+
package au.com.origma.perspectiveapi.v1alpha1;
8+
9+
import au.com.origma.perspectiveapi.v1alpha1.models.AnalyzeCommentRequest;
10+
import au.com.origma.perspectiveapi.v1alpha1.models.AnalyzeCommentResponse;
11+
12+
public interface PerspectiveAPI {
13+
14+
public static final String ANALYZE_ENDPOINT = "https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze";
15+
16+
public AnalyzeCommentResponse analyze(AnalyzeCommentRequest request);
17+
public AnalyzeCommentResponse analyze(String comment);
18+
19+
public static PerspectiveAPI create(String apiKey) {
20+
return new UnirestPerspectiveAPI(apiKey);
21+
}
22+
23+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (C) Origma Pty Ltd (ACN 629 381 184). All Rights Reserved
3+
* Unauthorized copying of this file, via any medium is strictly prohibited
4+
* Proprietary and confidential
5+
* Written by Ben McLean <ben@origma.com.au>, November 2019
6+
*/
7+
package au.com.origma.perspectiveapi.v1alpha1;
8+
9+
import au.com.origma.perspectiveapi.v1alpha1.models.AnalyzeCommentRequest;
10+
import au.com.origma.perspectiveapi.v1alpha1.models.AnalyzeCommentResponse;
11+
import au.com.origma.perspectiveapi.v1alpha1.models.AttributeType;
12+
import au.com.origma.perspectiveapi.v1alpha1.models.ContentType;
13+
import au.com.origma.perspectiveapi.v1alpha1.models.Entry;
14+
import kong.unirest.HttpResponse;
15+
import kong.unirest.Unirest;
16+
17+
public class UnirestPerspectiveAPI implements PerspectiveAPI {
18+
19+
String apiKey;
20+
boolean doNotStore = true;
21+
22+
public UnirestPerspectiveAPI(String apiKey) {
23+
super();
24+
this.apiKey = apiKey;
25+
}
26+
27+
@Override
28+
public AnalyzeCommentResponse analyze(AnalyzeCommentRequest request) {
29+
HttpResponse<AnalyzeCommentResponse> response = Unirest.post(ANALYZE_ENDPOINT)
30+
.queryString("key", apiKey)
31+
.body(request)
32+
.asObject(AnalyzeCommentResponse.class);
33+
34+
if(!response.isSuccess()){
35+
return null;
36+
}
37+
38+
return response.getBody();
39+
}
40+
41+
@Override
42+
public AnalyzeCommentResponse analyze(String comment) {
43+
return analyze(new AnalyzeCommentRequest.Builder()
44+
.addRequestedAttribute(AttributeType.TOXICITY, null)
45+
.comment(new Entry.Builder()
46+
.type(ContentType.PLAIN_TEXT)
47+
.text(comment)
48+
.build())
49+
.build());
50+
}
51+
52+
public boolean isDoNotStore() {
53+
return doNotStore;
54+
}
55+
56+
public void setDoNotStore(boolean doNotStore) {
57+
this.doNotStore = doNotStore;
58+
}
59+
60+
}
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/*
2+
* Copyright (C) Origma Pty Ltd (ACN 629 381 184). All Rights Reserved
3+
* Unauthorized copying of this file, via any medium is strictly prohibited
4+
* Proprietary and confidential
5+
* Written by Ben McLean <ben@origma.com.au>, November 2019
6+
*/
7+
package au.com.origma.perspectiveapi.v1alpha1.models;
8+
9+
import java.util.ArrayList;
10+
import java.util.HashMap;
11+
import java.util.List;
12+
import java.util.Map;
13+
14+
public class AnalyzeCommentRequest {
15+
16+
Entry comment;
17+
Context context;
18+
Map<AttributeType, RequestedAttribute> requestedAttributes;
19+
List<String> languages;
20+
Boolean doNotStore;
21+
String clientToken;
22+
String sessionId;
23+
Double suggestCommentScore;
24+
25+
public AnalyzeCommentRequest() {
26+
super();
27+
}
28+
29+
public AnalyzeCommentRequest(Entry comment, Context context, Map<AttributeType, RequestedAttribute> requestedAttributes,
30+
List<String> languages, Boolean doNotStore, String clientToken, String sessionId,
31+
Double suggestCommentScore) {
32+
super();
33+
this.comment = comment;
34+
this.context = context;
35+
this.requestedAttributes = requestedAttributes;
36+
this.languages = languages;
37+
this.doNotStore = doNotStore;
38+
this.clientToken = clientToken;
39+
this.sessionId = sessionId;
40+
this.suggestCommentScore = suggestCommentScore;
41+
}
42+
43+
public Entry getComment() {
44+
return comment;
45+
}
46+
47+
public void setComment(Entry comment) {
48+
this.comment = comment;
49+
}
50+
51+
public Context getContext() {
52+
return context;
53+
}
54+
55+
public void setContext(Context context) {
56+
this.context = context;
57+
}
58+
59+
public Map<AttributeType, RequestedAttribute> getRequestedAttributes() {
60+
return requestedAttributes;
61+
}
62+
63+
public void setRequestedAttributes(Map<AttributeType, RequestedAttribute> requestedAttributes) {
64+
this.requestedAttributes = requestedAttributes;
65+
}
66+
67+
public List<String> getLanguages() {
68+
return languages;
69+
}
70+
71+
public void setLanguages(List<String> languages) {
72+
this.languages = languages;
73+
}
74+
75+
public Boolean getDoNotStore() {
76+
return doNotStore;
77+
}
78+
79+
public void setDoNotStore(Boolean doNotStore) {
80+
this.doNotStore = doNotStore;
81+
}
82+
83+
public String getClientToken() {
84+
return clientToken;
85+
}
86+
87+
public void setClientToken(String clientToken) {
88+
this.clientToken = clientToken;
89+
}
90+
91+
public String getSessionId() {
92+
return sessionId;
93+
}
94+
95+
public void setSessionId(String sessionId) {
96+
this.sessionId = sessionId;
97+
}
98+
99+
public Double getSuggestCommentScore() {
100+
return suggestCommentScore;
101+
}
102+
103+
public void setSuggestCommentScore(Double suggestCommentScore) {
104+
this.suggestCommentScore = suggestCommentScore;
105+
}
106+
107+
public static class Builder {
108+
private Entry comment;
109+
private Context context;
110+
private Map<AttributeType, RequestedAttribute> requestedAttributes = new HashMap<>();
111+
private List<String> languages = new ArrayList<>();
112+
private Boolean doNotStore;
113+
private String clientToken;
114+
private String sessionId;
115+
private Double suggestCommentScore;
116+
117+
public Builder comment(Entry comment) {
118+
this.comment = comment;
119+
return this;
120+
}
121+
122+
public Builder context(Context context) {
123+
this.context = context;
124+
return this;
125+
}
126+
127+
public Builder requestedAttributes(Map<AttributeType, RequestedAttribute> requestedAttributes) {
128+
this.requestedAttributes = requestedAttributes;
129+
return this;
130+
}
131+
132+
public Builder addRequestedAttribute(AttributeType type, RequestedAttribute requestedAttribute) {
133+
requestedAttributes.put(type, requestedAttribute);
134+
return this;
135+
}
136+
137+
public Builder languages(List<String> languages) {
138+
this.languages = languages;
139+
return this;
140+
}
141+
142+
public Builder addLanguage(String language){
143+
languages.add(language);
144+
return this;
145+
}
146+
147+
public Builder doNotStore(Boolean doNotStore) {
148+
this.doNotStore = doNotStore;
149+
return this;
150+
}
151+
152+
public Builder clientToken(String clientToken) {
153+
this.clientToken = clientToken;
154+
return this;
155+
}
156+
157+
public Builder sessionId(String sessionId) {
158+
this.sessionId = sessionId;
159+
return this;
160+
}
161+
162+
public Builder suggestCommentScore(Double suggestCommentScore) {
163+
this.suggestCommentScore = suggestCommentScore;
164+
return this;
165+
}
166+
167+
public AnalyzeCommentRequest build() {
168+
return new AnalyzeCommentRequest(this);
169+
}
170+
}
171+
172+
private AnalyzeCommentRequest(Builder builder) {
173+
this.comment = builder.comment;
174+
this.context = builder.context;
175+
this.requestedAttributes = builder.requestedAttributes;
176+
this.languages = builder.languages;
177+
this.doNotStore = builder.doNotStore;
178+
this.clientToken = builder.clientToken;
179+
this.sessionId = builder.sessionId;
180+
this.suggestCommentScore = builder.suggestCommentScore;
181+
}
182+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (C) Origma Pty Ltd (ACN 629 381 184). All Rights Reserved
3+
* Unauthorized copying of this file, via any medium is strictly prohibited
4+
* Proprietary and confidential
5+
* Written by Ben McLean <ben@origma.com.au>, November 2019
6+
*/
7+
package au.com.origma.perspectiveapi.v1alpha1.models;
8+
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
public class AnalyzeCommentResponse {
13+
14+
Map<AttributeType, AttributeScore> attributeScores;
15+
List<String> languages;
16+
String clientToken;
17+
18+
public AnalyzeCommentResponse() {
19+
super();
20+
}
21+
22+
public AnalyzeCommentResponse(Map<AttributeType, AttributeScore> attributeScores, List<String> languages,
23+
String clientToken) {
24+
super();
25+
this.attributeScores = attributeScores;
26+
this.languages = languages;
27+
this.clientToken = clientToken;
28+
}
29+
30+
public Map<AttributeType, AttributeScore> getAttributeScores() {
31+
return attributeScores;
32+
}
33+
34+
public AttributeScore getAttributeScore(AttributeType type){
35+
return attributeScores.get(type);
36+
}
37+
38+
public void setAttributeScores(Map<AttributeType, AttributeScore> attributeScores) {
39+
this.attributeScores = attributeScores;
40+
}
41+
42+
public List<String> getLanguages() {
43+
return languages;
44+
}
45+
46+
public void setLanguages(List<String> languages) {
47+
this.languages = languages;
48+
}
49+
50+
public String getClientToken() {
51+
return clientToken;
52+
}
53+
54+
public void setClientToken(String clientToken) {
55+
this.clientToken = clientToken;
56+
}
57+
58+
}

0 commit comments

Comments
 (0)