python 文本分词+文本相似度分析

一、分词

  1. 分词工具
    1、python︱六款中文分词模块尝试:jieba、THULAC、SnowNLP、pynlpir、CoreNLP、pyLTP
    2、Hanlp
    首先要对句子进行初步处理。本文对文本依次进行了【去空去重、切词分词和停用词过滤】操作。

    原始数据会存在一些【空或重复的语句】,须过滤掉这些【无价值且影响效率】的语句。使用计算机自动地对中文文本进行词语切分的过程称为中文分词(Chinese Word Segmentation),即使中文句子中的词之间有空格标识。若要对一个句子进行分析,就需要将其切分成词的序列,然后以词为单位进行句子的分析,故中文分词是中文自然语言处理中最基本的一个环节。

  2. 生成分词列表
    1、首先停用词过滤,返回一个停用词表
    可以使用中科院的“计算所汉语词性标记集”以及哈工大停用词表

def StopWordsList(filepath):
    wlst = [w.strip() for w in open(filepath,'r',encoding='utf8').readlines()]
    return wlst

2、结巴分词后的停用词性 [标点符号、连词、助词、副词、介词、时语素、‘的’、数词、方位词、代词]

stop_flag = ['x', 'c', 'u','d', 'p', 't', 'uj', 'm', 'f', 'r']
对文本集中的文本进行中文分词,返回分词列表
def seg_sentence(sentence,stop_words):
    sentence_seged = jieba.cut(sentence.strip())
    # sentence_seged = set(sentence_seged)
    outstr = ''
    for word in sentence_seged:
        if word not in stop_words:
            if word != '\t':
                outstr += word
                outstr += ' '

    return outstr.split(' ')

二、 基于文本集建立词典,计算相似度

 #1、将【文本集】生产【分词列表】
    texts = [seg_sentence(seg,stop_words) for seg in open(tpath,'r',encoding='utf8').readlines()]
#一、建立词袋模型
    #2、基于文件集建立【词典】,并提取词典特征数
    dictionary = corpora.Dictionary(texts)
    feature_cnt = len(dictionary.token2id.keys())
    #3、基于词典,将【分词列表集】转换为【稀疏向量集】,也就是【语料库】
    corpus = [dictionary.doc2bow(text) for text in texts]
#二、建立TF-IDF模型
    #4、使用“TF-TDF模型”处理【语料库】
    tfidf = models.TfidfModel(corpus)
#三构建一个query文本,利用词袋模型的字典将其映射到向量空间    
    #5、同理,用词典把搜索词也转换为稀疏向量
    kw_vector = dictionary.doc2bow(seg_sentence(keyword,stop_words))
    #6、对稀疏向量建立索引
    index = similarities.SparseMatrixSimilarity(tfidf[corpus],num_features=feature_cnt)
    #7、相似的计算
    sim = index[tfidf[kw_vector]]

全部代码:

import jieba
import jieba.posseg as pseg
from gensim import corpora, models, similarities

def StopWordsList(filepath):
    wlst = [w.strip() for w in open(filepath, 'r', encoding='utf8').readlines()]
    return wlst


def seg_sentence(sentence, stop_words):
    # stop_flag = ['x', 'c', 'u', 'd', 'p', 't', 'uj', 'm', 'f', 'r']#过滤数字m
    stop_flag = ['x', 'c', 'u', 'd', 'p', 't', 'uj', 'f', 'r']
    sentence_seged = pseg.cut(sentence)
    # sentence_seged = set(sentence_seged)
    outstr = []
    for word,flag in sentence_seged:
        # if word not in stop_words:
        if word not in stop_words and flag not in stop_flag:
            outstr.append(word)
    return outstr


if __name__ == '__main__':
    spPath = 'stopwords.txt'
    tpath = 'test.txt'

    stop_words = StopWordsList(spPath)
    keyword = '吃鸡'

    # 1、将【文本集】生产【分词列表】
    texts = [seg_sentence(seg, stop_words) for seg in open(tpath, 'r', encoding='utf8').readlines()]
    orig_txt = [seg for seg in open(tpath, 'r', encoding='utf8').readlines()]

#一、建立词袋模型
    # 2、基于文件集建立【词典】,并提取词典特征数
    dictionary = corpora.Dictionary(texts)
    feature_cnt = len(dictionary.token2id.keys())
    # 3、基于词典,将【分词列表集】转换为【稀疏向量集】,也就是【语料库】
    corpus = [dictionary.doc2bow(text) for text in texts]
    # 4、使用“TF-TDF模型”处理【语料库】
#二、建立TF-IDF模型
    tfidf = models.TfidfModel(corpus)
#三构建一个query文本,利用词袋模型的字典将其映射到向量空间
    # 5、同理,用词典把搜索词也转换为稀疏向量
    kw_vector = dictionary.doc2bow(seg_sentence(keyword, stop_words))
    # 6、对稀疏向量建立索引
    index = similarities.SparseMatrixSimilarity(tfidf[corpus], num_features=feature_cnt)
    # 7、相似的计算
    sim = index[tfidf[kw_vector]]

    result_list = []
    for i in range(len(sim)):
        print('keyword 与 text%d 相似度为:%.2f' % (i + 1, sim[i]))
        if sim[i] > 0.4:
            result_list.append(orig_txt[i])

    print('原始的句子:',result_list)

参考资料

python文本相似度计算

猜你喜欢

转载自blog.csdn.net/qq_42988748/article/details/82657562