SPB Git

Repository Public Research preview

test_jats.py 3.0 KB 44 lines python

1from htr.parse.jats import parse_jats, section_role2from htr.segment.passages import SegmentConfig, passages_from_parsed34XML = b"""<?xml version="1.0"?>5<article article-type="research-article" xml:lang="en" xmlns:xlink="http://www.w3.org/1999/xlink">6<front><journal-meta><journal-title>Test Journal</journal-title><publisher><publisher-name>Pub</publisher-name></publisher></journal-meta>7<article-meta><article-id pub-id-type="doi">10.1/test</article-id><title-group><article-title>A title</article-title></title-group>8<contrib-group><contrib contrib-type="author"><name><surname>Doe</surname><given-names>Jane</given-names></name></contrib></contrib-group>9<pub-date pub-type="epub"><day>3</day><month>5</month><year>2016</year></pub-date><pub-date pub-type="ppub"><month>6</month><year>2016</year></pub-date>10<permissions><license xlink:href="http://creativecommons.org/licenses/by/4.0/"><license-p>CC BY</license-p></license></permissions>11<abstract><p>This is the abstract. It has two sentences about 42 mice.</p></abstract></article-meta></front>12<body><sec><title>Introduction</title><p>First paragraph with a citation <xref ref-type="bibr" rid="b1">1</xref> and another <xref ref-type="bibr" rid="b2">2</xref>. It also contains math <inline-formula><tex-math>$x^2$</tex-math></inline-formula> and a figure reference.</p><fig id="f1"><caption><p>Caption text here.</p></caption></fig>13<sec><title>2.1 Study design</title><p>Nested methods paragraph. Measured 3.5 mg per day.</p></sec></sec>14<sec><title>Acknowledgements</title><p>Thanks.</p></sec></body>15<back><ref-list><ref id="b1"><mixed-citation>Ref one</mixed-citation></ref></ref-list></back></article>"""161718def test_parse_basic():19    d = parse_jats(XML)20    assert d.title == "A title" and d.authors == ["Jane Doe"] and d.author_group == "doe_j"21    assert d.pub_date == "2016-06" and d.pub_year == 2016 and "ppub" in d.pub_date_evidence and d.pre2018_verified22    assert d.license_code == "CC-BY" and d.license_uri.endswith("by/4.0/")23    roles = [p.section_role for p in d.paragraphs]24    assert roles[0] == "abstract" and "introduction" in roles and "methods" in roles and "caption" in roles25    intro = next(p for p in d.paragraphs if p.section_role == "introduction")26    assert "[1]" in intro.text and "[2]" in intro.text and "$x^2$" in intro.text and "Caption text" not in intro.text27    assert all(d.body_text[p.char_start:p.char_end] == p.text for p in d.paragraphs)28    assert not any("Thanks" in p.text for p in d.paragraphs)29    assert "Ref one" not in d.body_text303132def test_section_roles():33    assert section_role("Introduction") == "introduction"34    assert section_role("IV. Results and Discussion") == "results"35    assert section_role("3.2 Empirical strategy") == "methods"36    assert section_role("Concluding remarks") == "conclusion"373839def test_passages_eligibility():40    d = parse_jats(XML)41    ps = passages_from_parsed("doc_x", d, SegmentConfig(min_words=5, min_sentences=1, min_quality=0.0, min_lang_prob=0.0))42    assert any(p.eligible for p in ps)43    assert not any(p.eligible for p in ps if p.section_role == "caption")44
Files main
Menu

Theme