10 分钟打造文本搜索引擎,附详细教程

超越传统基于关键词的搜索,提高搜索相关性。

科普:什么是神经搜索 (Neural Search)

神经搜索 (Neural Search) 是指利用深度神经网络,搜索图像、视频、文本等各种非结构化数据。与传统基于文本标签的搜索相比,神经搜索更加全面和有针对性。

教程:快速创建文本搜索引擎

目的:创建一个文本数据的神经搜索应用。

原理:输入查询句子,与数据集中的句子进行匹配并输出匹配结果。

DocArray 参考文档:

https://docarray.jina.ai/icon-default.png?t=M4ADhttps://docarray.jina.ai/

数据集 (Pride & Prejudice e-book) 下载:

https://www.gutenberg.org/files/1342/1342-0.txticon-default.png?t=M4ADhttps://www.gutenberg.org/files/1342/1342-0.txt

安装依赖

从 PyPI 安装 DocArray,方法如下:

1. 通过 Pip 安装: pip install docarray 

2. 通过 conda 安装: conda install -c conda-forge docarray 

代码详解

第一步:从 URL 加载数据集,将其转换为文本,并放入 Document (Jina 中一个基础的数据类型)。

from docarray import Document, DocumentArraydoc = Document(uri="https://www.gutenberg.org/files/1342/1342-0.txt").load_uri_to_text()

第二步:由于数据集 Pride & Prejudice e-book 是一系列长句子,我们需要先将其进行分词,再放到 DocumentArray 中。

每重起一行,就用 ‘\n’ 来分割句子。最终这个句子将以 Document 的形式,存储在 DocumentArray 中。

第三步:特征向量化(将特征转换为向量索引)。这里的特征就是 DocumentArray 中每个 Document 的向量。

特征向量化的实现方法众多,这里推荐使用特征哈希 (feature hashing) 方法,因为它运行更迅速、占用空间更少。

特征哈希的工作原理,是获取特征并应用一个哈希函数,该函数可以对值 (value) 进行散列,并将其作为索引返回。

DocArray 极大简化了这个过程:

# break large text into smaller chunksdocs = DocumentArray(Document(text = s.strip()) for s in doc.text.split('\n') if s.strip())
# apply feature hashing to embed the DocumentArraydocs.apply(lambda doc: doc.embed_feature_hashing())
# query sentence query = (Document(text="she entered the room").embed_feature_hashing().match(docs, limit=5, exclude_self=True, metric="jaccard", use_scipy=True))
# print the resultsprint(query.matches[:, ('text', 'scores__jaccard')])

第四步:获取输出。将查询句子转换为  Document ,并对其进行向量化,然后与 DocumentArray 中 Document 的向量进行匹配。

输入《傲慢与偏见》中句子「she entered the room」,查询结果如下:

 以上就是创建文本搜索引擎的完整过程,查看 Colab 请访问链接:

https://colab.research.google.com/github/jina-ai/tutorial-notebooks/blob/main/neural_text_search.ipynb#scrollTo=4glBnUHBiAwpicon-default.png?t=M4ADhttps://colab.research.google.com/github/jina-ai/tutorial-notebooks/blob/main/neural_text_search.ipynb#scrollTo=4glBnUHBiAwp

期待你能用 Jina 全家桶产品,创建更多有意思的 demo~

参考资料:

https://docarray.jina.ai

https://github.com/jina-ai/docarray

https://docs.jina.ai

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值