python jieba 文本相似度_gensim里的similarities文本相似度计算

本代码演示如何使用gensim的similarities接口结合jieba分词,处理中文文本,进行相似度计算。通过读取数据库,对文章摘要进行分词,构建语料库,应用TF-IDF模型,然后计算相似度并将结果存入数据库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# -*- coding:utf-8 -*

#本代码是在jupyter notebook上实现,author:huzhifei, create time:2018/8/14

#本脚本主要实现了基于python的gensim包里的similarities接口对文本做相似度的项目目的

#导入gensim与jieba包

from gensim import corpora, models, similarities

import jieba

#去除中英停用词

def get_custom_stopwords(stop_words_file):

with open(stop_words_file,encoding='utf-8')as f:

stopwords=f.read()

stopwords_list=stopwords.split('\n')

custom_stopwords_list=[i for i in stopwords_list]

return custom_stopwords_list

#调用停用词函数

stop_words_file="stopwordsHIT.txt"

stopwords=get_custom_stopwords(stop_words_file)

print(len(stopwords))

#jieba分词函数

def cut(sentence):

generator = jieba.cut(sentence)

return [word for word in generator if word not in stopwords]

#连接数据库

import pyodbc

conn = 'DRIVER={SQL Server Native Client 10.0};DATABASE=%s;SERVER=%s

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值