|讨论相关的松弛
SearchArray将Pandas String列变成术语索引。它可以有效地对短语和单个令牌进行有效的BM25 / TFIDF评分。
想想露西恩(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' ])) # Search w/ phrase
> BM25 scores :
> array ([ 0. , 0. , 0. , ..., 15.84568033 , 15.84568033 , 15.84568033 ])这些笔记本中记录了搜索片:
SearchArray指南| SEERCHARRAY离线实验|关于内部
pip install searcharray
def tokenize(value: str) -> List[str] )。data_dir传递到索引的内存图,以获取内存映射索引在Python数据堆中简化词汇搜索。
许多ML / AI从业人员可以寻求矢量搜索解决方案,然后意识到他们需要在某种程度上撒上BM25 /词汇搜索。让我们来获取传统的全文搜索,就像数据堆的其他部分一样。
SearchArray创建了一种以熊猫为中心的方式来创建和使用搜索索引作为大熊猫阵列的一部分。从某种意义上说,它在熊猫中构建了搜索引擎 - 允许任何人在没有外部系统的情况下进行原型想法并执行重新管理。
您可以在此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])
而且由于它只是熊猫,我们当然可以找回顶级比赛
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笔记本中可以看到更多用例
总体目标是重新创建Solr或Elasticsearch等搜索引擎的许多词汇功能(术语 /短语搜索),但在Pandas DataFrame中。
我们希望该索引能够尽可能高效且迅速地搜索。我们希望使用它的开销很少。
我们希望您能够使用一个合理的数据集(100x-1m文档)进行离线评估。和1000 s in Service快速重读。
我们的目标不是为“大数据”构建“大数据” 。也就是说,着重于熊猫的能力和表现力,而不是限制功能,而有利于可伸缩性。
为此,SearchArray的应用将倾向于专注于实验和顶级候选人重读。为了实验,我们希望在大熊猫中表达的任何想法在经典的词汇搜索引擎中如何实现它们的实施方式有些清晰的道路 /“合同”。为了重新管理,我们希望从基本系统加载一些顶部N结果,并能够修改它们。
我们在搜索,抹布和其他检索问题中都知道混合搜索技术占主导地位。然而,通常就巨型,怪异,大数据词汇搜索引擎而言,它看起来很奇怪,大多数数据科学家都与矢量数据库相连。我们希望词汇搜索对数据科学家和ML工程师建立这些系统更容易实现。
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有自己的独特函数查询语法://solr.apache.org/guide/7_7/function-queries.html。 Elasticsearch无痛。
与其重新创建这些,只需在现有的大熊猫列上使用大熊猫即可。然后,如果您需要在Solr或Elasticsearch中实现此功能,请尝试重新创建功能。可以说,Solr / es中的内容将是您可以在熊猫中做什么的子集。
# Calculate the number of hours into the past
df['hrs_into_past'] = (now - df['timestamp']).dt.total_seconds() / 3600
如果需要,则乘以BM25:
df['score'] = df['title_indexed'].score('Cat') * df['hrs_into_past']
我们专注于词汇,即“ BM25-ish”和相邻问题。还有其他出色的工具可以搜索矢量。
请访问有关Slack的#SearchArray频道