私は一連のファイルとクエリ doc を持っています。私の目的は、各ドキュメントのクエリ doc と比較して、最も類似したドキュメントを返すことです。最初にコサイン類似度を使用するには、ドキュメント文字列をベクトルにマップする必要があります。ドキュメントごとに計算する tf-idf 関数を作成しました。
文字列のインデックスを取得するには、そのような関数があります。
def getvectorKeywordIndex(self, documentList):
""" create the keyword associated to the position of the elements within the document vectors """
#Mapped documents into a single word string
vocabularyString = " ".join(documentList)
vocabularylist= vocabularyString.split(' ')
vocabularylist= list(set(vocabularylist))
print 'vocabularylist',vocabularylist
vectorIndex={}
offset=0
#Associate a position with the keywords which maps to the dimension on the vector used to represent this word
for word in vocabularylist:
vectorIndex[word]=offset
offset+=1
print vectorIndex
return vectorIndex,vocabularylist #(keyword:position),vocabularylist
コサイン類似度の場合、私の機能は次のとおりです。
def cosine_distance(self,index, queryDoc):
vector1= self.makeVector(index)
vector2= self.makeVector(queryDoc)
return numpy.dot(vector1, vector2) / (math.sqrt(numpy.dot(vector1, vector1)) * math.sqrt(numpy.dot(vector2, vector2)))
TF-IDFは;
def tfidf(self, term, key):
return (self.tf(term,key) * self.idf(term))
私の問題は、インデックスと語彙リスト、およびこの関数内の tf-idf を使用して makevector を作成するにはどうすればよいかということです。どんな答えでも大歓迎です。