I have a string which I'm trying to compare to a list of strings. The objective is to rank the list of strings from most similar to least similar using cosine similarity.
original_string = 'abc'
string_list = ['abc', 'abcd', 'abec', 'ab', 'abcde', 'qwe']
That's my code so far:
sparse_matrix = tfidf_vectorizer.fit_transform(string_list)
doc_term_matrix = sparse_matrix.todense()
df = pd.DataFrame(
doc_term_matrix,
columns=tfidf_vectorizer.get_feature_names(),
index=[df_list],
)
cosine = pd.DataFrame(cosine_similarity(df))
However this does pairwise between elements in the dataframe and I'm not sure how to transform the single string into a vector and add it as an argument to the cosine_similarity function as Y?