Skip to content

Commit bcb9b9a

Browse files
author
Ben McLean
committed
Implemented score suggestion
1 parent 1f4a7d2 commit bcb9b9a

File tree

4 files changed

+302
-0
lines changed

4 files changed

+302
-0
lines changed

src/au/com/origma/perspectiveapi/v1alpha1/PerspectiveAPI.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import au.com.origma.perspectiveapi.v1alpha1.models.AnalyzeCommentRequest;
2121
import au.com.origma.perspectiveapi.v1alpha1.models.AnalyzeCommentResponse;
22+
import au.com.origma.perspectiveapi.v1alpha1.models.SuggestScoreRequest;
23+
import au.com.origma.perspectiveapi.v1alpha1.models.SuggestScoreResponse;
2224

2325
/**
2426
* Interact with Google's PerspectiveAPI to analyze comments and content.<br>
@@ -34,6 +36,11 @@ public interface PerspectiveAPI {
3436
*/
3537
public static final String ANALYZE_ENDPOINT = "https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze";
3638

39+
/**
40+
* The endpoint to which suggestion requests are sent
41+
*/
42+
public static final String SUGGEST_ENDPOINT = "https://commentanalyzer.googleapis.com/v1alpha1/comments:suggestscore";
43+
3744
/**
3845
* Analyze a comment
3946
* @param request The request
@@ -48,6 +55,13 @@ public interface PerspectiveAPI {
4855
*/
4956
public AnalyzeCommentResponse analyze(String comment);
5057

