unstructured icon indicating copy to clipboard operation
unstructured copied to clipboard

bug/Empty table content should be a valid html str in metadata.text_as_html

Open dgunning opened this issue 3 years ago • 4 comments

Describe the bug When we fixed partition_html fails when parsing html with tables with no tbody we set the element.metadata.text_as_html to be ''.

However the empty string cannot be parsed down stream, which mean we have a dead table element among the other elements, requiring special error handling.

Basically, if unstructured parses a html element into a TableElement it should deliver a TableElement with a valid html text .. otherwise downstream client will have to do more exception handling

Potential solution: Instead of using '' for an empty table

use <table><tr><td> </td></tr></table>

This is a valid html table, and pandas read_html() correctly parses it as an empty list of tables. pd.read_html(table_html) == []

However note that this works when there is a space between the td tags

Additionally there is a deprecation warning introduced by the fix for Bug 1807 unstructured\documents\html.py:498: FutureWarning: The behavior of this method will change in future versions. Use specific 'len(elem)' or 'elem is not None' test instead.

To Reproduce

    table_html = """
    <table>
      <thead>
        <tr><th>Header 1</th><th>Header 2</th></tr>
        </thead>
    </table>
    """
    elements = partition_html(text=table_html)
    tables = pd.read_html(elements[0].metadata.text_as_html)
This produces lxml.etree.ParserError: Document is empty

Expected behavior Code to parse metadata.text_as_html should not fail

Screenshots If applicable, add screenshots to help explain your problem.

Environment Info Environment Info OS version: macOS-13.3.1-x86_64-i386-64bit Python version: 3.10.10 unstructured version: None unstructured-inference version: 0.7.7 pytesseract version: 0.3.10 Torch version: 2.1.0 Detectron2 is not installed

Additional context Add any other context about the problem here.

dgunning avatar Oct 27 '23 21:10 dgunning

@dgunning I noticed and fixed the lxml warning last week as it happens, so that's a small bit down :)

I'll take a look at the rest of it here ...

scanny avatar Nov 15 '23 23:11 scanny

Okay, a couple things:

  • In general, partitioners do not produce elements for "empty" things. Like there are plenty of empty paragraphs in the typical Word document because folks use those for visual spacing between paragraphs, but none of those end up as elements. So if a table truly is "empty", I'm inclined to believe the solution is to not emit an element for it at all.
  • That said, it's not clear to me why would we not consider the <th>Header 1</th>... elements to be content. Those are rendered as column headings by a browser and are important to make sense of the table (by humans at least). So I'm thinking even if we do not emit an element for an empty table, we would emit one for this table.

So I'm inclined to think there are actually two mis-behaviors here:

  1. partition_html() strips column-headings from tables, and
  2. partition_html() emits empty (and therefore unparseable) HTMLTable elements.

What's your take on that?

scanny avatar Nov 16 '23 00:11 scanny

I was curious as to why you strip column headers. In general you shouldn't .. but maybe there's another reason why that decision was taken in the first place. If you do start parsing column headers that would affect all tables .. but would that causes issues? I don't know. For SEC filing Form 3's it's slightly better to show the headers of an empty table. For my library edgartools it's better to show the table headers for non empty tables as well. So I'd lean towards parsing the headers so long as there's no adverse downstream impact. The other solution I added with the empty TD was just so an empty table wouldn't break downstream table recreation

dgunning avatar Nov 16 '23 01:11 dgunning

Thanks @dgunning :) I'm working on the fixes now.

scanny avatar Nov 17 '23 00:11 scanny