How to train a unigram tagger in nltk

This recipe helps you train a unigram tagger in nltk

Recipe Objective

This recipe explains how to train a unigram tagger.

Step 1: Importing library

Let us first import the necessary libraries. We'll import UnigramTagger and treebank from nltk.tag and nltk.corpus respectively.

from nltk.tag import UnigramTagger
from nltk.corpus import treebank

Step 2: Unigram Tagger

In simple words, Unigram Tagger is a context-based tagger whose context is a single word. To determine the POS(Part of Speech) tag Unigram tagger only uses a single word as its context. In the below example, we'll be training using the first 3000 tagged sentences of the treebank corpus as data. These tagged sentences are used by unigram to build a model. Since unigram is inherited from ContextTagger context() method is implemented instead of choose_tag() method.
UnigramTagger -> NgramTagger -> ContextTagger -> SequentialBackoffTagger.

train = treebank.tagged_sents()[:3000]
tag = UnigramTagger(train)
treebank.sents()[1]
tag = UnigramTagger(model ={'Elsevier': 'NN'})
tag.tag(treebank.sents()[1])

What Users are saying..

profile image

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Loan Eligibility Prediction Project using Machine learning on GCP
Loan Eligibility Prediction Project - Use SQL and Python to build a predictive model on GCP to determine whether an application requesting loan is eligible or not.

Tensorflow Transfer Learning Model for Image Classification
Image Classification Project - Build an Image Classification Model on a Dataset of T-Shirt Images for Binary Classification

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

Build Real Estate Price Prediction Model with NLP and FastAPI
In this Real Estate Price Prediction Project, you will learn to build a real estate price prediction machine learning model and deploy it on Heroku using FastAPI Framework.

OpenCV Project for Beginners to Learn Computer Vision Basics
In this OpenCV project, you will learn computer vision basics and the fundamentals of OpenCV library using Python.

Deploy Transformer-BART Model on Paperspace Cloud
In this MLOps Project you will learn how to deploy a Tranaformer BART Model for Abstractive Text Summarization on Paperspace Private Cloud

NLP Project to Build a Resume Parser in Python using Spacy
Use the popular Spacy NLP python library for OCR and text classification to build a Resume Parser in Python.

Build a Multi ClassText Classification Model using Naive Bayes
Implement the Naive Bayes Algorithm to build a multi class text classification model in Python.

Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.

Isolation Forest Model and LOF for Anomaly Detection in Python
Credit Card Fraud Detection Project - Build an Isolation Forest Model and Local Outlier Factor (LOF) in Python to identify fraudulent credit card transactions.