Skip to content

Commit 17ea957

Browse files
committed
OPENNLP-855: New SentimentAnalysisParser
- adapts existing Sentiment code to OpenNLP 3.x module structures - introduces SentimentDetector API interface
1 parent 2f376c3 commit 17ea957

19 files changed

+122
-80
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. 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+
18+
package opennlp.tools.sentiment;
19+
20+
import opennlp.tools.util.Span;
21+
22+
public interface SentimentDetector {
23+
24+
/**
25+
* Conducts a sentiment prediction for the specifed sentence.
26+
*
27+
* @param sentence The text to be analysed for its sentiment.
28+
* @return The predicted sentiment.
29+
*/
30+
String predict(String sentence);
31+
32+
/**
33+
* Conducts a sentiment prediction for the specifed sentence.
34+
*
35+
* @param tokens The text to be analysed for its sentiment.
36+
* @return The predicted sentiment.
37+
*/
38+
String predict(String[] tokens);
39+
40+
/**
41+
* Generates sentiment tags for the given sequence, typically a sentence,
42+
* returning token spans for any identified sentiments.
43+
*
44+
* @param tokens
45+
* an array of the tokens or words of the sequence, typically a
46+
* sentence
47+
* @return an array of spans for each of the names identified.
48+
*/
49+
Span[] find(String[] tokens);
50+
51+
/**
52+
* Generates sentiment tags for the given sequence, typically a sentence,
53+
* returning token spans for any identified sentiments.
54+
*
55+
* @param tokens
56+
* an array of the tokens or words of the sequence, typically a
57+
* sentence.
58+
* @param additionalContext
59+
* features which are based on context outside of the sentence but
60+
* which should also be used.
61+
*
62+
* @return an array of spans for each of the names identified.
63+
*/
64+
Span[] find(String[] tokens, String[][] additionalContext);
65+
}

opennlp-tools/src/main/java/opennlp/tools/sentiment/SentimentEvaluationMonitor.java renamed to opennlp-api/src/main/java/opennlp/tools/sentiment/SentimentEvaluationMonitor.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
import opennlp.tools.util.eval.EvaluationMonitor;
2121

2222
/**
23-
* Evaluation Monitor to be used by the evaluator
23+
* An sentiment specific {@link EvaluationMonitor} to be used by the evaluator.
24+
*
25+
* @see SentimentSample
2426
*/
25-
public interface SentimentEvaluationMonitor
26-
extends EvaluationMonitor<SentimentSample> {
27+
public interface SentimentEvaluationMonitor extends EvaluationMonitor<SentimentSample> {
2728

2829
}

opennlp-tools/src/main/java/opennlp/tools/sentiment/SentimentSample.java renamed to opennlp-api/src/main/java/opennlp/tools/sentiment/SentimentSample.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@
1717

1818
package opennlp.tools.sentiment;
1919

20+
import java.io.Serial;
2021
import java.util.List;
2122

23+
import opennlp.tools.commons.Sample;
24+
2225
/**
2326
* Class for holding text used for sentiment analysis.
2427
*/
25-
public class SentimentSample {
28+
public class SentimentSample implements Sample {
29+
30+
@Serial
31+
private static final long serialVersionUID = 2477213313738337539L;
2632

2733
private final String sentiment;
2834
private final List<String> sentence;

opennlp-tools/src/main/java/opennlp/tools/cmdline/sentiment/SentimentEvaluationErrorListener.java renamed to opennlp-core/opennlp-cli/src/main/java/opennlp/tools/cmdline/sentiment/SentimentEvaluationErrorListener.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ protected SentimentEvaluationErrorListener(OutputStream outputStream) {
5353
* the sentiment sampple prediction
5454
*/
5555
@Override
56-
public void misclassified(SentimentSample reference,
57-
SentimentSample prediction) {
56+
public void misclassified(SentimentSample reference, SentimentSample prediction) {
5857
printError(new String[] { reference.getSentiment() },
5958
new String[] { prediction.getSentiment() }, reference, prediction,
6059
reference.getSentence());

opennlp-tools/src/main/java/opennlp/tools/formats/SentimentSampleStreamFactory.java renamed to opennlp-core/opennlp-formats/src/main/java/opennlp/tools/formats/SentimentSampleStreamFactory.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import java.io.IOException;
2121

2222
import opennlp.tools.cmdline.ArgumentParser;
23-
import opennlp.tools.cmdline.CmdLineUtil;
2423
import opennlp.tools.cmdline.StreamFactoryRegistry;
24+
import opennlp.tools.cmdline.TerminateToolException;
2525
import opennlp.tools.cmdline.params.BasicFormatParams;
2626
import opennlp.tools.sentiment.SentimentSample;
2727
import opennlp.tools.sentiment.SentimentSampleStream;
@@ -47,37 +47,33 @@ protected SentimentSampleStreamFactory(Class<P> params) {
4747
}
4848

4949
/**
50-
* Creates a sentiment sample stream factory
50+
* Creates a sentiment sample stream.
5151
*
5252
* @param args
5353
* the necessary arguments
54-
* @return SentimentSample stream (factory)
54+
* @return A {@link SentimentSample} stream.
5555
*/
5656
@Override
5757
public ObjectStream<SentimentSample> create(String[] args) {
58-
BasicFormatParams params = ArgumentParser.parse(args,
59-
BasicFormatParams.class);
58+
BasicFormatParams params = ArgumentParser.parse(args, BasicFormatParams.class);
6059

61-
CmdLineUtil.checkInputFile("Data", params.getData());
62-
InputStreamFactory sampleDataIn = CmdLineUtil
63-
.createInputStreamFactory(params.getData());
64-
ObjectStream<String> lineStream = null;
60+
FormatUtil.checkInputFile("Data", params.getData());
61+
ObjectStream<String> lineStream;
6562
try {
66-
lineStream = new PlainTextByLineStream(sampleDataIn,
67-
params.getEncoding());
63+
InputStreamFactory sampleDataIn = FormatUtil.createInputStreamFactory(params.getData());
64+
lineStream = new PlainTextByLineStream(sampleDataIn, params.getEncoding());
6865
} catch (IOException ex) {
69-
CmdLineUtil.handleCreateObjectStreamError(ex);
66+
throw new TerminateToolException(-1,
67+
"IO Error while creating an Input Stream: " + ex.getMessage(), ex);
7068
}
71-
7269
return new SentimentSampleStream(lineStream);
7370
}
7471

7572
/**
7673
* Registers a SentimentSample stream factory
7774
*/
7875
public static void registerFactory() {
79-
StreamFactoryRegistry.registerFactory(SentimentSample.class,
80-
StreamFactoryRegistry.DEFAULT_FORMAT,
76+
StreamFactoryRegistry.registerFactory(SentimentSample.class, StreamFactoryRegistry.DEFAULT_FORMAT,
8177
new SentimentSampleStreamFactory<>(BasicFormatParams.class));
8278
}
8379

0 commit comments

Comments
 (0)