Day 10 : 30 days of Natural Language Processing Series with Projects
Project …

Welcome back peeps. Lately, I have been very busy with my office work and chasing hard deadlines. This week looks a bit lighter so let’s kick off with NLP projects as we proceed further in this 30 days of Natural Language Processing Series with Projects series.
Some of the other best Series —
100 days : Your Data Science and Machine Learning Degree Series with projects
Complete Data Visualization and Pre-processing Series with projects
Projects Videos —
All the projects, data structures, SQL, algorithms, system design, Data Science and ML , Data Analytics, Data Engineering, , Implemented Data Science and ML projects, Implemented Data Engineering Projects, Implemented Deep Learning Projects, Implemented Machine Learning Ops Projects, Implemented Time Series Analysis and Forecasting Projects, Implemented Applied Machine Learning Projects, Implemented Tensorflow and Keras Projects, Implemented PyTorch Projects, Implemented Scikit Learn Projects, Implemented Big Data Projects, Implemented Cloud Machine Learning Projects, Implemented Neural Networks Projects, Implemented OpenCV Projects,Complete ML Research Papers Summarized, Implemented Data Analytics projects, Implemented Data Visualization Projects, Implemented Data Mining Projects, Implemented Natural Leaning Processing Projects, MLOps and Deep Learning, Applied Machine Learning with Projects Series, PyTorch with Projects Series, Tensorflow and Keras with Projects Series, Scikit Learn Series with Projects, Time Series Analysis and Forecasting with Projects Series, ML System Design Case Studies Series videos will be published on our youtube channel ( just launched).
Subscribe today!
Tech Newsletter —
If you are interested, you can join my newsletter through which I send tech interview tips, techniques, patterns, hacks — Software Development, ML, Data Science, Startups and Technology projects to more than 30K readers. You can subscribe to Tech Brew :
In this post we will start with a project. The dataset is Harry potter book and we will see how we can implement the basic constructs of NLP.
Lets dive in —
1. Load the necessary libraries
import re
import string
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import nltk
from nltk.tokenize import sent_tokenize, word_tokenize
from wordcloud import WordCloud, STOPWORDS
from collections import Counter2. Load the dataset
f = open("../Path to file/B1.txt")
b = file.read()
f.close()3. See the data
b[9000:13000].lower()Output —
'ern.” the newscaster allowed himself a \ngrin. “most mysterious. and now, over to jim \nmcguffin with the weather. going to be any more \nshowers of owls tonight, jim?” \n\n“well, ted,” said the weatherman, “i don’t know about \nthat, but it’s not only the owls that have been acting \noddly today. viewers as far apart as kent, yorkshire, \nand dundee have been phoning in to tell me that \ninstead of the rain i promised yesterday, they’ve had a \ndownpour of shooting stars! perhaps people have \nbeen celebrating bonfire night early — it’s not until \nnext week, folks! but i can promise a wet night \ntonight.” \n\nmr. dursley sat frozen in his armchair. shooting stars \nall over britain? owls flying by daylight? mysterious \npeople in cloaks all over the place? and a whisper, a \nwhisper about the potters . . . \n\n\n\npage | 7 harry potter and the philosophers stone - j.k. rowling \n\n\n\n\nmrs. dursley came into the living room carrying two \ncups of tea. it was no good. he’d have to say \nsomething to her. he cleared his throat nervously. “er \n— petunia, dear — you haven’t heard from your sister \nlately, have you?” \n\nas he had expected, mrs. dursley looked shocked and \nangry. after all, they normally pretended she didn’t \nhave a sister. \n\n“no,” she said sharply. “why?” \n\n“funny stuff on the news,” mr. dursley mumbled. \n“owls . . . shooting stars . . . and there were a lot of \nfunny-looking people in town today ...” \n\n“so?” snapped mrs. dursley. \n\n“well, i just thought ... maybe ... it was something to \ndo with ... you know ... her crowd.” \n\nmrs. dursley sipped her tea through pursed lips. mr. \ndursley wondered whether he dared tell her he’d \nheard the name “potter.” he decided he didn’t dare. \ninstead he said, as casually as he could, “their son — \nhe’d be about dudley’s age now, wouldn’t he?” \n\n“i suppose so,” said mrs. dursley stiffly. \n\n“what’s his name again? howard, isn’t it?” \n\n“harry. nasty, common name, if you ask me.” \n\n“oh, yes,” said mr. dursley, his heart sinking \nhorribly. “yes, i quite agree.” \n\nhe didn’t say another word on the subject as they \nwent upstairs to bed. while mrs. dursley was in the \nbathroom, mr. dursley crept to the bedroom window \n\npage | 8 harry potter and the philosophers stone - j.k. rowling \n\n\n\n\nand peered down into the front garden. the cat was \nstill there. it was staring down privet drive as though \nit were waiting for something. \n\nwas he imagining things? could all this have \nanything to do with the potters? if it did ... if it got out \nthat they were related to a pair of — well, he didn’t \nthink he could bear it. \n\nthe dursleys got into bed. mrs. dursley fell asleep \nquickly but mr. dursley lay awake, turning it all over \nin his mind. his last, comforting thought before he fell \nasleep was that even if the potters were involved, \nthere was no reason for them to come near him and \nmrs. dursley. the potters knew very well what he and \npetunia thought about them and their kind. ... he \ncouldn’t see how he and petunia could get mixed up \nin anything that might be going on — he yawned and \nturned over — it couldn’t affect them. ... \n\nhow very wrong he was. \n\nmr. dursley might have been drifting into an uneasy \nsleep, but the cat on the wall outside was showing no \nsign of sleepiness. it was sitting as still as a statue, \nits eyes fixed unblinkingly on the far corner of privet \ndrive. it didn’t so much as quiver when a car door \nslammed on the next street, nor when two owls \nswooped overhead. in fact, it was nearly midnight \nbefore the cat moved at all. \n\na man appeared on the corner the cat had been \nwatching, appeared so suddenly and silently you’d \nhave thought he’d just popped out of the ground. the \ncat’s tail twitched and its eyes narrowed. \n\nnothing like this man had ever been seen on privet \ndrive. he was tall, thin, and very old, judging by the \nsilver of his hair and beard, which were both long \n\npage | 9 harry potter and the philosophers stone - j.k. rowling \n\n\n\n\nenough to tuck into his belt. he was wearing lo'4. Remove punctuation mark, ‘\n’ and convert it into lowercase
b_f = re.sub('\n','',b)
b_f = b_f.lower()
for s in string.punctuation:
b_final = b_f.replace(s,'')5. Get the stop words
wd = b_final.split(' ')
wdc = Counter(word_list)for wrds in wdc.most_common(80):
print(f"{wrds[0]}: \t{wrds[1]} ")Output —
the: 3931
and: 2209
to: 1835
a: 1666
he: 1489
harry: 1254
of: 1244
was: 1156
his: 931
in: 926
—: 883
it: 746
had: 693
said: 660
you: 627
at: 622
they: 579
on: 561
that: 532
as: 522
i: 463
but: 421
with: 414
stone: 389
potter: 373
for: 349
page: 348
|: 347
philosophers: 347
rowling: 347
be: 344
-: 336
j.k.: 336
out: 322
all: 310
were: 300
have: 288
him: 285
up: 283
what: 263
ron: 250
from: 236
if: 223
she: 222
into: 219
their: 216
back: 212
one: 212
hagrid: 210
about: 207
been: 207
not: 207
so: 194
this: 194
them: 194
there: 188
didn’t: 186
got: 186
get: 186
like: 185
hermione: 182
could: 178
“i: 177
when: 174
off: 173
looked: 167
just: 166
very: 164
professor: 160
...: 156
who: 153
down: 153
over: 152
her: 149
know: 144
your: 144
is: 144
by: 143
see: 142
he’d: 1386. Tokenize sentences
s = sent_tokenize(b_final)
print(s[1:10])
print(len(s))Output —
['they were the last people you’d expect to be involved in anything strange or mysterious, because they just didn’t hold with such nonsense.', 'mr. dursley was the director of a firm called grunnings, which made drills.', 'he was a big, beefy man with hardly any neck, although he did have a very large mustache.', 'mrs. dursley was thin and blonde and had nearly twice the usual amount of neck, which came in very useful as she spent so much of her time craning over garden fences, spying on the neighbors.', 'the dursley s had a small son called dudley and in their opinion there was no finer boy anywhere.', 'the dursleys had everything they wanted, but they also had a secret, and their greatest fear was that somebody would discover it.', 'they didn’t think they could bear it if anyone found out about the potters.', 'mrs. potter was mrs. dursley’s sister, but they hadn’t page | 2 harry potter and the philosophers stone - j.k. rowling met for several years; in fact, mrs. dursley pretended she didn’t have a sister, because her sister and her good-for-nothing husband were as undursleyish as it was possible to be.', 'the dursleys shuddered to think what the neighbors would say if the potters arrived in the street.']50307. Bag of words —
#Remove the footer
s_final = []
for st in sentence:
s = re.sub("page \d+ harry potter and the philosophers stone j.k. rowling",'',st)
s_final.append(s)
c = ''.join(s_final)
wrds = word_tokenize(c)
stw = list(stopwords.words('english'))
stws = stw + ['”','—','...','.',',','“','’',]
w = [wr for wr in words if wr not in stws]
print(len(w))Output —
45244
More Projects —
Complete Python And Projects — Mega Compilation
Complete Data Preprocessing and Data Visualization with Projects — Mega Compilation Part 2
Maths —
Statistics for Data Science and Machine Learning with Code Implementation
Maths for Data Science and Machine learning
In this post we covered Maths for ML . Topics like Linear Algebra, Calculus, Matrix and Vectors, Bayes Theorem and Cheatsheets etc are covered in detail.
Next Project : Coming soon!
For other projects, tune to —
Build Machine Learning Pipelines( With Code)
Recurrent Neural Network with Keras
Custom Layers in Keras
Follow for more updates, stay tuned and of-course let me end this post with a quote by Steve Jobs ;)
“Your time is limited, so don’t waste it living someone else’s life.”






