Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #139: Unexpected behavior of plus operator #142

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -498,6 +499,42 @@ private AnnotationFS getBacktrackedAnnotation(boolean after,
if (evaluateMatches.isEmpty()) {
return annotation;
}

// ensure correct ordering
Collections.sort(evaluateMatches, new Comparator<RuleElementMatch>() {

@Override
public int compare(RuleElementMatch rem1, RuleElementMatch rem2) {
List<AnnotationFS> textsMatched1 = rem1.getTextsMatched();
List<AnnotationFS> textsMatched2 = rem2.getTextsMatched();
if ((textsMatched1 == null || textsMatched1.isEmpty())
&& (textsMatched2 == null || textsMatched2.isEmpty())) {
return 0;
}
if (textsMatched1 == null
|| textsMatched1.isEmpty() && !(textsMatched2 == null || textsMatched2.isEmpty())) {
return -1;
}
if (!(textsMatched1 == null || textsMatched1.isEmpty())
&& (textsMatched2 == null || textsMatched2.isEmpty())) {
return 1;
}
if (textsMatched1.equals(textsMatched2)) {
return 0;
}
AnnotationFS first1 = textsMatched1.get(0);
AnnotationFS last1 = textsMatched1.get(textsMatched1.size() - 1);
AnnotationFS first2 = textsMatched2.get(0);
AnnotationFS last2 = textsMatched2.get(textsMatched2.size() - 1);
int compareBegin = Integer.compare(first1.getBegin(), first2.getBegin());
if (compareBegin != 0) {
return compareBegin;
}
int compareEnd = Integer.compare(last1.getEnd(), last2.getEnd());
return compareEnd;
}
});

if (after) {
List<AnnotationFS> textsMatched = evaluateMatches.get(evaluateMatches.size() - 1)
.getTextsMatched();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* 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
Expand All @@ -27,51 +27,52 @@
public class ComposedRuleElementWithQuantifierTest {

@Test
public void test() {
public void test() throws Exception {
String document = "Bla DDD, Bla, DDD,DDD, Bla, DDD,DDD Bla.";
String script = "";
script += "\"DDD\" -> T1;\n";
script += "(T1 COMMA)+? (T1 W){->MARKONCE(T2,1,2)};\n";
CAS cas = null;
try {
cas = RutaTestUtils.getCAS(document);
Ruta.apply(cas, script);
} catch (Exception e) {
e.printStackTrace();
}
CAS cas = RutaTestUtils.getCAS(document);
Ruta.apply(cas, script);

RutaTestUtils.assertAnnotationsEquals(cas, 2, 1, "DDD,DDD Bla");

cas.release();
}

@Test
public void testStackedWithWildCard() {
public void testStackedWithWildCard() throws Exception {
String document = "some text\n 1 HEADLINE\n some text";
String script = "";

script +="\"1 HEADLINE\"-> T1;\n";
script +="DOUBLE n;\n";
script +="BLOCK(eachTag) T1{} {\n";
script +="((NUM{STARTSWITH(T1)} (PERIOD NUM)?){PARSE(n)} (W #){-> T2})\n";
script +=" {-> T3};\n";
script +="}\n";

CAS cas = null;
try {
cas = RutaTestUtils.getCAS(document);
Ruta.apply(cas, script);
} catch (Exception e) {
e.printStackTrace();
}

script += "\"1 HEADLINE\"-> T1;\n";
script += "DOUBLE n;\n";
script += "BLOCK(eachTag) T1{} {\n";
script += "((NUM{STARTSWITH(T1)} (PERIOD NUM)?){PARSE(n)} (W #){-> T2})\n";
script += " {-> T3};\n";
script += "}\n";

CAS cas = RutaTestUtils.getCAS(document);
Ruta.apply(cas, script);

RutaTestUtils.assertAnnotationsEquals(cas, 2, 1, "HEADLINE");
RutaTestUtils.assertAnnotationsEquals(cas, 3, 1, "1 HEADLINE");
}

cas.release();

@Test
public void testRightToLeftMatching() throws Exception {
String document = "Indicator x y z a";

String script = "";
script += "\"Indicator\"-> T1;\n";
script += "SW{REGEXP(\"a\")-> T2};";
script += "T1 (SW)+ @T2{-> T3};";

CAS cas = RutaTestUtils.getCAS(document);
Ruta.apply(cas, script);

RutaTestUtils.assertAnnotationsEquals(cas, 1, 1, "Indicator");
RutaTestUtils.assertAnnotationsEquals(cas, 2, 1, "a");
RutaTestUtils.assertAnnotationsEquals(cas, 3, 1, "a");
}



}
2 changes: 2 additions & 0 deletions ruta-ep-engine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,12 @@
org.htmlparser.*,
org.apache.commons.text.*,
org.apache.commons.lang3.*,
org.apache.commons.collections4.*,
org.apache.commons.logging,
org.apache.commons.io.*,
org.apache.uima.fit.*,
org.apache.commons.math3.*,
com.github.benmanes.caffeine.cache.*,
org.springframework.*
</_exportcontents>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* 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
Expand Down Expand Up @@ -48,12 +48,12 @@ public class KEPPreferencePage extends PreferencePage implements IWorkbenchPrefe

private IPreferenceStore store;

private ArrayList<FieldEditor> fields = new ArrayList<FieldEditor>();
private ArrayList<FieldEditor> fields = new ArrayList<>();

public KEPPreferencePage() {
TextRulerLearnerController ctrl = TextRulerController
.getControllerForID("org.apache.uima.ruta.textruler.kep");
this.algorithmController = ctrl;
algorithmController = ctrl;
store = TextRulerPlugin.getDefault().getPreferenceStore();
setPreferenceStore(store);
}
Expand Down Expand Up @@ -84,6 +84,7 @@ protected Control createContents(Composite parent) {

case ML_FLOAT_PARAM:
case ML_INT_PARAM:
case ML_DOUBLE_PARAM:
case ML_STRING_PARAM: {
l = new StringFieldEditor(id, p.name, top);
fields.add(l);
Expand All @@ -100,20 +101,23 @@ protected Control createContents(Composite parent) {
return top;
}

@Override
public void init(IWorkbench workbench) {
}

@Override
protected void performDefaults() {
for (FieldEditor f : fields)
for (FieldEditor f : fields) {
f.loadDefault();
// super.performDefaults();
// super.performDefaults();
}
}

@Override
public boolean performOk() {
for (FieldEditor f : fields)
for (FieldEditor f : fields) {
f.store();
}
// return super.performOk();
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* 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
Expand Down Expand Up @@ -48,12 +48,12 @@ public class NaiveLP2PreferencePage extends PreferencePage implements IWorkbench

private IPreferenceStore store;

private ArrayList<FieldEditor> fields = new ArrayList<FieldEditor>();
private ArrayList<FieldEditor> fields = new ArrayList<>();

public NaiveLP2PreferencePage() {
TextRulerLearnerController ctrl = TextRulerController
.getControllerForID("org.apache.uima.ruta.textruler.lp2naive");
this.algorithmController = ctrl;
algorithmController = ctrl;
store = TextRulerPlugin.getDefault().getPreferenceStore();
setPreferenceStore(store);
}
Expand Down Expand Up @@ -84,6 +84,7 @@ protected Control createContents(Composite parent) {

case ML_FLOAT_PARAM:
case ML_INT_PARAM:
case ML_DOUBLE_PARAM:
case ML_STRING_PARAM: {
l = new StringFieldEditor(id, p.name, top);
fields.add(l);
Expand All @@ -100,20 +101,23 @@ protected Control createContents(Composite parent) {
return top;
}

@Override
public void init(IWorkbench workbench) {
}

@Override
protected void performDefaults() {
for (FieldEditor f : fields)
for (FieldEditor f : fields) {
f.loadDefault();
// super.performDefaults();
// super.performDefaults();
}
}

@Override
public boolean performOk() {
for (FieldEditor f : fields)
for (FieldEditor f : fields) {
f.store();
}
// return super.performOk();
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* 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
Expand Down Expand Up @@ -48,12 +48,12 @@ public class OptimizedLP2PreferencePage extends PreferencePage implements IWorkb

private IPreferenceStore store;

private ArrayList<FieldEditor> fields = new ArrayList<FieldEditor>();
private ArrayList<FieldEditor> fields = new ArrayList<>();

public OptimizedLP2PreferencePage() {
TextRulerLearnerController ctrl = TextRulerController
.getControllerForID("org.apache.uima.ruta.textruler.lp2opt");
this.algorithmController = ctrl;
algorithmController = ctrl;
store = TextRulerPlugin.getDefault().getPreferenceStore();
setPreferenceStore(store);
}
Expand Down Expand Up @@ -84,6 +84,7 @@ protected Control createContents(Composite parent) {

case ML_FLOAT_PARAM:
case ML_INT_PARAM:
case ML_DOUBLE_PARAM:
case ML_STRING_PARAM: {
l = new StringFieldEditor(id, p.name, top);
fields.add(l);
Expand All @@ -100,20 +101,23 @@ protected Control createContents(Composite parent) {
return top;
}

@Override
public void init(IWorkbench workbench) {
}

@Override
protected void performDefaults() {
for (FieldEditor f : fields)
for (FieldEditor f : fields) {
f.loadDefault();
// super.performDefaults();
// super.performDefaults();
}
}

@Override
public boolean performOk() {
for (FieldEditor f : fields)
for (FieldEditor f : fields) {
f.store();
}
// return super.performOk();
return true;
}
Expand Down
Loading