Natural Language Processing using Polyglot - Introduction
Last Updated :
28 Jun, 2021
This article explains about a python NLP package known as Polyglot that supports various multilingual applications and offers a wide range of analysis and broad language coverage. It is developed by Rami Al-Rfou. It consists of lots of features such as
- Language detection (196 Languages)
- Tokenization (165 Languages)
- Named Entity Recognition (40 Languages)
- Part of Speech Tagging (16 Languages)
- Sentiment Analysis (136 Languages) and many more
First, let's install some required packages:
Use Google Colab for easy and smooth installation.
pip install polyglot
# installing dependency packages
pip install pyicu
# installing dependency packages
pip install Morfessor
# installing dependency packages
pip install pycld2
Download some necessary models
Use Google colab for easy installation of models
%%bash
polyglot download ner2.en # downloading model ner
%%bash
polyglot download pos2.en # downloading model pos
%%bash
polyglot download sentiment2.en # downloading model sentiment
Code: Language Detection
python3
from polyglot.detect import Detector
spanish_text = u"""¡Hola ! Mi nombre es Ana. Tengo veinticinco años. Vivo en Miami, Florida"""
detector = Detector(spanish_text)
print(detector.language)
Output: :
It detected the text given as spanish with a confidence of 98
Code: Tokenization
Tokenization is the process of splitting the sentences into words and even paragraphs into sentences.
python3
# importing Text from polyglot library
from polyglot.text import Text
sentences = u"""Suggest a platform for placement preparation?. GFG is a very good platform for placement
preparation."""
# passing sentences through imported Text
text = Text(sentences)
# dividing sentences into words
print(text.words)
print('\n')
# separating sentences
print(text.sentences)
Output:
It has divided the sentences into words and even separated the two different sentences.
Code: Named Entity Recognition:
Polyglot recognizes three categories of entities:
- Location
- Organization
- Persons
python3
from polyglot.text import Text
sentence = """Google is an American multinational technology company and Sundar Pichai is the CEO of Google"""
text = Text(sentence, hint_language_code ='en')
print(text.entities)
Output:
I-ORG refers to organisation
I-LOC refers to location
I-PER refers to person
Code: Part of Speech Tagging
python3
from polyglot.text import Text
sentence = """GeeksforGeeks is the best place for learning things in simple manner."""
text = Text(sentence)
print(text.pos_tags)
Output:
Here ADP refers to adposition, ADJ refers to adjective and DET refers to determiner
Code - Sentiment Analysis
python3
from polyglot.text import Text
sentence1 = """ABC is one of the best university in the world."""
sentence2 = """ABC is one of the worst university in the world."""
text1 = Text(sentence1)
text2 = Text(sentence2)
print(text1.polarity)
print(text2.polarity)
Output:
1 refers that the sentence is in positive context
-1 refers that the sentence is in a negative context
Similar Reads
Top 7 Applications of NLP (Natural Language Processing) In the past, did you ever imagine that you could talk to your phone and get things done? Or that your phone would talk back to you! This has become a pretty normal thing these days with Siri, Alexa, Google Assistant, etc. You can ask any possible questions ranging from âWhatâs the weather outsideâ t
6 min read
Top Natural Language Processing (NLP) Books It is important to understand both theoretical foundations and practical applications when it comes to NLP. There are many books available that cover all the key concepts, methods, and tools you need. Whether you are a beginner or a professional, choosing the right book can be challenging. Top Natur
7 min read
Natural Language Processing (NLP) Tutorial Natural Language Processing (NLP) is a branch of Artificial Intelligence (AI) that helps machines to understand and process human languages either in text or audio form. It is used across a variety of applications from speech recognition to language translation and text summarization.Natural Languag
5 min read
Natural Language Processing(NLP) VS Programming Language In the world of computers, there are mainly two kinds of languages: Natural Language Processing (NLP) and Programming Languages. NLP is all about understanding human language while programming languages help us to tell computers what to do. But as technology grows, these two areas are starting to ov
4 min read
Natural Language Processing (NLP) - Overview Natural Language Processing (NLP) is a field that combines computer science, artificial intelligence and language studies. It helps computers understand, process and create human language in a way that makes sense and is useful. With the growing amount of text data from social media, websites and ot
9 min read