Skip to content

Commit e8df426

Browse files
committed
Extract operators for files without apply fields
1 parent fd738e3 commit e8df426

7 files changed

Lines changed: 84 additions & 5 deletions

File tree

mathml-core/src/main/java/com/formulasearchengine/mathmltools/exceptions/ParsingErrorHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void error(SAXParseException exception) throws SAXException {
4848
LOG.debug(exception.getMessage() + " Error handler is on level 'silent' that ignores this error.");
4949
return; // ignore
5050
case NOTIFY:
51-
LOG.error(exception.getMessage() + " Error handler is on level 'notify', no exception was thrown.");
51+
LOG.warn(exception.getMessage() + " Error handler is on level 'notify', no exception was thrown.");
5252
break;
5353
case SEVERE:
5454
case THROW_ALL:

mathml-core/src/main/java/com/formulasearchengine/mathmltools/helper/CMMLHelper.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ private CMMLHelper() {
2424
}
2525

2626
/**
27-
* Get the first node of the MathML-Content annotations within a MathML document.
27+
* Get the first apply node of the MathML-Content annotations within a MathML document.
2828
*
2929
* @param mathml full MathML document
3030
* @return first node of the MathML-Content annotations within a MathML document
@@ -39,6 +39,22 @@ public static Node getFirstApplyNode(String mathml) {
3939
}
4040
}
4141

42+
/**
43+
* Get the first node of the MathML-Content annotations within a MathML document.
44+
*
45+
* @param mathml full MathML document
46+
* @return first node of the MathML-Content annotations within a MathML document
47+
*/
48+
public static Node getFirstNode(String mathml) {
49+
try {
50+
// get the apply node of the ContentMathML root
51+
return getFirstNode(new CMMLInfo(mathml));
52+
} catch (Exception e) {
53+
logger.error("failed to get apply node", e);
54+
return null;
55+
}
56+
}
57+
4258
/**
4359
* Get the first node of the MathML-Content annotations within a MathML document.
4460
* Before, the MathML document was converted to strict CMML and subsequently also converted
@@ -91,6 +107,31 @@ public static Node getFirstApplyNode(CMMLInfo cmmlInfo) throws XPathExpressionEx
91107
return applyRoot;
92108
}
93109

110+
111+
112+
/**
113+
* Get the root node from the content mathml.
114+
* It will search for the first node of the MathML-Content annotations
115+
* or the semantics/apply node within a CMMLInfo document.
116+
*
117+
* @param cmmlInfo CMMLInfo document
118+
* @return first node of the MathML-Content annotations within a MathML document
119+
* @throws XPathExpressionException parser exception
120+
*/
121+
public static Node getFirstNode(CMMLInfo cmmlInfo) throws XPathExpressionException {
122+
// 1. search for a separate cmml semantic
123+
XPath xpath = XMLHelper.namespaceAwareXpath("m", CMMLInfo.NS_MATHML);
124+
Node applyRoot = getElement(cmmlInfo, "m:math/m:semantics/m:annotation-xml[@encoding='MathML-Content']/*[1]", xpath);
125+
if (applyRoot == null) {
126+
// 2. search for a main cmml semantic
127+
applyRoot = getElement(cmmlInfo, "*//m:semantics/*[1]", xpath);
128+
if (applyRoot == null) {
129+
// 3. try to take the apply right beneath the math elements
130+
applyRoot = getElement(cmmlInfo, "m:math/*[1]", xpath);
131+
}
132+
}
133+
return applyRoot;
134+
}
94135
/**
95136
* Extracts a single node for the specified XPath expression.
96137
*

mathml-core/src/test/java/com/formulasearchengine/mathmltools/mml/CMMLInfoTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,4 +360,10 @@ public final void testDtQueryTest() throws Exception {
360360
assertEquals(s, mml.getXQueryString());
361361
}
362362

363-
}
363+
364+
@Test
365+
public void testAFormula() throws Exception {
366+
Node mainElement = new CMMLInfo(getFileContents(MML_TEST_DIR + "A.mml"));
367+
assertThat(mainElement, notNullValue());
368+
}
369+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<math xmlns="http://www.w3.org/1998/Math/MathML" alttext="A">
2+
<semantics>
3+
<ci id="p51.m5.1" xref="p51.m5.1.pmml">A</ci>
4+
<annotation-xml encoding="MathML-Presentation">
5+
<mi id="p51.m5.1.pmml" xref="p51.m5.1">A</mi>
6+
</annotation-xml>
7+
<annotation encoding="application/x-tex">A</annotation>
8+
</semantics>
9+
</math>

mathml-similarity/src/test/java/com/formulasearchengine/mathmltools/similarity/node/MathNodeGeneratorTest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,23 @@ public void generateMathNode_mathoid_strict() throws Exception {
3838
// mathml from mathoid and get the strict cmml semantics
3939
testConverterOnFile(CMMLHelper::getStrictCmml, "mathnode4", true);
4040
}
41+
@Test
42+
public void generateMathNodeMathoidStrictAny() throws Exception {
43+
// mathml from mathoid and get the strict cmml semantics
44+
testConverterOnFile(CMMLHelper::getFirstNode, "mathnode4", true);
45+
}
46+
47+
@Test
48+
public void generateMathNodeHyplag() throws Exception {
49+
// mathml from hyplag and get the strict cmml semantics
50+
testConverterOnFile(CMMLHelper::getFirstNode, "mathnode5", true);
51+
}
4152

4253
private void testConverterOnFile(Function<String, Node> convert, String basicFilename, boolean checkAbstract) throws Exception {
4354
String expected = IOUtils.toString(this.getClass().getResourceAsStream(basicFilename + "_expected.txt"), "UTF-8");
4455
String mathml = IOUtils.toString(this.getClass().getResourceAsStream(basicFilename + "_test.txt"), "UTF-8");
45-
MathNode mathNode = MathNodeGenerator.generateMathNode(convert.apply(mathml));
56+
final Node applyRoot = convert.apply(mathml);
57+
MathNode mathNode = MathNodeGenerator.generateMathNode(applyRoot);
4658

4759
if (checkAbstract) {
4860
mathNode = MathNodeGenerator.toAbstract(mathNode);
@@ -51,4 +63,4 @@ private void testConverterOnFile(Function<String, Node> convert, String basicFil
5163
assertThat(MathNodeGenerator.printMathNode(mathNode, ""), CoreMatchers.equalTo(expected));
5264
}
5365

54-
}
66+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ci:A
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<math xmlns="http://www.w3.org/1998/Math/MathML" xmlns:m="http://www.w3.org/1998/Math/MathML" alttext="A">
3+
<semantics>
4+
<ci id="p51.m5.1" xref="p51.m5.1.pmml">A</ci>
5+
<annotation-xml encoding="MathML-Presentation">
6+
<mi id="p51.m5.1.pmml" xref="p51.m5.1">A</mi>
7+
</annotation-xml>
8+
<annotation encoding="application/x-tex">A</annotation>
9+
</semantics>
10+
</math>

0 commit comments

Comments
 (0)