58+
/**
59+
* Suggest a score for a comment
60+
* @param request The request
61+
* @return The response
62+
*/
63+
public SuggestScoreResponse suggestScore(SuggestScoreRequest request);
64+
5165
/**
5266
* Instantiate the default implementation of the PerspectiveAPI
5367
* @param apiKey The Google API key

src/au/com/origma/perspectiveapi/v1alpha1/UnirestPerspectiveAPI.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import au.com.origma.perspectiveapi.v1alpha1.models.AttributeType;
2323
import au.com.origma.perspectiveapi.v1alpha1.models.ContentType;
2424
import au.com.origma.perspectiveapi.v1alpha1.models.Entry;
25+
import au.com.origma.perspectiveapi.v1alpha1.models.SuggestScoreRequest;
26+
import au.com.origma.perspectiveapi.v1alpha1.models.SuggestScoreResponse;
2527
import kong.unirest.HttpResponse;
2628
import kong.unirest.Unirest;
2729

@@ -69,6 +71,20 @@ public AnalyzeCommentResponse analyze(String comment) {
6971
.doNotStore(doNotStore)
7072
.build());
7173
}
74+
75+
@Override
76+
public SuggestScoreResponse suggestScore(SuggestScoreRequest request) {
77+
HttpResponse<SuggestScoreResponse> response = Unirest.post(SUGGEST_ENDPOINT)
78+
.queryString("key", apiKey)
79+
.body(request)
80+
.asObject(SuggestScoreResponse.class);
81+
82+
if(!response.isSuccess()){
83+
return null;
84+
}
85+
86+
return response.getBody();
87+
}
7288

7389
/**
7490
* If the simple analysis will default to do not store
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
/*
2+
* Copyright 2019 Origma Pty Ltd (ACN 629 381 184) and contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* Written by Ben McLean <ben@origma.com.au>, November 2019
17+
*/
18+
package au.com.origma.perspectiveapi.v1alpha1.models;
19+
20+
import java.util.ArrayList;
21+
import java.util.HashMap;
22+
import java.util.List;
23+
import java.util.Map;
24+
25+
/**
26+
* Send a suggested score for a request
27+
*
28+
* @author Ben McLean &lt;ben@origma.com.au&gt;
29+
*
30+
*/
31+
public class SuggestScoreRequest {
32+
33+
Entry comment;
34+
Context context;
35+
Map<AttributeType, AttributeScore> attributeScores;
36+
List<String> languages;
37+
String communityId;
38+
String clientToken;
39+
40+
/**
41+
* Same as AnalyzeComment request.
42+
*
43+
* @return the comment for which the score is being suggested
44+
*/
45+
public Entry getComment() {
46+
return comment;
47+
}
48+
49+
/**
50+
* Same as AnalyzeComment request.
51+
*
52+
* @return the context of the request
53+
*/
54+
public Context getContext() {
55+
return context;
56+
}
57+
58+
/**
59+
* Similar to AnalyzeComment response. This holds the attribute scores that the
60+
* client believes the comment should have. It has the same format as the
61+
* attributeScores field in the AnalyzeComment response, as it's what the client
62+
* believes is the correct "answer" for what this comment should be scored as.
63+
* The client can specify just summary scores, just span scores, or both.
64+
*
65+
* @return the attribute scores of the request
66+
*/
67+
public Map<AttributeType, AttributeScore> getAttributeScores() {
68+
return attributeScores;
69+
}
70+
71+
/**
72+
* Returns the languages of the request
73+
*
74+
* @return the languages of the request
75+
*/
76+
public List<String> getLanguages() {
77+
return languages;
78+
}
79+
80+
/**
81+
* Opaque identifier associating this score suggestion with a particular
82+
* community. If set, this field allows us to differentiate suggestions from
83+
* different communities, as each community may have different norms.
84+
*
85+
* @return the community id
86+
*/
87+
public String getCommunityId() {
88+
return communityId;
89+
}
90+
91+
/**
92+
* An opaque token that is echoed back in the response.
93+
*
94+
* @return the client token
95+
*/
96+
public String getClientToken() {
97+
return clientToken;
98+
}
99+
100+
/**
101+
* Builds a SuggestScoreRequest
102+
*
103+
* @author Ben McLean &lt;ben@origma.com.au&gt;
104+
*
105+
*/
106+
public static class Builder {
107+
private Entry comment;
108+
private Context context;
109+
private Map<AttributeType, AttributeScore> attributeScores = new HashMap<>();
110+
private List<String> languages = new ArrayList<>();
111+
private String communityId;
112+
private String clientToken;
113+
114+
/**
115+
* Set the comment for a suggestion
116+
* @param comment the comment
117+
* @return The builder
118+
*/
119+
public Builder comment(Entry comment) {
120+
this.comment = comment;
121+
return this;
122+
}
123+
124+
/**
125+
* Set the context for a suggestion
126+
* @param context the context
127+
* @return The builder
128+
*/
129+
public Builder context(Context context) {
130+
this.context = context;
131+
return this;
132+
}
133+
134+
/**
135+
* Set the attribute scores for a suggestion
136+
* @param attributeScores the attribute scores
137+
* @return The builder
138+
*/
139+
public Builder attributeScores(Map<AttributeType, AttributeScore> attributeScores) {
140+
this.attributeScores = attributeScores;
141+
return this;
142+
}
143+
144+
/**
145+
* Add an attribute score
146+
* @param type The attribute type
147+
* @param score The attribute score
148+
* @return The builder
149+
*/
150+
public Builder addAttributeScore(AttributeType type, AttributeScore score) {
151+
this.attributeScores.put(type, score);
152+
return this;
153+
}
154+
155+
/**
156+
* Set the languages for a suggestion
157+
* @param languages the languages
158+
* @return The builder
159+
*/
160+
public Builder languages(List<String> languages) {
161+
this.languages = languages;
162+
return this;
163+
}
164+
165+
/**
166+
* Add a language
167+
* @param language the language
168+
* @return The builder
169+
*/
170+
public Builder addLanugage(String language) {
171+
this.languages.add(language);
172+
return this;
173+
}
174+
175+
/**
176+
* Set the community id
177+
* @param communityId the community id
178+
* @return The builder
179+
*/
180+
public Builder communityId(String communityId) {
181+
this.communityId = communityId;
182+
return this;
183+
}
184+
185+
/**
186+
* Set the client token
187+
* @param clientToken the client token
188+
* @return The builder
189+
*/
190+
public Builder clientToken(String clientToken) {
191+
this.clientToken = clientToken;
192+
return this;
193+
}
194+
195+
/**
196+
* Build a new SuggestScoreRequest instance
197+
* @return a new SuggestScoreRequest instance
198+
*/
199+
public SuggestScoreRequest build() {
200+
return new SuggestScoreRequest(this);
201+
}
202+
}
203+
204+
private SuggestScoreRequest(Builder builder) {
205+
this.comment = builder.comment;
206+
this.context = builder.context;
207+
this.attributeScores = builder.attributeScores;
208+
this.languages = builder.languages;
209+
this.communityId = builder.communityId;
210+
this.clientToken = builder.clientToken;
211+
}
212+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
/*
3+
* Copyright 2019 Origma Pty Ltd (ACN 629 381 184) and contributors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
* Written by Ben McLean <ben@origma.com.au>, November 2019
18+
*/
19+
package au.com.origma.perspectiveapi.v1alpha1.models;
20+
21+
/**
22+
* A SuggestScoreRequest response
23+
*
24+
* @author Ben McLean &lt;ben@origma.com.au&gt;
25+
*
26+
*/
27+
public class SuggestScoreResponse {
28+
29+
String clientToken;
30+
31+
/**
32+
* Create an empty instance
33+
*/
34+
public SuggestScoreResponse() {}
35+
36+
/**
37+
* Create an instance
38+
*/
39+
public SuggestScoreResponse(String clientToken) {
40+
super();
41+
this.clientToken = clientToken;
42+
}
43+
44+
/**
45+
* Mirrors the request's clientToken
46+
* @return the client token
47+
*/
48+
public String getClientToken() {
49+
return clientToken;
50+
}
51+
52+
/**
53+
* Sets the client token
54+
* @param clientToken the client token
55+
*/
56+
public void setClientToken(String clientToken) {
57+
this.clientToken = clientToken;
58+
}
59+
60+
}

0 commit comments

Comments
 (0)