Skip to content

Commit 5e57dee

Browse files
committed
OPENNLP-2940 - Add tests
1 parent bb63f9b commit 5e57dee

File tree

9 files changed

+686
-0
lines changed

9 files changed

+686
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 org.junit.jupiter.api.Assertions;
21+
import org.junit.jupiter.api.Test;
22+
23+
/**
24+
* Tests for the {@link SentimentContextGenerator} class.
25+
*/
26+
public class SentimentContextGeneratorTest {
27+
28+
@Test
29+
void testGetContextReturnsInput() {
30+
SentimentContextGenerator cg = new SentimentContextGenerator();
31+
String[] tokens = {"I", "love", "this"};
32+
Assertions.assertArrayEquals(tokens, cg.getContext(tokens));
33+
}
34+
35+
@Test
36+
void testGetContextEmptyArray() {
37+
SentimentContextGenerator cg = new SentimentContextGenerator();
38+
String[] tokens = {};
39+
Assertions.assertArrayEquals(tokens, cg.getContext(tokens));
40+
}
41+
42+
@Test
43+
void testGetContextWithIndexReturnsEmpty() {
44+
SentimentContextGenerator cg = new SentimentContextGenerator();
45+
String[] result = cg.getContext(0, new String[] {"a"}, new String[] {"b"}, null);
46+
Assertions.assertNotNull(result);
47+
Assertions.assertEquals(0, result.length);
48+
}
49+
50+
@Test
51+
void testUpdateAdaptiveDataMismatchThrows() {
52+
SentimentContextGenerator cg = new SentimentContextGenerator();
53+
Assertions.assertThrows(IllegalArgumentException.class,
54+
() -> cg.updateAdaptiveData(new String[] {"a", "b"}, new String[] {"x"}));
55+
}
56+
57+
@Test
58+
void testUpdateAdaptiveDataMatchingLengths() {
59+
SentimentContextGenerator cg = new SentimentContextGenerator();
60+
Assertions.assertDoesNotThrow(
61+
() -> cg.updateAdaptiveData(new String[] {"a"}, new String[] {"x"}));
62+
}
63+
64+
@Test
65+
void testUpdateAdaptiveDataWithNulls() {
66+
SentimentContextGenerator cg = new SentimentContextGenerator();
67+
Assertions.assertDoesNotThrow(() -> cg.updateAdaptiveData(null, null));
68+
}
69+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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 java.io.IOException;
21+
import java.nio.charset.StandardCharsets;
22+
23+
import org.junit.jupiter.api.Assertions;
24+
import org.junit.jupiter.api.BeforeAll;
25+
import org.junit.jupiter.api.Test;
26+
27+
import opennlp.tools.formats.ResourceAsStreamFactory;
28+
import opennlp.tools.util.InputStreamFactory;
29+
import opennlp.tools.util.PlainTextByLineStream;
30+
import opennlp.tools.util.TrainingParameters;
31+
32+
/**
33+
* Tests for the {@link SentimentEvaluator} class.
34+
*/
35+
public class SentimentEvaluatorTest {
36+
37+
private static SentimentModel model;
38+
39+
@BeforeAll
40+
static void trainModel() throws IOException {
41+
InputStreamFactory in = new ResourceAsStreamFactory(
42+
SentimentEvaluatorTest.class, "/opennlp/tools/sentiment/train.txt");
43+
44+
SentimentSampleStream sampleStream = new SentimentSampleStream(
45+
new PlainTextByLineStream(in, StandardCharsets.UTF_8));
46+
47+
model = SentimentME.train("eng", sampleStream,
48+
TrainingParameters.defaultParams(), new SentimentFactory());
49+
}
50+
51+
@Test
52+
void testEvaluateSample() {
53+
SentimentME me = new SentimentME(model);
54+
SentimentEvaluator evaluator = new SentimentEvaluator(me);
55+
56+
SentimentSample sample = new SentimentSample("positive",
57+
new String[] {"I", "love", "this", "great", "product"});
58+
59+
evaluator.evaluateSample(sample);
60+
61+
Assertions.assertNotNull(evaluator.getFMeasure());
62+
}
63+
64+
@Test
65+
void testGetFMeasureBeforeEvaluation() {
66+
SentimentME me = new SentimentME(model);
67+
SentimentEvaluator evaluator = new SentimentEvaluator(me);
68+
69+
Assertions.assertNotNull(evaluator.getFMeasure());
70+
// FMeasure with no data should be -1 or 0
71+
Assertions.assertTrue(evaluator.getFMeasure().getFMeasure() <= 0);
72+
}
73+
74+
@Test
75+
void testEvaluateMultipleSamples() {
76+
SentimentME me = new SentimentME(model);
77+
SentimentEvaluator evaluator = new SentimentEvaluator(me);
78+
79+
evaluator.evaluateSample(new SentimentSample("positive",
80+
new String[] {"wonderful", "amazing", "great"}));
81+
evaluator.evaluateSample(new SentimentSample("negative",
82+
new String[] {"terrible", "horrible", "awful"}));
83+
84+
Assertions.assertNotNull(evaluator.getFMeasure());
85+
}
86+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 java.io.IOException;
21+
22+
import org.junit.jupiter.api.Assertions;
23+
import org.junit.jupiter.api.Test;
24+
25+
import opennlp.tools.ml.model.Event;
26+
import opennlp.tools.util.ObjectStream;
27+
import opennlp.tools.util.ObjectStreamUtils;
28+
29+
/**
30+
* Tests for the {@link SentimentEventStream} class.
31+
*/
32+
public class SentimentEventStreamTest {
33+
34+
@Test
35+
void testEventOutcome() throws IOException {
36+
SentimentSample sample = new SentimentSample("positive",
37+
new String[] {"I", "love", "this"});
38+
39+
ObjectStream<Event> eventStream = new SentimentEventStream(
40+
ObjectStreamUtils.createObjectStream(sample),
41+
new SentimentContextGenerator());
42+
43+
Event event = eventStream.read();
44+
Assertions.assertNotNull(event);
45+
Assertions.assertEquals("positive", event.getOutcome());
46+
Assertions.assertArrayEquals(new String[] {"I", "love", "this"}, event.getContext());
47+
}
48+
49+
@Test
50+
void testMultipleEvents() throws IOException {
51+
SentimentSample pos = new SentimentSample("positive",
52+
new String[] {"great", "product"});
53+
SentimentSample neg = new SentimentSample("negative",
54+
new String[] {"terrible", "service"});
55+
56+
ObjectStream<Event> eventStream = new SentimentEventStream(
57+
ObjectStreamUtils.createObjectStream(pos, neg),
58+
new SentimentContextGenerator());
59+
60+
Event first = eventStream.read();
61+
Assertions.assertEquals("positive", first.getOutcome());
62+
63+
Event second = eventStream.read();
64+
Assertions.assertEquals("negative", second.getOutcome());
65+
66+
Assertions.assertNull(eventStream.read());
67+
}
68+
69+
@Test
70+
void testOneEventPerSample() throws IOException {
71+
SentimentSample sample = new SentimentSample("positive",
72+
new String[] {"a", "b", "c", "d", "e"});
73+
74+
ObjectStream<Event> eventStream = new SentimentEventStream(
75+
ObjectStreamUtils.createObjectStream(sample),
76+
new SentimentContextGenerator());
77+
78+
Assertions.assertNotNull(eventStream.read());
79+
Assertions.assertNull(eventStream.read());
80+
}
81+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 org.junit.jupiter.api.Assertions;
21+
import org.junit.jupiter.api.Test;
22+
23+
import opennlp.tools.tokenize.WhitespaceTokenizer;
24+
25+
/**
26+
* Tests for the {@link SentimentFactory} class.
27+
*/
28+
public class SentimentFactoryTest {
29+
30+
@Test
31+
void testCreateContextGenerator() {
32+
SentimentFactory factory = new SentimentFactory();
33+
SentimentContextGenerator cg = factory.createContextGenerator();
34+
Assertions.assertNotNull(cg);
35+
}
36+
37+
@Test
38+
void testGetTokenizerDefaultsToWhitespace() {
39+
SentimentFactory factory = new SentimentFactory();
40+
Assertions.assertSame(WhitespaceTokenizer.INSTANCE, factory.getTokenizer());
41+
}
42+
43+
@Test
44+
void testGetTokenizerIsCached() {
45+
SentimentFactory factory = new SentimentFactory();
46+
Assertions.assertSame(factory.getTokenizer(), factory.getTokenizer());
47+
}
48+
49+
@Test
50+
void testValidateArtifactMapDoesNotThrow() {
51+
SentimentFactory factory = new SentimentFactory();
52+
Assertions.assertDoesNotThrow(factory::validateArtifactMap);
53+
}
54+
}

0 commit comments

Comments
 (0)