2017-04-26 184 views
-5

爲您提供了四個文檔,編號爲1到4,每個文檔都有一個文本句子。根據TF-IDF分數計算,確定與第一個文檔最相似的文檔的標識符。Python中兩個文本文檔之間的相似性

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

輸出整數(可以是2或3或4),不留任何前導或尾隨空格。

+2

你試過的,顯示你的代碼。 –

回答

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) 
相關問題