xytpai's picture
Expand books to full six-source corpus with programmatic Markdown, audited JSONL and preserved formulas
0b8a041 verified
Raw History Blame Contribute Delete
2.04 kB
from format_markdown import transform
cases=[
("chapter","CHAPTER IV\n\nThe story begins.","## CHAPTER IV"),
("named","Preface\n\nSome opening prose.","## Preface"),
("html_heading","<h2>Methods</h2>\n<p>A plain paragraph.</p>","## Methods"),
("list","• First item\n• Second item","- First item"),
("html_list","<ol><li>First</li><li>Second</li></ol>","1. First"),
("table","<table><tr><th>Time</th><th>Value</th></tr><tr><td>1</td><td>3</td></tr></table>","| Time | Value |"),
("pre","<pre>if x &lt; y:\n print(x)</pre>","if x < y:"),
]
for name,text,expect in cases:
out,edits=transform(text)
assert expect in out,(name,out)
print(name,edits)
protected=[
"```python\nCHAPTER IV\n\n<p>literal</p>\n```\n",
"$$\nx = 3 \\\\\ny=2\n$$\n",
" <p>code literal</p>\n",
"<table><tr><td colspan='2'>Do not infer</td></tr></table>",
"An inline ` <b>code</b> ` example.",
]
for text in protected:
out,_=transform(text)
assert out==text,(text,out)
poetry="The rivers flow beneath the moon,\nAnd stars shall sing a silver tune.\n"
assert transform(poetry,"Poems")[0]==poetry
print('protected code/math/complex-table/poetry: OK')
# Typical wrapped prose, containing real sentences, must preserve all tokens.
import textwrap
paragraph=("This is an ordinary prose paragraph with a sequence of clear explanatory statements. "
"The following words continue the explanation without introducing any code or mathematical notation. ")*8
wrapped=textwrap.fill(paragraph,72)
out,e=transform(wrapped,"An ordinary book")
assert out.split()==wrapped.split()
assert e.get('prose_hardwraps_joined',0)>0
print('prose whitespace-only join',e)
# Regression: do not render instructional HTML or promote references to headings.
for raw in ['Appendix B shows an equation of the form \\(x=1\\) can be rearranged:', 'Here is an HTML example:\n<h1>Heading</h1>\n<p>Example</p>', '| unordered list | <ul><li>item 1</li><li>item 2</li></ul> |', '<!DOCTYPE html> <html><body><p>Example</p></body></html>']:
assert transform(raw)[0]==raw