How to swap noun cardinals in NLTK

This recipe helps you swap noun cardinals in NLTK

Recipe Objective

This recipe explains how to swap noun carinal.

Swapping Noun Cardinal

The func1 function uses a helper function func0 which helps the func1 function to swap any cardinal that occurs after a noun with the noun so that the cardinal occurs immediately before the noun. Cardinals occur before a cardinal's noun and cardinals in a block refer to a number and are tagged as CD. These Swapping noun cardinals are useful to put the cardinal before the noun. If CD is not found at the beginning or all while we are looking for CD tag then we'll return the tag as it is. We'll swap the noun and cardinal as soon as we encounter a CD with a noun preceding it therefore there must be a noun after CD.

Learn How to Build a Multi Class Text Classification Model using BERT

def func0(t):
   def g(word):
     return word[1] == t
   return g

def func1(w, pred, s=0, itr=1):
   length = len(w)
   e = length if itr > 0 else -1

   for j in range(s, e, itr):
     if pred(w[j]):
       return j

   return None

def func2(w):
   i = func1(w, func0('CD'))
   if not i or not w[i-1][1].startswith('NN'):
     return w

   N, NT = w[i-1]
   w[i-1] = w[i]
   w[i] = N, NT
   return w

func2([('an', 'DT'), ('dog', 'NN'), ('20', 'CD')])

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Credit Card Default Prediction using Machine learning techniques
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Deep Learning Project for Time Series Forecasting in Python
Deep Learning for Time Series Forecasting in Python -A Hands-On Approach to Build Deep Learning Models (MLP, CNN, LSTM, and a Hybrid Model CNN-LSTM) on Time Series Data.

Classification Projects on Machine Learning for Beginners - 1
Classification ML Project for Beginners - A Hands-On Approach to Implementing Different Types of Classification Algorithms in Machine Learning for Predictive Modelling

NLP and Deep Learning For Fake News Classification in Python
In this project you will use Python to implement various machine learning methods( RNN, LSTM, GRU) for fake news classification.

Build Regression (Linear,Ridge,Lasso) Models in NumPy Python
In this machine learning regression project, you will learn to build NumPy Regression Models (Linear Regression, Ridge Regression, Lasso Regression) from Scratch.

Learn How to Build a Logistic Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple logistic regression model in PyTorch for customer churn prediction.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Time Series Project to Build a Multiple Linear Regression Model
Learn to build a Multiple linear regression model in Python on Time Series Data

Learn to Build Generative Models Using PyTorch Autoencoders
In this deep learning project, you will learn how to build a Generative Model using Autoencoders in PyTorch

Time Series Forecasting Project-Building ARIMA Model in Python
Build a time series ARIMA model in Python to forecast the use of arrival rate density to support staffing decisions at call centres.