Python编程:通过交集并集计算文档相似度

简介: Python编程:通过交集并集计算文档相似度

分词函数

def split_word(document):
    """
    分词,去除停用词
    """
    stop_words = {":", "的", ",", "”"}
    text = []
    for word in jieba.cut(document):
        if word not in stop_words:
            text.append(word)
    return text

通过交集并集计算文档相似度

from itertools import combinations
documents = [
    "窝趣公寓完成近2亿元B轮融资主打品质和轻松社交的居住环境",
    "IBM的区块链副总裁JesseLund:比特币将达到100万美元",
    "窝趣公寓完成近2亿元B轮融资"
]
# 计算两两组合的相似度
for doc1, doc2 in combinations(documents, 2):
    words1 = split_word(doc1)
    words2 = split_word(doc2)
    words1_set = set(words1)
    words2_set = set(words2)
    similar12 = len(words1_set & words2_set) / len(words1_set | words2_set)
    print("{:.2f}".format(similar12), doc1, doc2)

计算结果

0.00 窝趣公寓完成近2亿元B轮融资主打品质和轻松社交的居住环境 IBM的区块链副总裁JesseLund:比特币将达到100万美元
0.53 窝趣公寓完成近2亿元B轮融资主打品质和轻松社交的居住环境 窝趣公寓完成近2亿元B轮融资
0.00 IBM的区块链副总裁JesseLund:比特币将达到100万美元 窝趣公寓完成近2亿元B轮融资
相关文章
|
存储 索引 Python
leetcode-350:两个数组的交集 II(python中Counter的用法,海象运算符:=)
leetcode-350:两个数组的交集 II(python中Counter的用法,海象运算符:=)
138 0
|
存储 Python
Python多个set中的交集
Python多个set中的交集
213 1
|
存储 自然语言处理 数据处理
使用Python计算多个集合的交集详解
使用Python计算多个集合的交集详解
399 1
|
存储 JavaScript 前端开发
【经典算法】LeetCode350:两个数组的交集 II(Java/C/Python3/JavaScript实现含注释说明,Easy)
【经典算法】LeetCode350:两个数组的交集 II(Java/C/Python3/JavaScript实现含注释说明,Easy)
89 1
|
机器学习/深度学习 Python
Python应用专题 | 23:Pandas中两个dataframe的交集和差集
如何求Pandas中两个dataframe的交集和差集?
|
C++ Python
LeetCode 350. 两个数组的交集 II C/C++/Python
LeetCode 350. 两个数组的交集 II C/C++/Python
161 0
|
Python
使用python比较两个数组,获取交集和并集
使用python比较两个数组,获取交集和并集
331 0
|
Python
Python求列表的差集、交集与并集?
公众号新增加了一个栏目,就是每天给大家解答一道Python常见的面试题,反正每天不贪多,一天一题,正好合适,只希望这个面试栏目,给那些正在准备面试的同学,提供一点点帮助!
292 0
Python求列表的差集、交集与并集?
|
自然语言处理 Python
Python编程:通过交集并集计算文档相似度
Python编程:通过交集并集计算文档相似度
180 0
|
算法 Python
<LeetCode天梯>Day009 两个数组的交集 II | 初级算法 | Python
<LeetCode天梯>Day009 两个数组的交集 II | 初级算法 | Python
<LeetCode天梯>Day009 两个数组的交集 II | 初级算法 | Python

推荐镜像

更多