Repository navigation
Closed
Description
Describe the bug
error during parsing html doc, exist duplicate string in output.
the root case in _unfurl_break_tags() function.
maybe the following code is not necessary
if child.text:
_tag_elem = etree.Element(child.tag)
_tag_elem.text = child.text
unfurled.append(_tag_elem)source code:
def _unfurl_break_tags(tag_elem: etree._Element) -> List[etree._Element]:
"""Sequence of `tag_elem` and its children with `<br>` elements removed.
NOTE that these are "loose" `etree._Element` instances that are NOT linked to the original HTML
element-tree, so methods like `.getchildren()`, `.find()` etc. will happily produce empty
results.
"""
unfurled: List[etree._Element] = []
if tag_elem.text:
_tag_elem = etree.Element(tag_elem.tag)
_tag_elem.text = tag_elem.text
unfurled.append(_tag_elem)
children = tag_elem.getchildren()
for child in children:
if not _has_break_tags(child):
unfurled.append(child)
else:
if child.text:
_tag_elem = etree.Element(child.tag)
_tag_elem.text = child.text
unfurled.append(_tag_elem)
unfurled.extend(_unfurl_break_tags(child))
return unfurledTo Reproduce
b.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p>
aaaa
<br>
<span>bbbb<br>ccc</span>
<br>
eee
</p>
</body>
</html>doc = HTMLDocument.from_file('b.html')
pages =doc._parse_pages_from_element_tree()
print([e.text for e in pages[0].elements])Expected behavior
the output string should be ['aaaa', 'bbbb', 'ccc', 'eee'], but now the output is ['aaaa', 'bbbb', 'bbbb', 'ccc', 'eee']
Screenshots
none
Environment Info
OS version: macOS-14.1.2-arm64-arm-64bit
Python version: 3.10.13
unstructured version: 0.11.6
unstructured-inference is not installed
pytesseract is not installed
Torch is not installed
Detectron2 is not installed
PaddleOCR is not installed
Libmagic version: file-5.41
Additional context
None
Activity
scanny commentedon Jun 9, 2024
@FounderHy thanks for reporting this. I've dug into this and now have what I think is a clear understanding of the problem.
@MthwRobinson The core of the situation is that in
<span>aaaa<br/>bbbb</span>, "bbbb" is the tail of the<br/>element and_unfurl_break_tags()is not considering the tail. When it removes the<br/>it also removes its tail. So this produces["aaaa"]instead of["aaaa", "bbbb"]. It gets worse with multiple<br/>elements as in<p>aaaa<br/>bbbb<br/>cccc<br/>dddd</p>all of "bbbb", "cccc", and "dddd" are dropped.Then there's also the problem of repeating text in certain cases but I want to ignore that for now because I think it's going to resolve itself here in the approach I propose.
There are a couple possible approaches to fix this problem. The one I prefer is simply replacing
<br/>elements with"\n"and then parsing normally. So essentially avoiding the problem rather than designing a better implementation of it.In this scenario, the HTML:
would be parsed to:
"aaaa\nbbbb\ncccc\ndddd\neeee"One of the things I like about this appoach is that the integrity of the paragraph (
<p>) is preserved, and it would become a singleElementobject rather than a fragmented one where some fragments might be misclassified asTitleelements or plainTextelements instead ofNarrativeText.Also, the integrity of the
<span>element (or<b>or<i>, etc.) is preserved which will produce better emphasis metadata.What do you think of that approach? As a principle I'm thinking that the safest assumption is that
<br/>is not intended to start a new paragraph, merely to insert a line break in an otherwise coherent block item for formatting purposes.Another alternative would be to replace
<br/>with a single space. I think that would produce better results in some cases, depending on how\nis likely to be interpreted downstream.MthwRobinson commentedon Jun 10, 2024
@scanny - Is there a scenario where think single space would be better than
\n? My initial instinct is to go with "replace with\n" since that better reflects how<br/>is displayed. I could see maybe wanting to keep together a sentence that has a<br/>in the middle of it for whatever reason. Break lines in the middle of sentences is common in.txtfiles, so behavior would be similar to that.scanny commentedon Jun 10, 2024
@MthwRobinson There are a few considerations I think that decision bears on:
Should
<br/>be interpreted to indicate a paragraph boundary (and so anElementboundary)?It sounds like we're agreed the answer is "no", so we should change that behavior. I think that's clearly the right answer by the way. A
<br/>element is phrasing content in HTML parlance, so affects how the text is displayed within a paragraph (or other block-item) but does not produce a new block item with inter-paragraph spacing etc. added.Should whitespace used to format the source HTML be included in the
Elementobjects formed from that HTML?This is the newlines, indentation spaces, and extra inter-word spaces in
Element.textlike:"\n foo bar\n ".Because this whitespace does not appear when the HTML is rendered, I think the answer is clearly "no". I would state the underlying principle as:
Instead I would expect that text to appear in the
Elementas:"foo bar", just as it does in the browser. The whitespace used to format the HTML "code" is accidental and not part of the document (evidenced by changes made to that whitespace not changing what appears in the browser).How should a
<br/>element be translated to text within theElementit becomes "part" of?I think it's reasonable to prefer it appears as a newline (
"\n"). That's consistent with how it would be rendered by a browser.The challenge is distinguishing such a newline from the "accidental" whitespace described in point 2 above.
The simple approach is to replace all
<br/>elements with"\n"or" "globally before processing the document. This fixes the problem in point 1 very efficiently. However, whichever character it is replaced with becomes a single space in theElementas a result of whitespace normalization.We could probably remedy that by postponing replacement until the last possible moment, like when processing the text of a
<p>element. It would be substantially more complicated, but I'm sure we could manage it at the cost of somewhat increased code fragility and maintenance cost in that vicinity.So I think the question comes down to "how bad do we want it", probably based on some assessment of "will it make any downstream difference"? If we think the answer to the latter is "probably none at all" I'd be inclined to go with the simple-to-implement-and-maintain approach.
One other thing to consider is I believe we'll find there are less-than-carefully placed extra newlines in the text produced by other partitioners (for varying reasons), so I'm not sure a downstream processor would be well-advised to assign a distinction to a newline over any other whitespace character.
rfctr(html): replace html parser (#3218)