Skip to content

Commit f178748

Browse files
committed
Add option to disable length restriction
By default MathWebSearch does not allow additional elements in matching XML trees. This has the effect that for example the query $x+y$ does not match $x+y+z$ Change-Id: I6f307f49f1c1489452cc8b9f421f1314bc6b108a
1 parent 4349755 commit f178748

3 files changed

Lines changed: 63 additions & 24 deletions

File tree

src/main/java/com/formulasearchengine/mathmlquerygenerator/NtcirTopicReader.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ public void setHeader (String header) {
8484
queryGenerator.setHeader( header );
8585
}
8686

87+
public void setRestricLength ( boolean restrictLength) {
88+
queryGenerator.setRestrictLength( restrictLength );
89+
}
90+
8791
public List<NtcirPattern> extractPatterns () throws XPathExpressionException {
8892
XPath xpath = namespaceAwareXpath( "t", NS_NII );
8993
XPathExpression xNum = xpath.compile( "./t:num" );

src/main/java/com/formulasearchengine/mathmlquerygenerator/XQueryGenerator.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ public class XQueryGenerator {
2828
private String footer = "data($m/*[1]/@alttext)";
2929
private Node mainElement = null;
3030

31+
public boolean isRestrictLength () {
32+
return restrictLength;
33+
}
34+
35+
/**
36+
* If set to true a query like $x+y$ does not match $x+y+z$.
37+
* @param restrictLength
38+
*/
39+
public void setRestrictLength (boolean restrictLength) {
40+
this.restrictLength = restrictLength;
41+
}
42+
43+
private boolean restrictLength = true;
44+
3145
public XQueryGenerator (Document xml) {
3246
this.xml = xml;
3347
}
@@ -87,6 +101,10 @@ private Node getMainElement () {
87101

88102
}
89103

104+
/**
105+
* Resets the current xQuery expression and sets a new main element.
106+
* @param mainElement
107+
*/
90108
public void setMainElement (Node mainElement) {
91109
this.mainElement = mainElement;
92110
qvar = new HashMap<>();
@@ -203,8 +221,8 @@ private String generateConstraint (Node node, boolean isRoot) {
203221
}
204222
}
205223
}
206-
if ( !isRoot ) {
207-
if ( lengthConstraint.equals( "" ) ) {
224+
if ( !isRoot && restrictLength ) {
225+
if ( lengthConstraint.equals( "" ) ) {
208226
lengthConstraint += "fn:count($x" + relativeXPath + "/*) = " + i + "\n";
209227
} else {
210228
lengthConstraint += " and fn:count($x" + relativeXPath + "/*) = " + i + "\n";

src/test/java/com/formulasearchengine/mathmlquerygenerator/XQueryGeneratorTest.java

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -74,26 +74,43 @@ public void testCmmlConversion() {
7474
}
7575

7676
public void testHeaderAndFooter() throws Exception {
77-
final String testHead = "declare default element namespace \"http://www.w3.org/1998/Math/MathML\";\n" +
78-
"<result>{\n" +
79-
"let $m := .";
80-
final String testFooter = "$x}\n" +
81-
"</result>";
82-
final String testInput = getFileContents( "com/formulasearchengine/mathmlquerygenerator/cmml/q1.xml" );
83-
final String expectedOutput = "declare default element namespace \"http://www.w3.org/1998/Math/MathML\";\n" +
84-
"<result>{\n" +
85-
"let $m := .for $x in $m//*:ci\n" +
86-
"[./text() = 'E']\n" +
87-
"where\n" +
88-
"fn:count($x/*) = 0\n" +
89-
"\n" +
90-
"return\n" +
91-
"$x}\n" +
92-
"</result>";
93-
Document query = XMLHelper.String2Doc(testInput);
94-
XQueryGenerator xQueryGenerator = new XQueryGenerator(query);
95-
xQueryGenerator.setFooter(testFooter);
96-
xQueryGenerator.setHeader(testHead);
97-
assertEquals(expectedOutput, xQueryGenerator.toString());
98-
}
77+
final String testHead = "declare default element namespace \"http://www.w3.org/1998/Math/MathML\";\n" +
78+
"<result>{\n" +
79+
"let $m := .";
80+
final String testFooter = "$x}\n" +
81+
"</result>";
82+
final String testInput = getFileContents( "com/formulasearchengine/mathmlquerygenerator/cmml/q1.xml" );
83+
final String expectedOutput = "declare default element namespace \"http://www.w3.org/1998/Math/MathML\";\n" +
84+
"<result>{\n" +
85+
"let $m := .for $x in $m//*:ci\n" +
86+
"[./text() = 'E']\n" +
87+
"where\n" +
88+
"fn:count($x/*) = 0\n" +
89+
"\n" +
90+
"return\n" +
91+
"$x}\n" +
92+
"</result>";
93+
Document query = XMLHelper.String2Doc(testInput);
94+
XQueryGenerator xQueryGenerator = new XQueryGenerator(query);
95+
xQueryGenerator.setFooter(testFooter);
96+
xQueryGenerator.setHeader(testHead);
97+
assertEquals(expectedOutput, xQueryGenerator.toString());
98+
}
99+
100+
public void testNoRestriction() throws Exception {
101+
final String testHead = "" ;
102+
final String testFooter = "";
103+
final String testInput = getFileContents( "com/formulasearchengine/mathmlquerygenerator/mws/qqx2x.xml" );
104+
final String expectedOutput = "for $x in $m//*:apply\n" +
105+
"[*[1]/name() = 'plus' and *[2]/name() = 'apply' and *[2][*[1]/name() = 'csymbol' and *[1][./text() = 'superscript'] and *[3]/name() = 'cn' and *[3][./text() = '2']]]\n" +
106+
"where\n" +
107+
"$x/*[2]/*[2] = $x/*[3]\n" +
108+
"return\n" ;
109+
Document query = XMLHelper.String2Doc(testInput);
110+
XQueryGenerator xQueryGenerator = new XQueryGenerator(query);
111+
xQueryGenerator.setFooter(testFooter);
112+
xQueryGenerator.setHeader( testHead );
113+
xQueryGenerator.setRestrictLength( false );
114+
assertEquals( expectedOutput, xQueryGenerator.toString() );
115+
}
99116
}

0 commit comments

Comments
 (0)