Skip to content

Commit

Permalink
Extract operators for files without apply fields
Browse files Browse the repository at this point in the history
  • Loading branch information
physikerwelt committed Jan 8, 2019
1 parent fd738e3 commit e8df426
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void error(SAXParseException exception) throws SAXException {
LOG.debug(exception.getMessage() + " Error handler is on level 'silent' that ignores this error.");
return; // ignore
case NOTIFY:
LOG.error(exception.getMessage() + " Error handler is on level 'notify', no exception was thrown.");
LOG.warn(exception.getMessage() + " Error handler is on level 'notify', no exception was thrown.");
break;
case SEVERE:
case THROW_ALL:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private CMMLHelper() {
}

/**
* Get the first node of the MathML-Content annotations within a MathML document.
* Get the first apply node of the MathML-Content annotations within a MathML document.
*
* @param mathml full MathML document
* @return first node of the MathML-Content annotations within a MathML document
Expand All @@ -39,6 +39,22 @@ public static Node getFirstApplyNode(String mathml) {
}
}

/**
* Get the first node of the MathML-Content annotations within a MathML document.
*
* @param mathml full MathML document
* @return first node of the MathML-Content annotations within a MathML document
*/
public static Node getFirstNode(String mathml) {
try {
// get the apply node of the ContentMathML root
return getFirstNode(new CMMLInfo(mathml));
} catch (Exception e) {
logger.error("failed to get apply node", e);
return null;
}
}

/**
* Get the first node of the MathML-Content annotations within a MathML document.
* Before, the MathML document was converted to strict CMML and subsequently also converted
Expand Down Expand Up @@ -91,6 +107,31 @@ public static Node getFirstApplyNode(CMMLInfo cmmlInfo) throws XPathExpressionEx
return applyRoot;
}



/**
* Get the root node from the content mathml.
* It will search for the first node of the MathML-Content annotations
* or the semantics/apply node within a CMMLInfo document.
*
* @param cmmlInfo CMMLInfo document
* @return first node of the MathML-Content annotations within a MathML document
* @throws XPathExpressionException parser exception
*/
public static Node getFirstNode(CMMLInfo cmmlInfo) throws XPathExpressionException {
// 1. search for a separate cmml semantic
XPath xpath = XMLHelper.namespaceAwareXpath("m", CMMLInfo.NS_MATHML);
Node applyRoot = getElement(cmmlInfo, "m:math/m:semantics/m:annotation-xml[@encoding='MathML-Content']/*[1]", xpath);
if (applyRoot == null) {
// 2. search for a main cmml semantic
applyRoot = getElement(cmmlInfo, "*//m:semantics/*[1]", xpath);
if (applyRoot == null) {
// 3. try to take the apply right beneath the math elements
applyRoot = getElement(cmmlInfo, "m:math/*[1]", xpath);
}
}
return applyRoot;
}
/**
* Extracts a single node for the specified XPath expression.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,10 @@ public final void testDtQueryTest() throws Exception {
assertEquals(s, mml.getXQueryString());
}

}

@Test
public void testAFormula() throws Exception {
Node mainElement = new CMMLInfo(getFileContents(MML_TEST_DIR + "A.mml"));
assertThat(mainElement, notNullValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<math xmlns="http://www.w3.org/1998/Math/MathML" alttext="A">
<semantics>
<ci id="p51.m5.1" xref="p51.m5.1.pmml">A</ci>
<annotation-xml encoding="MathML-Presentation">
<mi id="p51.m5.1.pmml" xref="p51.m5.1">A</mi>
</annotation-xml>
<annotation encoding="application/x-tex">A</annotation>
</semantics>
</math>
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,23 @@ public void generateMathNode_mathoid_strict() throws Exception {
// mathml from mathoid and get the strict cmml semantics
testConverterOnFile(CMMLHelper::getStrictCmml, "mathnode4", true);
}
@Test
public void generateMathNodeMathoidStrictAny() throws Exception {
// mathml from mathoid and get the strict cmml semantics
testConverterOnFile(CMMLHelper::getFirstNode, "mathnode4", true);
}

@Test
public void generateMathNodeHyplag() throws Exception {
// mathml from hyplag and get the strict cmml semantics
testConverterOnFile(CMMLHelper::getFirstNode, "mathnode5", true);
}

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

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

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ci:A
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<math xmlns="http://www.w3.org/1998/Math/MathML" xmlns:m="http://www.w3.org/1998/Math/MathML" alttext="A">
<semantics>
<ci id="p51.m5.1" xref="p51.m5.1.pmml">A</ci>
<annotation-xml encoding="MathML-Presentation">
<mi id="p51.m5.1.pmml" xref="p51.m5.1">A</mi>
</annotation-xml>
<annotation encoding="application/x-tex">A</annotation>
</semantics>
</math>

0 comments on commit e8df426

Please sign in to comment.