句子相似度_tf/idf
import math
from math import isnan
import pandas as pd
#结巴分词,切开之后,有分隔符
def jieba_function(sent):
import jieba
sent1 = jieba.cut(sent)
s = []
for each in sent1:
s.append(each)
return ' '.join(str(i) for i in s)
def count_cos_similarity(vec_1, vec_2):
if len(vec_1) != len(vec_2):
return 0 s = sum(vec_1[i] * vec_2[i] for i in range(len(vec_2)))
den1 = math.sqrt(sum([pow(number, 2) for number in vec_1]))
den2 = math.sqrt(sum([pow(number, 2) for number in vec_2]))
return s / (den1 * den2)
#计算文本向量,传入文本,接受的是字符串
def tf(sent1, sent2):
from sklearn.feature_extraction.text import CountVectorizer sent1 = jieba_function(sent1)
sent2 = jieba_function(sent2) count_vec = CountVectorizer() sentences = [sent1, sent2]
print('sentences',sentences)
print('vector',count_vec.fit_transform(sentences).toarray())## 输出特征向量化后的表示
print('cut_word',count_vec.get_feature_names())#输出的是切分的词, 输出向量各个维度的特征含义 #转换成维度相同的
vec_1 = count_vec.fit_transform(sentences).toarray()[0]
vec_2 = count_vec.fit_transform(sentences).toarray()[1]
similarity=count_cos_similarity(vec_1, vec_2)
if isnan(similarity):
similarity=0.0 print('count_cos_similarity',similarity)
def tfidf(sent1, sent2):
from sklearn.feature_extraction.text import TfidfVectorizer sent1 = jieba_function(sent1)
sent2 = jieba_function(sent2) tfidf_vec = TfidfVectorizer() sentences = [sent1, sent2]
vec_1 = tfidf_vec.fit_transform(sentences).toarray()[0]
vec_2 = tfidf_vec.fit_transform(sentences).toarray()[1]
similarity=count_cos_similarity(vec_1, vec_2)
if isnan(similarity):
similarity=0.0
return similarity if __name__=='__main__': sent1 = '我喜欢看电视也喜欢看电影,'
sent2 = '我不喜欢看电视也不喜欢看电影'
print('<<<<tf<<<<<<<')
tf(sent1, sent2)
print('<<<<tfidf<<<<<<<')
tfidf(sent1, sent2)
句子相似度_tf/idf的更多相关文章
- 使用 TF-IDF 加权的空间向量模型实现句子相似度计算
使用 TF-IDF 加权的空间向量模型实现句子相似度计算 字符匹配层次计算句子相似度 计算两个句子相似度的算法有很多种,但是对于从未了解过这方面算法的人来说,可能最容易想到的就是使用字符串匹配相关的算 ...
- NLP入门(一)词袋模型及句子相似度
本文作为笔者NLP入门系列文章第一篇,以后我们就要步入NLP时代. 本文将会介绍NLP中常见的词袋模型(Bag of Words)以及如何利用词袋模型来计算句子间的相似度(余弦相似度,cosi ...
- [LeetCode] 737. Sentence Similarity II 句子相似度 II
Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...
- [LeetCode] 734. Sentence Similarity 句子相似度
Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...
- LSTM 句子相似度分析
使用句子中出现单词的Vector加权平均进行文本相似度分析虽然简单,但也有比较明显的缺点:没有考虑词序且词向量区别不明确.如下面两个句子: "北京的首都是中国"与"中国的 ...
- [LeetCode] Sentence Similarity 句子相似度
Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...
- Wordvec_句子相似度
import jiebafrom jieba import analyseimport numpyimport gensimimport codecsimport pandas as pdimport ...
- [LeetCode] Sentence Similarity II 句子相似度之二
Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...
- [LeetCode] 737. Sentence Similarity II 句子相似度之二
Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...
随机推荐
- 2.5、CDH 搭建Hadoop在安装(设置Cloudera Manager数据库)
步骤5:设置Cloudera Manager数据库 Cloudera Manager Server包含一个可以为自己创建和配置数据库的脚本.该脚本可以: 创建Cloudera Manager Serv ...
- http4e eclipse plugin 插件介绍
感谢作者的分享: http://blog.csdn.net/wiker_yong/article/details/10066905 以及作者的破解jar.目前看网站留言说已经git了. 官网链接地址: ...
- Docker学习之安装mysql
1.从Docker检索mysql镜像 指令: docker search mysql 2.镜像下载 指令: docker pull mysql:5.7.19 3.查看本地镜像列表 指令: docker ...
- Python+Selenium学习--定位iframe中的对象
场景 在web 应用中经常会出现frame 嵌套的应用,假设页面上有A.B 两个frame,其中B 在A 内,那么定位B 中的内容则需要先到A,然后再到B. switch_to_frame ...
- js和jquery实现图片无缝轮播的不同写法
多掌握一种方法总是会有好处的,学习编程就要多思考,举一反三 下面写一下实现图片自动播放的代码,由于学习的是javascript,代码量很大,所以又学习了jquery库的操作,非常简便 还有非常有逼格的 ...
- CodeForces - 55D(数位dp,离散化)
题目来源:http://codeforces.com/problemset/problem/55/D Volodya is an odd boy and his taste is strange as ...
- 在IDEA中使用gradle配置打可执行jar包[可执行jar与其所依赖的jar分离]
下面是我的项目的build.gradle文件的所有内容,注意高亮部分是关键: group 'com.xbs' version '1.0-SNAPSHOT' apply plugin: 'java' a ...
- 489. Robot Room Cleaner扫地机器人
[抄题]: Given a robot cleaner in a room modeled as a grid. Each cell in the grid can be empty or block ...
- 运行./build.sh出现错误:bash: ./build.sh: Permission denied
原因:文件为只读,或者用户没有权限. 修改方法: 运行命令: chmod 777 build.sh
- 在pom.xml中引入jar包坐标的依赖范围
A依赖B,需要在A的pom.xml文件中添加B的坐标,添加坐标时需要指定依赖范围,依赖范围包括: compile:编译范围,指A在编译时依赖B,此范围为默认依赖范围.编译范围的依赖会用在编译.测试.运 ...