from gensim import corpora, models, similarities
documents = ["This is a book about cars, dinosaurs, and fences"]
# remove common words and tokenize
stoplist = set('for a of the and to in - , is'.split())
texts = [[word for word in document.lower().split() if word not in stoplist]
for document in documents]
# Remove commas
texts[0] = [text.replace(',','') for text in texts[0]]
dictionary = corpora.Dictionary(texts)
corpus = [dictionary.doc2bow(text) for text in texts]
lsi = models.LsiModel(corpus, id2word=dictionary, num_topics=2)
doc = "I like cars and birds"
vec_bow = dictionary.doc2bow(doc.lower().split())
vec_lsi = lsi[vec_bow]
index = similarities.MatrixSimilarity(lsi[corpus])
sims = index[vec_lsi] # perform a similarity query against the corpus
print(sims)
In dem obigen Code, den ich zu vergleichen bin wie viel „Dies ist ein Buch über Autos, Dinosaurier, und Zaun“ ist ähnlich wie „Ich mag Autos und Vögel“, um die Kosinusähnlichkeit Technik .Text Ähnlichkeit mit GENSIM und Kosinusähnlichkeit
Die beiden Sätze haben effektiv 1 Wörter gemeinsam, das ist "Autos", aber wenn ich den Code ausführen, bekomme ich, dass sie 100% ähnlich sind. Das ergibt für mich keinen Sinn.
Kann jemand vorschlagen, wie ich meinen Code verbessern kann, damit ich eine vernünftige Nummer bekomme?