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)