You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by jo...@apache.org on 2011/09/22 14:49:53 UTC
svn commit: r1174099 [6/8] - in
/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker:
./ action/ condition/ constraint/ engine/ parser/ resource/ rule/
rule/quantifier/ type/ verbalize/ visitor/
Modified: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/parser/TextMarkerParser.tokens
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/parser/TextMarkerParser.tokens?rev=1174099&r1=1174098&r2=1174099&view=diff
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/parser/TextMarkerParser.tokens (original)
+++ uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/parser/TextMarkerParser.tokens Thu Sep 22 12:49:48 2011
@@ -14,11 +14,11 @@ LOGN=99
NOT=64
Identifier=122
ACTION=92
-CLEAR=74
NOTEQUAL=147
-ENDSWITH=63
-DOUBLELIST=95
+CLEAR=74
VBAR=139
+DOUBLELIST=95
+ENDSWITH=63
RPAREN=124
CREATE=32
GREATER=142
@@ -31,8 +31,8 @@ TYPELIST=97
LESS=141
REGEXP=26
GET=69
-UNMARK=55
PARTOF=23
+UNMARK=55
LAST=19
COMMENT=152
REMOVEDUPLICATE=67
@@ -85,8 +85,8 @@ BlockString=82
IntString=85
HexDigit=106
COLOR=36
-POSITION=25
LPAREN=123
+POSITION=25
IF=29
AT=130
LogLevel=77
@@ -95,9 +95,9 @@ THEN=75
FILL=34
COMMA=133
IS=61
-GETLIST=70
-REPLACE=45
AMPER=140
+REPLACE=45
+GETLIST=70
EQUAL=146
GATHER=33
INLIST=17
@@ -107,44 +107,44 @@ GETFEATURE=54
DOT=131
ListIdentifier=6
PARTOFNEQ=24
-ADD=65
BOOLEANLIST=93
+ADD=65
MARKTABLE=43
HexLiteral=108
XOR=103
-MARK=39
PERCENT=144
-PackageString=79
+MARK=39
PARSE=31
+PackageString=79
OldColor=78
-MERGE=68
MARKLAST=44
+MERGE=68
CONTEXTCOUNT=13
BEFORE=59
EXEC=51
-AFTER=60
MINUS=136
+AFTER=60
DecimalLiteral=109
-TRUE=104
SEMI=134
+TRUE=104
FEATURE=30
SymbolString=90
-StringString=87
-StringLiteral=116
COLON=132
+StringLiteral=116
+StringString=87
SCORE=27
QUESTION=145
UnicodeEscape=118
-STARTSWITH=62
RCURLY=128
+STARTSWITH=62
ASSIGN=52
TOTALCOUNT=15
DECLARE=9
-ISINTAG=18
DocComment=4
+ISINTAG=18
MOFN=20
-SETFEATURE=53
OctalEscape=119
+SETFEATURE=53
DoubleString=86
CIRCUMFLEX=129
ALT_NOTEQUAL=148
Modified: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/resource/AnonymousWordList.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/resource/AnonymousWordList.java?rev=1174099&r1=1174098&r2=1174099&view=diff
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/resource/AnonymousWordList.java (original)
+++ uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/resource/AnonymousWordList.java Thu Sep 22 12:49:48 2011
@@ -69,13 +69,11 @@ public class AnonymousWordList implement
public List<String> contains(String string, boolean ignoreCase, int ignoreLength, boolean edit,
double distance, String ignoreToken) {
- // TODO Auto-generated method stub
return null;
}
public List<String> containsFragment(String string, boolean ignoreCase, int ignoreLength,
boolean edit, double distance, String ignoreToken) {
- // TODO Auto-generated method stub
return null;
}
Modified: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/resource/MultiTreeWordList.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/resource/MultiTreeWordList.java?rev=1174099&r1=1174098&r2=1174099&view=diff
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/resource/MultiTreeWordList.java (original)
+++ uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/resource/MultiTreeWordList.java Thu Sep 22 12:49:48 2011
@@ -976,7 +976,6 @@ public class MultiTreeWordList implement
// double distance, int index, boolean ignoreCase, String ignoreToken, boolean fragment,
// EditDistanceCostMap edm) {
//
- // // TODO: fragment implementieren, falls es gebraucht wird.
//
// EditDistanceResultMap resultMap = new EditDistanceResultMap();
//
Modified: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/AbstractRuleElement.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/AbstractRuleElement.java?rev=1174099&r1=1174098&r2=1174099&view=diff
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/AbstractRuleElement.java (original)
+++ uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/AbstractRuleElement.java Thu Sep 22 12:49:48 2011
@@ -15,45 +15,137 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
-*/
+ */
package org.apache.uima.textmarker.rule;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.uima.textmarker.TextMarkerBlock;
import org.apache.uima.textmarker.TextMarkerElement;
import org.apache.uima.textmarker.TextMarkerStream;
+import org.apache.uima.textmarker.action.AbstractTextMarkerAction;
+import org.apache.uima.textmarker.condition.AbstractTextMarkerCondition;
+import org.apache.uima.textmarker.rule.quantifier.NormalQuantifier;
import org.apache.uima.textmarker.rule.quantifier.RuleElementQuantifier;
-import org.apache.uima.textmarker.type.TextMarkerBasic;
import org.apache.uima.textmarker.visitor.InferenceCrowd;
-
public abstract class AbstractRuleElement extends TextMarkerElement implements RuleElement {
protected RuleElementQuantifier quantifier;
- public AbstractRuleElement(RuleElementQuantifier quantifier) {
+ protected List<AbstractTextMarkerCondition> conditions;
+
+ protected List<AbstractTextMarkerAction> actions;
+
+ private RuleElementContainer container;
+
+ protected TextMarkerBlock parent;
+
+ public AbstractRuleElement(RuleElementQuantifier quantifier,
+ List<AbstractTextMarkerCondition> conditions, List<AbstractTextMarkerAction> actions,
+ RuleElementContainer container, TextMarkerBlock parent) {
super();
this.quantifier = quantifier;
+ this.conditions = conditions;
+ this.actions = actions;
+ this.container = container;
+ this.parent = parent;
+ if (this.conditions == null) {
+ this.conditions = new ArrayList<AbstractTextMarkerCondition>();
+ }
+ if (this.actions == null) {
+ this.actions = new ArrayList<AbstractTextMarkerAction>();
+ }
+ if (this.quantifier == null) {
+ this.quantifier = new NormalQuantifier();
+ }
}
@SuppressWarnings("unchecked")
protected final InferenceCrowd emptyCrowd = new InferenceCrowd(Collections.EMPTY_LIST);
+ protected void doneMatching(RuleMatch ruleMatch, RuleApply ruleApply, TextMarkerStream stream,
+ InferenceCrowd crowd) {
+ if (!ruleMatch.isApplied()) {
+ ruleApply.add(ruleMatch);
+ if (ruleMatch.matchedCompletely()) {
+ ruleMatch.getRule().getRoot().applyRuleElements(ruleMatch, stream, crowd);
+ }
+ ruleMatch.setApplied(true);
+ }
+ }
+
+ public void apply(RuleMatch ruleMatch, TextMarkerStream stream, InferenceCrowd crowd) {
+ for (AbstractTextMarkerAction action : actions) {
+ crowd.beginVisit(action, null);
+ action.execute(ruleMatch, this, stream, crowd);
+ crowd.endVisit(action, null);
+ }
+ }
+
+ protected List<RuleElementMatch> getMatch(RuleMatch ruleMatch,
+ ComposedRuleElementMatch containerMatch) {
+ List<RuleElementMatch> matchInfo;
+ if (containerMatch != null) {
+ matchInfo = containerMatch.getInnerMatches().get(this);
+ } else {
+ matchInfo = ruleMatch.getMatchInfo(this).get(0);
+ }
+ return matchInfo;
+ }
+
public List<RuleElementMatch> evaluateMatches(List<RuleElementMatch> matches,
TextMarkerBlock parent) {
return quantifier.evaluateMatches(matches, parent, emptyCrowd);
}
- public boolean continueMatch(int index, List<RuleElement> elements, TextMarkerBasic next,
- RuleElementMatch match, List<RuleElementMatch> matches, TextMarkerStream stream) {
- return quantifier.continueMatch(index, elements, next, match, matches, stream, emptyCrowd);
+ public List<Integer> getSelfIndexList() {
+ List<Integer> result = new ArrayList<Integer>(1);
+ if (getContainer() == null) {
+ return null;
+ }
+ int indexOf = getContainer().getRuleElements().indexOf(this);
+ result.add(indexOf + 1);
+ return result;
}
public RuleElementQuantifier getQuantifier() {
return quantifier;
}
+ public TextMarkerBlock getParent() {
+ return parent;
+ }
+
+ public List<AbstractTextMarkerCondition> getConditions() {
+ return conditions;
+ }
+
+ public void setConditions(List<AbstractTextMarkerCondition> conditions) {
+ this.conditions = conditions;
+ }
+
+ public List<AbstractTextMarkerAction> getActions() {
+ return actions;
+ }
+
+ public void setActions(List<AbstractTextMarkerAction> actions) {
+ this.actions = actions;
+ }
+
+ public void setQuantifier(RuleElementQuantifier quantifier) {
+ this.quantifier = quantifier;
+ }
+
+ public TextMarkerRule getRule() {
+ return container.getRule();
+ }
+
+ public RuleElementContainer getContainer() {
+ return container;
+ }
+
}
Added: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/AnnotationComparator.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/AnnotationComparator.java?rev=1174099&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/AnnotationComparator.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/AnnotationComparator.java Thu Sep 22 12:49:48 2011
@@ -0,0 +1,43 @@
+/*
+ * 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 org.apache.uima.textmarker.rule;
+
+import java.util.Comparator;
+
+import org.apache.uima.cas.text.AnnotationFS;
+
+public class AnnotationComparator implements Comparator<AnnotationFS> {
+
+ @Override
+ public int compare(AnnotationFS o1, AnnotationFS o2) {
+ if (o1.getBegin() < o2.getBegin()) {
+ return -1;
+ } else if (o1.getBegin() > o2.getBegin()) {
+ return 1;
+ } else if (o1.getEnd() > o2.getEnd()) {
+ return 1;
+ } else if (o1.getEnd() < o2.getEnd()) {
+ return -1;
+ } else {
+ return o1.getType().getName().compareTo(o2.getType().getName());
+ }
+
+ }
+}
Propchange: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/AnnotationComparator.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/ComposedRuleElement.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/ComposedRuleElement.java?rev=1174099&r1=1174098&r2=1174099&view=diff
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/ComposedRuleElement.java (original)
+++ uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/ComposedRuleElement.java Thu Sep 22 12:49:48 2011
@@ -15,73 +15,226 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
-*/
+ */
package org.apache.uima.textmarker.rule;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.List;
-import org.apache.uima.cas.FSIterator;
import org.apache.uima.cas.text.AnnotationFS;
import org.apache.uima.textmarker.TextMarkerBlock;
import org.apache.uima.textmarker.TextMarkerStream;
+import org.apache.uima.textmarker.action.AbstractTextMarkerAction;
+import org.apache.uima.textmarker.condition.AbstractTextMarkerCondition;
import org.apache.uima.textmarker.rule.quantifier.RuleElementQuantifier;
-import org.apache.uima.textmarker.type.TextMarkerBasic;
import org.apache.uima.textmarker.visitor.InferenceCrowd;
+public class ComposedRuleElement extends AbstractRuleElement implements RuleElementContainer {
-public class ComposedRuleElement extends AbstractRuleElement {
+ protected List<RuleElement> elements;
- private List<RuleElement> elements;
-
- private TextMarkerBlock parent;
+ protected RuleElementContainer caretaker;
public ComposedRuleElement(List<RuleElement> elements, RuleElementQuantifier quantifier,
- TextMarkerBlock parent) {
- super(quantifier);
+ List<AbstractTextMarkerCondition> conditions, List<AbstractTextMarkerAction> actions,
+ RuleElementContainer container, TextMarkerBlock parent) {
+ super(quantifier, conditions, actions, container, parent);
this.elements = elements;
- this.parent = parent;
+ this.caretaker = new RuleElementCaretaker(this);
+ }
+ public void apply(RuleMatch match, TextMarkerStream symbolStream, InferenceCrowd crowd) {
+ applyRuleElements(match, symbolStream, crowd);
+ super.apply(match, symbolStream, crowd);
}
- public void apply(RuleMatch matchInfos, TextMarkerStream symbolStream, InferenceCrowd crowd) {
- for (RuleElement each : elements) {
- each.apply(matchInfos, symbolStream, crowd);
+ @Override
+ public void startMatch(RuleMatch ruleMatch, RuleApply ruleApply,
+ ComposedRuleElementMatch containerMatch, RuleElement entryPoint, TextMarkerStream stream,
+ InferenceCrowd crowd) {
+ RuleElement anchorElement = getAnchoringRuleElement(stream);
+ ComposedRuleElementMatch composedMatch = createComposedMatch(ruleMatch, containerMatch);
+ anchorElement.startMatch(ruleMatch, ruleApply, composedMatch, entryPoint, stream, crowd);
+ }
+
+ protected ComposedRuleElementMatch createComposedMatch(RuleMatch ruleMatch,
+ ComposedRuleElementMatch containerMatch) {
+ ComposedRuleElementMatch composedMatch = new ComposedRuleElementMatch(this, containerMatch);
+ includeMatch(ruleMatch, containerMatch, composedMatch);
+ return composedMatch;
+ }
+
+ @Override
+ public void continueMatch(boolean after, AnnotationFS annotation, RuleMatch ruleMatch,
+ RuleApply ruleApply, ComposedRuleElementMatch containerMatch, RuleElement entryPoint,
+ TextMarkerStream stream, InferenceCrowd crowd) {
+ RuleElement nextElement = getNextElement(after, this);
+ ComposedRuleElementMatch composedMatch = createComposedMatch(ruleMatch, containerMatch);
+ nextElement.continueMatch(after, annotation, ruleMatch, ruleApply, composedMatch, null, stream,
+ crowd);
+
+ }
+
+ public void fallbackContinue(boolean after, boolean failed, AnnotationFS annotation,
+ RuleMatch ruleMatch, RuleApply ruleApply, ComposedRuleElementMatch containerMatch,
+ RuleElement entryPoint, TextMarkerStream stream, InferenceCrowd crowd) {
+ RuleElementContainer container = getContainer();
+ doMatch(containerMatch, stream, crowd);
+ if (this.equals(entryPoint)) {
+ return;
+ }
+ if (container == null) {
+ fallback(after, failed, annotation, ruleMatch, ruleApply, containerMatch, entryPoint, stream,
+ crowd);
+ } else {
+ ComposedRuleElementMatch parentContainerMatch = containerMatch.getContainerMatch();
+ RuleElement nextElement = container.getNextElement(after, this);
+ List<RuleElementMatch> match = getMatch(ruleMatch, parentContainerMatch);
+ List<RuleElementMatch> evaluateMatches = quantifier.evaluateMatches(match, parent, crowd);
+ ruleMatch.setMatched(ruleMatch.matched() && evaluateMatches != null);
+ if (failed) {
+ if (nextElement != null) {
+ AnnotationFS backtrackedAnnotation = getBacktrackedAnnotation(evaluateMatches);
+ if (backtrackedAnnotation != null) {
+ nextElement.continueMatch(after, backtrackedAnnotation, ruleMatch, ruleApply,
+ parentContainerMatch, null, stream, crowd);
+ } else {
+ fallback(after, failed, annotation, ruleMatch, ruleApply, parentContainerMatch,
+ entryPoint, stream, crowd);
+ }
+ } else {
+ fallback(after, failed, annotation, ruleMatch, ruleApply, parentContainerMatch,
+ entryPoint, stream, crowd);
+ }
+ } else {
+ if (quantifier.continueMatch(after, annotation, this, ruleMatch, parentContainerMatch,
+ stream, crowd)) {
+ continueMatch(after, annotation, ruleMatch, ruleApply, parentContainerMatch, null,
+ stream, crowd);
+ } else if (nextElement != null) {
+ nextElement.continueMatch(after, annotation, ruleMatch, ruleApply, parentContainerMatch,
+ null, stream, crowd);
+ } else {
+ fallback(after, failed, annotation, ruleMatch, ruleApply, parentContainerMatch,
+ entryPoint, stream, crowd);
+ }
+ }
}
}
- public List<TextMarkerBasic> getAnchors(TextMarkerStream symbolStream) {
- return elements.get(0).getAnchors(symbolStream);
+ private AnnotationFS getBacktrackedAnnotation(List<RuleElementMatch> evaluateMatches) {
+ if (evaluateMatches == null) {
+ return null;
+ }
+ List<AnnotationFS> textsMatched = evaluateMatches.get(evaluateMatches.size() - 1)
+ .getTextsMatched();
+ AnnotationFS backtrackedAnnotation = textsMatched.get(textsMatched.size() - 1);
+ return backtrackedAnnotation;
+ }
+
+ private void fallback(boolean after, boolean failed, AnnotationFS annotation,
+ RuleMatch ruleMatch, RuleApply ruleApply, ComposedRuleElementMatch containerMatch,
+ RuleElement entryPoint, TextMarkerStream stream, InferenceCrowd crowd) {
+ Object sidestep = null;
+ RuleElementContainer parentContainer = getContainer();
+ if (parentContainer instanceof ComposedRuleElement) {
+ ComposedRuleElement parentElement = (ComposedRuleElement) parentContainer;
+ parentElement.fallbackContinue(after, failed, annotation, ruleMatch, ruleApply,
+ containerMatch, entryPoint, stream, crowd);
+ } else if (sidestep != null) {
+ // TODO call sidestep
+ } else {
+ doneMatching(ruleMatch, ruleApply, stream, crowd);
+ }
}
- public FSIterator<AnnotationFS> getAnchors2(TextMarkerStream symbolStream) {
- return elements.get(0).getAnchors2(symbolStream);
+ private void includeMatch(RuleMatch ruleMatch, ComposedRuleElementMatch containerMatch,
+ ComposedRuleElementMatch composedMatch) {
+ if (containerMatch == null) {
+ ruleMatch.setRootMatch(composedMatch);
+ } else {
+ containerMatch.addInnerMatch(this, composedMatch);
+ }
}
- public TextMarkerMatcher getMatcher() {
- return elements.get(0).getMatcher();
+ private void doMatch(ComposedRuleElementMatch match, TextMarkerStream stream, InferenceCrowd crowd) {
+ List<AnnotationFS> textsMatched = match.getTextsMatched();
+ if (textsMatched == null || textsMatched.isEmpty()) {
+ return;
+ }
+ int begin = textsMatched.get(0).getBegin();
+ int end = textsMatched.get(textsMatched.size() - 1).getEnd();
+ AnnotationFS annotation = stream.getCas().createAnnotation(stream.getCas().getAnnotationType(),
+ begin, end);
+
+ List<EvaluatedCondition> evaluatedConditions = new ArrayList<EvaluatedCondition>(
+ conditions.size());
+ for (AbstractTextMarkerCondition condition : conditions) {
+ crowd.beginVisit(condition, null);
+ EvaluatedCondition eval = condition.eval(annotation, this, stream, crowd);
+ crowd.endVisit(condition, null);
+ evaluatedConditions.add(eval);
+ }
+ match.setConditionInfo(evaluatedConditions);
+ match.evaluateInnerMatches();
}
- public TextMarkerBlock getParent() {
- return parent;
+ public Collection<AnnotationFS> getAnchors(TextMarkerStream stream) {
+ RuleElement anchorElement = getAnchoringRuleElement(stream);
+ Collection<AnnotationFS> anchors = anchorElement.getAnchors(stream);
+ return anchors;
}
- public RuleElementMatch match(TextMarkerBasic anchor, TextMarkerStream stream,
- InferenceCrowd crowd) {
- RuleElementMatch result = new RuleElementMatch(this);
- TextMarkerBasic current = anchor;
- List<RuleElementMatch> innerMatches = new ArrayList<RuleElementMatch>();
+ @Override
+ public int estimateAnchors(TextMarkerStream stream) {
+ int result = 1;
for (RuleElement each : elements) {
- RuleElementMatch match = each.match(current, stream, crowd);
- current = stream.nextAnnotation(match);
- innerMatches.add(match);
+ result += each.estimateAnchors(stream);
}
- result.setInnerMatches(innerMatches);
- return result;
+ return result / elements.size();
+ }
+
+ @Override
+ public RuleElement getAnchoringRuleElement(TextMarkerStream stream) {
+ return caretaker.getAnchoringRuleElement(stream);
}
- public List<RuleElement> getElements() {
+ public List<RuleElement> getRuleElements() {
return elements;
}
+
+ public void setRuleElements(List<RuleElement> elements) {
+ this.elements = elements;
+ }
+
+ @Override
+ public RuleElement getFirstElement() {
+ return caretaker.getFirstElement();
+ }
+
+ @Override
+ public RuleElement getLastElement() {
+ return caretaker.getLastElement();
+ }
+
+ @Override
+ public void applyRuleElements(RuleMatch ruleMatch, TextMarkerStream stream, InferenceCrowd crowd) {
+ caretaker.applyRuleElements(ruleMatch, stream, crowd);
+ }
+
+ public String toString() {
+ String simpleName = getQuantifier().getClass().getSimpleName();
+ return "(" + (elements == null ? "null" : elements.toString()) + ")"
+ + (simpleName.equals("NormalQuantifier") ? "" : simpleName)
+ + (conditions.isEmpty() ? "" : "(" + conditions.toString() + ")" + "\\n")
+ + (actions.isEmpty() ? "" : "{" + actions.toString() + "}");
+ }
+
+ @Override
+ public RuleElement getNextElement(boolean after, RuleElement ruleElement) {
+ return caretaker.getNextElement(after, ruleElement);
+ }
+
}
Added: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/ComposedRuleElementMatch.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/ComposedRuleElementMatch.java?rev=1174099&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/ComposedRuleElementMatch.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/ComposedRuleElementMatch.java Thu Sep 22 12:49:48 2011
@@ -0,0 +1,186 @@
+/*
+ * 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 org.apache.uima.textmarker.rule;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.TreeSet;
+
+import org.apache.uima.cas.text.AnnotationFS;
+
+public class ComposedRuleElementMatch extends RuleElementMatch {
+
+ private Map<RuleElement, List<RuleElementMatch>> innerMatches;
+
+ public ComposedRuleElementMatch(RuleElement ruleElement, ComposedRuleElementMatch containerMatch) {
+ super(ruleElement, containerMatch);
+ baseConditionMatched = false;
+ ComposedRuleElement cre = (ComposedRuleElement) ruleElement;
+ innerMatches = new TreeMap<RuleElement, List<RuleElementMatch>>(new RuleElementComparator(cre));
+ List<RuleElement> ruleElements = cre.getRuleElements();
+ for (RuleElement eachRuleElement : ruleElements) {
+ innerMatches.put(eachRuleElement, null);
+ }
+ }
+
+ private void setInnerMatches(Map<RuleElement, List<RuleElementMatch>> innerMatches) {
+ this.innerMatches = innerMatches;
+ // textsMatched = new ArrayList<AnnotationFS>();
+ // boolean matched = true;
+ // for (Entry<RuleElement, List<RuleElementMatch>> entry : innerMatches.entrySet()) {
+ // for (RuleElementMatch ruleElementMatch : entry.getValue()) {
+ // List<AnnotationFS> list = ruleElementMatch.getTextsMatched();
+ // textsMatched.addAll(list);
+ // matched &= ruleElementMatch.matched();
+ // }
+ // }
+ // baseConditionMatched = matched;
+ // conditionsMatched = matched;
+ }
+
+ public Map<RuleElement, List<RuleElementMatch>> getInnerMatches() {
+ return innerMatches;
+ }
+
+ public void addInnerMatch(RuleElement ruleElement, RuleElementMatch ruleElementMatch) {
+ List<RuleElementMatch> list = innerMatches.get(ruleElement);
+ if (list == null) {
+ list = new ArrayList<RuleElementMatch>();
+ innerMatches.put(ruleElement, list);
+ }
+ list.add(ruleElementMatch);
+ textsMatched.addAll(ruleElementMatch.getTextsMatched());
+
+ evaluateInnerMatches();
+ }
+
+ public void evaluateInnerMatches() {
+ boolean allDone = true;
+ Set<Entry<RuleElement, List<RuleElementMatch>>> entrySet = innerMatches.entrySet();
+ for (Entry<RuleElement, List<RuleElementMatch>> entry : entrySet) {
+ RuleElement element = entry.getKey();
+ allDone &= (element.getQuantifier().isOptional(element.getParent()) || entry.getValue() != null);
+ }
+ baseConditionMatched = allDone;
+ }
+
+ public ComposedRuleElementMatch copy() {
+ ComposedRuleElementMatch copy = new ComposedRuleElementMatch(ruleElement, containerMatch);
+ copy.setBaseConditionMatched(baseConditionMatched);
+ copy.setConditions(conditions);
+ copy.setConditionsMatched(conditionsMatched);
+ copy.setTextsMatched(textsMatched);
+ Map<RuleElement, List<RuleElementMatch>> newMap = new TreeMap<RuleElement, List<RuleElementMatch>>(
+ new RuleElementComparator((ComposedRuleElement) ruleElement));
+ for (Entry<RuleElement, List<RuleElementMatch>> entry : innerMatches.entrySet()) {
+ List<RuleElementMatch> value = entry.getValue();
+ if (value != null) {
+ List<RuleElementMatch> newValue = new ArrayList<RuleElementMatch>();
+ for (RuleElementMatch each : value) {
+ newValue.add(each.copy());
+ }
+ newMap.put(entry.getKey(), newValue);
+ } else {
+ newMap.put(entry.getKey(), null);
+ }
+ }
+ copy.setInnerMatches(newMap);
+ return copy;
+ }
+
+ public ComposedRuleElementMatch copy(ComposedRuleElementMatch extendedContainerMatch) {
+ ComposedRuleElementMatch copy = new ComposedRuleElementMatch(ruleElement, containerMatch);
+ copy.setBaseConditionMatched(baseConditionMatched);
+ copy.setConditions(conditions);
+ copy.setConditionsMatched(conditionsMatched);
+ copy.setTextsMatched(textsMatched);
+ Map<RuleElement, List<RuleElementMatch>> newMap = new TreeMap<RuleElement, List<RuleElementMatch>>(
+ new RuleElementComparator((ComposedRuleElement) ruleElement));
+ for (Entry<RuleElement, List<RuleElementMatch>> entry : innerMatches.entrySet()) {
+ RuleElement key = entry.getKey();
+ List<RuleElementMatch> value = entry.getValue();
+ if (key.equals(extendedContainerMatch.getRuleElement())) {
+ // if (value.isEmpty()) {
+ extendedContainerMatch.setContainerMatch(copy);
+ if (value != null) {
+ List<RuleElementMatch> newValue = new ArrayList<RuleElementMatch>();
+ newValue.addAll(value);
+ newValue.set(newValue.size() - 1, extendedContainerMatch);
+ newMap.put(extendedContainerMatch.getRuleElement(), newValue);
+ }
+ // } else {
+ // List<RuleElementMatch> newValue = new ArrayList<RuleElementMatch>();
+ // newValue.addAll(value);
+ // newValue.add(extendedContainerMatch);
+ // newMap.put(extendedContainerMatch.getRuleElement(), newValue);
+ // }
+ } else {
+ if (value != null) {
+ List<RuleElementMatch> newValue = new ArrayList<RuleElementMatch>();
+ for (RuleElementMatch each : value) {
+ if (each instanceof ComposedRuleElementMatch) {
+ newValue.add(((ComposedRuleElementMatch) each).copy(extendedContainerMatch));
+ } else {
+ newValue.add(each.copy());
+ }
+ }
+ newMap.put(entry.getKey(), newValue);
+ } else {
+ newMap.put(entry.getKey(), null);
+ }
+ }
+ }
+ copy.setInnerMatches(newMap);
+ return copy;
+ }
+
+ public String toString() {
+ return "CREM" + innerMatches.toString();
+ }
+
+ public List<AnnotationFS> getTextsMatched() {
+ // TODO find a better solution for this
+ Collection<AnnotationFS> set = new TreeSet<AnnotationFS>(new AnnotationComparator());
+ Collection<List<RuleElementMatch>> values = innerMatches.values();
+ for (List<RuleElementMatch> list : values) {
+ if (list != null) {
+ for (RuleElementMatch ruleElementMatch : list) {
+ set.addAll(ruleElementMatch.getTextsMatched());
+ }
+ }
+ }
+ return Arrays.asList(set.toArray(new AnnotationFS[0]));
+ }
+
+ public void setConditionInfo(List<EvaluatedCondition> evaluatedConditions) {
+ conditions = evaluatedConditions;
+ for (EvaluatedCondition each : conditions) {
+ conditionsMatched &= each.isValue();
+ }
+
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/ComposedRuleElementMatch.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/EvaluatedCondition.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/EvaluatedCondition.java?rev=1174099&r1=1174098&r2=1174099&view=diff
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/EvaluatedCondition.java (original)
+++ uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/EvaluatedCondition.java Thu Sep 22 12:49:48 2011
@@ -15,7 +15,7 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
-*/
+ */
package org.apache.uima.textmarker.rule;
@@ -25,7 +25,6 @@ import java.util.List;
import org.apache.uima.textmarker.condition.AbstractTextMarkerCondition;
import org.apache.uima.textmarker.condition.NotCondition;
-
public class EvaluatedCondition {
private final AbstractTextMarkerCondition condition;
Modified: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleApply.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleApply.java?rev=1174099&r1=1174098&r2=1174099&view=diff
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleApply.java (original)
+++ uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleApply.java Thu Sep 22 12:49:48 2011
@@ -15,7 +15,7 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
-*/
+ */
package org.apache.uima.textmarker.rule;
@@ -24,7 +24,6 @@ import java.util.List;
import org.apache.uima.textmarker.ScriptApply;
-
public class RuleApply extends ScriptApply {
private int tried = 0;
@@ -46,7 +45,7 @@ public class RuleApply extends ScriptApp
}
public void add(RuleMatch match) {
- if (match.matched()) {
+ if (match.matchedCompletely()) {
applied++;
}
tried++;
@@ -55,6 +54,12 @@ public class RuleApply extends ScriptApp
}
}
+ public void addAll(List<RuleMatch> matches) {
+ for (RuleMatch ruleMatch : matches) {
+ add(ruleMatch);
+ }
+ }
+
public List<RuleMatch> getList() {
return list;
}
Modified: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleElement.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleElement.java?rev=1174099&r1=1174098&r2=1174099&view=diff
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleElement.java (original)
+++ uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleElement.java Thu Sep 22 12:49:48 2011
@@ -15,37 +15,44 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
-*/
+ */
package org.apache.uima.textmarker.rule;
+import java.util.Collection;
import java.util.List;
-import org.apache.uima.cas.FSIterator;
import org.apache.uima.cas.text.AnnotationFS;
import org.apache.uima.textmarker.TextMarkerBlock;
import org.apache.uima.textmarker.TextMarkerStream;
-import org.apache.uima.textmarker.type.TextMarkerBasic;
+import org.apache.uima.textmarker.rule.quantifier.RuleElementQuantifier;
import org.apache.uima.textmarker.visitor.InferenceCrowd;
-
public interface RuleElement {
- void apply(RuleMatch matchInfos, TextMarkerStream symbolStream, InferenceCrowd crowd);
+ void apply(RuleMatch match, TextMarkerStream stream, InferenceCrowd crowd);
+
+ void startMatch(RuleMatch ruleMatch, RuleApply ruleApply,
+ ComposedRuleElementMatch containerMatch, RuleElement entryPoint, TextMarkerStream stream, InferenceCrowd crowd);
- RuleElementMatch match(TextMarkerBasic currentBasic, TextMarkerStream stream, InferenceCrowd crowd);
+ void continueMatch(boolean after, AnnotationFS annotation, RuleMatch ruleMatch,
+ RuleApply ruleApply, ComposedRuleElementMatch containerMatch, RuleElement entryPoint,
+ TextMarkerStream stream, InferenceCrowd crowd);
List<RuleElementMatch> evaluateMatches(List<RuleElementMatch> matches, TextMarkerBlock parent);
- List<TextMarkerBasic> getAnchors(TextMarkerStream symbolStream);
+ Collection<AnnotationFS> getAnchors(TextMarkerStream symbolStream);
+
+ TextMarkerBlock getParent();
- FSIterator<AnnotationFS> getAnchors2(TextMarkerStream symbolStream);
+ TextMarkerRule getRule();
- boolean continueMatch(int index, List<RuleElement> elements, TextMarkerBasic next,
- RuleElementMatch match, List<RuleElementMatch> matches, TextMarkerStream stream);
+ RuleElementContainer getContainer();
- TextMarkerBlock getParent();
+ RuleElementQuantifier getQuantifier();
+
+ int estimateAnchors(TextMarkerStream stream);
- TextMarkerMatcher getMatcher();
+ List<Integer> getSelfIndexList();
}
Added: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleElementCaretaker.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleElementCaretaker.java?rev=1174099&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleElementCaretaker.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleElementCaretaker.java Thu Sep 22 12:49:48 2011
@@ -0,0 +1,117 @@
+/*
+ * 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 org.apache.uima.textmarker.rule;
+
+import java.util.List;
+
+import org.apache.uima.textmarker.TextMarkerStream;
+import org.apache.uima.textmarker.visitor.InferenceCrowd;
+
+public class RuleElementCaretaker implements RuleElementContainer {
+
+ private final RuleElementContainer container;
+
+ public RuleElementCaretaker(RuleElementContainer container) {
+ super();
+ this.container = container;
+ }
+
+ public RuleElement getElementAfter(RuleElement element) {
+ int indexOf = container.getRuleElements().indexOf(element);
+ if (indexOf < container.getRuleElements().size() - 1) {
+ RuleElement ruleElement = container.getRuleElements().get(indexOf + 1);
+ return ruleElement;
+ }
+ return null;
+ }
+
+ public RuleElement getElementBefore(RuleElement element) {
+ int indexOf = container.getRuleElements().indexOf(element);
+ if (indexOf > 0) {
+ RuleElement ruleElement = container.getRuleElements().get(indexOf - 1);
+ return ruleElement;
+ }
+ return null;
+ }
+
+ @Override
+ public List<RuleElement> getRuleElements() {
+ return container.getRuleElements();
+ }
+
+ @Override
+ public RuleElement getAnchoringRuleElement(TextMarkerStream stream) {
+ List<RuleElement> ruleElements = container.getRuleElements();
+ if (ruleElements.size() == 1) {
+ return ruleElements.get(0);
+ }
+ return ruleElements.get(0);
+
+ // TODO activate dynamic anchoring
+ // int min = Integer.MAX_VALUE;
+ // RuleElement minElement = null;
+ // int i = 1;
+ // for (RuleElement each : ruleElements) {
+ // int estimate = each.estimateAnchors(stream);
+ // // TODO what about this formula?
+ // double factor = stream.getIndexPenalty();
+ // estimate = (int) (Math.log(estimate) * (i * i * i * factor));
+ // if (estimate < min) {
+ // min = estimate;
+ // minElement = each;
+ // }
+ // i++;
+ // }
+ // return minElement;
+ }
+
+ @Override
+ public RuleElement getFirstElement() {
+ List<RuleElement> ruleElements = container.getRuleElements();
+ return ruleElements.get(0);
+ }
+
+ @Override
+ public RuleElement getLastElement() {
+ List<RuleElement> ruleElements = container.getRuleElements();
+ return ruleElements.get(ruleElements.size() - 1);
+ }
+
+ @Override
+ public void applyRuleElements(RuleMatch ruleMatch, TextMarkerStream stream, InferenceCrowd crowd) {
+ for (RuleElement eachElement : getRuleElements()) {
+ eachElement.apply(ruleMatch, stream, crowd);
+ }
+ }
+
+ @Override
+ public TextMarkerRule getRule() {
+ return container.getRule();
+ }
+
+ @Override
+ public RuleElement getNextElement(boolean after, RuleElement ruleElement) {
+ if (after) {
+ return getElementAfter(ruleElement);
+ } else {
+ return getElementBefore(ruleElement);
+ }
+ }
+}
Propchange: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleElementCaretaker.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleElementComparator.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleElementComparator.java?rev=1174099&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleElementComparator.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleElementComparator.java Thu Sep 22 12:49:48 2011
@@ -0,0 +1,41 @@
+/*
+ * 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 org.apache.uima.textmarker.rule;
+
+import java.util.Comparator;
+import java.util.List;
+
+public class RuleElementComparator implements Comparator<RuleElement> {
+ private final RuleElementContainer container;
+
+ public RuleElementComparator(RuleElementContainer container) {
+ this.container = container;
+ }
+
+ public int compare(RuleElement re1, RuleElement re2) {
+ List<RuleElement> elements = container.getRuleElements();
+ int i1 = elements.indexOf(re1);
+ int i2 = elements.indexOf(re2);
+ if (i1 == i2)
+ return 0;
+ return i1 < i2 ? -1 : 1;
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleElementComparator.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleElementContainer.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleElementContainer.java?rev=1174099&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleElementContainer.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleElementContainer.java Thu Sep 22 12:49:48 2011
@@ -0,0 +1,47 @@
+/*
+ * 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 org.apache.uima.textmarker.rule;
+
+import java.util.List;
+
+import org.apache.uima.textmarker.TextMarkerStream;
+import org.apache.uima.textmarker.visitor.InferenceCrowd;
+
+public interface RuleElementContainer {
+
+ List<RuleElement> getRuleElements();
+
+ // RuleElement getElementAfter(RuleElement element);
+ //
+ // RuleElement getElementBefore(RuleElement element);
+
+ RuleElement getFirstElement();
+
+ RuleElement getLastElement();
+
+ RuleElement getAnchoringRuleElement(TextMarkerStream stream);
+
+ void applyRuleElements(RuleMatch ruleMatch, TextMarkerStream stream, InferenceCrowd crowd);
+
+ TextMarkerRule getRule();
+
+ RuleElement getNextElement(boolean after, RuleElement ruleElement);
+
+}
Propchange: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleElementContainer.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleElementIsolator.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleElementIsolator.java?rev=1174099&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleElementIsolator.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleElementIsolator.java Thu Sep 22 12:49:48 2011
@@ -0,0 +1,87 @@
+/*
+ * 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 org.apache.uima.textmarker.rule;
+
+import java.util.List;
+
+import org.apache.uima.textmarker.TextMarkerStream;
+import org.apache.uima.textmarker.visitor.InferenceCrowd;
+
+public class RuleElementIsolator implements RuleElementContainer {
+
+ private RuleElementContainer container;
+
+ public RuleElementIsolator() {
+ super();
+ }
+
+ public RuleElementIsolator(RuleElementContainer container) {
+ super();
+ this.container = container;
+ }
+
+ @Override
+ public List<RuleElement> getRuleElements() {
+ return container.getRuleElements();
+ }
+
+ @Override
+ public RuleElement getAnchoringRuleElement(TextMarkerStream stream) {
+ return getRuleElement();
+ }
+
+ @Override
+ public RuleElement getFirstElement() {
+ return getRuleElement();
+ }
+
+ @Override
+ public RuleElement getLastElement() {
+ return getRuleElement();
+ }
+
+ @Override
+ public void applyRuleElements(RuleMatch ruleMatch, TextMarkerStream stream, InferenceCrowd crowd) {
+ getRuleElement().apply(ruleMatch, stream, crowd);
+ }
+
+ public RuleElementContainer getContainer() {
+ return container;
+ }
+
+ public void setContainer(TextMarkerRule container) {
+ this.container = container.getRoot();
+ }
+
+ private RuleElement getRuleElement() {
+ return container.getRuleElements().get(0);
+ }
+
+ @Override
+ public TextMarkerRule getRule() {
+ return container.getRule();
+ }
+
+ @Override
+ public RuleElement getNextElement(boolean after, RuleElement textMarkerRuleElement) {
+ return null;
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleElementIsolator.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleElementMatch.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleElementMatch.java?rev=1174099&r1=1174098&r2=1174099&view=diff
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleElementMatch.java (original)
+++ uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleElementMatch.java Thu Sep 22 12:49:48 2011
@@ -15,34 +15,34 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
-*/
+ */
package org.apache.uima.textmarker.rule;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
import org.apache.uima.cas.text.AnnotationFS;
public class RuleElementMatch {
- private final RuleElement ruleElement;
+ protected final RuleElement ruleElement;
- private List<AnnotationFS> textsMatched;
+ protected List<AnnotationFS> textsMatched;
- private boolean conditionsMatched = true;
+ protected boolean conditionsMatched = true;
- private boolean baseConditionMatched = true;
+ protected boolean baseConditionMatched = true;
- private List<EvaluatedCondition> conditions;
+ protected List<EvaluatedCondition> conditions;
- private List<RuleElementMatch> innerMatches;
+ protected ComposedRuleElementMatch containerMatch;
- public RuleElementMatch(RuleElement ruleElement) {
+ public RuleElementMatch(RuleElement ruleElement, ComposedRuleElementMatch containerMatch) {
super();
this.ruleElement = ruleElement;
- textsMatched = Collections.emptyList();
+ this.containerMatch = containerMatch;
+ textsMatched = new ArrayList<AnnotationFS>();
}
public void setMatchInfo(boolean baseCondition, List<AnnotationFS> texts,
@@ -56,10 +56,13 @@ public class RuleElementMatch {
conditionsMatched &= each.isValue();
}
}
+ if (containerMatch != null) {
+ containerMatch.addInnerMatch(ruleElement, this);
+ }
}
public boolean matched() {
- return conditionsMatched;
+ return baseConditionMatched && conditionsMatched;
}
public RuleElement getRuleElement() {
@@ -67,16 +70,33 @@ public class RuleElementMatch {
}
public List<AnnotationFS> getTextsMatched() {
- if (textsMatched.size() > 1) {
- System.out.println("TEXTMATCHED > 1");
- }
return textsMatched;
}
+ public void setTextsMatched(List<AnnotationFS> textsMatched) {
+ this.textsMatched = textsMatched;
+ }
+
public List<EvaluatedCondition> getConditions() {
return conditions;
}
+ public void setConditions(List<EvaluatedCondition> conditions) {
+ this.conditions = conditions;
+ }
+
+ public void setConditionsMatched(boolean conditionsMatched) {
+ this.conditionsMatched = conditionsMatched;
+ }
+
+ public void setBaseConditionMatched(boolean baseConditionMatched) {
+ this.baseConditionMatched = baseConditionMatched;
+ }
+
+ public void setContainerMatch(ComposedRuleElementMatch containerMatch) {
+ this.containerMatch = containerMatch;
+ }
+
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
@@ -91,29 +111,20 @@ public class RuleElementMatch {
return string;
}
- public void setMatched(boolean matched) {
- conditionsMatched = matched;
- }
-
public boolean isBaseConditionMatched() {
return baseConditionMatched;
}
- public void setInnerMatches(List<RuleElementMatch> innerMatches) {
- this.innerMatches = innerMatches;
- textsMatched = new ArrayList<AnnotationFS>();
- boolean matched = true;
- for (RuleElementMatch ruleElementMatch : innerMatches) {
- List<AnnotationFS> list = ruleElementMatch.getTextsMatched();
- textsMatched.addAll(list);
- matched &= ruleElementMatch.matched();
- }
- baseConditionMatched = matched;
- conditionsMatched = matched;
+ public ComposedRuleElementMatch getContainerMatch() {
+ return containerMatch;
}
- public List<RuleElementMatch> getInnerMatches() {
- return innerMatches;
+ public RuleElementMatch copy() {
+ RuleElementMatch copy = new RuleElementMatch(ruleElement, containerMatch);
+ copy.setBaseConditionMatched(baseConditionMatched);
+ copy.setConditions(conditions);
+ copy.setConditionsMatched(conditionsMatched);
+ copy.setTextsMatched(textsMatched);
+ return copy;
}
-
}
Modified: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleMatch.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleMatch.java?rev=1174099&r1=1174098&r2=1174099&view=diff
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleMatch.java (original)
+++ uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/RuleMatch.java Thu Sep 22 12:49:48 2011
@@ -19,144 +19,248 @@
package org.apache.uima.textmarker.rule;
+import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import java.util.TreeMap;
+import java.util.Map.Entry;
+import java.util.Set;
import org.apache.uima.cas.text.AnnotationFS;
import org.apache.uima.jcas.tcas.Annotation;
import org.apache.uima.textmarker.ScriptApply;
import org.apache.uima.textmarker.TextMarkerStream;
import org.apache.uima.textmarker.action.AbstractTextMarkerAction;
-import org.apache.uima.textmarker.type.TextMarkerBasic;
public class RuleMatch {
- private static class RuleElementComparator implements Comparator<RuleElement> {
- private final TextMarkerRule rule;
+ private static class RuleMatchComparator implements Comparator<RuleElementMatch> {
- public RuleElementComparator(TextMarkerRule rule) {
- this.rule = rule;
- }
+ public int compare(RuleElementMatch rem1, RuleElementMatch rem2) {
+ AnnotationFS fs1 = rem1.getTextsMatched().get(0);
+ AnnotationFS fs2 = rem2.getTextsMatched().get(0);
- public int compare(RuleElement re1, RuleElement re2) {
- List<RuleElement> elements = rule.getElements();
- int i1 = elements.indexOf(re1);
- int i2 = elements.indexOf(re2);
- if (i1 == i2)
- return 0;
- return i1 < i2 ? -1 : 1;
+ return fs1.getBegin() < fs2.getBegin() ? -1 : 1;
}
}
- private boolean matched;
+ private RuleMatchComparator ruleElementComparator = new RuleMatchComparator();
+
+ private boolean matched = true;
- private TextMarkerBasic first;
+ private boolean applied = false;
private final TextMarkerRule rule;
private Map<AbstractTextMarkerAction, ScriptApply> delegateApply;
- private Map<RuleElement, List<RuleElementMatch>> map;
+ // private Map<RuleElement, List<RuleElementMatch>> map;
+
+ private ComposedRuleElementMatch rootMatch;
public RuleMatch(TextMarkerRule rule) {
super();
this.rule = rule;
- map = new TreeMap<RuleElement, List<RuleElementMatch>>(new RuleElementComparator(rule));
+ // map = new TreeMap<RuleElement, List<RuleElementMatch>>(
+ // new RuleElementComparator(rule.getRoot()));
delegateApply = new HashMap<AbstractTextMarkerAction, ScriptApply>(0);
}
public boolean processMatchInfo(RuleElement ruleElement,
List<RuleElementMatch> ruleElementMatches, TextMarkerStream stream) {
// return true, if you changed the matches -> current basic needs to be set correctly
+ // TODO remove this here?!
boolean result = false;
- List<RuleElementMatch> evaluatedMatches = ruleElement.evaluateMatches(ruleElementMatches,
- ruleElement.getParent());
- if (evaluatedMatches != null) {
- ruleElementMatches = evaluatedMatches;
- result = true;
- }
- map.put(ruleElement, ruleElementMatches);
- matched = evaluatedMatches != null;
-
- if (first == null && !ruleElementMatches.isEmpty()) {
- List<AnnotationFS> textsMatched = ruleElementMatches.get(0).getTextsMatched();
- if (!textsMatched.isEmpty()) {
- AnnotationFS annotation = textsMatched.get(0);
- if (annotation instanceof TextMarkerBasic) {
- first = (TextMarkerBasic) annotation;
- } else if (annotation.getBegin() == stream.getDocumentAnnotation().getBegin()
- && annotation.getEnd() == stream.getDocumentAnnotation().getEnd()) {
- first = stream.getFirstBasicOfAll();
- } else {
- first = stream.getFirstBasicInWindow(annotation);
- }
- }
- }
+ // List<RuleElementMatch> evaluatedMatches = ruleElement.evaluateMatches(ruleElementMatches,
+ // ruleElement.getParent());
+ // if (evaluatedMatches != null) {
+ // ruleElementMatches = evaluatedMatches;
+ // result = true;
+ // }
+ //
+ // matched = evaluatedMatches != null;
+
return result;
}
- public Map<RuleElement, List<RuleElementMatch>> getMatchInfos() {
- return map;
+ public boolean matched() {
+ return matched;
}
- public List<RuleElementMatch> getMatchInfo(TextMarkerRuleElement element) {
- return map.get(element);
+ public boolean matchedCompletely() {
+ return matched && rootMatch.matched();
}
- public boolean matched() {
- return matched;
+ public List<AnnotationFS> getMatchedAnnotationsOf(RuleElement element, TextMarkerStream stream) {
+ return getMatchedAnnotations(stream, element.getSelfIndexList(), element.getContainer());
}
- public AnnotationFS getMatchedAnnotation(TextMarkerStream stream, List<Integer> indexes) {
- int begin = Integer.MAX_VALUE;
- int end = 0;
- for (RuleElement element : map.keySet()) {
- int indexOf = rule.getElements().indexOf(element) + 1;
- if (indexes == null || indexes.isEmpty() || indexes.contains(indexOf)) {
- for (RuleElementMatch rem : map.get(element)) {
- List<AnnotationFS> textsMatched = rem.getTextsMatched();
- if (!textsMatched.isEmpty()) {
+ public List<AnnotationFS> getMatchedAnnotations(TextMarkerStream stream, List<Integer> indexes,
+ RuleElementContainer container) {
+ List<AnnotationFS> result = new ArrayList<AnnotationFS>();
+
+ if (container == null) {
+ container = rule.getRoot();
+ }
+
+ // TODO refactor this!
+ if (indexes == null) {
+ List<RuleElement> ruleElements = container.getRuleElements();
+ indexes = new ArrayList<Integer>();
+ for (RuleElement ruleElement : ruleElements) {
+ indexes.add(ruleElements.indexOf(ruleElement) + 1);
+ }
+ }
+ // TODO refactor this
+ // this method should not be called by elements that need the actual annotation, e.g, for
+ // accessing theirs features.
+ // this is a hotfix for that case...
+ if (indexes.size() == 1) {
+ RuleElement ruleElement = container.getRuleElements().get(indexes.get(0) - 1);
+ List<List<RuleElementMatch>> matchInfo = getMatchInfo(ruleElement);
+ if (matchInfo.size() == 1) {
+ List<RuleElementMatch> list = matchInfo.get(0);
+ if (list != null && list.size() == 1) {
+ List<AnnotationFS> textsMatched = list.get(0).getTextsMatched();
+ if (textsMatched.size() == 1) {
+ return textsMatched;
+ }
+ }
+ }
+ }
+
+ List<List<List<RuleElementMatch>>> reverseList = new ArrayList<List<List<RuleElementMatch>>>();
+ for (Integer index : indexes) {
+ if (index > container.getRuleElements().size()) {
+ continue;
+ }
+ RuleElement ruleElement = container.getRuleElements().get(index - 1);
+ List<List<RuleElementMatch>> matchInfo = getMatchInfo(ruleElement);
+ int i = 0;
+ for (List<RuleElementMatch> list : matchInfo) {
+ if (reverseList.size() <= i) {
+ reverseList.add(new ArrayList<List<RuleElementMatch>>());
+ }
+ List<List<RuleElementMatch>> l = reverseList.get(i);
+ l.add(list);
+ i++;
+ }
+ }
+ for (List<List<RuleElementMatch>> list : reverseList) {
+ int begin = Integer.MAX_VALUE;
+ int end = 0;
+ for (List<RuleElementMatch> list2 : list) {
+ if (list2 != null) {
+ for (RuleElementMatch ruleElementMatch : list2) {
+
+ List<AnnotationFS> textsMatched = ruleElementMatch.getTextsMatched();
begin = Math.min(textsMatched.get(0).getBegin(), begin);
end = Math.max(textsMatched.get(textsMatched.size() - 1).getEnd(), end);
}
}
}
+ if (stream.getJCas() != null && end != 0) {
+ Annotation annotation = new Annotation(stream.getJCas());
+ annotation.setBegin(begin);
+ annotation.setEnd(end);
+ result.add(annotation);
+ }
}
- if (stream.getJCas() != null && end != 0) {
- Annotation annotation = new Annotation(stream.getJCas());
- annotation.setBegin(begin);
- annotation.setEnd(end);
- return annotation;
- } else {
- return null;
- }
+ return result;
}
@Override
public String toString() {
StringBuilder result = new StringBuilder();
result.append((matched ? "Matched : " : "Not Matched :"));
- result.append(getMatchInfos());
+ result.append(getRootMatch());
return result.toString();
}
- public TextMarkerBasic getFirstBasic() {
- return first;
+ public Map<AbstractTextMarkerAction, ScriptApply> getDelegateApply() {
+ return delegateApply;
}
- public final TextMarkerRule getRule() {
+ public void addDelegateApply(AbstractTextMarkerAction action, ScriptApply scriptApply) {
+ delegateApply.put(action, scriptApply);
+ }
+
+ public void setMatched(boolean matched) {
+ this.matched = matched;
+ }
+
+ public RuleMatch copy(ComposedRuleElementMatch extendedContainerMatch) {
+ RuleMatch copy = new RuleMatch(rule);
+ copy.setMatched(matched);
+ if (extendedContainerMatch.getContainerMatch() == null) {
+ copy.setRootMatch(extendedContainerMatch);
+ } else {
+ copy.setRootMatch(rootMatch.copy(extendedContainerMatch));
+ }
+
+ Map<AbstractTextMarkerAction, ScriptApply> newDelegateApply = new HashMap<AbstractTextMarkerAction, ScriptApply>(
+ delegateApply);
+ copy.setDelegateApply(newDelegateApply);
+ return copy;
+ }
+
+ public void setDelegateApply(Map<AbstractTextMarkerAction, ScriptApply> delegateApply) {
+ this.delegateApply = delegateApply;
+ }
+
+ public boolean isApplied() {
+ return applied;
+ }
+
+ public void setApplied(boolean applied) {
+ this.applied = applied;
+ }
+
+ public TextMarkerRule getRule() {
return rule;
}
- public Map<AbstractTextMarkerAction, ScriptApply> getDelegateApply() {
- return delegateApply;
+ public ComposedRuleElementMatch getRootMatch() {
+ return rootMatch;
}
- public void addDelegateApply(AbstractTextMarkerAction action, ScriptApply scriptApply) {
- delegateApply.put(action, scriptApply);
+ public void setRootMatch(ComposedRuleElementMatch rootMatch) {
+ this.rootMatch = rootMatch;
+ }
+
+ public Map<RuleElement, List<RuleElementMatch>> getMatchInfos() {
+ assert (false);
+ return null;
+ }
+
+ public List<List<RuleElementMatch>> getMatchInfo(RuleElement element) {
+ return getMatchInfo(rootMatch, element);
}
+ public List<List<RuleElementMatch>> getMatchInfo(RuleElementMatch rootMatch, RuleElement element) {
+ List<List<RuleElementMatch>> result = new ArrayList<List<RuleElementMatch>>();
+ RuleElement root = rootMatch.getRuleElement();
+ if (element.equals(root)) {
+ List<RuleElementMatch> list = new ArrayList<RuleElementMatch>(1);
+ list.add(rootMatch);
+ result.add(list);
+ } else if (rootMatch instanceof ComposedRuleElementMatch) {
+ ComposedRuleElementMatch crem = (ComposedRuleElementMatch) rootMatch;
+ Set<Entry<RuleElement, List<RuleElementMatch>>> entrySet = crem.getInnerMatches().entrySet();
+ for (Entry<RuleElement, List<RuleElementMatch>> entry : entrySet) {
+ List<RuleElementMatch> value = entry.getValue();
+ if (element.equals(entry.getKey())) {
+ result.add(value);
+ } else {
+ if (value != null) {
+ for (RuleElementMatch eachMatch : value) {
+ result.addAll(getMatchInfo(eachMatch, element));
+ }
+ }
+ }
+ }
+ }
+ return result;
+ }
}
Added: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/TextMarkerDisjunctiveTypeMatcher.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/TextMarkerDisjunctiveTypeMatcher.java?rev=1174099&view=auto
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/TextMarkerDisjunctiveTypeMatcher.java (added)
+++ uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/TextMarkerDisjunctiveTypeMatcher.java Thu Sep 22 12:49:48 2011
@@ -0,0 +1,64 @@
+/*
+ * 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 org.apache.uima.textmarker.rule;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.uima.cas.Type;
+import org.apache.uima.textmarker.TextMarkerBlock;
+import org.apache.uima.textmarker.TextMarkerStream;
+import org.apache.uima.textmarker.expression.type.TypeExpression;
+
+public class TextMarkerDisjunctiveTypeMatcher extends TextMarkerTypeMatcher {
+ private final List<TypeExpression> expressions;
+
+ public TextMarkerDisjunctiveTypeMatcher(List<TypeExpression> expressions) {
+ super(null);
+ this.expressions = expressions;
+ }
+
+ @Override
+ public List<Type> getTypes(TextMarkerBlock parent, TextMarkerStream stream) {
+ List<Type> result = new ArrayList<Type>();
+ for (TypeExpression expr : expressions) {
+ Type type = getType(expr, parent, stream);
+ result.add(type);
+ }
+ return result;
+ }
+
+ @Override
+ public int estimateAnchors(TextMarkerBlock parent, TextMarkerStream stream) {
+ int count = 0;
+ List<Type> types = getTypes(parent, stream);
+ for (Type type : types) {
+ count += stream.getHistogram(type);
+
+ }
+ return count;
+ }
+
+ @Override
+ public String toString() {
+ return expressions.toString();
+ }
+
+}
Propchange: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/TextMarkerDisjunctiveTypeMatcher.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/TextMarkerLiteralMatcher.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/TextMarkerLiteralMatcher.java?rev=1174099&r1=1174098&r2=1174099&view=diff
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/TextMarkerLiteralMatcher.java (original)
+++ uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/TextMarkerLiteralMatcher.java Thu Sep 22 12:49:48 2011
@@ -20,9 +20,9 @@
package org.apache.uima.textmarker.rule;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.List;
-import org.apache.uima.cas.FSIterator;
import org.apache.uima.cas.Type;
import org.apache.uima.cas.text.AnnotationFS;
import org.apache.uima.textmarker.TextMarkerBlock;
@@ -40,8 +40,8 @@ public class TextMarkerLiteralMatcher im
this.expression = expression;
}
- public List<TextMarkerBasic> getMatchingBasics(TextMarkerStream stream, TextMarkerBlock parent) {
- List<TextMarkerBasic> result = new ArrayList<TextMarkerBasic>();
+ public List<AnnotationFS> getMatchingAnnotations(TextMarkerStream stream, TextMarkerBlock parent) {
+ List<AnnotationFS> result = new ArrayList<AnnotationFS>();
AnnotationFS windowAnnotation = stream.getDocumentAnnotation();
List<TextMarkerBasic> list = stream.getBasicsInWindow(windowAnnotation);
for (TextMarkerBasic each : list) {
@@ -52,17 +52,7 @@ public class TextMarkerLiteralMatcher im
return result;
}
- @Override
- public FSIterator<AnnotationFS> getMatchingBasics2(TextMarkerStream stream, TextMarkerBlock parent) {
- assert (false);
- return null;
- }
-
- public Type getType(TextMarkerStream stream) {
- return null;
- }
-
- public boolean match(TextMarkerBasic annotation, TextMarkerStream stream, TextMarkerBlock parent) {
+ public boolean match(AnnotationFS annotation, TextMarkerStream stream, TextMarkerBlock parent) {
if (annotation == null) {
return false;
}
@@ -78,7 +68,25 @@ public class TextMarkerLiteralMatcher im
return expression;
}
- public Type getType(TextMarkerBlock parent, TextMarkerStream stream) {
+ @Override
+ public int estimateAnchors(TextMarkerBlock parent, TextMarkerStream stream) {
+ return Integer.MAX_VALUE;
+ }
+
+ @Override
+ public List<AnnotationFS> getAnnotationsAfter(TextMarkerRuleElement ruleElement,
+ AnnotationFS annotation, TextMarkerStream stream, TextMarkerBlock parent) {
+ return null;
+ }
+
+ @Override
+ public Collection<AnnotationFS> getAnnotationsBefore(TextMarkerRuleElement ruleElement,
+ AnnotationFS annotation, TextMarkerStream stream, TextMarkerBlock parent) {
+ return null;
+ }
+
+ @Override
+ public List<Type> getTypes(TextMarkerBlock parent, TextMarkerStream stream) {
return null;
}
Modified: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/TextMarkerMatcher.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/TextMarkerMatcher.java?rev=1174099&r1=1174098&r2=1174099&view=diff
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/TextMarkerMatcher.java (original)
+++ uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/TextMarkerMatcher.java Thu Sep 22 12:49:48 2011
@@ -19,26 +19,31 @@
package org.apache.uima.textmarker.rule;
+import java.util.Collection;
import java.util.List;
-import org.apache.uima.cas.FSIterator;
import org.apache.uima.cas.Type;
import org.apache.uima.cas.text.AnnotationFS;
import org.apache.uima.textmarker.TextMarkerBlock;
import org.apache.uima.textmarker.TextMarkerStream;
import org.apache.uima.textmarker.expression.TextMarkerExpression;
-import org.apache.uima.textmarker.type.TextMarkerBasic;
public interface TextMarkerMatcher {
- List<TextMarkerBasic> getMatchingBasics(TextMarkerStream stream, TextMarkerBlock parent);
+ Collection<AnnotationFS> getMatchingAnnotations(TextMarkerStream stream, TextMarkerBlock parent);
- FSIterator<AnnotationFS> getMatchingBasics2(TextMarkerStream stream, TextMarkerBlock parent);
+ boolean match(AnnotationFS annotation, TextMarkerStream stream, TextMarkerBlock parent);
- boolean match(TextMarkerBasic currentBasic, TextMarkerStream stream, TextMarkerBlock parent);
-
- Type getType(TextMarkerBlock parent, TextMarkerStream stream);
+ List<Type> getTypes(TextMarkerBlock parent, TextMarkerStream stream);
TextMarkerExpression getExpression();
+ int estimateAnchors(TextMarkerBlock parent, TextMarkerStream stream);
+
+ Collection<AnnotationFS> getAnnotationsAfter(TextMarkerRuleElement ruleElement,
+ AnnotationFS annotation, TextMarkerStream stream, TextMarkerBlock parent);
+
+ Collection<AnnotationFS> getAnnotationsBefore(TextMarkerRuleElement ruleElement,
+ AnnotationFS annotation, TextMarkerStream stream, TextMarkerBlock parent);
+
}
Modified: uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/TextMarkerRule.java
URL: http://svn.apache.org/viewvc/uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/TextMarkerRule.java?rev=1174099&r1=1174098&r2=1174099&view=diff
==============================================================================
--- uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/TextMarkerRule.java (original)
+++ uima/sandbox/trunk/TextMarker/uimaj-ep-textmarker-engine/src/main/java/org/apache/uima/textmarker/rule/TextMarkerRule.java Thu Sep 22 12:49:48 2011
@@ -15,34 +15,28 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
-*/
+ */
package org.apache.uima.textmarker.rule;
-import java.util.ArrayList;
import java.util.List;
-import java.util.Map;
-import org.apache.uima.cas.FSIterator;
-import org.apache.uima.cas.text.AnnotationFS;
import org.apache.uima.textmarker.TextMarkerBlock;
import org.apache.uima.textmarker.TextMarkerEnvironment;
import org.apache.uima.textmarker.TextMarkerStatement;
import org.apache.uima.textmarker.TextMarkerStream;
-import org.apache.uima.textmarker.type.TextMarkerBasic;
import org.apache.uima.textmarker.visitor.InferenceCrowd;
-
public class TextMarkerRule extends TextMarkerStatement {
- private final List<RuleElement> elements;
+ private ComposedRuleElement root;
private final int id;
public TextMarkerRule(List<RuleElement> elements, TextMarkerBlock parent, int id) {
super(parent);
- this.elements = elements;
this.id = id;
+ this.root = new ComposedRuleElement(elements, null, null, null, null, parent);
}
@Override
@@ -51,89 +45,21 @@ public class TextMarkerRule extends Text
}
public RuleApply apply(TextMarkerStream stream, InferenceCrowd crowd, boolean remember) {
- RuleApply result = new RuleApply(this, remember);
- crowd.beginVisit(this, result);
- List<TextMarkerBasic> anchors = getAnchors(stream);
- // FSIterator<AnnotationFS> anchors = getAnchors2(stream);
- for (TextMarkerBasic each : anchors) {
-
- // while (anchors.isValid()) {
- TextMarkerBasic currentBasic = each;
- // TextMarkerBasic currentBasic = (TextMarkerBasic) anchors.get();
- RuleMatch ruleMatch = new RuleMatch(this);
- List<RuleElementMatch> ruleElementMatches = new ArrayList<RuleElementMatch>();
- for (int i = 0; i < elements.size(); i++) {
- RuleElement re = elements.get(i);
- // for really lazy rule elements, that don't even want to match anything
- if (re.continueMatch(i, elements, currentBasic, null, ruleElementMatches, stream)) {
- RuleElementMatch match = re.match(currentBasic, stream, crowd);
- ruleElementMatches.add(match);
- TextMarkerBasic nextBasic = stream.nextAnnotation(match);
- // AnnotationFS fs1 = anchors.get();
- if (re.continueMatch(i, elements, nextBasic, match, ruleElementMatches, stream)) {
- TextMarkerBasic lastBasic = currentBasic;
- if (nextBasic != null) {
- i--;
- currentBasic = nextBasic;
- continue;
- } else if (!match.matched()) {
- nextBasic = lastBasic;
- ruleElementMatches.remove(ruleElementMatches.size() - 1);
- }
- }
- TextMarkerBasic previousBasic = currentBasic;
- currentBasic = nextBasic;
- boolean changedMatches = ruleMatch.processMatchInfo(re, ruleElementMatches, stream);
- if (!ruleMatch.matched() /* || currentBasic == null */) {
- break;
- } else {
- if (currentBasic == null
- && re.continueMatch(i, elements, nextBasic, match, ruleElementMatches, stream)
- && stream.nextAnnotation(match) != null) {
- currentBasic = previousBasic;
- } else if (currentBasic == null && changedMatches && match.matched()) {
- } else if (currentBasic == null && changedMatches) {
- currentBasic = previousBasic;
- }
- }
- ruleElementMatches = new ArrayList<RuleElementMatch>();
-
- }
- }
- if (ruleMatch.matched()) {
- fireRule(ruleMatch, stream, crowd);
- }
- result.add(ruleMatch);
- // anchors.moveToNext();
- }
- crowd.endVisit(this, result);
- return result;
- }
-
- private void fireRule(RuleMatch matchInfos, TextMarkerStream symbolStream, InferenceCrowd crowd) {
- if (matchInfos.matched()) {
- Map<RuleElement, List<RuleElementMatch>> map = matchInfos.getMatchInfos();
- for (RuleElement eachElement : map.keySet()) {
- eachElement.apply(matchInfos, symbolStream, crowd);
- }
- }
- }
-
- public List<TextMarkerBasic> getAnchors(TextMarkerStream symbolStream) {
- return elements.get(0).getAnchors(symbolStream);
- }
-
- public FSIterator<AnnotationFS> getAnchors2(TextMarkerStream symbolStream) {
- return elements.get(0).getAnchors2(symbolStream);
+ RuleApply ruleApply = new RuleApply(this, remember);
+ crowd.beginVisit(this, ruleApply);
+ RuleMatch ruleMatch = new RuleMatch(this);
+ root.startMatch(ruleMatch, ruleApply, null, null, stream, crowd);
+ crowd.endVisit(this, ruleApply);
+ return ruleApply;
}
@Override
public String toString() {
- return elements.toString();
+ return root.toString();
}
- public final List<RuleElement> getElements() {
- return elements;
+ public final List<RuleElement> getRuleElements() {
+ return root.getRuleElements();
}
public int getId() {
@@ -145,4 +71,12 @@ public class TextMarkerRule extends Text
return getParent().getEnvironment();
}
+ public void setRuleElements(List<RuleElement> elements) {
+ root.setRuleElements(elements);
+ }
+
+ public ComposedRuleElement getRoot() {
+ return root;
+ }
+
}