Bug描述
BM25Retriever无法在中文上工作。
版本
主版本
重现步骤
from llama_index.retrievers.bm25 import BM25Retriever
from llama_index.core import Document
from llama_index.core.node_parser import SentenceSplitter
from llama_index.core.response.notebook_utils import display_source_node
documents = [Document(text="床前明月光"),
Document(text="疑是地上霜"),
Document(text="举头望明月"),
Document(text="低头思故乡")]
splitter = SentenceSplitter(chunk_size=1024)
nodes = splitter.get_nodes_from_documents(documents)
retriever = BM25Retriever.from_defaults(nodes=nodes, similarity_top_k=2)
nodes = retriever.retrieve("故乡")
for node in nodes:
display_source_node(node)
相关日志/回溯
output:
Node ID: d3d59a82-e489-47da-8229-a430c7f58c00 Similarity: 0.0 Text: 床前明月光
Node ID: 6105e407-408e-48a5-af44-a82515babb56 Similarity: 0.0 Text: 疑是地上霜
Node ID: fd059a68-d496-4a61-847f-c96e108a69b2 Similarity: 0.0 Text: 举头望明月
Node ID: f19761bc-d381-4679-bc77-f32cbad57db5 Similarity: 0.0 Text: 低头思故乡
9条答案
按热度按时间chhqkbe11#
你好!我是Dosu。作为一个机器人,我可以帮你解决bug,回答问题,还可以帮你成为贡献者。在等待人类维护者的时候,我会全程帮助你。
要解决
BM25Retriever
未正确处理中文文本的问题,你需要修改分词器以适当处理中文文本。当前的分词器是针对英文文本设计的,不适合中文。以下是如何修改分词器以处理中文文本的方法:
jieba
库,这是一个流行的中文文本分割库:BM25Retriever
时使用这个新的分词器:通过使用
chinese_tokenizer
,BM25Retriever
将能够正确处理中文文本并计算有意义的相似度分数1(https://github.com/run-llama/llama_index/blob/main/llama-index-legacy/llama_index/legacy/retrievers/bm25_retriever.py)2(https://github.com/run-llama/llama_index/blob/main/llama-index-integrations/retrievers/llama-index-retrievers-bm25/llama_index/retrievers/bm25/base.py)。xmjla07d2#
我认为应该在文档中提到非英文的分词器选项。
egdjgwm83#
你认为有必要添加以下代码吗,它可以有效地实现一个能够分割中英文关键词的分词器:
也许这个分词器可以取代BM25Retriever的默认分词器:
tokenize_remove_stopwords
px9o7tmv4#
关于您的情况,我不确定,不同语言的分词方式可能会有所不同。但是,就我个人而言,分词器函数本身很简单:
因此,您可以根据所使用的编程语言实现任何需要/想要的处理过程,例如分词、词干提取或词形还原,以及去除停用词等。
muk1a3rh5#
在版本0.10.57中,分词器选项被标记为已弃用,建议使用词干提取器代替。我如何使用词干提取器处理中文?
zengzsys6#
这是来自QQ邮箱的假期自动回复邮件。
您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。
pvabu6sv7#
在0.10.57版本中,分词器选项被标记为已弃用,建议使用词干提取器代替。我该如何使用词干提取器处理中文?
这引起了我的兴趣。我检查了bm25s和一个新的BM25Retriever。我的当前理解如下:
我的总结:
此外,关于词干提取器。
bm25s使用pystemmer作为可选组件,并且它不支持日语。在日语中,词干提取过于简单,我们通常使用归一化(更像是词形还原而不是词干提取),它与分词紧密相关。因此,如果我实现的话,我会在分词器的内部实现某种形式的词干提取、归一化或词形还原,而不是使用词干提取器选项。
所以目前为止,我会使用旧版本的BM25Retriever并传递我的分词器。
j1dl9f468#
我认为BM25Retriever和BM25SRetriever应该是不同的,因为它们的实现在分词方面非常不同,尤其是在日本。
qvtsj1bj9#
感谢您的建议。是的,我同意您的看法,BM25Retriever和BM25SRetriever应该是不同的东西。最后,我终于用bm25s为自己制作了一个中文版的BM25Retriver。
如果有人需要的话,这是我的POC代码: