TF-IDF

TF-IDFopen in new window表示词频乘以逆文档频率。

本文介绍了如何使用一个在底层使用TF-IDFopen in new window的检索器,使用scikit-learn包。

有关TF-IDF的详细信息,请参阅这篇博文open in new window

# !pip install scikit-learn
from langchain.retrievers import TFIDFRetriever

使用文本创建新的检索器

retriever = TFIDFRetriever.from_texts(["foo", "bar", "world", "hello", "foo bar"])

使用文档创建新的检索器

现在,您可以使用您创建的文档来创建一个新的检索器。

from langchain.schema import Document
retriever = TFIDFRetriever.from_documents([Document(page_content="foo"), Document(page_content="bar"), Document(page_content="world"), Document(page_content="hello"), Document(page_content="foo bar")])

使用检索器

现在我们可以使用检索器了!

result = retriever.get_relevant_documents("foo")
result
[Document(page_content='foo', metadata={}),
 Document(page_content='foo bar', metadata={}),
 Document(page_content='hello', metadata={}),
 Document(page_content='world', metadata={})]
Last Updated:
Contributors: 刘强