| 在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频道


免费创建高清无水印Sora视频
Vora是一个免费创建高清无水印Sora视频的AI工具


最适合小白的AI自动化工作流平台
无需编码,轻松生成可复用、可变现的AI自动化工作流

大模型驱动的Excel数据处理工具
基于大模型交互的表格处理系统,允许用户通过对话方式完成数据整理和可视化分析。系统采用机器学习算法解析用户指令,自动执行排序、公式计算和数据透视等操作,支持多种文件格式导入导出。数据处理响应速度保持在0.8秒以内,支持超过100万行数据的即时分析。


AI辅助编程,代码自动修复
Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。


AI论文写作指导平台
AIWritePaper论文写作是一站式AI论文写作辅助工具,简化了选题、文献检索至论文撰写的整个过程。通过简单设定,平台可快速生成高质量论文大纲和全文,配合图表、参考文献等一应俱全,同时提供开题报告和答辩PPT等增值服务,保障数据安全,有效提升写作效率和论文质量。


AI一键生成PPT,就用博思AIPPT!
博思AIPPT,新一代的AI生成PPT平台,支持智能生成PPT、AI美化PPT、文本&链接生成PPT、导入Word/PDF/Markdown文档生成PPT等,内置海量精美PPT模板,涵盖商务、教育、科技等不同风格,同时针对每个页面提供多种版式,一键自适应切换,完美适配各种办公场景。


AI赋能电商视觉革命,一站式智能商拍平台
潮际好麦深耕服装行业,是国内AI试衣效果最好的软件。使用先进AIGC能力为电商卖家批量提供优质的、低成本的商拍图。合作品牌有Shein、Lazada、安踏、百丽等65个国内外头部品牌,以及国内10万+淘宝、天猫、京东等主流平台的品牌商家,为卖家节省将近85%的出图成本,提升约3倍出图效率,让品牌能够快速上架。


企业专属的AI法律顾问
iTerms是法大大集团旗下法律子品牌,基于最先进的大语言模型(LLM)、专业的法律知识库和强大的智能体架构,帮助企业扫清合规障碍,筑牢风控防线,成为您企业专属的AI法律顾问。


稳定高效的流量提升解决方案,助力品牌曝光
稳定高效的流量提升解决方案,助力品牌曝光


最新版Sora2模型免费使用,一键生成无水印视频
最新版Sora2模型免费使用,一键生成无水印视频
最新AI工具、AI资讯
独家AI资源、AI项目落地

微信扫一扫关注公众号