Skip to main content

Urdu Word and Sentence Similarity using SpaCy

The similarity is the common measure of understanding how much close two words or sentences are to
each other. There are multiple ways to find out the similarity of two documents and the most common being used in NLP is Cosine Similarity. Cosine Similarity is counted using vectors (word2vector) and provides information about how much two vectors are close in the context of orientation.

Some helpful links to understand the similarity concepts:
  1. https://towardsdatascience.com/overview-of-text-similarity-metrics-3397c4601f50
  2. https://www.sciencedirect.com/topics/computer-science/cosine-similarity
It mostly depends on the quality of the vectors of the documents. If you want to get better results, build a better word 2 vector model. To use the similarity feature of SpaCy, you need to build a language model (you can build a language model by following my article https://www.urdunlp.com/2019/08/how-to-build-urdu-language-model-in.html).

Here is how I've calculated the cosine similarity of the words and sentences.

import spacy

nlp = spacy.load('ur_model')

doc1 = nlp("عمران")
doc2 = nlp("عرفان")
print("Cosine Similarity of words.")

cosine_similarity = doc1.similarity(doc2)
print(cosine_similarity)

print("Cosine Similarity of sentences.")
doc3 = nlp("میں کھیلتا ہوں")
doc4 = nlp("میں کام کرتا ہوں")

cosine_similarity = doc3.similarity(doc4)
print(cosine_similarity)
If you have any questions, feel free to ask in comments.


Comments

  1. Salam , is there any tutorials available for URDU_NLP in youtube

    ReplyDelete
  2. OSError: [E050] Can't find model 'ur_model'. It doesn't seem to be a Python package or a valid path to a data directory.

    ReplyDelete
    Replies
    1. Download model from here

      https://github.com/mirfan899/Urdu

      Delete
    2. how can add ur_model in jupyter notebook?

      Delete
    3. Just install it as package then load it with spacy.
      pip install ur_model-0.0.0.tar.gz

      Delete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. I think by using above spacy module, cosine similarity gives high similarity results even two sentences are not much similar.

    ReplyDelete
  6. Yes, to get better results you need to train large model with vectors.

    ReplyDelete

Post a Comment

Popular posts from this blog

Text Summarization for Urdu: Part 1

 Text Summarization is an important task for large documents to get the idea of the document. There are two main summarization techniques used in NLP for text summarization. Extractive Text Summarization :  This approach's name is self-explanatory. Most important sentences or phrases are extracted from the original text and a short summary provided with these important sentences. See the figure for the explanation. Abstractive Text Summarization : This approach uses more advanced deep learning techniques to generate new sentences by learning from the original text. It is a complex task and requires heavy computing power such as GPU. Let's dive into the code for generating the text summary. I'm using Arabic as a parameter because the contributor did an excellent job of handling a lot of things like stemming, Urdu characters support, etc. from summa.summarizer import summarize text = """ اسلام آباد : صدر مملکت ڈاکٹر عارف علوی بھی کورونا وائرس کا شکار ہوگئے۔ سما

Urdu Tokenization using SpaCy

SpaCy is an NLP library which supports many languages. It’s fast and has DNNs build in for performing many NLP tasks such as POS and NER. It has extensive support and good documentation. It is fast and provides GPU support and can be integrated with Tensorflow, PyTorch, Scikit-Learn, etc. SpaCy provides the easiest way to add any language support. A new language can be added by simply following Adding Languages article. I’ve added the Urdu language with dictionary-based lemmatization, lexical support and stop words( Urdu ). Here is how you can use the tokenizer for the Urdu language. First, install SpaCy . $ pip install spacy Now import spacy and create a blank object with support of Urdu language. I’m using blank because there is no proper model available for Urdu yet, but tokenization support available. import spacy nlp = spacy.blank('ur') doc = nlp(" کچھ ممالک ایسے بھی ہیں جہاں اس برس روزے کا دورانیہ 20 گھنٹے تک ہے۔") print("Urdu Tokeniza

How to build Urdu language model in SpaCy

Urdu alphabets SpaCy is the most commonly used NLP library for building NLP and chatbot apps. The Urdu language does not have resources for building chatbot and NLP apps. Most of the tools are proprietary or data is licensed. After adding the support for the Urdu language, I'm going to show you how to build an Urdu model which can be used for multiple applications such as word and sentence similarity, chatbots, knowledgebase, etc. Follow the steps to build the model. Step 1: Build word frequencies for Urdu. I've created a script that can be used to build word frequencies. There are multiple resources available for building word frequencies, you can choose whatever you want but the format should be like this. frequency document_id word Here is the script I'm using to build word frequencies for SpaCy. from __future__ import unicode_literals import string import codecs import glob from collections import Counter import re import plac from multiprocessing