Package for calculating the similarity between a pair or multiple sentences. This class uses vectorization and tokenization, along cosine similarity to calculate the similarity.
Project description
Strimilarity
Python package for calculating the similarity between a pair or multiple sentences. This package uses vectorization and tokenization, along cosine similarity to calculate the similarity. It allows the use of functions for comparing only the lexical or semantic similarity, or the use of a function calculating a wighted avarage of both types of similarities.
Installation
Dependencies
- nltk
- sklearn
- tensorflow
- tensorflow_hub
Installation
pip install strimilarity
Usage
Initialize
import strimilarity
starty = strimilarity.Strimilarity()
Languages
Strimilarity uses nltk for finding out the stopwords to remove from the sentences to compare. This allows the lexical similarity to be calculated without any words that may increase the similarity between sentences even when they don't contribute much to the actual meaning of the sentence.
Right now, Strimilarity by default uses English stopwords, but it can be initialized in Spanish as well.
Using spanish stopwords:
starty = strimilarity.Strimilarity('es')
Get similarity between a pair of sentences
If you want to get the similarity of just two sentences Strimilarity has three different functions:
- similarity_pair
- lexical_similarity_pair
- semantic_similarity_pair
Similarity Pair
This function returns the weighted avarage of the lexical and semantic similarity between two sentences.
Get similarity:
sentence1 = 'All you have to decide is what to do with the time that is given to you.'
sentence2 = 'You have to decide what to do now, we do not have time.'
starty = strimilarity.Strimilarity()
similarity = starty.similarity_pair(sentence1, sentence2)
print(similarity)
Result:
0.5634442369896036
This function also has the w_lexical and w_semantic optional parameters. This represent the weight for the lexical similarity and the weight of the semantic similarity, used in calculating the weighted avarage to get to a final similarity score.
Lexical Similarity Pair
This function returns the lexical similarity between two sentences. In simple terms it represent in a percentage the amount of wrods in common between the two sentences.
Get similarity:
sentence1 = 'All you have to decide is what to do with the time that is given to you.'
sentence2 = 'You have to decide what to do now, we do not have time.'
starty = strimilarity.Strimilarity()
similarity = starty.lexical_similarity_pair(sentence1, sentence2)
print(similarity)
Result:
0.5892556509887895
Semantic Similarity Pair
This function returns the semantic similarity between two sentences. In simple terms it represent in a percentage the how similar two sentences are in meaning.
Get similarity
sentence1 = 'All you have to decide is what to do with the time that is given to you.'
sentence2 = 'You have to decide what to do now, we do not have time.'
starty = strimilarity.Strimilarity()
similarity = starty.semantic_similarity_pair(sentence1, sentence2)
print(similarity)
Result:
0.5376328
Get similarity between multiple sentences
If you want to get a matrix of similarities between multiple sentences Strimilarity has three different functions:
- similarity
- lexical_similarity
- semantical_similarity
Similarity
This function returns a matrix with the weighted avarages of the lexical and semantic similarities between sentences.
This function also has the w_lexical and w_semantic optional parameters. This represent the weight for the lexical similarity and the weight of the semantic similarity, used in calculating the weighted avarage to get to a final similarity score.
Get similarity:
sentences = [
'All you have to decide is what to do with the time that is given to you.',
'You have to decide what to do now, we do not have time.',
'Do whatever you want to do, we have time.',
'Hello there. Genral Kenobi.'
]
starty = strimilarity.Strimilarity()
similarity = starty.similarity(sentences)
print(similarity)
Result:
[[1.00000012 0.56344418 0.18751006 0.05443659]
[0.56344418 1.00000006 0.533435 0.05470197]
[0.18751006 0.533435 1.00000006 0.06253323]
[0.05443659 0.05470197 0.06253323 1.00000012]]
Result using seaborn to plot data:
Lexical Similarity
This function returns a matrix with the lexical similarities between multiple sentences.
Get similarity:
sentences = [
'All you have to decide is what to do with the time that is given to you.',
'You have to decide what to do now, we do not have time.',
'Do whatever you want to do, we have time.',
'Hello there. Genral Kenobi.'
]
starty = strimilarity.Strimilarity()
similarity = starty.lexical_similarity(sentences)
print(similarity)
Result:
[[1. 0.58925565 0.16666667 0. ]
[0.58925565 1. 0.53033009 0. ]
[0.16666667 0.53033009 1. 0. ]
[0. 0. 0. 1. ]]
Result using seaborn to plot data:
Semantic Similarity
This function returns a matrix with the semantic similarities between multiple sentences.
Get similarity:
sentences = [
'All you have to decide is what to do with the time that is given to you.',
'You have to decide what to do now, we do not have time.',
'Do whatever you want to do, we have time.',
'Hello there. Genral Kenobi.'
]
starty = strimilarity.Strimilarity()
similarity = starty.semantic_similarity(sentences)
print(similarity)
Result:
[[1.0000002 0.5376327 0.20835346 0.10887319]
[0.5376327 1.0000001 0.5365399 0.10940395]
[0.20835346 0.5365399 1.0000001 0.12506646]
[0.10887319 0.10940395 0.12506646 1.0000002 ]]
Result using seaborn to plot data:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file strimilarity-1.0.1.tar.gz.
File metadata
- Download URL: strimilarity-1.0.1.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
750029965de8c6c1e617285cdd2e36b294465667f379abe5bb832ff9554d0d10
|
|
| MD5 |
54c021e92534c9b6c4a04996ecd841a4
|
|
| BLAKE2b-256 |
831965905b03e2dfca1e928b6af70d5f7ec47f561285e9a3f4e924f055fd6d43
|
File details
Details for the file strimilarity-1.0.1-py3-none-any.whl.
File metadata
- Download URL: strimilarity-1.0.1-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
54432a7d430c5fd3bfacff5ad1f490e6e4c9b7d31cc48b3e889859a9995b9541
|
|
| MD5 |
244954ed3eec1061d99eec1ad7656ff5
|
|
| BLAKE2b-256 |
bc4df2884872098df6ef05d10964d1a48310f84d57bb42be428df1d414ff9c6f
|