Python 文本相似性。余弦相似性。指定结果

Python 文本相似性。余弦相似性。指定结果,python,Python,我刚刚完成了我的第一个余弦相似代码,并成功地对结果进行了编码。但是,我想以更具体的方式显示结果。有没有办法知道37.8%是如何计算出来的?非常酷当然也会是一个图形或任何类似的东西 这是我的密码: f=open (r"C:\Users\Output11.txt") doc1=str(f.read()) f1=open(r"C:\Users\Output22.txt") doc2=str(f1.read()) def cosine_distance(s1, s2): allsentences

我刚刚完成了我的第一个余弦相似代码,并成功地对结果进行了编码。但是,我想以更具体的方式显示结果。有没有办法知道37.8%是如何计算出来的?非常酷当然也会是一个图形或任何类似的东西

这是我的密码:

f=open (r"C:\Users\Output11.txt")
doc1=str(f.read())
f1=open(r"C:\Users\Output22.txt")
doc2=str(f1.read())
def cosine_distance(s1, s2):
    allsentences=[doc1 , doc2]

    from sklearn.feature_extraction.text import CountVectorizer
    from scipy.spatial import distance

    vectorizer=CountVectorizer()
    all_sentences_to_vector = vectorizer.fit_transform(allsentences)
    text_to_vector_v1 = all_sentences_to_vector.toarray()[0].tolist()
    text_to_vector_v2 = all_sentences_to_vector.toarray()[1].tolist()
    cosine = distance.cosine(text_to_vector_v1, text_to_vector_v2)
    print('Similarity of two sentences are equal to ',round((1-cosine)*100,2),'%')
    return cosine
cosine_distance(doc1 , doc2)
结果正如我所说:

Similarity of two sentences are equal to  37.8 %

你只需要做一些打印。。。您的百分比是通过执行
四舍五入((1-余弦)*100,2)
来计算的。因此,您可以打印
cosine
。打印:您可以检查库
matplotlib