Search code examples
pythonsimilarityspacy

Print only top 10 similarities in spacy


I am trying to calculate the similarity between two lists. They are quite different in size (one has ten sentences) and the other has 20k sentences. The goal is for each sentence in the short list to find the most similar sentence in the large list.

I tried to use spacy, but I can't use something like most_similar to only print the top 3 similarities for each sentence (In this test only top 3, with my massive data, top 10).

import spacy

little_list = ['nouveau livre révélant', 'bibliothèque pour la modélisation du sujet', 'potentiellement embarrassant']

big_list = ['plusieurs internationaux', 'quotidiens', 'il a grossi', 'pour rendre ces histoires disponibles', 'a aidé à prendre le contrôle', 'une catégorie pouvant inclure les emplois mêmes']

nlp = spacy.load("fr_core_news_sm")

little = list(nlp.pipe(little_list))
big = list(nlp.pipe(big_list))

scores = [(token1.text, token2.text, token1.similarity(token2)) for token2 in big for token1 in little]

import pandas as pd

df = pd.DataFrame(scores)

print(df)

My output:

                        0                          1           2
0   nouveau livre révélant  plusieurs internationaux    -0.131661
1   bibliothèque pour la modélisation du sujet  plusieurs internationaux    0.072430
2   potentiellement embarrassant    plusieurs internationaux    -0.267387
3   nouveau livre révélant  quotidiens  0.060414
4   bibliothèque pour la modélisation du sujet  quotidiens  0.036732
5   potentiellement embarrassant    quotidiens  -0.024117
6   nouveau livre révélant  il a grossi 0.243315
7   bibliothèque pour la modélisation du sujet  il a grossi -0.033608
8   potentiellement embarrassant    il a grossi 0.214442
9   nouveau livre révélant  pour rendre ces histoires disponibles   0.053788
10  bibliothèque pour la modélisation du sujet  pour rendre ces histoires disponibles   0.061164
11  potentiellement embarrassant    pour rendre ces histoires disponibles   0.045495
12  nouveau livre révélant  a aidé à prendre le contrôle    0.512961
13  bibliothèque pour la modélisation du sujet  a aidé à prendre le contrôle    0.372229
14  potentiellement embarrassant    a aidé à prendre le contrôle    0.118832
15  nouveau livre révélant  une catégorie pouvant inclure les emplois mêmes 0.159283
16  bibliothèque pour la modélisation du sujet  une catégorie pouvant inclure les emplois mêmes 0.191632
17  potentiellement embarrassant    une catégorie pouvant inclure les emplois mêmes 0.086039

I would like to print only the three major similarities of the zero column sentences.

I hope I have been able to explain clearly!


Solution

  • Okay, I think I understand what you're going for.

    From where you are with your df. You can group by the sentences in the small list, then sort the scores within the groups, and print the top three.

    grouped = df.groupby(by=0, axis=0)
    
    for sent1, group in grouped:
      print("Three most similar documents to: {sent1}\n")
      sorted_group = group.sort_values(by=2, ascending=False)
      print(sorted_group.iloc[:3, [1,2]])
      print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n')