python文本去除相似文本

使用python,讲评论文本数据中相似的文本进行删除,留下不相似的语句

import nltk
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

def remove_similar_comments(comments, threshold):
    # 使用TF-IDF向量化文本数据
    vectorizer = TfidfVectorizer()
    vectors = vectorizer.fit_transform(comments)

    # 计算余弦相似度矩阵
    similarity_matrix = cosine_similarity(vectors)

    # 根据相似度阈值移除相似的评论
    removed_comments = []
    for i in range(len(comments)):
        is_similar = False
        for j in range(i+1, len(comments)):
            if similarity_matrix[i][j] >= threshold:
                is_similar = True
                break
        if not is_similar:
            removed_comments.append(comments[i])

    return removed_comments

# 示例用法
comments = [
    "这个产品真棒!",
    "我很喜欢这个产品!",
    "这个产品一般般。",
    "我不太喜欢这个产品。",
    "这个产品质量很好!"
]

threshold = 0.8
filtered_comments = remove_similar_comments(comments, threshold)

print("保留的评论:")
for comment in filtered_comments:
    print(comment)

【以下回答由 GPT 生成】

问题分析

要解决这个问题,我们需要比较文本之间的相似性,并删除相似的句子。通常,计算文本相似性涉及以下步骤: 1. 清洗文本数据,例如去除特殊符号、停用词等。 2. 对清洗后的文本进行分词处理。 3. 计算文本之间的相似性。 4. 根据相似性进行筛选,删除相似的句子。

解决方案

根据问题的描述,我们可以采用以下步骤来实现程序:

  1. 安装必要的Python库,例如nltkgensim等。
pip install nltk gensim
  1. 导入必要的库,并下载nltk的停用词和分词器。
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize

nltk.download('stopwords')
nltk.download('punkt')
  1. 清洗文本数据。去除特殊符号、停用词等。
def clean_text(text):
    # 去除特殊符号
    text = text.lower().strip()
    text = re.sub(r'[^\w\s]', '', text)

    # 去除停用词
    stop_words = set(stopwords.words('english'))
    words = word_tokenize(text)
    words = [word for word in words if word not in stop_words]

    return ' '.join(words)
  1. 计算文本之间的相似性。可以使用词袋模型和余弦相似度来计算。
from gensim import corpora
from gensim.models import TfidfModel
from gensim.similarities import MatrixSimilarity

def calculate_similarity(texts):
    # 构建词袋模型
    corpus = [word_tokenize(text) for text in texts]
    dictionary = corpora.Dictionary(corpus)
    corpus = [dictionary.doc2bow(text) for text in corpus]

    # 计算TF-IDF
    tfidf = TfidfModel(corpus)
    corpus_tfidf = tfidf[corpus]

    # 构建索引
    index = MatrixSimilarity(corpus_tfidf)

    # 计算相似性
    similarity_matrix = index[corpus_tfidf]

    return similarity_matrix
  1. 删除相似的句子。
def remove_similar_sentences(texts, similarity_matrix, threshold):
    # 根据相似性阈值删除相似的句子
    cleaned_texts = []
    for i, row in enumerate(similarity_matrix):
        similar_indices = [j for j, similarity in enumerate(row) if similarity > threshold]
        cleaned_text = [text for j, text in enumerate(texts) if j not in similar_indices]
        cleaned_texts.append(cleaned_text)

    return cleaned_texts
  1. 完整的解决方案示例:
import re
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from gensim import corpora
from gensim.models import TfidfModel
from gensim.similarities import MatrixSimilarity

def clean_text(text):
    # 去除特殊符号
    text = text.lower().strip()
    text = re.sub(r'[^\w\s]', '', text)

    # 去除停用词
    stop_words = set(stopwords.words('english'))
    words = word_tokenize(text)
    words = [word for word in words if word not in stop_words]

    return ' '.join(words)

def calculate_similarity(texts):
    # 构建词袋模型
    corpus = [word_tokenize(text) for text in texts]
    dictionary = corpora.Dictionary(corpus)
    corpus = [dictionary.doc2bow(text) for text in corpus]

    # 计算TF-IDF
    tfidf = TfidfModel(corpus)
    corpus_tfidf = tfidf[corpus]

    # 构建索引
    index = MatrixSimilarity(corpus_tfidf)

    # 计算相似性
    similarity_matrix = index[corpus_tfidf]

    return similarity_matrix

def remove_similar_sentences(texts, similarity_matrix, threshold):
    # 根据相似性阈值删除相似的句子
    cleaned_texts = []
    for i, row in enumerate(similarity_matrix):
        similar_indices = [j for j, similarity in enumerate(row) if similarity > threshold]
        cleaned_text = [text for j, text in enumerate(texts) if j not in similar_indices]
        cleaned_texts.append(cleaned_text)

    return cleaned_texts

# 示例用法
texts = [
    "This is a sample sentence.",
    "This is another sample sentence.",
    "This is a similar sentence.",
    "This sentence is not similar."
]

cleaned_texts = [clean_text(text) for text in texts]
similarity_matrix = calculate_similarity(cleaned_texts)
cleaned_texts = remove_similar_sentences(texts, similarity_matrix, 0.8)

print(cleaned_texts)

注意事项

  • 需要确保已安装所需的Python库,并且下载了nltk的停用词和分词器。
  • 以上代码仅为示例,具体应根据实际需求进行修改和调整。
  • 相似性阈值的选择可能需要根据实际情况进行调试。根据问题的描述,设定一个合适的阈值来确定相似的句子。
  • 上述方案主要使用了词袋模型和余弦相似度进行相似性计算,如果对结果不满意,可以尝试其他的文本相似性计算方法,如Word2Vec、BERT等。

以上是使用Python删除文本中相似内容的解决方案。希望能对您有所帮助。如果您有任何问题,请随时提问。



【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^