|関連するスラックで話し合います
SearchArrayは、Pandas String列を用語インデックスに変えます。それは、フレーズと個々のトークンの効率的なBM25 / TFIDFスコアリングです。
ルーセンを考えてください、しかしパンダのコラムとして。
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はこれらのノートブックに記録されています。
SearchArrayガイド| SearchArrayオフライン実験|内部について
pip install searcharray
def tokenize(value: str) -> List[str] )を渡します。data_dirを渡すことによるメモリマップPythonデータスタックの語彙検索を簡素化する。
多くのML / AI開業医は、ベクター検索ソリューションに手を伸ばし、ある程度のBM25 /語彙検索を振りかける必要があることに気付きます。データスタックの他の部分のように動作するように、従来のフルテキスト検索を取得しましょう。
SearchArrayは、パンダアレイの一部として検索インデックスを作成および使用するパンダ中心の方法を作成します。ある意味では、パンダの検索エンジンを構築して、外部システムなしでアイデアをプロトタイプし、再ランキングを実行できるようにします。
このコラブノートブックで、エンドツーエンドの検索関連実験を見ることができます
つまり、映画のタイトルや概要のようなテキストの束を持つデータフレームを取りましょう。
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': [...
コラブノートブックでは、より多くのユースケースを見ることができます
全体的な目標は、SolRやElasticSearchなどの検索エンジンの字句機能(用語 /フレーズ検索)の多くを再現することですが、パンダのデータフレームです。
インデックスは、検索で可能な限りメモリ効率的かつ高速にすることを望んでいます。最小限のオーバーヘッドを使用するように使用したいと考えています。
オフライン評価のために、合理的なデータセット(100倍のドキュメント)を比較的効率的に使用できるようにしてください。サービスでの速い再ランキングのための1000年代。
「ビッグデータ」のために構築する代わりに、私たちの目標は小さなデータを構築することです。つまり、パンダの能力と表現力に焦点を当て、スケーラビリティを支持して機能を制限します。
この目的のために、SearchArrayのアプリケーションは、実験とトップN候補の再ランキングに焦点を当てる傾向があります。実験のために、パンダで表現されたアイデアが、古典的な語彙検索エンジンでどのように実装されるかについて、やや明確なパス /「契約」を持っていることを望んでいます。再ランキングの場合、ベースシステムからいくつかのトップN結果をロードし、それらを変更できるようにしたいと考えています。
検索、RAG、およびその他の検索問題ハイブリッド検索手法が支配的であることがわかります。しかし、多くの場合、ほとんどのデータサイエンティストがベクトルデータベースに結合されているように見える巨大で奇妙なビッグデータの語彙検索エンジンの観点からキャストされます。語彙検索は、これらのシステムを構築するデータサイエンティストとMLエンジニアにとってより親しみやすいものにしたいと考えています。
Pythonライブラリはすでにトークン化を非常によく行っています。ルーセンができることを超えることさえ...ルーセンのトークン化の能力をシミュレートし、それを超える能力をあなたに与えます。
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 )STEMINGライブラリ、または必要なPython機能を使用して独自に作成します。
solrには独自の関数クエリsyntaxhttps://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チャンネルにアクセスしてください