package com.formulasearchengine.mathosphere.mlp.text; import com.formulasearchengine.mathosphere.mlp.contracts.TextExtractorMapper; import org.apache.commons.io.IOUtils; import org.junit.Ignore; import org.junit.Test; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.iterableWithSize; /** * Created by Moritz on 15.12.2015. */ public class MathConverterTest { @Test public void testGo() throws Exception { String wikiText = IOUtils.toString(getClass().getResourceAsStream("legendre_wiki.txt")); final MathConverter mathConverter = new MathConverter(wikiText); final String real = mathConverter.getStrippedOutput(); assertThat(real, containsString("Let FORMULA_")); } @Test public void testGo2() throws Exception { String wikiText = IOUtils.toString(getClass().getResourceAsStream("../performance/hamiltonian_wiki.txt")); final MathConverter mathConverter = new MathConverter(wikiText); final String real = mathConverter.getOutput(); assertThat(real, containsString("denoted by <math>H</math> , also \"Ȟ\" or <math>\\hat{H}</math> . ")); } @Test public void testGo3() throws Exception { String wikiText = "Let '''<math> \\Omega </math>''' be an [[open subset]] of ℝ''<sup>n</sup>''. A function '''<math> u </math>''' belonging to '''[[Lp space|<math>L^1(\\Omega)</math>]]''' is said of '''bounded variation''' ('''BV function'''), and written"; final MathConverter mathConverter = new MathConverter(wikiText); final String real = mathConverter.getOutput(); assertThat(real, containsString("<math>L^1(\\Omega)</math>")); } @Test public void testGo4() throws Exception { String wikiText = "{| style="margin: 1em auto 1em auto;"\n" + "|-\n" + "| style="width:350px" |\n" + "<!--START-->{| style="text-align: center; border: 1px solid darkgray; width:300px"\n" + "|-\n" + "|colspan="2"|<span style="color:darkgray;">No column/row is empty:</span>\n" + "|- style="vertical-align:top;"\n" + "|[[File:Predicate logic; 2 variables; example matrix a2e1.svg|thumb|center|120px|1. <math>\\forall x \\exist y Lyx</math>:<br>Everyone is loved by someone.]]\n" + "|[[File:Predicate logic; 2 variables; example matrix a1e2.svg|thumb|center|120px|2. <math>\\forall x \\exist y Lxy</math>:<br>Everyone loves someone.]]\n" + "|}<!--END-->\n" + "| rowspan="2" style="width:210px" |\n" + "<!--START-->{| style="text-align: center; border: 1px solid darkgray; width:160px"\n" + "|-\n" + "|colspan="2"|<span style="color:darkgray;">The diagonal is<br>nonempty/full:</span>\n" + "|- style="vertical-align:top;"\n" + "|[[File:Predicate logic; 2 variables; example matrix e(12).svg|thumb|center|120px|5. <math>\\exist x Lxx</math>:<br>Someone loves himself.]]\n" + "|- style="vertical-align:top;"\n" + "|[[File:Predicate logic; 2 variables; example matrix a(12).svg|thumb|center|120px|6. <math>\\forall x Lxx</math>:<br>Everyone loves himself.]]\n" + "|}<!--END-->\n" + "| rowspan="2" style="width:250px" |\n" + "<!--START-->{| style="text-align: center; border: 1px solid darkgray; width:160px"\n" + "|-\n" + "|colspan="2"|<span style="color:darkgray;">The matrix is<br>nonempty/full:</span>\n" + "|- style="vertical-align:top;"\n" + "|[[File:Predicate logic; 2 variables; example matrix e12.svg|thumb|center|120px|7. <math>\\exist x \\exist y Lxy</math>:<br>Someone loves someone.<br><br>8. <math>\\exist x \\exist y Lyx</math>:<br>Someone is loved by someone.]]\n" + "|- style="vertical-align:top;"\n" + "|[[File:Predicate logic; 2 variables; example matrix a12.svg|thumb|center|120px|9. <math>\\forall x \\forall y Lxy</math>:<br>Everyone loves everyone.<br><br>10. <math>\\forall x \\forall y Lyx</math>:<br>Everyone is loved by everyone.]]\n" + "|}<!--END-->\n" + "|rowspan="2"|[[File:Predicate logic; 2 variables; implications.svg|thumb|250px|right|[[Hasse diagram]] of the implications]]\n" + "|-\n" + "|\n" + "<!--START-->{| style="text-align: center; border: 1px solid darkgray; width:300px"\n" + "|-\n" + "|colspan="2"|<span style="color:darkgray;">One row/column is full:</span>\n" + "|- style="vertical-align:top;"\n" + "|[[File:Predicate logic; 2 variables; example matrix e1a2.svg|thumb|center|120px|3. <math>\\exist x \\forall y Lxy</math>:<br>Someone loves everyone.]]\n" + "|[[File:Predicate logic; 2 variables; example matrix e2a1.svg|thumb|center|120px|4. <math>\\exist x \\forall y Lyx</math>:<br>Someone is loved by everyone.]]\n" + "|}<!--END-->\n" + "|}"; wikiText = TextExtractorMapper.unescape(wikiText); final MathConverter mathConverter = new MathConverter(wikiText); final String real = mathConverter.getOutput(); //assertThat(real, containsString ("<math>L^1(\\Omega)</math>")); } @Test public void testGo5() throws Exception { String wikiText = "{{mvar|φ}}"; wikiText = TextExtractorMapper.unescape(wikiText); final MathConverter mathConverter = new MathConverter(wikiText); final String real = mathConverter.getOutput(); assertThat(real, containsString("<math>\\varphi</math>")); } @Test public void testGo6() throws Exception { String wikiText = "{{math|10/19 &middot; 7000 &minus; 18/38 &middot; 7000 {{=}} 368.42}}"; wikiText = TextExtractorMapper.unescape(wikiText); final MathConverter mathConverter = new MathConverter(wikiText); final String real = mathConverter.getOutput(); assertThat(real, containsString("<math>10/19 {\\cdot} 7000 - 18/38 {\\cdot} 7000</math>")); } @Test public void testGo7() throws Exception { String wikiText = "{{NumBlk|::|Input &nbsp; <math>\\int_{-\\infty}^{\\infty} c_{\\omega}\\,x_{\\omega}(t) \\, \\operatorname{d}\\omega</math> &nbsp; produces output &nbsp; <math>\\int_{-\\infty}^{\\infty} c_{\\omega}\\,y_{\\omega}(t) \\, \\operatorname{d}\\omega\\,</math>|{{EquationRef|Eq.1}}}}\n"; wikiText = TextExtractorMapper.unescape(wikiText); final MathConverter mathConverter = new MathConverter(wikiText); final String real = mathConverter.getOutput(); assertThat(real, containsString("<math>\\int_{-\\infty}^{\\infty}")); } @Test public void testGo8() throws Exception { String wikiText = "<ref><math>\\beta= \\alpha/3</math> where <math>\\alpha</math> is the\n" + "quantity used by Deshpande–Fleck</ref>"; wikiText = TextExtractorMapper.unescape(wikiText); final MathConverter mathConverter = new MathConverter(wikiText); final String real = mathConverter.getOutput(); assertThat(real, containsString("<math>\\beta= \\alpha/3")); } @Test public void testGo9() throws Exception { String wikiText = "Word\n<math>x</math>\nend."; //wikiText = TextExtractorMapper.unescape(wikiText); final MathConverter mathConverter = new MathConverter(wikiText); final String real = mathConverter.getOutput(); assertThat(real, containsString("Word <math>x</math> end.")); } @Test public void testGo10() throws Exception { String wikiText = "''a''<sub>x</sub>."; //wikiText = TextExtractorMapper.unescape(wikiText); final MathConverter mathConverter = new MathConverter(wikiText); final String real = mathConverter.getOutput(); assertThat(real, containsString("<math>a</math> x")); } @Test public void testGo11() throws Exception { String wikiText = "Let the coin tosses be represented by a sequence {{nowrap|1=''X''<sub>0</sub>, ''X''<sub>1</sub>, &hellip;}}"; wikiText = TextExtractorMapper.unescape(wikiText); final MathConverter mathConverter = new MathConverter(wikiText); final String real = mathConverter.getOutput(); assertThat(real, containsString("<math>\\mathit{X}_{0}")); } @Test public void findFormulaFromWikiText() throws Exception { String text = WikiTextUtilsTest.getTestResource("com/formulasearchengine/mathosphere/mlp/gold/eval_dataset_sample.xml"); text = TextExtractorMapper.unescape(text); final MathConverter mathConverter = new MathConverter(text); final String real = mathConverter.getStrippedOutput(); System.out.println(real); } @Test public void testWiki2Tex() throws Exception { String text = "<sub>a</sub>, <sup>b</sup>, '''c''', ''d''"; final MathConverter mathConverter = new MathConverter(text); final String real = mathConverter.wiki2Tex(text); assertThat(real, containsString("_{a}")); assertThat(real, containsString("^{b}")); assertThat(real, containsString("\\mathbf{c}")); assertThat(real, containsString("\\mathit{d}")); assertThat(real, equalTo("_{a}, ^{b}, \\mathbf{c}, \\mathit{d}")); } }