AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當前位置:首頁 > 科技  > 軟件

解鎖搜索的力量:關鍵詞、相似性和語義解釋

來源: 責編: 時間:2024-07-12 17:26:55 692觀看
導讀譯者 | 李睿審校 | 重樓深入研究不同的搜索技術為了設定場景,假設有一系列關于各種技術主題的文本,并希望查找與“機器學習” (Machine Learning)相關的信息。接下來將研究關鍵字搜索、相似性搜索和語義搜索如何提供不同

譯者 | 李睿SVh28資訊網——每日最新資訊28at.com

審校 | 重樓SVh28資訊網——每日最新資訊28at.com

深入研究不同的搜索技術

SVh28資訊網——每日最新資訊28at.com

為了設定場景,假設有一系列關于各種技術主題的文本,并希望查找與“機器學習” Machine Learning相關的信息。接下來將研究關鍵字搜索、相似性搜索和語義搜索如何提供不同程度的深度和理解,從簡單的關鍵字匹配到識別相關概念和場景。SVh28資訊網——每日最新資訊28at.com

首先看看程序使用的標準代碼組件。SVh28資訊網——每日最新資訊28at.com

1.使用的標準代碼組件

A.導入的庫

Python  import os import re from whoosh.index import create_in from whoosh.fields import Schema, TEXT from whoosh.qparser import QueryParser from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.metrics.pairwise import cosine_similarity from transformers import pipeline import numpy as np

在這個塊中導入了以下必要的庫:SVh28資訊網——每日最新資訊28at.com

  • os用于文件系統的操作。
  • re表示正則表達式。
  • whoosh用于創建和管理搜索索引。
  • scikit-learn用于TF-IDF矢量化和相似度計算。
  • transformers使用深度學習模型進行特征提取。
  • numpy用于數值運算,特別是排序。

B.文檔初始化樣例

Python  # Sample documents used for demonstrating all three search techniques documents = [ "Machine learning is a field of artificial intelligence that uses statistical techniques.", "Natural language processing (NLP) is a part of artificial intelligence that deals with the interaction between computers and humans using natural language. ", "Deep learning models are a subset of machine learning algorithms that use neural networks with many layers.", "AI is transforming the world by automating tasks, providing insights through data analysis, and enabling new technologies like autonomous vehicles and advanced robotics. ", "Natural language processing can be challenging due to the complexity and variability of human language. ", "The application of machine learning in healthcare is revolutionizing the way diseases are diagnosed and treated.", "Autonomous vehicles rely heavily on AI and machine learning to navigate and make decisions.", "Speech recognition technology has advanced considerably thanks to deep learning models. " ]

定義一個示例文檔列表,其中包含與人工智能、機器學習和自然語言處理中的各種主題相關的文本。SVh28資訊網——每日最新資訊28at.com

C.高亮功能

Python  def highlight_term(text, term): return re.sub(f"({term})", r'/033[1;31m/1/033[0m', text, flags=re.IGNORECASE)

用于美化輸出,以突出顯示文本中的搜索詞。SVh28資訊網——每日最新資訊28at.com

2.關鍵字搜索

將搜索查詢與文檔中找到的精確或部分關鍵字相匹配的傳統方法。SVh28資訊網——每日最新資訊28at.com

嚴重依賴于精確的詞匹配和簡單的查詢操作符(AND、OR、NOT)。SVh28資訊網——每日最新資訊28at.com

A.關鍵字搜索如何工作

由于搜索查詢是“機器學習”(Machine Learning),因此關鍵字搜索會查找精確的文本匹配,并且只返回包含“機器學習”(Machine Learning)的文本。一些將被返回的文本示例是“機器學習正在改變許多行業。”“最近開設了一門機器學習的課程。”SVh28資訊網——每日最新資訊28at.com

B.檢查關鍵字搜索背后的代碼

Python  # Function for Keyword Search using Whoosh def keyword_search(query_str): schema = Schema(content=TEXT(stored=True)) if not os.path.exists("index"): os.mkdir("index") index = create_in("index", schema) writer = index.writer() for doc in documents: writer.add_document(content=doc) writer.commit() with index.searcher() as searcher: query = QueryParser("content", index.schema).parse(query_str) results = searcher.search(query) highlighted_results = [(highlight_term(result['content'], query_str), result.score) for result in results] return highlighted_results

使用了Whoosh庫來執行關鍵字搜索。SVh28資訊網——每日最新資訊28at.com

