- หารือเกี่ยวกับความเกี่ยวข้อง Slack
SearchArray เปลี่ยนคอลัมน์สตริง Pandas เป็นดัชนีคำ มันหมอบลงอย่างมีประสิทธิภาพ 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 Experiment | เกี่ยวกับ Internals
pip install searcharray
def tokenize(value: str) -> List[str] ) เมื่อทำการจัดทำดัชนีdata_dir ไปยังดัชนีสำหรับดัชนีการแมปหน่วยความจำเพื่อให้การค้นหาคำศัพท์ง่ายขึ้นในสแต็กข้อมูล Python
ผู้ปฏิบัติงาน ML / AI หลายคนเข้าถึงโซลูชันการค้นหาเวกเตอร์จากนั้นตระหนักว่าพวกเขาจำเป็นต้องโรยในระดับหนึ่งของการค้นหา BM25 / คำศัพท์ มาค้นหาข้อความแบบเต็มรูปแบบแบบดั้งเดิมเพื่อทำตัวเหมือนส่วนอื่น ๆ ของสแต็คข้อมูล
SearchArray สร้างวิธีการสร้างและใช้ดัชนีการค้นหาเป็นเพียงส่วนหนึ่งของอาร์เรย์แพนด้า ในแง่หนึ่งมันจะสร้างเครื่องมือค้นหาในแพนด้า - เพื่อให้ทุกคนสามารถสร้างความคิดต้นแบบและดำเนินการใหม่โดยไม่มีระบบภายนอก
คุณสามารถดูการทดสอบการค้นหาแบบครบวงจรเต็มรูปแบบในโน้ตบุ๊ก colab นี้
เช่นลองใช้ dataframe ที่มีข้อความมากมายเช่นชื่อภาพยนตร์และภาพรวม:
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'})```
(สังเกตเห็นการโทเค็นที่โง่ - ไม่ต้องกังวลคุณสามารถผ่านโทเค็นของคุณเองได้)
จากนั้นค้นหาได้รับ T สูงสุดด้วย Cat
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 แต่ในแพนด้า dataframe
เราต้องการให้ดัชนีเป็นหน่วยความจำที่มีประสิทธิภาพและรวดเร็วในการค้นหามากที่สุด เราต้องการใช้มันเพื่อให้มีค่าใช้จ่ายน้อยที่สุด
เราต้องการให้คุณสามารถทำงานกับชุดข้อมูลที่สมเหตุสมผล (เอกสาร 100x-1m) ค่อนข้างมีประสิทธิภาพสำหรับการประเมินออฟไลน์ และ 1,000s สำหรับการเดินกลับอย่างรวดเร็วในการบริการ
แทนที่จะสร้าง 'ข้อมูลขนาดใหญ่' เป้าหมายของเราคือการสร้างสำหรับ ข้อมูลขนาดเล็ก นั่นคือมุ่งเน้นไปที่ความสามารถและการแสดงออกของแพนด้ามากกว่าการ จำกัด ฟังก์ชั่นในความสามารถในการปรับขนาด
ด้วยเหตุนี้แอปพลิเคชันของ SearchArray จะมุ่งเน้นไปที่การทดลองและการเปลี่ยนเส้นทางผู้สมัครอันดับต้น ๆ สำหรับการทดลองเราต้องการความคิดใด ๆ ที่แสดงในแพนด้าเพื่อให้มีเส้นทางที่ค่อนข้างชัดเจน / "สัญญา" ในวิธีที่พวกเขาจะนำไปใช้ในเครื่องมือค้นหาคำศัพท์แบบคลาสสิก สำหรับ Reranking เราต้องการโหลดผลลัพธ์ N อันดับต้น ๆ จากระบบฐานและสามารถแก้ไขได้
เรารู้ในการค้นหาผ้าขี้ริ้วและปัญหาการดึงข้อมูลอื่น ๆ แต่บ่อยครั้งที่การแสดงในแง่ของเครื่องมือค้นหาคำศัพท์ขนาดใหญ่แปลก ๆ ที่ดูแปลก ๆ สำหรับนักวิทยาศาสตร์ข้อมูลส่วนใหญ่ที่เข้าร่วมกับฐานข้อมูลเวกเตอร์ เราต้องการให้การค้นหาคำศัพท์สามารถเข้าถึงได้ง่ายขึ้นสำหรับนักวิทยาศาสตร์ด้านข้อมูลและวิศวกร ML ที่สร้างระบบเหล่านี้
ห้องสมุด Python ทำได้ดีมาก แม้เกินสิ่งที่ Lucene สามารถทำได้ ... ให้ความสามารถในการจำลองและ/หรือเกินความสามารถในการโทเค็นของ Lucene
ใน SearchArray tokenizer เป็นฟังก์ชั่นใช้สตริงและปล่อยโทเค็นชุดหนึ่ง เช่นใบ้, ช่องว่าง, tokenization:
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 มีฟังก์ชั่นที่ไม่ซ้ำกันแบบสอบถาม Syntaxhttps: //solr.apache.org/guide/7_7/function-queries.html Elasticsearch มีความเจ็บปวด
แทนที่จะสร้างสิ่งเหล่านี้ใหม่เพียงแค่ใช้แพนด้าในคอลัมน์แพนด้าที่มีอยู่ จากนั้นในภายหลังหากคุณต้องการใช้สิ่งนี้ใน Solr หรือ Elasticsearch พยายามที่จะสร้างฟังก์ชั่นใหม่ สิ่งที่อยู่ในโซล / เอสจะเป็นส่วนหนึ่งของสิ่งที่คุณสามารถทำได้ในแพนด้า
# 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" และปัญหาที่อยู่ติดกัน มีเครื่องมือที่ยอดเยี่ยมอื่น ๆ สำหรับการค้นหาเวกเตอร์ที่นั่น
เยี่ยมชม #SearchArray Channel บน Relevance Slack