Unstructuredunstructured 0.27.6 · Python 3.11+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
21 small wins to finish your pathNext lesson

Where hand-written parsing breaks

The splitter from lesson 1 knows one thing about one file. The handbook has six files and no two of them are written the same way.

The second document is the refund policy, and it is a web page rather than a markdown file.

html
<h1>Refunds</h1>
<p>Refunds go back to the card used for the order.</p>
<h2>How long it takes</h2>
<table>
  <tr><th>Payment method</th><th>Working days</th></tr>
  <tr><td>Card</td><td>5</td></tr>
  <tr><td>Bank transfer</td><td>8</td></tr>
</table>
<h2>What we cannot refund</h2>
<p>Gift cards and opened software are not refundable.</p>

The same splitter, the second file

Example
text = open("refunds.html").read()
for block in text.split("\n## "):
    first = block.strip().splitlines()[0]
    print(len(block), "|", first[:50])

One block, three hundred and forty characters long, starting with a tag. The file has two headings in it and the splitter found neither, because HTML does not mark a heading with hashes.

The fix is another rule, for HTML this time, and then a third rule for the CSV export, and a fourth for the email. Each one has to be written, tested and maintained, and each one only knows about the file it was written for.

And the third file

The open orders are a spreadsheet export. Four lines of comma separated values.

text
order,status,courier,days
A17,shipped,Bluedart,2
B02,packing,,0
C41,delivered,Delhivery,3
Example
text = open("orders.csv").read()
for block in text.split("\n## "):
    print(len(block), "|", block.strip().splitlines()[0])

One block again, and the heading it reports is the column names. Three files, three shapes, one rule that fits one of them.

What is actually needed

Every one of these files contains the same four kinds of thing: headings, paragraphs, lists and tables. The formats differ, the content does not. What is needed is one function that reads any of them and reports the same four kinds of thing, with a label on each.

That function is partition, and it arrives in lesson 3.

Try it yourself
  • Run the lesson 1 splitter over ravi.eml, the customer email, and see what it reports.
  • Write the HTML rule yourself with text.split("<h2>"). Count how many lines it takes to also strip the closing tags.
  • Count how many rules you would need for a folder with ten formats in it.

You understood something today that you didn't yesterday.