Schema和TEXT采用單個字段內容定義模式。SVh28資訊網——每日最新資訊28at.com

  • os.path.exists和os.path.existsmkdir:檢查索引目錄是否存在,如果不存在則創建它。
  • create_in:在名為index的目錄中建立索引。
  • writer:打開一個寫入器,將文檔添加到索引中。
  • add_document:向索引中添加文檔。
  • commit:將更改提交到索引。
  • with index.searcher():打開一個搜索器來搜索索引。
  • QueryParser:解析查詢字符串。
  • searcher.search:使用解析后的查詢搜索索引。
  • highlighted_results:高亮顯示結果中的搜索詞,并存儲結果及其分數。

將在本文后面檢查關鍵字搜索輸出和其他搜索技術。SVh28資訊網——每日最新資訊28at.com

3.相似性搜索

該方法根據相關單詞或主題的存在等特征,將提供的文本與其他文本進行比較,從而找到與搜索查詢相似的文本。SVh28資訊網——每日最新資訊28at.com

A.相似性搜索的工作原理

回到之前相同的搜索查詢“機器學習”,相似度搜索將返回概念上類似的文本,例如“醫療保健中的人工智能應用使用機器學習技術”和“預測建模通常依賴于機器學習”。SVh28資訊網——每日最新資訊28at.com

B.檢查相似度搜索背后的代碼

Python  # Function for Similarity Search using Scikit-learn def similarity_search(query_str): vectorizer = TfidfVectorizer() tfidf_matrix = vectorizer.fit_transform(documents) query_vec = vectorizer.transform([query_str]) similarity = cosine_similarity(query_vec, tfidf_matrix) similar_docs = similarity[0].argsort()[-3:][::-1] # Top 3 similar documents similarities = similarity[0][similar_docs] highlighted_results = [(highlight_term(documents[i], query_str), similarities[idx]) for idx, i in enumerate(similar_docs)] return highlighted_results

使用Scikit-learn庫編寫了一個函數來執行相似性搜索。SVh28資訊網——每日最新資訊28at.com

  • TfidfVectorizer:將文檔轉換為TF-IDF功能。
  • fit_transform:它將矢量器適配到文檔中,并將文檔轉換為TF-IDF矩陣。Fit從文檔列表中學習詞匯表,并識別唯一的單詞,以計算它們的TF和IDF值。
  • transform:使用在fit步驟中學習的相同詞匯表和統計信息,將查詢字符串轉換為TF-IDF向量。
  • cosine_similarity:計算查詢向量和TF-IDF矩陣之間的余弦相似度。
  • argsort()[-3:][::-1]:按相似度降序獲取前3個相似文檔的索引。這一步只與本文相關,如果不想將搜索結果限制在前3名,可以取消這一步驟。
  • highlighted_results:高亮顯示結果中的搜索詞,并存儲結果及其相似性得分。

4.語義搜索

現在進入了強大搜索技術的領域。此方法理解搜索詞的含義/場景,并使用該概念返回文本,即使沒有直接提到搜索詞。SVh28資訊網——每日最新資訊28at.com

A.語義搜索如何工作

同樣的搜索查詢“機器學習”(Machine Learning),當與語義搜索一起應用時,會產生與機器學習概念相關的文本,例如“人工智能和數據驅動的決策正在改變行業”和“神經網絡是許多人工智能系統的。”SVh28資訊網——每日最新資訊28at.com

B.檢查語義搜索背后的代碼

Python  # Function for Semantic Search using Transformersdef semantic_search(query_str): semantic_searcher = pipeline("feature-extraction", model="distilbert-base-uncased") query_embedding = semantic_searcher(query_str)[0][0]  def get_similarity(query_embedding, doc_embedding): return cosine_similarity([query_embedding], [doc_embedding])[0][0]  doc_embeddings = [semantic_searcher(doc)[0][0] for doc in documents] similarities = [get_similarity(query_embedding, embedding) for embedding in doc_embeddings] sorted_indices = np.argsort(similarities)[-3:][::-1] highlighted_results = [(highlight_term(documents[i], query_str), similarities[i]) for i in sorted_indices] return highlighted_results

使用Hugging Face transformers庫執行語義搜索的函數。SVh28資訊網——每日最新資訊28at.com

