Semantic Similarity and Relatedness Toolkit
A toolkit to estimate semantic similarity and relatedness between two words/sentences.
Installation
pip install semantic-kit
Functions
- Lesk algorithm and improved version
- Similarity algorithms including WordNet , word2vec similarity, LDA, and googlenews-based methods
- Distance algorithms like jaccard, soren, levenshtein, and their improved versions
- Use Open Multilingual Wordnet to generate relevant keywords from multiple language
Examples
Lesk Algorithm
from semantickit.relatedness.lesk import lesk
from semantickit.relatedness.lesk_max_overlap import lesk_max_overlap
sent = ['I', 'went', 'to', 'the', 'bank', 'to', 'deposit', 'money', '.']
m1, s1 = lesk(sent, 'bank', 'n')
m2, s2 = lesk_max_overlap(sent, 'bank', 'n')
print(m1,s1)
print(m2,s2)
WordNet-based Similarity
from semantickit.similarity.wordnet_similarity import wordnet_similarity_all
print(wordnet_similarity_all("dog.n.1","cat.n.1"))
Corpus-based Similarity
from semantickit.similarity.word2vec_similarity import build_model,similarity_model
# build similarity model based on text source
build_model(data_path="text8",save_path="wiki_model")
# estimate similarity between words using the built model
sim=similarity_model("wiki_model","france","spain")
# print result
print("word2vec similarity: ",sim)
Pre-trained model-based Similarity
from semantickit.similarity.googlenews_similarity import googlenews_similarity
data_path= r'GoogleNews-vectors-negative300.bin'
sim=googlenews_similarity(data_path,'human','people')
print(sim)
Weighted Levenshtein
from semantickit.distance.n_gram.train_ngram import TrainNgram
from semantickit.distance.weighted_levenshtein import weighted_levenshtein,Build_TFIDF
# train model
train_data_path = 'wlev/icd10_train.txt'
wordict_path = 'wlev/word_dict.model'
transdict_path = 'wlev/trans_dict.model'
words_path="wlev/dict_words.txt"
trainer = TrainNgram()
trainer.train(train_data_path, wordict_path, transdict_path)
# build words tf-idf file
Build_TFIDF(train_data_path,words_path)
# estimate weight lev distance
s0='颈结缔组织良性肿瘤'
s1='耳软骨良性肿瘤'
result=weighted_levenshtein(s0,s1, word_dict_path=wordict_path,trans_dict_path=transdict_path,data_path=train_data_path,words_path=words_path)
print(result)
Get related words
# use wordnet to generate multi-lang keywords
from semantickit.lang.wordnet import *
if __name__=="__main__":
text = "digitalization meets carbon neutrality, digital economy"
nltk.download("wordnet")
nltk.download('omw')
dict_lang_all=get_all_related_word_from_text(text)
print()
for lang in dict_lang_all:
print(lang, ','.join(dict_lang_all[lang]))
Chinese WordVector Similarity
from semantickit.similarity.chinese_word2vec_similarity import ChineseWord2Vec
cwv=ChineseWord2Vec(
data_path="data/source",
output_segment_path="data/segment",
stop_words_path="data/stop_words.txt",
user_dict_path="data/user_dict.txt",
word2vec_model_path="models/word2Vec.model"
)
cwv.train()
sim=cwv.similarity('沙瑞金','易学习')
print(sim)
License
The Semantic-Kit project is provided by Donghua Chen.
Metadata
Release files for semantic-kit 0.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| semantic-kit-0.0.3.tar.gz | 43.8 kB | Without provenance | Details for semantic-kit-0.0.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| semantic_kit-0.0.3-py3-none-any.whl | Python 3 | none | any | Details for semantic_kit-0.0.3-py3-none-any.whl |
Total release size: 94.1 kB
Release files / semantic-kit-0.0.3.tar.gz
| Download URL | semantic-kit-0.0.3.tar.gz |
|---|---|
| Size | 43.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bec985817c89b5ded2817eefae8ee45916dfa90cea244cf79357c054933f1ebb
|
|
BLAKE2b-256 checksum How to use checksums |
895e6a81d63925f1448ff076528f03a4b69612f0eb4e66e524077ddf515203aa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.6
|
Release files / semantic_kit-0.0.3-py3-none-any.whl
| Download URL | semantic_kit-0.0.3-py3-none-any.whl |
|---|---|
| Size | 50.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e216ad45f94c9754fd9883d5fcfedd1c0be41e882bad8afbe6098a563750b192
|
|
BLAKE2b-256 checksum How to use checksums |
db4a317c4fa130fcf0bf0b1834cb416c504b2cb8bcdbe1c444ed620c3e813dc8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.6
|
Security for semantic-kit 0.0.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.