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/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

发表回复

登录后才能评论