在semantic_searcher = pipeline("feature-extraction", model="distilbert-base-uncased")代碼片段中,有很多操作在進行。SVh28資訊網——每日最新資訊28at.com

  • pipeline:這是從transformer庫導入的函數,它幫助使用預訓練的模型設置各種類型的NLP任務。
  • Feature extractio:Pipeline執行特征提取(Feature extraction)任務,將文本轉換為可用于各種下游任務的數字表示(嵌入)。
  • 用于這一任務的預訓練模型是distilbert-base-uncased模型,它是BERT模型的一個更小、更快的版本,經過訓練以理解不區分大小寫的英文文本。
  • query_embedding:獲取查詢字符串的嵌入。
  • get_similarity這是一個嵌套函數,用于計算查詢嵌入和文檔嵌入之間的余弦相似度。
  • doc_embeddings:獲取所有文檔的嵌入。
  • similarities:計算查詢嵌入與所有文檔嵌入之間的相似度。
  • argsort()[-3:][::-1]:按相似度降序獲取前3個相似文檔的索引。
  • highlighted_results:高亮顯示結果中的搜索詞,并存儲結果及其相似度分數。

輸出

既然已經了解了各種搜索技術的景,并且已經設置了文檔以進行搜索,現在看一下基于每個搜索技術的搜索查詢的輸出。SVh28資訊網——每日最新資訊28at.com

Python  # Main execution if __name__ == "__main__": query = input("Enter your search term: ") print("/nKeyword Search Results:") keyword_results = keyword_search(query) for result, score in keyword_results: print(f"{result} (Score: {score:.2f})")  print("/nSimilarity Search Results:") similarity_results = similarity_search(query) for result, similarity in similarity_results: print(f"{result} (Similarity: {similarity * 100:.2f}%)")  print("/nSemantic Search Results:") semantic_results = semantic_search(query) for result, similarity in semantic_results: print(f"{result} (Similarity: {similarity * 100:.2f}%)")

現在使用搜索詞“機器學習”(Machine Learning)和下面的搜索結果圖像來搜索文檔。SVh28資訊網——每日最新資訊28at.com

SVh28資訊網——每日最新資訊28at.com

搜索結果中的亮點:SVh28資訊網——每日最新資訊28at.com

(1)highlighted_results函數幫助高亮搜索詞SVh28資訊網——每日最新資訊28at.com

(2)相似性搜索和語義搜索只返回3個結果,這是因為其代碼將這兩種搜索技術的搜索結果限制為3個。SVh28資訊網——每日最新資訊28at.com

(3)關鍵詞搜索使用TF-IDF根據文檔中相對于查詢的術語出現的頻率和重要性來計算分數。SVh28資訊網——每日最新資訊28at.com

(4)相似性搜索使用向量化和余弦相似性來度量文檔在向量空間中與查詢的相似程度。SVh28資訊網——每日最新資訊28at.com

(5)語義搜索使用來自轉換器模型的嵌入和余弦相似度來捕獲文檔與查詢的語義和相關性。SVh28資訊網——每日最新資訊28at.com

(6)需要注意,由于語義搜索的強大功能,它檢索了與自然語言處理相關的文本,因為自然語言處理在的場景中與機器學習更為接近。SVh28資訊網——每日最新資訊28at.com

現在使用其他搜索詞“Artificially Intelligent”和“Artificial Intelligence”的搜索結果(需要注意,“Artificially”的拼寫錯誤是故意的),并討論結果。SVh28資訊網——每日最新資訊28at.com

SVh28資訊網——每日最新資訊28at.com

搜索結果中的亮點:SVh28資訊網——每日最新資訊28at.com

(1)搜索“人工智能”(Artificially Intelligent),由于缺乏精確或部分匹配的術語,關鍵詞搜索沒有結果。SVh28資訊網——每日最新資訊28at.com

(2)由于向量表示或相似度不匹配,相似度搜索的結果為零。SVh28資訊網——每日最新資訊28at.com

(3)語義搜索可以有效地找到場景相關的文檔,顯示出理解和匹配概念的能力,而不僅僅是精確的單詞。SVh28資訊網——每日最新資訊28at.com

