Python中两个文本文档之间的相似性

时间:2017-04-26 10:26:40

标签: python machine-learning

您将获得四个文档,编号为1到4,每个文档都有一个文本句子。确定与第一个文档最相似的文档标识符,根据TF-IDF分数计算。

My name is Ankit,
Ankit name is very famous,
Ankit like his name
India has a lot of beautiful cities

输出整数(可以是2或3或4),不留前导或尾随空格。

1 个答案:

答案 0 :(得分:2)

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)