Bug描述
当使用UnstructuredElementNodeParser解析带有表格的html文档时,如果表格的行由于使用了colspan而具有不同数量的列,这些表格将被忽略。
我追踪到了UnstructuredElementNodeParser调用了llama-index-core/llama_index/core/node_parser/relational/utils.py函数html_to_df()。在这个函数中,它故意忽略了每行具有不同列的表格:
第52行:# Check if the all rows have the same number of columns if not all(len(row) == len(data[0]) for row in data): return None
为什么它关心表格的行是否具有相同数量的列?
版本
0.10.54
重现步骤
解析一个带有表格的html文档,该表格的一些行中的单元格在不同的行上使用了colspan:
| data1 | data2 |
| merged data |
相关日志/回溯
- 无响应*
1条答案
按热度按时间3hvapo4f1#
嘿@bhoppeadoy!我在这里帮助你解决任何bug、问题或贡献。让我们一起解决这个问题!
在
llama-index-core/llama_index/core/node_parser/relational/utils.py
中,html_to_df()
函数检查表格的所有行是否具有相同的列数,以确保创建的DataFrame的完整性和一致性。如果行具有不同的列数,这表明表格结构不规则或格式错误,这可能导致在转换为DataFrame 1(https://github.com/run-llama/llama_index/blob/main/llama-index-core/llama_index/core/node_parser/relational/utils.py)时出现错误或不正确的数据表示。目前,
html_to_df()
函数不处理HTML表格中的colspan
和rowspan
属性。它只是提取每个<td>
元素的文本内容,并假设所有行具有相同的列数,而不考虑这些属性2(https://github.com/run-llama/llama_index/blob/main/llama-index-legacy/llama_index/legacy/node_parser/relational/unstructured_element.py)。已经存在针对
html_to_df()
函数的测试,涵盖了具有不规则结构的表格,例如具有colspan
或rowspan
3(https://github.com/run-llama/llama_index/blob/main/llama-index-core/tests/node_parser/test_unstructured.py)的表格。要处理每行具有不同列数的表格,可以使用
UnstructuredElementNodeParser
类。此类处理每个元素,检查它是否是HTML表格,然后将其转换为DataFrame,这本质上支持每行具有不同列数的表格4(https://github.com/run-llama/llama_index/blob/main/llama-index-core/llama_index/core/node_parser/relational/unstructured_element.py)。如果你需要处理
colspan
和rowspan
属性,你可能需要在html_to_df()
函数中实现额外的逻辑,或者在LlamaIndex代码库内使用可以处理此类表格的其他方法。