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的更多相关文章

  1. 使用 TF-IDF 加权的空间向量模型实现句子相似度计算

    使用 TF-IDF 加权的空间向量模型实现句子相似度计算 字符匹配层次计算句子相似度 计算两个句子相似度的算法有很多种,但是对于从未了解过这方面算法的人来说,可能最容易想到的就是使用字符串匹配相关的算 ...

  2. NLP入门(一)词袋模型及句子相似度

      本文作为笔者NLP入门系列文章第一篇,以后我们就要步入NLP时代.   本文将会介绍NLP中常见的词袋模型(Bag of Words)以及如何利用词袋模型来计算句子间的相似度(余弦相似度,cosi ...

  3. [LeetCode] 737. Sentence Similarity II 句子相似度 II

    Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...

  4. [LeetCode] 734. Sentence Similarity 句子相似度

    Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...

  5. LSTM 句子相似度分析

    使用句子中出现单词的Vector加权平均进行文本相似度分析虽然简单,但也有比较明显的缺点:没有考虑词序且词向量区别不明确.如下面两个句子: "北京的首都是中国"与"中国的 ...

  6. [LeetCode] Sentence Similarity 句子相似度

    Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...

  7. Wordvec_句子相似度

    import jiebafrom jieba import analyseimport numpyimport gensimimport codecsimport pandas as pdimport ...

  8. [LeetCode] Sentence Similarity II 句子相似度之二

    Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...

  9. [LeetCode] 737. Sentence Similarity II 句子相似度之二

    Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...

随机推荐

  1. asp.net mvc areas

    http://www.codeproject.com/Articles/714356/Areas-in-ASP-NET-MVC

  2. cdnbest如何配置ssl证书

    cdnbest添加ssl证书有三种方式: 一.第一种在站点设置中添加: 点打开,加入证书后点提交 可以点检测功能检查证书是否有效,打勾说明证书是有效的 二. 第二种是在域名记录里添加: 如下图点击,添 ...

  3. veil-catapult

    veil-catapult进行payload投放,直接使payload在内存中执行. kaliIP:192.168.1.119 目标IP:192.168.1.121 1.powershell powe ...

  4. VM虚拟机的配置文件(.vmx)损坏

    为了禁用时间同步,使用sublime修改vmx文件 文件第一行为.encoding = "GBK" 修改完毕,无法打开虚拟机,报 VM虚拟机的配置文件(.vmx)损坏错误 因为su ...

  5. java并发:jdk1.8中ConcurrentHashMap源码浅析

    ConcurrentHashMap是线程安全的.可以在多线程中对ConcurrentHashMap进行操作. 在jdk1.7中,使用的是锁分段技术Segment.数据结构是数组+链表. 对比jdk1. ...

  6. JAVA 关于JNI本地库加载

    1.调用JNI的时候,通常我们使用System.loadLibrary(String libname)来load JNI library, 同样也可以使用System.load(String file ...

  7. Linux系统一本通(实用篇)

    本人最近一直在ubuntu,接下来和大家分享我曾经踩过的坑,和一些非常实用的命令知识- 安装中的磁盘分配 一般来说,在linux系统中都有最少两个挂载点,分别是/ (根目录)及 swap(交换分区), ...

  8. Codeforces Beta Round #65 (Div. 2)

    Codeforces Beta Round #65 (Div. 2) http://codeforces.com/contest/71 A #include<bits/stdc++.h> ...

  9. 项目总结02:百度地图js 基本用法介绍

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  10. 集成bug统计链接

    http://crab.baidu.com/http://bugly.qq.com/ http://bughd.com/ http://www.umeng.com/analyticshttp://tr ...