(4)在第二次搜索中,拼寫錯誤的“人工智能”(Artificial Intelligence沒有正確地產生關鍵字搜索結果,但由于匹配的情況,其中一個文本產生了相似性得分。在智能、語義搜索方面,像往常一樣,從文檔中檢索到場景匹配的文本。SVh28資訊網——每日最新資訊28at.com

結論

現在已經了解了各種搜索技術的性能,以下了解一些關鍵要點:SVh28資訊網——每日最新資訊28at.com

(1)搜索方法的正確選擇應取決于任務的要求。對任務進行徹底的分析,并插入正確的搜索方法以獲得最佳性能。SVh28資訊網——每日最新資訊28at.com

(2)使用關鍵字搜索進行簡單、直接的術語匹配。SVh28資訊網——每日最新資訊28at.com

(3)當需要查找具有輕微術語變化但仍然基于關鍵字匹配的文檔時,可以使用相似度搜索。SVh28資訊網——每日最新資訊28at.com

(4)對需要深入理解內容的任務使用語義搜索,例如在處理各種術語或需要捕獲查詢的底層含義時。SVh28資訊網——每日最新資訊28at.com

(5)還可以考慮將這些方法結合起來,以實現良好的平衡,利用每種方法的優勢來提高總體性能。SVh28資訊網——每日最新資訊28at.com

(6)每種搜索技術都有進一步優化的余地,本文沒有對此進行介紹。SVh28資訊網——每日最新資訊28at.com

參考文獻

以下參考文獻的文本已被這個文檔用于本文的搜索:SVh28資訊網——每日最新資訊28at.com

  • Is Deep Learning and Machine Learning the Same Thing?
  • CloudBank
  • What is artificial intelligence?
  • AI augmenting human capabilities across the care delivery value chain
  • The power of Artificial Intelligence
  • ChatGPT and Artificial Intelligence

原文標題:Unlocking the Power of Search: Keywords, Similarity, and Semantics Explained,作者:Pavan VemuriSVh28資訊網——每日最新資訊28at.com


SVh28資訊網——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-100739-0.html解鎖搜索的力量:關鍵詞、相似性和語義解釋

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 突破與創新:Vue.js 創始人尤雨溪 2024 年度技術前瞻

下一篇: 點線面的智慧: 轉轉JTS技術如何塑造上門履約地理布局

標簽:
  • 熱門焦點
  • 一加Ace2 Pro官宣:普及16G內存 引領24G

    一加官方今天繼續為本月發布的新機一加Ace2 Pro帶來預熱,公布了內存方面的信息。“淘汰 8GB ,12GB 起步,16GB 普及,24GB 引領,還有呢?#一加Ace2Pro#,2023 年 8 月,敬請期待。”同時
  • 摸魚心法第一章——和配置文件說拜拜

    為了能摸魚我們團隊做了容器化,但是帶來的問題是服務配置文件很麻煩,然后大家在群里進行了“親切友好”的溝通圖片圖片圖片圖片對比就對比,簡單對比下獨立配置中心和k8s作為配
  • Python異步IO編程的進程/線程通信實現

    這篇文章再講3種方式,同時講4中進程間通信的方式一、 Python 中線程間通信的實現方式共享變量共享變量是多個線程可以共同訪問的變量。在Python中,可以使用threading模塊中的L
  • 使用AIGC工具提升安全工作效率

    在日常工作中,安全人員可能會涉及各種各樣的安全任務,包括但不限于:開發某些安全工具的插件,滿足自己特定的安全需求;自定義github搜索工具,快速查找所需的安全資料、漏洞poc、exp
  • 2023年,我眼中的字節跳動

    此時此刻(2023年7月),字節跳動從未上市,也從未公布過任何官方的上市計劃;但是這并不妨礙它成為中國最受關注的互聯網公司之一。從2016-17年的抖音強勢崛起,到2018年的“頭騰
  • 支持aptX Lossless無損傳輸 iQOO TWS 1賽道版發布限時優惠價369元

    2023年7月4日,“無損音質,聲動人心”iQOO TWS 1正式發布,支持aptX Lossless無損傳輸,限時優惠價369元。iQOO TWS 1耳機率先支持端到端aptX Lossless無
  • iQOO Neo8系列新品發布會

    旗艦雙芯 更強更Pro
  • 三翼鳥智能家居亮相電博會,讓用戶體驗更真實

    2021電博會在青島國際會展中心開幕中,三翼鳥直接把“家”搬到了現場,成為了展會的一大看點。這也是三翼鳥繼9月9日發布了行業首個一站式定制智慧家平臺后的
  • 北京:科技教育體驗基地開始登記

      北京“科技館之城”科技教育體驗基地登記和認證工作日前啟動。首批北京科技教育體驗基地擬于2023年全國科普日期間掛牌,后續還將開展常態化登記。  北京科技教育體驗基
Top