我正在使用 NLP 從不同年份的 SEC 檔案中提取包含某些關鍵字的句子。我通過 pandas 資料框將輸出存盤在 sqlite 中。到現在為止還挺好。當我想比較兩個不同年份(比如 2022 年和 2021 年)的句子時,問題就來了。
我一直在使用以下查詢:
query = "select Nvidia_2022.Research as Research_2022, Nvidia_2021.Research as Research_2021 from Nvidia_2022 join Nvidia_2021 where '%' || Nvidia_2022.Research || '%' like '%' || Nvidia_2021.Research || '%'"
這在大多數情況下適用于完全相同的句子。這是輸出。
['Such license and development arrangements can further enhance the reach of our technology.'
'Such license and development arrangements can further enhance the reach of our technology.']
但是,有時句子會略有不同,例如:
['We have invested over $29 billion in research and development since our inception, yielding inventions that are essential to modern computing.'
'We have invested over $24 billion in research and development since our inception, yielding inventions that are essential to modern computing.']
290 億美元對 240 億美元
或者句子末尾還有其他區別:
'Our Compute & Networking segment includes Data Center platforms and systems for AI, HPC, and accelerated computing; Mellanox networking and interconnect solutions; automotive AI Cockpit, autonomous driving development agreements, and autonomous vehicle solutions; cryptocurrency mining processors, or CMP; Jetson for robotics and other embedded platforms; and NVIDIA AI Enterprise and other software.'
'Our Compute & Networking segment includes Data Center platforms and systems for AI, HPC, and accelerated computing; Mellanox networking and interconnect solutions; automotive AI Cockpit, autonomous driving development agreements, and autonomous vehicle solutions; and Jetson for robotics and other embedded platforms.'
我的問題:
在 sqlite 或其他 sql 資料庫中有沒有辦法盡可能多地做文本比較作業,然后將最復雜的句子傳遞給 python 來做類似 levenshtein_distance 或 transformers 句子比較之類的事情?
還是我應該停止使用 SQL 比較查詢,并立即開始使用 python 進行繁重的作業?
我正在嘗試使用盡可能多的 sql,因為它往往比在 python 中計算距離要快得多。
uj5u.com熱心網友回復:
sqlite3 支持使用FTS5 Extension進行全文搜索。
您必須創建一個虛擬表,然后您才能使用MATCH
關鍵字。
-- create a virtual table
CREATE VIRTUAL TABLE email USING fts5(sender, title, body);
-- populate it ...
-- perform a full text search
SELECT * FROM email WHERE email MATCH 'fts5' ORDER BY rank;
uj5u.com熱心網友回復:
一些像雪花這樣的實作有編輯距離: https ://docs.snowflake.com/en/sql-reference/functions/editdistance.html
如果您真的想在 sql 中執行此操作,您可以將其標記為類似
- 在空間上拆分 varchar --> 陣列
- 將陣列取消嵌套/展平為 CTE
- 對句子重復步驟 1 和 2 進行比較
- 加入 2 個 CTE 以查看共同令牌的數量
但我認為 sql 對于這類操作不一定更快,并且不如 python 庫那么健壯
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/470331.html