Skip to main content

中文文本相似度计算器 | Text Similarity Calculator

Project description

Xiangshi

中文文本相似度计算器

Pypi Version Downloads

简体中文 | English

相识是一个中文文本相似度计算器,提供4个传统相似度算法,分别是:余弦相似度,Simhash,Minhash以及Jaccard(杰卡德)。

在线计算文本相似度

下载与安装

Pip安装:

pip3 install xiangshi

国内较慢的话可以使用清华镜像:

pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple xiangshi

使用方法

计算文本相似度

余弦相似度

import xiangshi as xs
xs.cossim("如何更换花呗绑定银行卡", "花呗更改绑定银行卡")

Simhash & Minhash & Jaccard相似度

import xiangshi as xs
# Simhash
xs.simhash("如何更换花呗绑定银行卡", "花呗更改绑定银行卡")
# Minhash
xs.minhash("如何更换花呗绑定银行卡", "花呗更改绑定银行卡")
# Jaccard
xs.jaccard("如何更换花呗绑定银行卡", "花呗更改绑定银行卡")

其它加权方法

默认的加权方法是计算每个单词在文中出现的数量,以下还有其他两种加权方法可供选择。

TFIDF

arg = [
    "西班牙失业率创新高",
    "澳大利亚失业率高达5.1%",
    "花呗更改绑定银行卡",
    "我什么时候开通了花呗",
    "从这个角度来看, 我们一般认为,抓住了问题的关键,其他一切则会迎刃而解。"
    "从这个角度来看, 每个人都不得不面对这些问题。"

]
xs.weight = "TFIDF" # 将加权方式设置为TFIDF
xs.construct(arg) # 输入TFIDF文本,相同的文本只需调用这个函数一次

xs.cossim("如何更换花呗绑定银行卡", "花呗更改绑定银行卡")
xs.simhash("如何更换花呗绑定银行卡", "花呗更改绑定银行卡")

没有加权

xs.weight = "None" # 将加权方式设置为None
xs.cossim("如何更换花呗绑定银行卡", "花呗更改绑定银行卡")

英文文本相似度

在v4.2.1之后,相识支持英文文本相似度(即使用英文停用词、英文分词方法)。

import xiangshi as xs
xs.lang = "en"
xs.cossim("A mathematician found a solution to the problem.", "The problem was solved by a young mathematician.")

修改默认函数

import xiangshi as xs
#计算Simhash时取前多少的TFIDF值。默认值为64
xs.feature = 64
#计算Minhash时算出多少个哈希值。默认值为16
xs.HashNums = 16
#计算Minhash时的最大哈希。默认值为4294967311
xs.prime = 4294967311

修改停用词

在v4.2.1之后,相识支持更改默认停用词:

import xiangshi as xs
stopwords = ["你好", "世界"]
xs.update_stopwords(stopwords)

新版本

v4.2.1:

  • 支持英文文本相似度

注意:

  • v4.2.0+文本相似度的计算结果可能和v4.1.0不一样,因为v4.1.0加权方式不同。
  • v4.2.0+文本相似度的输入均为两个string,且不与v4.1.0反向兼容。
  • v4.2.0+不再支持文本聚类(如果还有人需要的话请联系我,我会另开一个包)

其他链接

相识寓意

同是天涯沦落人,相逢何必曾相识

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xiangshi-4.2.2.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

xiangshi-4.2.2-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file xiangshi-4.2.2.tar.gz.

File metadata

  • Download URL: xiangshi-4.2.2.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.4

File hashes

Hashes for xiangshi-4.2.2.tar.gz
Algorithm Hash digest
SHA256 6b656545a90cc7c1aa3db4aebd57aee3a0352542efa2c13ea58588d3aeaed73b
MD5 96f88d8affc3f603ccf257c19f3bef00
BLAKE2b-256 bc33c8284f8841976ae6f2014b5149f24db0082afd756616377969ae1dd00403

See more details on using hashes here.

File details

Details for the file xiangshi-4.2.2-py3-none-any.whl.

File metadata

  • Download URL: xiangshi-4.2.2-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.4

File hashes

Hashes for xiangshi-4.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 49783f99a532b0c45fc84dd01a6f0be2a72a7a6fede054724e31a1438c5baeab
MD5 f39ed9f1fd8da3493b1e6e20c50d168d
BLAKE2b-256 64f4fd0588d034bfd0ff817347d16dd8cea7126260e1610b9a87eb38ade142cc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page