Python文本相似度詳解

Python是一種流行的編程語言,具有易於使用的語言結構, 因此在自然語言處理(NLP)中備受歡迎。在本文中,我們將介紹Python中文本相似度的計算方法,包括文本相似度計算、文檔相似度、文本匹配、相似度分析、文本進度條注釋等方面。

一、Python文本相似度計算

文本相似度計算是一種常見的NLP任務。在Python中,我們可以使用NLTK(Natural Language Toolkit)等庫來完成這項任務。以下是一個計算文本相似度的示例代碼:

import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from nltk.stem import WordNetLemmatizer
from sklearn.metrics.pairwise import cosine_similarity

# 從文本中提取文本特徵
def extract_features(text):
    # 令牌化並去除停用詞
    stop_words = set(stopwords.words('english'))
    words = [word for word in word_tokenize(text.lower()) if word.isalpha() and word not in stop_words]
    #stemming
    stemmer = PorterStemmer()
    words = [stemmer.stem(word) for word in words]
    #lemmatization
    lemmatizer = WordNetLemmatizer()
    words = [lemmatizer.lemmatize(word) for word in words]
    #返回所有單詞的列表
    return words

# 計算文本相似度
def calculate_similarity(text1,text2):
    #提取文本特徵
    text1_words = extract_features(text1)
    text2_words = extract_features(text2)

    #將提取出的文本特徵轉換成向量
    all_words = list(set(text1_words + text2_words))
    text1_vector = [text1_words.count(word) for word in all_words]
    text2_vector = [text2_words.count(word) for word in all_words]

    # 計算餘弦相似度
    cosine_sim = cosine_similarity([text1_vector],[text2_vector])[0][0]
    return cosine_sim

#測試
text1 = "Python is a popular programming language."
text2 = "Python is commonly used in natural language processing."
print("Similarity score:",calculate_similarity(text1,text2))

二、Python文檔相似度

文檔相似度是指計算兩篇文檔之間的相似度。在Python中,我們可以使用gensim等庫來實現這一任務。以下是一個計算文檔相似度的示例代碼:

import gensim
from gensim import corpora, similarities

#處理文本並將其轉換成相應的向量表示形式
def prepare_corpus(docs):
    stoplist = set('for a of the and to in'.split())
    texts = [[word for word in doc.lower().split() if word not in stoplist] for doc in docs]
    dictionary = corpora.Dictionary(texts)
    corpus = [dictionary.doc2bow(text) for text in texts]

    #tf-idf
    tfidf = models.TfidfModel(corpus)

    #將文檔向量化
    corpus_tfidf = tfidf[corpus]

    return dictionary,corpus_tfidf

#計算文檔相似度
def calculate_similarity(docs):
    dictionary,corpus_tfidf = prepare_corpus(docs)

    #將語料庫轉換成lsi模型
    lsi = models.LsiModel(corpus_tfidf,id2word=dictionary,num_topics=2)

    #計算相似性
    index = similarities.MatrixSimilarity(lsi[corpus_tfidf])

    #返回相似度矩陣
    return index

#測試
docs = ["Python is a popular programming language used in natural language processing",
        "Natural language processing is a subfield of computer science and artificial intelligence",
        "Python is useful for web development and scientific computing"]
index = calculate_similarity(docs)
sims = index[lsi[corpus_tfidf]]
print(list(enumerate(sims)))

三、Python文本相似度匹配

文本匹配是指在給定一組文本和一個查詢文本的情況下,找到與查詢文本最相似的文本。在Python中,我們可以使用fuzzywuzzy等庫來完成這項任務。以下是一個計算文本匹配度的示例代碼:

from fuzzywuzzy import fuzz

# 計算字元串相似度
def calculate_similarity(str1,str2):
    # 計算jaro_winkler係數
    jaro_winkler = fuzz.jaro_winkler(str1,str2)
    return jaro_winkler

