本文介绍了Python中两个文本文档之间的相似性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为您提供了四个文档,编号为1到4,每个文档都有一个文本句子.确定根据TF-IDF分数计算出的与第一个文档最相似的文档的标识符.
You are provided with four documents, numbered 1 to 4, each with a single sentence of text. Determine the identifier of the document which is the most similar to the first document, as computed according to the TF-IDF scores.
My name is Ankit,
Ankit name is very famous,
Ankit like his name
India has a lot of beautiful cities
输出整数(可以是2或3或4),不留前导或尾随空格.
Output the integer (which may be either 2 or 3 or 4), leaving no leading or trailing spaces.
推荐答案
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
vect = TfidfVectorizer(min_df=1)
tfidf = vect.fit_transform(["My name is Ankit",
"Ankit name is very famous",
"Ankit like his name",
"India has a lot of beautiful cities"])
print ((tfidf * tfidf.T).A)
这篇关于Python中两个文本文档之间的相似性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!