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/11 15:17:36 UTC

[opennlp] branch main updated: OPENNLP-1437 : Change removeChar() in NumberUtil.java with String.replaceAll + Junits (#487)

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 2654bffb OPENNLP-1437 : Change removeChar() in NumberUtil.java with String.replaceAll + Junits (#487)
2654bffb is described below

commit 2654bffbacd31afc3862163ffcab00acc0222560
Author: Atita Arora <at...@users.noreply.github.com>
AuthorDate: Wed Jan 11 16:17:28 2023 +0100

    OPENNLP-1437 : Change removeChar() in NumberUtil.java with String.replaceAll + Junits (#487)
---
 .../java/opennlp/uima/normalizer/NumberUtil.java   | 28 +--------
 .../opennlp/uima/normalizer/NumberUtilTest.java    | 68 ++++++++++++++++++++++
 2 files changed, 69 insertions(+), 27 deletions(-)

diff --git a/opennlp-uima/src/main/java/opennlp/uima/normalizer/NumberUtil.java b/opennlp-uima/src/main/java/opennlp/uima/normalizer/NumberUtil.java
index 73fd1575..157d8cb6 100644
--- a/opennlp-uima/src/main/java/opennlp/uima/normalizer/NumberUtil.java
+++ b/opennlp-uima/src/main/java/opennlp/uima/normalizer/NumberUtil.java
@@ -50,28 +50,6 @@ public final class NumberUtil {
     return isLocaleSupported;
   }
 
-  /**
-   * Removes a character from given string {@code s}.
-   *
-   * @param s The string to process.
-   * @param remove The {@link Character} to remove from {@code s}.
-   */
-  private static String removeChar(String s, char remove) {
-
-    StringBuilder result = new StringBuilder();
-
-    int lastPosition = 0;
-    int position;
-    while ((position = s.indexOf(remove, lastPosition)) != -1) {
-      result.append(s.substring(lastPosition, position));
-      lastPosition = position + 1;
-    }
-
-    result.append(s.substring(lastPosition, s.length()));
-
-    return result.toString();
-  }
-
   /**
    * Parses a specified {@link String number} for a certain {@code languageCode}.
    *
@@ -90,12 +68,8 @@ public final class NumberUtil {
     }
 
     Locale locale = new Locale(languageCode);
-
     NumberFormat numberFormat = NumberFormat.getInstance(locale);
-
-    number = number.trim();
-    number = removeChar(number, ' ');
-
+    number = number.replaceAll("\\s", "");
     return numberFormat.parse(number);
   }
 }
diff --git a/opennlp-uima/src/test/java/opennlp/uima/normalizer/NumberUtilTest.java b/opennlp-uima/src/test/java/opennlp/uima/normalizer/NumberUtilTest.java
new file mode 100644
index 00000000..ee9beecc
--- /dev/null
+++ b/opennlp-uima/src/test/java/opennlp/uima/normalizer/NumberUtilTest.java
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package opennlp.uima.normalizer;
+
+import java.text.ParseException;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests for opennlp.uima.normalizer.NumberUtil
+ */
+class NumberUtilTest {
+  String VALID_LANGUAGE_CODE = "en";
+  String INVALID_LANGUAGE_CODE = "INVALID";
+
+  @Test
+  void isLanguageSupported_EN_Pass() {
+    Assertions.assertTrue(NumberUtil.isLanguageSupported(VALID_LANGUAGE_CODE));
+  }
+
+  @Test
+  void isLanguageSupported_INVALID_FAIL() {
+    Assertions.assertFalse(NumberUtil.isLanguageSupported(INVALID_LANGUAGE_CODE));
+  }
+
+
+  @Test
+  void parse_long() throws ParseException {
+    String numberStr = "  1 2 3 4 5 6 7 8 9 1 0      ";
+    Long longValue = 12345678910L;
+    Number result = NumberUtil.parse(numberStr , VALID_LANGUAGE_CODE);
+    Assertions.assertEquals(longValue , result);
+  }
+
+
+  @Test
+  void parse_double() throws ParseException {
+    String numberStr = "     12   3456.78   910      ";
+    Double doubleValue = 123456.78910;
+    Number result = NumberUtil.parse(numberStr , VALID_LANGUAGE_CODE);
+    Assertions.assertEquals(doubleValue , result);
+  }
+
+  @Test
+  void parse_double_with_exception() throws ParseException {
+    String numberStr = "     12   3456.78   910      ";
+    Double doubleValue = 123456.78910;
+    IllegalArgumentException thrown = Assertions.assertThrows(IllegalArgumentException.class , () -> {
+      Number result = NumberUtil.parse(numberStr , INVALID_LANGUAGE_CODE);
+    } , "java.lang.IllegalArgumentException: Language INVALID is not supported!");
+  }
+
+}