This is an automated email from the ASF dual-hosted git repository.

mawiesne 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 6a6f9e6d OPENNLP-1587 Reduce creation of prefix String objects in 
SequenceCodec classes
6a6f9e6d is described below

commit 6a6f9e6d5003919f657320f97d5add74adffdb72
Author: Martin Wiesner <[email protected]>
AuthorDate: Mon Jul 1 14:19:29 2024 +0200

    OPENNLP-1587 Reduce creation of prefix String objects in SequenceCodec 
classes
---
 .../java/opennlp/tools/namefind/BilouCodec.java    | 22 +++++++++-------------
 .../main/java/opennlp/tools/namefind/BioCodec.java | 12 +++++-------
 .../java/opennlp/tools/util/SequenceCodec.java     |  2 ++
 3 files changed, 16 insertions(+), 20 deletions(-)

diff --git a/opennlp-tools/src/main/java/opennlp/tools/namefind/BilouCodec.java 
b/opennlp-tools/src/main/java/opennlp/tools/namefind/BilouCodec.java
index 8b9a20bc..7a37e299 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/namefind/BilouCodec.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/namefind/BilouCodec.java
@@ -90,7 +90,7 @@ public class BilouCodec implements SequenceCodec<String> {
 
       if (name.length() > 1) {
         if (name.getType() == null) {
-          outcomes[name.getStart()] = "default" + "-" + BilouCodec.START;
+          outcomes[name.getStart()] = DEFAULT_PREFIX + BilouCodec.START;
         }
         else {
           outcomes[name.getStart()] = name.getType() + "-" + BilouCodec.START;
@@ -98,7 +98,7 @@ public class BilouCodec implements SequenceCodec<String> {
         // now iterate from begin + 1 till end
         for (int i = name.getStart() + 1; i < name.getEnd() - 1; i++) {
           if (name.getType() == null) {
-            outcomes[i] = "default" + "-" + BilouCodec.CONTINUE;
+            outcomes[i] = DEFAULT_PREFIX + BilouCodec.CONTINUE;
           }
           else {
             outcomes[i] = name.getType() + "-" + BilouCodec.CONTINUE;
@@ -106,7 +106,7 @@ public class BilouCodec implements SequenceCodec<String> {
         }
 
         if (name.getType() == null) {
-          outcomes[name.getEnd() - 1] = "default" + "-" + BilouCodec.LAST;
+          outcomes[name.getEnd() - 1] = DEFAULT_PREFIX + BilouCodec.LAST;
         }
         else {
           outcomes[name.getEnd() - 1] = name.getType() + "-" + BilouCodec.LAST;
@@ -114,7 +114,7 @@ public class BilouCodec implements SequenceCodec<String> {
       }
       else {
         if (name.getType() == null) {
-          outcomes[name.getEnd() - 1] = "default" + "-" + BilouCodec.UNIT;
+          outcomes[name.getEnd() - 1] = DEFAULT_PREFIX + BilouCodec.UNIT;
         }
         else {
           outcomes[name.getEnd() - 1] = name.getType() + "-" + BilouCodec.UNIT;
@@ -152,23 +152,19 @@ public class BilouCodec implements SequenceCodec<String> {
 
     for (String outcome : outcomes) {
       if (outcome.endsWith(BilouCodec.START)) {
-        start.add(outcome.substring(0, outcome.length()
-                - BilouCodec.START.length()));
+        start.add(outcome.substring(0, outcome.length() - 
BilouCodec.START.length()));
       } else if (outcome.endsWith(BilouCodec.CONTINUE)) {
-        cont.add(outcome.substring(0, outcome.length()
-                - BilouCodec.CONTINUE.length()));
+        cont.add(outcome.substring(0, outcome.length() - 
BilouCodec.CONTINUE.length()));
       } else if (outcome.endsWith(BilouCodec.LAST)) {
-        last.add(outcome.substring(0, outcome.length()
-                - BilouCodec.LAST.length()));
+        last.add(outcome.substring(0, outcome.length() - 
BilouCodec.LAST.length()));
       } else if (outcome.endsWith(BilouCodec.UNIT)) {
-        unit.add(outcome.substring(0, outcome.length()
-                - BilouCodec.UNIT.length()));
+        unit.add(outcome.substring(0, outcome.length() - 
BilouCodec.UNIT.length()));
       } else if (!outcome.equals(BilouCodec.OTHER)) {
         return false;
       }
     }
 
-    if (start.size() == 0 && unit.size() == 0) {
+    if (start.isEmpty() && unit.isEmpty()) {
       return false;
     } else {
       // Start, must have matching Last
diff --git a/opennlp-tools/src/main/java/opennlp/tools/namefind/BioCodec.java 
b/opennlp-tools/src/main/java/opennlp/tools/namefind/BioCodec.java
index 622b228a..f7d6c5e8 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/namefind/BioCodec.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/namefind/BioCodec.java
@@ -99,7 +99,7 @@ public class BioCodec implements SequenceCodec<String> {
     Arrays.fill(outcomes, BioCodec.OTHER);
     for (Span name : names) {
       if (name.getType() == null) {
-        outcomes[name.getStart()] = "default" + "-" + BioCodec.START;
+        outcomes[name.getStart()] = DEFAULT_PREFIX + BioCodec.START;
       }
       else {
         outcomes[name.getStart()] = name.getType() + "-" + BioCodec.START;
@@ -107,7 +107,7 @@ public class BioCodec implements SequenceCodec<String> {
       // now iterate from begin + 1 till end
       for (int i = name.getStart() + 1; i < name.getEnd(); i++) {
         if (name.getType() == null) {
-          outcomes[i] = "default" + "-" + BioCodec.CONTINUE;
+          outcomes[i] = DEFAULT_PREFIX + BioCodec.CONTINUE;
         }
         else {
           outcomes[i] = name.getType() + "-" + BioCodec.CONTINUE;
@@ -135,18 +135,16 @@ public class BioCodec implements SequenceCodec<String> {
 
     for (String outcome : outcomes) {
       if (outcome.endsWith(BioCodec.START)) {
-        start.add(outcome.substring(0, outcome.length()
-                - BioCodec.START.length()));
+        start.add(outcome.substring(0, outcome.length() - 
BioCodec.START.length()));
       } else if (outcome.endsWith(BioCodec.CONTINUE)) {
-        cont.add(outcome.substring(0, outcome.length()
-                - BioCodec.CONTINUE.length()));
+        cont.add(outcome.substring(0, outcome.length() - 
BioCodec.CONTINUE.length()));
       } else if (!outcome.equals(BioCodec.OTHER)) {
         // got unexpected outcome
         return false;
       }
     }
 
-    if (start.size() == 0) {
+    if (start.isEmpty()) {
       return false;
     } else {
       for (String contPreffix : cont) {
diff --git a/opennlp-tools/src/main/java/opennlp/tools/util/SequenceCodec.java 
b/opennlp-tools/src/main/java/opennlp/tools/util/SequenceCodec.java
index 7f304f5a..b6d74c36 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/util/SequenceCodec.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/util/SequenceCodec.java
@@ -27,6 +27,8 @@ import java.util.List;
  */
 public interface SequenceCodec<T> {
 
+  String DEFAULT_PREFIX = "default-";
+
   /**
    * Decodes a sequence of {@link T objects} into {@link Span} objects.
    *

Reply via email to