|討論相關的鬆弛
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快速重讀。
我們的目標不是為“大數據”構建“大數據” 。也就是說,著重於熊貓的能力和表現力,而不是限制功能,而有利於可伸縮性。
為此,搜索活動的應用將傾向於專注於實驗和頂級候選人重讀。為了實驗,我們希望在大熊貓中表達的任何想法在經典的詞彙搜索引擎中如何實現它們的實施方式有些清晰的道路 /“合同”。為了重新管理,我們希望從基本系統加載一些頂部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頻道