Reputation: 9752
I have defined the following function that takes an input string, and compares it to a large list of strings (both vectorized using tfidf):
def find_new_similar(tfidf_matrix2, index, tfidf_matrix, top_n = 5):
cosine_similarities = linear_kernel(tfidf_matrix2[index:index+1], tfidf_matrix).flatten()
related_docs_indices = [i for i in cosine_similarities.argsort()[::-1] if i != index]
return [(i, cosine_similarities[i]) for i in related_docs_indices][0:top_n], index
when I call this function my output is:
find_new_similar(tfidf_matrix2, 1, tfidf_matrix)
Out[15]:
([(923576, 0.51192576542407131),
(558563, 0.51192576542407131),
(1554977, 0.51192576542407131),
(1604772, 0.51192576542407131),
(514529, 0.50251903670563314)],
1)
where the first element of each tuple (ie. 923576, 558563) is an index for a large file of terms. I would like to use these indexes and return the value at the index.
I have tried:
for i, score in find_new_similar(tfidf_matrix2, 0, tfidf_matrix):
print (score, corpus[i], i)
Traceback (most recent call last):
File "<ipython-input-18-792db65f6fd0>", line 1, in <module>
for i, score in find_new_similar(tfidf_matrix2, 0, tfidf_matrix):
ValueError: too many values to unpack (expected 2)
can anyone help? thanks?
Upvotes: 2
Views: 3527
Reputation: 2639
You function returns a list and index
return [(i, cosine_similarities[i]) for i in related_docs_indices][0:top_n], index
change your code to
for i, score in find_new_similar(tfidf_matrix2, 0, tfidf_matrix)[0]:
print (score, corpus[i], i)
to get the list and iterate over it.
Upvotes: 4