Word similarity is a number between 0 to 1 which tells us how close two words are, semantically. This is done by finding similarity between word vectors in the vector space. spaCy, one of the fastest NLP libraries widely used today, provides a simple method for this task.
spaCy’s Model –
spaCy supports two methods to find word similarity: using context-sensitive tensors, and using word vectors. Below is the code to download these models.
# Downloading the small model containing tensors. python -m spacy download en_core_web_sm # Downloading over 1 million word vectors. python -m spacy download en_core_web_lg
Below is the code to find word similarity, which can be extended to sentences and documents.
cat True 6.6808186 False dog True 7.0336733 False Similarity: 0.80168545
The ‘en_core_web_md’ model yields vectors of dimension 300*1 for ‘dog’ and ‘cat’. One may also use the larger model, ‘en_vectors_web_lg’ which yields vectors of larger dimension for the same two words.
Using Custom Language Models –
By simply switching the language model, we can find a similarity between Latin, French or German documents. spaCy supports a total of 49 languages at present. spaCy also allows one to fix word vectors for words as per user need. Below is an example.
Before custom setting array([0., 0., 0., 0., 0., 0., 0., 0., --- ]) After custom setting array([ 0.68106073, 0.6037007, 0.9526876, -0.25600302, -0.24049562, --- ])
- Python | Measure similarity between two sentences using cosine similarity
- Python | Named Entity Recognition (NER) using spaCy
- Python | PoS Tagging and Lemmatization using spaCy
- Python | Perform Sentence Segmentation Using Spacy
- Measure similarity between images using Python-OpenCV
- Movie recommender based on plot summary using TF-IDF Vectorization and Cosine similarity
- Python | Test list element similarity
- Python | Similarity metrics of strings
- Python | Percentage similarity of lists
- Measuring the Document Similarity in Python
- Python - Alternate elements Similarity
- NLP | WuPalmer - WordNet Similarity
- NLP | Leacock Chordorow (LCH) and Path similarity for Synset
- Python program to read file word by word
- Find the first repeated word in a string in Python using Dictionary
- Python | Word Embedding using Word2Vec
- Python | Program that matches a word containing 'g' followed by one or more e's using regex
- Python | Count occurrences of each word in given text file (Using dictionary)
- Converting WhatsApp chat data into a Word Cloud using Python
- How to obtain the line number in which given word is present using Python?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.