Skip to content

Commit f0c2dd0

Browse files
authored
Merge pull request #116 from dscorbett/WS-1258
WS-1258: Expose a sentiment model type option
2 parents 0748e9b + 84e9c7b commit f0c2dd0

File tree

7 files changed

+207
-11
lines changed

7 files changed

+207
-11
lines changed

json/src/main/java/com/basistech/rosette/apimodel/jackson/ApiModelMixinModule.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import com.basistech.rosette.apimodel.Response;
5858
import com.basistech.rosette.apimodel.SentenceWithDependencies;
5959
import com.basistech.rosette.apimodel.SentencesResponse;
60+
import com.basistech.rosette.apimodel.SentimentModelType;
6061
import com.basistech.rosette.apimodel.SentimentOptions;
6162
import com.basistech.rosette.apimodel.SentimentResponse;
6263
import com.basistech.rosette.apimodel.SyntaxDependenciesResponse;
@@ -139,8 +140,10 @@ public void setupModule(Module.SetupContext context) {
139140
context.setMixInAnnotations(Concept.class, ConceptMixin.class);
140141

141142
context.setMixInAnnotations(AccuracyMode.class, AccuracyModeMixin.class);
143+
context.setMixInAnnotations(SentimentModelType.class, SentimentModelTypeMixin.class);
142144
SimpleSerializers keySerializers = new SimpleSerializers();
143145
keySerializers.addSerializer(new AccuracyModeSerializer());
146+
keySerializers.addSerializer(new SentimentModelTypeSerializer());
144147
context.addKeySerializers(keySerializers);
145148

146149
context.setMixInAnnotations(BatchRequest.class, BatchRequestMixin.class);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2017 Basis Technology Corp.
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+
17+
package com.basistech.rosette.apimodel.jackson;
18+
19+
import com.basistech.rosette.apimodel.SentimentModelType;
20+
import com.fasterxml.jackson.core.JsonParser;
21+
import com.fasterxml.jackson.databind.DeserializationContext;
22+
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
23+
24+
import java.io.IOException;
25+
26+
/**
27+
* A Jackson deserializer for {@link SentimentModelType}.
28+
*/
29+
public class SentimentModelTypeDeserializer extends StdDeserializer<SentimentModelType> {
30+
public SentimentModelTypeDeserializer() {
31+
super(SentimentModelType.class);
32+
}
33+
34+
@Override
35+
public SentimentModelType deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException {
36+
return SentimentModelType.forValue(jp.getText());
37+
}
38+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2017 Basis Technology Corp.
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+
17+
package com.basistech.rosette.apimodel.jackson;
18+
19+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
20+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
21+
22+
@JsonSerialize(using = SentimentModelTypeSerializer.class, keyUsing = SentimentModelTypeSerializer.class)
23+
@JsonDeserialize(using = SentimentModelTypeDeserializer.class)
24+
public class SentimentModelTypeMixin extends BaseMixin {
25+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2017 Basis Technology Corp.
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+
17+
package com.basistech.rosette.apimodel.jackson;
18+
19+
import com.basistech.rosette.apimodel.SentimentModelType;
20+
import com.fasterxml.jackson.core.JsonGenerator;
21+
import com.fasterxml.jackson.databind.JsonSerializer;
22+
import com.fasterxml.jackson.databind.SerializerProvider;
23+
24+
import java.io.IOException;
25+
26+
/**
27+
* A Jackson serializer for {@link SentimentModelType}.
28+
*/
29+
public class SentimentModelTypeSerializer extends JsonSerializer<SentimentModelType> {
30+
@Override
31+
public void serialize(
32+
final SentimentModelType sentimentModelType,
33+
final JsonGenerator jsonGenerator,
34+
final SerializerProvider serializerProvider)
35+
throws IOException {
36+
jsonGenerator.writeString(sentimentModelType.getLabel());
37+
}
38+
39+
@Override
40+
public Class<SentimentModelType> handledType() {
41+
return SentimentModelType.class;
42+
}
43+
}

json/src/main/java/com/basistech/rosette/apimodel/jackson/SentimentOptionsMixin.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 Basis Technology Corp.
2+
* Copyright 2017 Basis Technology Corp.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package com.basistech.rosette.apimodel.jackson;
1818

19+
import com.basistech.rosette.apimodel.SentimentModelType;
1920
import com.fasterxml.jackson.annotation.JsonCreator;
2021
import com.fasterxml.jackson.annotation.JsonProperty;
2122
import com.fasterxml.jackson.annotation.JsonTypeName;
@@ -25,7 +26,8 @@ public abstract class SentimentOptionsMixin extends OptionsMixin {
2526
@JsonCreator
2627
protected SentimentOptionsMixin(
2728
@JsonProperty("calculateEntityConfidence") Boolean calculateEntityConfidence,
28-
@JsonProperty("calculateEntitySalience") Boolean calculateEntitySalience
29+
@JsonProperty("calculateEntitySalience") Boolean calculateEntitySalience,
30+
@JsonProperty("modelType") SentimentModelType modelType
2931
) {
3032
//
3133
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2017 Basis Technology Corp.
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+
17+
package com.basistech.rosette.apimodel;
18+
19+
import java.util.EnumSet;
20+
21+
/**
22+
* A model type for sentiment analysis.
23+
*/
24+
public enum SentimentModelType {
25+
SVM("svm"),
26+
DNN("dnn"),
27+
/**/;
28+
29+
private final String label;
30+
31+
SentimentModelType(final String label) {
32+
this.label = label;
33+
}
34+
35+
/**
36+
* Gets this model type's label.
37+
* @return this model type's label
38+
*/
39+
public String getLabel() {
40+
return label;
41+
}
42+
43+
/**
44+
* Checks if a value is a valid {@code SentimentModelType}
45+
* @param value an input value
46+
* @return the {@code SentimentModelType} corresponding to the input value
47+
* @throws IllegalArgumentException if the input string is invalid
48+
*/
49+
public static SentimentModelType forValue(final String value) throws IllegalArgumentException {
50+
for (final SentimentModelType sentimentModelType : EnumSet.allOf(SentimentModelType.class)) {
51+
if (sentimentModelType.label.equalsIgnoreCase(value)) {
52+
return sentimentModelType;
53+
}
54+
}
55+
throw new IllegalArgumentException("invalid sentiment model type: " + value);
56+
}
57+
}

model/src/main/java/com/basistech/rosette/apimodel/SentimentOptions.java

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 Basis Technology Corp.
2+
* Copyright 2017 Basis Technology Corp.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,18 +22,26 @@
2222
* Sentiment options.
2323
*/
2424
public final class SentimentOptions extends Options {
25-
public static final SentimentOptions DEGAULT_OPTIONS = new SentimentOptions(false, false);
25+
public static final SentimentOptions DEFAULT_OPTIONS = new SentimentOptions(false, false, SentimentModelType.SVM);
2626
private Boolean calculateEntityConfidence;
2727
private Boolean calculateEntitySalience;
28+
private SentimentModelType modelType;
2829

2930
/**
30-
* Constructor for {@code SentimentOptions}
31-
*
32-
* @param calculateEntityConfidence return confidence score for the entities.
31+
* Constructs a {@code SentimentOptions}.
32+
* @param calculateEntityConfidence whether to return confidence
33+
* scores for entities
34+
* @param calculateEntitySalience whether to return salience values
35+
* for entities
36+
* @param modelType the model type
3337
*/
34-
protected SentimentOptions(Boolean calculateEntityConfidence, Boolean calculateEntitySalience) {
38+
protected SentimentOptions(
39+
final Boolean calculateEntityConfidence,
40+
final Boolean calculateEntitySalience,
41+
final SentimentModelType modelType) {
3542
this.calculateEntityConfidence = calculateEntityConfidence;
3643
this.calculateEntitySalience = calculateEntitySalience;
44+
this.modelType = modelType;
3745
}
3846

3947
/**
@@ -50,6 +58,14 @@ public Boolean getCalculateEntitySalience() {
5058
return calculateEntitySalience;
5159
}
5260

61+
/**
62+
* Gets the model type.
63+
* @return the model type
64+
*/
65+
public SentimentModelType getModelType() {
66+
return modelType;
67+
}
68+
5369
@Override
5470
public boolean equals(Object o) {
5571
if (this == o) {
@@ -60,17 +76,19 @@ public boolean equals(Object o) {
6076
}
6177
SentimentOptions that = (SentimentOptions)o;
6278
return Objects.equals(this.calculateEntityConfidence, that.calculateEntityConfidence)
63-
&& Objects.equals(this.calculateEntitySalience, that.calculateEntitySalience);
79+
&& Objects.equals(this.calculateEntitySalience, that.calculateEntitySalience)
80+
&& modelType == that.modelType;
6481
}
6582

6683
@Override
6784
public int hashCode() {
68-
return Objects.hash(calculateEntityConfidence, calculateEntitySalience);
85+
return Objects.hash(calculateEntityConfidence, calculateEntitySalience, modelType);
6986
}
7087

7188
public static class Builder {
7289
private Boolean calculateEntityConfidence;
7390
private Boolean calculateEntitySalience;
91+
private SentimentModelType modelType;
7492

7593
/**
7694
* DocumentRequest calculate entity confidence score. If the value is {@code true}, then the endpoint will
@@ -94,8 +112,18 @@ public Builder calculateEntitySalience(Boolean calculateEntitySalience) {
94112
return this;
95113
}
96114

115+
/**
116+
* Sets the model type for sentiment analysis.
117+
* @param modelType the model type
118+
* @return {@code this}
119+
*/
120+
public Builder modelType(final SentimentModelType modelType) {
121+
this.modelType = modelType;
122+
return this;
123+
}
124+
97125
public SentimentOptions build() {
98-
return new SentimentOptions(calculateEntityConfidence, calculateEntitySalience);
126+
return new SentimentOptions(calculateEntityConfidence, calculateEntitySalience, modelType);
99127
}
100128
}
101129
}

0 commit comments

Comments
 (0)