Search code examples
pythonlistsetcosine-similaritysentence-similarity

Sorting the result for sentence similarity in python


I am trying to find the similarity between the sentences tokenised document and a sentence getting the result saved in a list. I want to sort the results based on the similarity score. When I try to sort the output based on the similarity score I get an error?

results=[]


#embedding all the documents and find the similarity between search text and all the tokenize sentences
for docs_sent_token in docs_sent_tokens:
   sentence_embeddings = model.encode(docs_sent_token)
   sim_score1 = cosine_sim(search_sentence_embeddings, sentence_embeddings)
   if sim_score1 > 0:
               results.append({
                   sim_score1,
                   docs_sent_token,
               })
   results.sort(key=lambda k : k['sim_score1'] , reverse=True)
print(results)

This is the error I get.

TypeError: 'set' object is not subscriptable

This issue can be solved using dictionaries.

if sim_score1 > 0:
                results.append({
                    'Score':sim_score1,
                    'Token':docs_sent_token,
                })
    results.sort(key=lambda k : k['Score'] , reverse=True)
print(results)

But is there any possible way to get the sorting done using the list? I want to get the result in this format.

[{0.91, 'Sentence 1'}, {0.87, 'Sentence 2'}, {0.33, 'Sentence 3'}, {0.30, 'Sentence 4'},

Solution

  • sets don't have indices or keys to indicate a value to sort by. You can create a list of tuples or dicts instead, sort it and convert it to sets later on

    results.append((
        sim_score1,
        docs_sent_token
    ))
    
    # results = [(0.91, 'Sentence 1'), (0.33, 'Sentence 3'), (0.87, 'Sentence 2'), (0.30, 'Sentence 4')]
    results.sort(key=lambda k: k[0], reverse=True)
    results = [set(t) for t in results]
    
    # or
    
    results.append({
        'Score': sim_score1,
        'Token': docs_sent_token
    })
    
    # results = [{'Score': 0.91, 'Token': 'Sentence 1'}, {'Score': 0.33, 'Token': 'Sentence 3'}, {'Score': 0.87, 'Token': 'Sentence 2'}, {'Score': 0.30, 'Token': 'Sentence 4'}]
    results.sort(key=lambda k: k['Score'], reverse=True)
    results = [set(d.values()) for d in results]
    
    print(results)
    

    Output

    [{0.91, 'Sentence 1'}, {0.87, 'Sentence 2'}, {0.33, 'Sentence 3'}, {0.3, 'Sentence 4'}]