#測試
str1 = "Python is a popular programming language."
str2 = "Python is used for web development and scientific computing."
print("Similarity score:",calculate_similarity(str1,str2))

四、Python相似度分析

相似度分析是指使用文本相似度來分析文本並從中提取有意義的信息。在Python中,我們可以使用pandas等庫來完成這項任務。以下是幾個分析篇幅相似度的示例代碼:

import pandas as pd
import numpy as np

# 進行數據分析
def analyze_similarity(df):
    #計算相似度矩陣
    similarity_matrix = np.zeros([len(df),len(df)])
    for i in range(len(df)):
        for j in range(i,len(df)):
            similarity_matrix[i][j] = calculate_similarity(df.iloc[i]['text'],df.iloc[j]['text'])
            similarity_matrix[j][i] = similarity_matrix[i][j]

    #轉換為DataFrame
    df_similarity = pd.DataFrame(similarity_matrix,index=df['id'],columns=df['id'])

    #篩選相似度大於等於0.7的文章
    mask = df_similarity >= 0.7
    df_pairs = pd.DataFrame({'id1':df_similarity[mask].stack().index.get_level_values('level_0'),
                             'id2':df_similarity[mask].stack().index.get_level_values('level_1')}).drop_duplicates()

    #返回符合條件的文章
    return df_pairs

#測試
df = pd.DataFrame({'id':[1,2,3],
                   'text':["Python is a popular programming language.",
                           "Python is used for web development and scientific computing.",
                           "Natural language processing is a subfield of computer science and artificial intelligence."]})
df_pairs = analyze_similarity(df)
print(df_pairs)

五、Python文本進度條注釋

在Python中,我們可以使用tqdm等庫來為某些長時間運行的任務添加進度條注釋。以下是一個將文本向量化過程添加進度條注釋的示例代碼:

from tqdm import tqdm

#將文本向量化並顯示進度條注釋
def vectorize_text(docs):
    vectors = []
    for doc in tqdm(docs):
        vector = extract_features(doc)
        vectors.append(vector)
    
    return vectors

#測試
docs = ["Python is a popular programming language.",
        "Natural language processing is a subfield of computer science and artificial intelligence.",
        "Python is used for web development and scientific computing."]
vectors = vectorize_text(docs)
print(vectors)

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/181659.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-23 06:44
下一篇 2024-11-23 06:44

相關推薦

  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • 如何查看Anaconda中Python路徑

    對Anaconda中Python路徑即conda環境的查看進行詳細的闡述。 一、使用命令行查看 1、在Windows系統中,可以使用命令提示符(cmd)或者Anaconda Pro…

    編程 2025-04-29
  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python清華鏡像下載

    Python清華鏡像是一個高質量的Python開發資源鏡像站,提供了Python及其相關的開發工具、框架和文檔的下載服務。本文將從以下幾個方面對Python清華鏡像下載進行詳細的闡…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智慧、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29
  • Python字典去重複工具

    使用Python語言編寫字典去重複工具,可幫助用戶快速去重複。 一、字典去重複工具的需求 在使用Python編寫程序時,我們經常需要處理數據文件,其中包含了大量的重複數據。為了方便…

    編程 2025-04-29
  • 蝴蝶優化演算法Python版

    蝴蝶優化演算法是一種基於仿生學的優化演算法,模仿自然界中的蝴蝶進行搜索。它可以應用於多個領域的優化問題,包括數學優化、工程問題、機器學習等。本文將從多個方面對蝴蝶優化演算法Python版…

    編程 2025-04-29
  • python強行終止程序快捷鍵

    本文將從多個方面對python強行終止程序快捷鍵進行詳細闡述,並提供相應代碼示例。 一、Ctrl+C快捷鍵 Ctrl+C快捷鍵是在終端中經常用來強行終止運行的程序。當你在終端中運行…

    編程 2025-04-29

發表回復

登錄後才能評論