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 2020/01/25 13:36:59 UTC
[opennlp] branch master updated: OPENNLP-1269 -- add simpler
alternative to NGramStringModel (#358)
This is an automated email from the ASF dual-hosted git repository.
jzemerick pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/opennlp.git
The following commit(s) were added to refs/heads/master by this push:
new 219c64b OPENNLP-1269 -- add simpler alternative to NGramStringModel (#358)
219c64b is described below
commit 219c64b33b88873b5afadd3e31b7599e0ecfb589
Author: Tim Allison <ta...@gmail.com>
AuthorDate: Sat Jan 25 08:36:51 2020 -0500
OPENNLP-1269 -- add simpler alternative to NGramStringModel (#358)
---
.../DefaultLanguageDetectorContextGenerator.java | 11 +-
.../java/opennlp/tools/ngram/NGramCharModel.java | 219 +++++++++++++++++++++
.../opennlp/tools/ngram/NGramCharModelTest.java | 106 ++++++++++
3 files changed, 329 insertions(+), 7 deletions(-)
diff --git a/opennlp-tools/src/main/java/opennlp/tools/langdetect/DefaultLanguageDetectorContextGenerator.java b/opennlp-tools/src/main/java/opennlp/tools/langdetect/DefaultLanguageDetectorContextGenerator.java
index 41f9490..8d25201 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/langdetect/DefaultLanguageDetectorContextGenerator.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/langdetect/DefaultLanguageDetectorContextGenerator.java
@@ -20,8 +20,7 @@ package opennlp.tools.langdetect;
import java.util.ArrayList;
import java.util.Collection;
-import opennlp.tools.ngram.NGramModel;
-import opennlp.tools.util.StringList;
+import opennlp.tools.ngram.NGramCharModel;
import opennlp.tools.util.normalizer.AggregateCharSequenceNormalizer;
import opennlp.tools.util.normalizer.CharSequenceNormalizer;
@@ -58,13 +57,11 @@ public class DefaultLanguageDetectorContextGenerator implements LanguageDetector
public String[] getContext(CharSequence document) {
Collection<String> context = new ArrayList<>();
- NGramModel model = new NGramModel();
+ NGramCharModel model = new NGramCharModel();
model.add(normalizer.normalize(document), minLength, maxLength);
- for (StringList tokenList : model) {
- if (tokenList.size() > 0) {
- context.add(tokenList.getToken(0));
- }
+ for (String token : model) {
+ context.add(token);
}
return context.toArray(new String[context.size()]);
}
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ngram/NGramCharModel.java b/opennlp-tools/src/main/java/opennlp/tools/ngram/NGramCharModel.java
new file mode 100644
index 0000000..494b900
--- /dev/null
+++ b/opennlp-tools/src/main/java/opennlp/tools/ngram/NGramCharModel.java
@@ -0,0 +1,219 @@
+/*
+ * 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.tools.ngram;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.NoSuchElementException;
+
+import opennlp.tools.util.StringUtil;
+
+/**
+ * The {@link NGramCharModel} can be used to create character ngrams.
+ *
+ * @see {@link NGramModel}
+ */
+public class NGramCharModel implements Iterable<String> {
+
+ protected static final String COUNT = "count";
+
+ private Map<String, Integer> mNGrams = new HashMap<>();
+
+ /**
+ * Initializes an empty instance.
+ */
+ public NGramCharModel() {
+ }
+
+ /**
+ * Retrieves the count of the given ngram.
+ *
+ * @param ngram an ngram
+ * @return count of the ngram or 0 if it is not contained
+ *
+ */
+ public int getCount(String ngram) {
+
+ Integer count = mNGrams.get(ngram);
+
+ if (count == null) {
+ return 0;
+ }
+
+ return count;
+ }
+
+ /**
+ * Sets the count of an existing ngram.
+ *
+ * @param ngram
+ * @param count
+ */
+ public void setCount(String ngram, int count) {
+
+ Integer oldCount = mNGrams.put(ngram, count);
+
+ if (oldCount == null) {
+ mNGrams.remove(ngram);
+ throw new NoSuchElementException();
+ }
+ }
+
+ /**
+ * Adds one NGram, if it already exists the count increase by one.
+ *
+ * @param ngram
+ */
+ public void add(String ngram) {
+ if (contains(ngram)) {
+ setCount(ngram, getCount(ngram) + 1);
+ } else {
+ mNGrams.put(ngram, 1);
+ }
+ }
+
+ /**
+ * Adds CharSequence that will be ngrammed into chars.
+ *
+ * @param chars
+ * @param minLength
+ * @param maxLength
+ */
+ public void add(CharSequence chars, int minLength, int maxLength) {
+
+ for (int lengthIndex = minLength; lengthIndex < maxLength + 1; lengthIndex++) {
+ for (int textIndex = 0;
+ textIndex + lengthIndex - 1 < chars.length(); textIndex++) {
+
+ String gram = StringUtil.toLowerCase(
+ chars.subSequence(textIndex, textIndex + lengthIndex));
+
+ add(gram);
+ }
+ }
+ }
+
+ /**
+ * Removes the specified tokens form the NGram model, they are just dropped.
+ *
+ * @param ngram
+ */
+ public void remove(String ngram) {
+ mNGrams.remove(ngram);
+ }
+
+ /**
+ * Checks fit he given tokens are contained by the current instance.
+ *
+ * @param ngram
+ *
+ * @return true if the ngram is contained
+ */
+ public boolean contains(String ngram) {
+ return mNGrams.containsKey(ngram);
+ }
+
+ /**
+ * Retrieves the number of {@link String} entries in the current instance.
+ *
+ * @return number of different grams
+ */
+ public int size() {
+ return mNGrams.size();
+ }
+
+ /**
+ * Retrieves an {@link Iterator} over all {@link String} entries.
+ *
+ * @return iterator over all grams
+ */
+ @Override
+ public Iterator<String> iterator() {
+ return mNGrams.keySet().iterator();
+ }
+
+ /**
+ * Retrieves the total count of all Ngrams.
+ *
+ * @return total count of all ngrams
+ */
+ public int numberOfGrams() {
+ int counter = 0;
+
+ for (String ngram : this) {
+ counter += getCount(ngram);
+ }
+
+ return counter;
+ }
+
+ /**
+ * Deletes all ngram which do appear less than the cutoffUnder value
+ * and more often than the cutoffOver value.
+ *
+ * @param cutoffUnder
+ * @param cutoffOver
+ */
+ public void cutoff(int cutoffUnder, int cutoffOver) {
+
+ if (cutoffUnder > 0 || cutoffOver < Integer.MAX_VALUE) {
+
+ for (Iterator<String> it = iterator(); it.hasNext(); ) {
+
+ String ngram = it.next();
+
+ int count = getCount(ngram);
+
+ if (count < cutoffUnder ||
+ count > cutoffOver) {
+ it.remove();
+ }
+ }
+ }
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ boolean result;
+
+ if (obj == this) {
+ result = true;
+ }
+ else if (obj instanceof NGramCharModel) {
+ NGramCharModel model = (NGramCharModel) obj;
+
+ result = mNGrams.equals(model.mNGrams);
+ }
+ else {
+ result = false;
+ }
+
+ return result;
+ }
+
+ @Override
+ public String toString() {
+ return "Size: " + size();
+ }
+
+ @Override
+ public int hashCode() {
+ return mNGrams.hashCode();
+ }
+}
diff --git a/opennlp-tools/src/test/java/opennlp/tools/ngram/NGramCharModelTest.java b/opennlp-tools/src/test/java/opennlp/tools/ngram/NGramCharModelTest.java
new file mode 100644
index 0000000..2bf8053
--- /dev/null
+++ b/opennlp-tools/src/test/java/opennlp/tools/ngram/NGramCharModelTest.java
@@ -0,0 +1,106 @@
+/*
+ * 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.tools.ngram;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Tests for {@link NGramCharModel}
+ */
+public class NGramCharModelTest {
+
+ @Test
+ public void testZeroGetCount() throws Exception {
+ NGramCharModel ngramModel = new NGramCharModel();
+ int count = ngramModel.getCount("");
+ Assert.assertEquals(0, count);
+ Assert.assertEquals(0, ngramModel.size());
+ }
+
+ @Test
+ public void testZeroGetCount2() throws Exception {
+ NGramCharModel ngramModel = new NGramCharModel();
+ ngramModel.add("the");
+ int count = ngramModel.getCount("fox");
+ Assert.assertEquals(0, count);
+ Assert.assertEquals(1, ngramModel.size());
+ }
+
+ @Test
+ public void testAdd() throws Exception {
+ NGramCharModel ngramModel = new NGramCharModel();
+ ngramModel.add("fox");
+ int count = ngramModel.getCount("the");
+ Assert.assertEquals(0, count);
+ Assert.assertEquals(1, ngramModel.size());
+ }
+
+ @Test
+ public void testAdd1() throws Exception {
+ NGramCharModel ngramModel = new NGramCharModel();
+ ngramModel.add("the");
+ int count = ngramModel.getCount("the");
+ Assert.assertEquals(1, count);
+ Assert.assertEquals(1, ngramModel.size());
+ }
+
+ @Test
+ public void testAdd2() throws Exception {
+ NGramCharModel ngramModel = new NGramCharModel();
+ ngramModel.add("the", 1, 3);
+ int count = ngramModel.getCount("th");
+ Assert.assertEquals(1, count);
+ Assert.assertEquals(6, ngramModel.size());
+ }
+
+ @Test
+ public void testRemove() throws Exception {
+ NGramCharModel ngramModel = new NGramCharModel();
+ String ngram = "the";
+ ngramModel.add(ngram);
+ ngramModel.remove(ngram);
+ Assert.assertEquals(0, ngramModel.size());
+ }
+
+ @Test
+ public void testContains() throws Exception {
+ NGramCharModel ngramModel = new NGramCharModel();
+ String token = "the";
+ ngramModel.add(token);
+ Assert.assertFalse(ngramModel.contains("fox"));
+ }
+
+ @Test
+ public void testContains2() throws Exception {
+ NGramCharModel ngramModel = new NGramCharModel();
+ String token = "the";
+ ngramModel.add(token, 1, 3);
+ Assert.assertTrue(ngramModel.contains("the"));
+ }
+
+
+ @Test
+ public void testCutoff1() throws Exception {
+ NGramCharModel ngramModel = new NGramCharModel();
+ String token = "the";
+ ngramModel.add(token, 1, 3);
+ ngramModel.cutoff(2, 4);
+ Assert.assertEquals(0, ngramModel.size());
+ }
+}