Author: markg
Date: Tue Feb 4 17:33:59 2014
New Revision: 1564395
URL: http://svn.apache.org/r1564395
Log:
OPENNLP-643
Removed old constructors in lieu of Map<String,Pattern[]> constructor and
changed find methods appropriately. Updated unit tests for RegexNameFinder.
Modified:
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/namefind/RegexNameFinder.java
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/namefind/RegexNameFinderFactory.java
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/namefind/RegexNameFinderTest.java
Modified:
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/namefind/RegexNameFinder.java
URL:
http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/namefind/RegexNameFinder.java?rev=1564395&r1=1564394&r2=1564395&view=diff
==============================================================================
---
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/namefind/RegexNameFinder.java
(original)
+++
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/namefind/RegexNameFinder.java
Tue Feb 4 17:33:59 2014
@@ -30,8 +30,6 @@ import opennlp.tools.util.Span;
*/
public final class RegexNameFinder implements TokenNameFinder {
- private Pattern mPatterns[];
- private String sType;
private Map<String, Pattern[]> regexMap;
public RegexNameFinder(Map<String, Pattern[]> regexMap) {
@@ -42,24 +40,6 @@ public final class RegexNameFinder imple
}
- public RegexNameFinder(Pattern patterns[], String type) {
- if (patterns == null || patterns.length == 0) {
- throw new IllegalArgumentException("patterns must not be null or
empty!");
- }
-
- mPatterns = patterns;
- sType = type;
- }
-
- public RegexNameFinder(Pattern patterns[]) {
- if (patterns == null || patterns.length == 0) {
- throw new IllegalArgumentException("patterns must not be null or
empty!");
- }
-
- mPatterns = patterns;
- sType = null;
- }
-
@Override
public Span[] find(String tokens[]) {
Map<Integer, Integer> sentencePosTokenMap = new HashMap<>();
@@ -83,7 +63,7 @@ public final class RegexNameFinder imple
Collection<Span> annotations = new LinkedList<>();
- if (mPatterns == null && regexMap != null) {
+ if (regexMap != null) {
for (Map.Entry<String, Pattern[]> entry : regexMap.entrySet()) {
for (Pattern mPattern : entry.getValue()) {
Matcher matcher = mPattern.matcher(sentenceString);
@@ -101,25 +81,10 @@ public final class RegexNameFinder imple
}
}
}
- } else {
- for (Pattern mPattern : mPatterns) {
- Matcher matcher = mPattern.matcher(sentenceString);
-
- while (matcher.find()) {
- Integer tokenStartIndex =
- sentencePosTokenMap.get(matcher.start());
- Integer tokenEndIndex =
- sentencePosTokenMap.get(matcher.end());
-
- if (tokenStartIndex != null && tokenEndIndex != null) {
- Span annotation = new Span(tokenStartIndex, tokenEndIndex, sType);
- annotations.add(annotation);
- }
- }
- }
}
+
return annotations.toArray(
new Span[annotations.size()]);
}
@@ -138,7 +103,7 @@ public final class RegexNameFinder imple
private Span[] getAnnotations(String text) {
Collection<Span> annotations = new LinkedList<>();
- if (mPatterns == null && regexMap != null) {
+ if (regexMap != null) {
for (Map.Entry<String, Pattern[]> entry : regexMap.entrySet()) {
for (Pattern mPattern : entry.getValue()) {
Matcher matcher = mPattern.matcher(text);
@@ -152,20 +117,7 @@ public final class RegexNameFinder imple
}
}
}
- } else {
- for (Pattern mPattern : mPatterns) {
- Matcher matcher = mPattern.matcher(text);
-
- while (matcher.find()) {
- Integer tokenStartIndex = matcher.start();
- Integer tokenEndIndex = matcher.end();
- Span annotation = new Span(tokenStartIndex, tokenEndIndex, sType);
- annotations.add(annotation);
-
- }
- }
}
-
return annotations.toArray(
new Span[annotations.size()]);
}
@@ -175,19 +127,5 @@ public final class RegexNameFinder imple
// nothing to clear
}
- public Pattern[] getmPatterns() {
- return mPatterns;
- }
-
- public void setmPatterns(Pattern[] mPatterns) {
- this.mPatterns = mPatterns;
- }
-
- public String getsType() {
- return sType;
- }
-
- public void setsType(String sType) {
- this.sType = sType;
- }
+
}
Modified:
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/namefind/RegexNameFinderFactory.java
URL:
http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/namefind/RegexNameFinderFactory.java?rev=1564395&r1=1564394&r2=1564395&view=diff
==============================================================================
---
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/namefind/RegexNameFinderFactory.java
(original)
+++
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/namefind/RegexNameFinderFactory.java
Tue Feb 4 17:33:59 2014
@@ -22,8 +22,8 @@ import opennlp.tools.util.Span;
/**
*
- * Returns RegexNameFinders based on multiple methods: 1. A selection of
- * defaults 2. A configuration and a selection of defaults
+ * Returns a RegexNameFinder based on A selection of
+ * defaults or a configuration and a selection of defaults
*/
public class RegexNameFinderFactory {
@@ -50,7 +50,7 @@ public class RegexNameFinderFactory {
}
/**
- * Allows for use of selecte
+ * Reterned a RegexNamefinder that will utilize specified default regexes.
*
* @param defaults the OpenNLP default regexes
* @return
Modified:
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/namefind/RegexNameFinderTest.java
URL:
http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/namefind/RegexNameFinderTest.java?rev=1564395&r1=1564394&r2=1564395&view=diff
==============================================================================
---
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/namefind/RegexNameFinderTest.java
(original)
+++
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/namefind/RegexNameFinderTest.java
Tue Feb 4 17:33:59 2014
@@ -14,15 +14,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
-
package opennlp.tools.namefind;
+import java.util.HashMap;
+import java.util.Map;
import static org.junit.Assert.assertTrue;
import java.util.regex.Pattern;
import opennlp.tools.util.Span;
+import org.junit.Before;
import org.junit.Test;
@@ -33,12 +34,19 @@ public class RegexNameFinderTest {
@Test
public void testFindSingleTokenPattern() {
- Pattern testPattern = Pattern.compile("test");
+ Pattern testPattern = Pattern.compile("test");
String sentence[] = new String[]{"a", "test", "b", "c"};
+
+ Pattern[] patterns = new Pattern[]{testPattern};
+ Map<String, Pattern[]> regexMap = new HashMap<>();
+ String type = "testtype";
+
+ regexMap.put(type, patterns);
+
RegexNameFinder finder =
- new RegexNameFinder(new Pattern[]{testPattern});
+ new RegexNameFinder(regexMap);
Span[] result = finder.find(sentence);
@@ -54,8 +62,14 @@ public class RegexNameFinderTest {
String sentence[] = new String[]{"a", "80", "year", "b", "c"};
+ Pattern[] patterns = new Pattern[]{testPattern};
+ Map<String, Pattern[]> regexMap = new HashMap<>();
+ String type = "match";
+
+ regexMap.put(type, patterns);
+
RegexNameFinder finder =
- new RegexNameFinder(new Pattern[]{testPattern}, "match");
+ new RegexNameFinder(regexMap);
Span[] result = finder.find(sentence);
@@ -71,9 +85,14 @@ public class RegexNameFinderTest {
Pattern testPattern = Pattern.compile("[0-8] year"); // does match "0
year"
String sentence[] = new String[]{"a", "80", "year", "c"};
+Pattern[] patterns = new Pattern[]{testPattern};
+ Map<String, Pattern[]> regexMap = new HashMap<>();
+ String type = "testtype";
+
+ regexMap.put(type, patterns);
RegexNameFinder finder =
- new RegexNameFinder(new Pattern[]{testPattern});
+ new RegexNameFinder(regexMap);
Span[] result = finder.find(sentence);