| 在Relevance Slack上讨论
SearchArray将Pandas字符串列转换为词项索引。它允许对短语和单个词元进行高效的BM25 / TFIDF评分。
可以将其视为Pandas列形式的Lucene。
from searcharray import SearchArray import pandas as pd df['title_indexed'] = SearchArray.index(df['title']) np.sort(df['title_indexed'].array.score(['cat', 'in', 'the', 'hat'])) # 使用短语搜索 > BM25分数: > array([ 0. , 0. , 0. , ..., 15.84568033, 15.84568033, 15.84568033])
SearchArray在以下笔记本中有详细文档:
SearchArray指南 | SearchArray离线实验 | 关于内部原理
pip install searcharray
def tokenize(value: str) -> List[str]
)函数data_dir
进行内存映射简化Python数据栈中的词法搜索。
许多机器学习/人工智能从业者倾向于使用向量搜索解决方案,然后意识到他们需要加入一定程度的BM25 / 词法搜索。让我们让传统的全文搜索表现得像数据栈的其他部分一样。
SearchArray创建了一种以Pandas为中心的方式,将搜索索引作为Pandas数组的一部分来创建和使用。从某种意义上说,它在Pandas中构建了一个搜索引擎 - 允许任何人在没有外部系统的情况下原型化想法并执行重新排序。
你可以在这个colab笔记本中看到一个完整的端到端搜索相关性实验。
例如,让我们取一个包含大量文本(如电影标题和概述)的数据框:
In[1]: df = pd.DataFrame({'title': titles, 'overview': overviews}, index=ids)
Out[1]:
title overview
374430 Black Mirror: White Christmas This feature-length special consists of three ...
19404 The Brave-Hearted Will Take the Bride Raj is a rich, carefree, happy-go-lucky second...
278 The Shawshank Redemption Framed in the 1940s for the double murder of h...
372058 Your Name. High schoolers Mitsuha and Taki are complete s...
238 The Godfather Spanning the years 1945 to 1955, a chronicle o...
... ... ...
65513 They Came Back The lives of the residents of a small French t...
65515 The Eleventh Hour An ex-Navy SEAL, Michael Adams, (Matthew Reese...
65521 Pyaar Ka Punchnama Outspoken and overly critical Nishant Agarwal ...
32767 Romero Romero is a compelling and deeply moving look ...
索引文本:
In[2]: df['title_indexed'] = SearchArray.index(df['title'])
df
Out[2]:
title overview title_indexed
374430 Black Mirror: White Christmas This feature-length special consists of three ... Terms({'Black', 'Mirror:', 'White'...
19404 The Brave-Hearted Will Take the Bride Raj is a rich, carefree, happy-go-lucky second... Terms({'The', 'Brave-Hearted', 'Wi...
278 The Shawshank Redemption Framed in the 1940s for the double murder of h... Terms({'The', 'Shawshank', 'Redemp...
372058 Your Name. High schoolers Mitsuha and Taki are complete s... Terms({'Your', 'Name.'}, {'Your': ...
238 The Godfather Spanning the years 1945 to 1955, a chronicle o... Terms({'The', 'Godfather'}, {'The'...
... ... ... ...
65513 They Came Back The lives of the residents of a small French t... Terms({'Back', 'They', 'Came'},...
65515 The Eleventh Hour An ex-Navy SEAL, Michael Adams, (Matthew Reese... Terms({'The', 'Hour', 'Eleventh': ...
65521 Pyaar Ka Punchnama Outspoken and overly critical Nishant Agarwal ... Terms({'Ka', 'Pyaar', 'Punchnama':...
32767 Romero Romero is a compelling and deeply moving look ... Terms({'Romero'})
65534 Poison Paul Braconnier and his wife Blandine only hav... Terms({'Poison'})```
(注意这里的简单分词 - 不用担心,你可以传入自己的分词器)。
然后搜索,获取包含"Cat"的前N个结果
In[3]: np.sort(df['title_indexed'].array.score('Cat'))
Out[3]: array([ 0. , 0. , 0. , ..., 15.84568033,
15.84568033, 15.84568033])
In[4]: df['title_indexed'].score('Cat').argsort()
Out[4]:
array([0, 18561, 18560, ..., 15038, 19012, 4392])
由于这只是pandas,我们当然可以检索最匹配的结果
In[5]: df.iloc[top_n_cat[-10:]]
Out[5]:
title overview title_indexed
24106 The Black Cat American honeymooners in Hungary are trapped i... Terms({'Black': 1, 'The': 1, 'Cat': 1}, ...
12593 Fritz the Cat A hypocritical swinging college student cat ra... Terms({'Cat': 1, 'the': 1, 'Fritz': 1}, ...
39853 The Cat Concerto Tom enters from stage left in white tie and ta... Terms({'The': 1, 'Cat': 1, 'Concerto': 1...
75491 The Rabbi's Cat Based on the best-selling graphic novel by Joa... Terms({'The': 1, 'Cat': 1, "Rabbi's": 1}...
57353 Cat Run When a sexy, high-end escort holds the key evi... Terms({'Cat': 1, 'Run': 1}, {'Cat': [0],...
25508 Cat People Sketch artist Irena Dubrovna (Simon) and Ameri... Terms({'Cat': 1, 'People': 1}, {'Cat': [...
11694 Cat Ballou A woman seeking revenge for her murdered fathe... Terms({'Cat': 1, 'Ballou': 1}, {'Cat': [...
25078 Cat Soup The surreal black comedy follows Nyatta, an an... Terms({'Cat': 1, 'Soup': 1}, {'Cat': [0]...
35888 Cat Chaser A Miami hotel owner finds danger when be becom... Terms({'Cat': 1, 'Chaser': 1}, {'Cat': [...
6217 Cat People After years of separation, Irina (Nastassja Ki... Terms({'Cat': 1, 'People': 1}, {'Cat': [...
更多用例可以在colab笔记本中查看
总体目标是在Pandas数据框中重现像Solr或Elasticsearch这样的搜索引擎的许多词法特性(词项/短语搜索)。
我们希望索引尽可能内存效率高且搜索速度快。我们希望使用它时有最小的开销。 我们希望你能够相对高效地处理一个合理规模的数据集(10万到100万文档)进行离线评估。以及在服务中快速重新排序数千个文档。
我们的目标不是构建"大数据"系统,而是为"小数据"构建。也就是说,专注于Pandas的能力和表达性,而不是为了可扩展性而限制功能。
为此,searcharray的应用将倾向于专注于实验和前N个候选项的重新排序。对于实验,我们希望在Pandas中表达的任何想法都有一个相对清晰的路径/"合约",说明如何在经典的词法搜索引擎中实现。对于重新排序,我们希望能从基础系统加载一些前N个结果,并能够修改它们。
我们知道在搜索、RAG和其他检索问题中,混合搜索技术占主导地位。然而,它经常被描述为一个巨大、奇怪、看起来对大多数数据科学家来说很奇怪的大数据词法搜索引擎与向量数据库的结合。我们希望词法搜索对于构建这些系统的数据科学家和机器学习工程师来说更容易接近。
Python库在分词方面已经做得非常好,甚至超过了Lucene的能力...让你能够模拟和/或超越Lucene的分词能力。
在SearchArray中,分词器是一个接受字符串并输出一系列标记的函数。例如,简单的默认空格分词:
def ws_tokenizer(string): return string.split()
你可以将任何符合这个签名的分词器传递给索引:
def ws_lowercase_tokenizer(string): return string.lower().split() df['title_indexed'] = SearchArray.index(df['title'], tokenizer=ws_lowercase_tokenizer)
使用词干提取库或任何你想要的Python功能创建你自己的分词器。
Solr有自己独特的函数查询语法。Elasticsearch有Painless。
我们不重新创建这些,而是简单地在现有的Pandas列上使用Pandas。然后,如果你需要在Solr或Elasticsearch中实现这一点,尝试重新创建功能。可以说,Solr/ES中的功能会是你在Pandas中能做的事情的一个子集。
# 计算过去的小时数
df['hrs_into_past'] = (now - df['timestamp']).dt.total_seconds() / 3600
然后如果你想的话,可以乘以BM25:
df['score'] = df['title_indexed'].score('Cat') * df['hrs_into_past']
我们专注于词法,即"BM25式"和相关问题。外面有其他很棒的向量搜索工具。
访问Relevance Slack上的#searcharray频道
字节跳动发布的AI编程神器IDE
Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。
AI小说写作助手,一站式润色、改写、扩写
蛙蛙写作—国内先进的AI写作平台,涵盖小说、学术、社交媒体等多场景。提供续写、改写、润色等功能,助力创作者高效优化写作流程。界面简洁,功能全面,适合各类写作者提升内容品质和工作效率。
全能AI智能助手,随时解答生活与工作的多样问题
问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。