You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opennlp.apache.org by jz...@apache.org on 2023/01/03 14:49:34 UTC
[opennlp] branch main updated: OPENNLP-1340 - Get the probability for a sequence without copying the probabilities (#481)
This is an automated email from the ASF dual-hosted git repository.
jzemerick pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opennlp.git
The following commit(s) were added to refs/heads/main by this push:
new 5e3d16c8 OPENNLP-1340 - Get the probability for a sequence without copying the probabilities (#481)
5e3d16c8 is described below
commit 5e3d16c8bae7515ea5897ae6e461f00e42b924e2
Author: Richard Zowalla <13...@users.noreply.github.com>
AuthorDate: Tue Jan 3 15:49:29 2023 +0100
OPENNLP-1340 - Get the probability for a sequence without copying the probabilities (#481)
---
.../src/main/java/opennlp/tools/util/Sequence.java | 27 ++++++++++-
.../test/java/opennlp/tools/util/SequenceTest.java | 53 ++++++++++++++++++++++
2 files changed, 79 insertions(+), 1 deletion(-)
diff --git a/opennlp-tools/src/main/java/opennlp/tools/util/Sequence.java b/opennlp-tools/src/main/java/opennlp/tools/util/Sequence.java
index eefa6807..1f7b271c 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/util/Sequence.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/util/Sequence.java
@@ -122,7 +122,32 @@ public class Sequence implements Comparable<Sequence> {
* @return Retrieves a list of outcomes for this {@link Sequence}.
*/
public List<String> getOutcomes() {
- return outcomes;
+ return List.copyOf(outcomes);
+ }
+
+ /**
+ * @return Retrieves the size of the outcomes for this {@link Sequence}.
+ */
+ public int getSize() {
+ return outcomes.size();
+ }
+
+ /**
+ * @param index must be greater than or equal to zero and must be less than {@link Sequence#getSize()}.
+ * @return the outcome at the specified index.
+ * @throws IndexOutOfBoundsException thrown if the given index is out of range.
+ */
+ public String getOutcome(int index) {
+ return outcomes.get(index);
+ }
+
+ /**
+ * @param index must be greater than or equal to zero and must be less than {@link Sequence#getSize()}.
+ * @return the probability at the specified index.
+ * @throws IndexOutOfBoundsException thrown if the given index is out of range.
+ */
+ public double getProb(int index) {
+ return probs.get(index);
}
/**
diff --git a/opennlp-tools/src/test/java/opennlp/tools/util/SequenceTest.java b/opennlp-tools/src/test/java/opennlp/tools/util/SequenceTest.java
index 669b65da..3c6a0d41 100644
--- a/opennlp-tools/src/test/java/opennlp/tools/util/SequenceTest.java
+++ b/opennlp-tools/src/test/java/opennlp/tools/util/SequenceTest.java
@@ -85,4 +85,57 @@ public class SequenceTest {
sequence.add("test", 0.1d);
sequence.toString();
}
+
+ @Test
+ void testGetAtIndex() {
+ final Sequence sequence = new Sequence();
+ sequence.add("A", 1d);
+ sequence.add("B", 2d);
+ sequence.add("C", 3d);
+
+ Assertions.assertEquals(3, sequence.getSize());
+
+ Assertions.assertEquals("A", sequence.getOutcome(0));
+ Assertions.assertEquals("B", sequence.getOutcome(1));
+ Assertions.assertEquals("C", sequence.getOutcome(2));
+
+ Assertions.assertEquals(1d, sequence.getProb(0));
+ Assertions.assertEquals(2d, sequence.getProb(1));
+ Assertions.assertEquals(3d, sequence.getProb(2));
+ }
+
+ @Test
+ void testGetAtIndexInvalid() {
+ final Sequence sequence = new Sequence();
+ sequence.add("A", 1d);
+
+ Assertions.assertThrows(IndexOutOfBoundsException.class, () -> {
+ sequence.getOutcome(-1);
+ });
+
+ Assertions.assertThrows(IndexOutOfBoundsException.class, () -> {
+ sequence.getOutcome(sequence.getSize() + 1);
+ });
+
+ Assertions.assertThrows(IndexOutOfBoundsException.class, () -> {
+ sequence.getProb(-1);
+ });
+
+ Assertions.assertThrows(IndexOutOfBoundsException.class, () -> {
+ sequence.getProb(sequence.getSize() + 1);
+ });
+ }
+
+ @Test
+ void testListCopy() {
+ final Sequence sequence = new Sequence();
+ sequence.add("A", 1d);
+
+ Assertions.assertEquals(1, sequence.getSize());
+
+ Assertions.assertThrows(UnsupportedOperationException.class, () -> {
+ sequence.getOutcomes().add("This should fail! " +
+ "It should not be possible to modify the internal state");
+ });
+ }
}