aboutsummaryrefslogtreecommitdiffstats
path: root/container-search
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@gmail.com>2022-01-11 18:45:55 +0100
committerJon Bratseth <bratseth@gmail.com>2022-01-11 18:45:55 +0100
commit2627f74bff87b619393cf74fe36228c9cb5ad4ed (patch)
treebdc8ccd44553f9b2fb26cac06286fdbc24a27323 /container-search
parent77f8677d12f3cc3013e3c1cca9469e05abf9edae (diff)
Propagate the original term in the evaluation
Diffstat (limited to 'container-search')
-rw-r--r--container-search/src/main/java/com/yahoo/prelude/semantics/rule/TermCondition.java22
-rw-r--r--container-search/src/main/java/com/yahoo/prelude/semantics/rule/TermProduction.java4
-rw-r--r--container-search/src/test/java/com/yahoo/prelude/semantics/test/MusicTestCase.java19
-rw-r--r--container-search/src/test/java/com/yahoo/prelude/semantics/test/rulebases/music.sr19
4 files changed, 45 insertions, 19 deletions
diff --git a/container-search/src/main/java/com/yahoo/prelude/semantics/rule/TermCondition.java b/container-search/src/main/java/com/yahoo/prelude/semantics/rule/TermCondition.java
index 6e2d3de7d08..a2bbf72a53b 100644
--- a/container-search/src/main/java/com/yahoo/prelude/semantics/rule/TermCondition.java
+++ b/container-search/src/main/java/com/yahoo/prelude/semantics/rule/TermCondition.java
@@ -14,8 +14,8 @@ import com.yahoo.prelude.semantics.engine.RuleEvaluation;
public class TermCondition extends Condition {
private final RuleBaseLinguistics linguistics;
- private String originalTerm;
- private String term;
+ private final String originalTerm;
+ private final String term;
public TermCondition(String term, RuleBaseLinguistics linguistics) {
this(null, term, linguistics);
@@ -28,12 +28,6 @@ public class TermCondition extends Condition {
this.term = linguistics.process(term);
}
- public String getTerm() { return term; }
-
- public void setTerm(String term) {
- this.term = term;
- }
-
protected boolean doesMatch(RuleEvaluation e) {
// TODO: Move this into the respective namespaces when query becomes one */
if (getNameSpace() != null) {
@@ -44,10 +38,10 @@ public class TermCondition extends Condition {
if (e.currentItem() == null) return false;
if ( ! labelMatches(e)) return false;
- String matchedValue = termMatches(e.currentItem().getItem());
- boolean matches = matchedValue!=null && labelMatches(e.currentItem().getItem(), e);
+ boolean matches = labelMatches(e.currentItem().getItem(), e) &&
+ linguistics.process(e.currentItem().getItem().stringValue()).equals(term);
if ((matches && !e.isInNegation() || (!matches && e.isInNegation()))) {
- e.addMatch(e.currentItem(), matchedValue);
+ e.addMatch(e.currentItem(), originalTerm);
e.setValue(term);
e.next();
}
@@ -55,12 +49,6 @@ public class TermCondition extends Condition {
}
}
- /** Returns a non-null replacement term if there is a match, null otherwise */
- private String termMatches(TermItem queryTerm) {
- boolean matches = linguistics.process(queryTerm.stringValue()).equals(term);
- return matches ? term : null;
- }
-
public String toInnerString() {
return getLabelString() + term;
}
diff --git a/container-search/src/main/java/com/yahoo/prelude/semantics/rule/TermProduction.java b/container-search/src/main/java/com/yahoo/prelude/semantics/rule/TermProduction.java
index db8d4b42521..29e4982ac17 100644
--- a/container-search/src/main/java/com/yahoo/prelude/semantics/rule/TermProduction.java
+++ b/container-search/src/main/java/com/yahoo/prelude/semantics/rule/TermProduction.java
@@ -15,7 +15,7 @@ import com.yahoo.protect.Validator;
public abstract class TermProduction extends Production {
/** The label of this term, or null if none */
- private String label = null;
+ private String label;
/** The type of term to produce */
private TermType termType;
@@ -62,7 +62,7 @@ public abstract class TermProduction extends Production {
protected void insertMatch(RuleEvaluation e, Match matched, Item newItem, int offset) {
if (getWeight() != 100)
newItem.setWeight(getWeight());
- int insertPosition = matched.getPosition()+offset;
+ int insertPosition = matched.getPosition() + offset;
// This check is necessary (?) because earlier items may have been removed
// after we recorded the match position. It is sort of hackish. A cleaner
diff --git a/container-search/src/test/java/com/yahoo/prelude/semantics/test/MusicTestCase.java b/container-search/src/test/java/com/yahoo/prelude/semantics/test/MusicTestCase.java
new file mode 100644
index 00000000000..006dcb3c714
--- /dev/null
+++ b/container-search/src/test/java/com/yahoo/prelude/semantics/test/MusicTestCase.java
@@ -0,0 +1,19 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.prelude.semantics.test;
+
+import org.junit.Test;
+
+/**
+ * Tests the rewriting in the semanticsearcher system test
+ *
+ * @author bratseth
+ */
+public class MusicTestCase {
+
+ @Test
+ public void testMusic() {
+ var tester = new RuleBaseTester("music.sr");
+ tester.assertSemantics("AND song:together artist:youngbloods", "together by youngbloods");
+ }
+
+}
diff --git a/container-search/src/test/java/com/yahoo/prelude/semantics/test/rulebases/music.sr b/container-search/src/test/java/com/yahoo/prelude/semantics/test/rulebases/music.sr
new file mode 100644
index 00000000000..5166a8b8e5d
--- /dev/null
+++ b/container-search/src/test/java/com/yahoo/prelude/semantics/test/rulebases/music.sr
@@ -0,0 +1,19 @@
+## Some test rules
+
+# Spelling correction
+bahc -> bach;
+
+# Stopwords
+somelongstopword -> ;
+[stopword] -> ;
+[stopword] :- someotherlongstopword, yetanotherstopword;
+
+#
+[song] by [artist] -> song:[song] artist:[artist];
+
+[song] :- together, imagine, tinseltown;
+[artist] :- youngbloods, beatles, zappa;
+
+# Negative
+various +> -kingz;
+