-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathNLP_Point2Point.py
More file actions
468 lines (222 loc) · 6.99 KB
/
Copy pathNLP_Point2Point.py
File metadata and controls
468 lines (222 loc) · 6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# coding: utf-8
# In[48]:
import os
import nltk
import nltk.corpus
# In[49]:
print(os.listdir(nltk.data.find('corpora')))
# In[5]:
from nltk.corpus import brown
brown.words()
# In[6]:
nltk.corpus.gutenberg.fileids()
# In[7]:
hamlet=nltk.corpus.gutenberg.words('shakespeare-hamlet.txt')
hamlet
# In[21]:
for word in hamlet[:500]:
print(word, end='_')
# In[17]:
#word_tokenize
# In[23]:
AI = "Hello Mr. Smith, how are you doing today? The weather is great, and Python is awesome. The sky is pinkish-blue. You shouldn't eat cardboard."
# In[24]:
from nltk.tokenize import sent_tokenize, word_tokenize
AI_token=word_tokenize(AI)
AI_token
# In[ ]:
len(AI_token)
# In[22]:
from nltk.probability import FreqDist
fdist=FreqDist()
# In[23]:
for word in AI_token:
fdist[word.lower()]+=1
fdist
# In[ ]:
fdist['you']
# In[ ]:
len(fdist)
# In[24]:
fdist_top10=fdist.most_common(10)
fdist_top10
# In[28]:
from nltk.tokenize import blankline_tokenize
AI_blank=blankline_tokenize(AI)
AI_blank
# In[27]:
AI_blank[0]
# In[ ]:
#Three types of tokens we have:-
#Biagrams:- Tokens of two consecutive written words known as bigram
#Trigram:-Token of three consecutive written words known as Trigram
#Ngram:-Token of any number of consecutive words known as Ngram
# In[ ]:
from nltk.util import bigrams, trigrams, ngrams
# In[30]:
string='The best and most beautiful things in the world cannot be seen or even touched, they must be felt with the heart'
quotes_tokens=nltk.word_tokenize(string)
quotes_tokens
# In[31]:
quotes_bigrams=list(nltk.bigrams(quotes_tokens))
quotes_bigrams
# In[ ]:
quotes_trigrams=list(nltk.trigrams(quotes_tokens))
quotes_trigrams
# In[ ]:
quotes_ngrams=list(nltk.ngrams(quotes_tokens, 5))
quotes_ngrams
# In[ ]:
#Stemings:-Changes to the token
#Normalize words into its base form or root form
#Affection, Affects, Affections, Affected, Affection, Affecting
#Affect
#Steming algorith works by cutting ends or beginning of the words
#taking into account the most common word
# In[1]:
from nltk.stem import PorterStemmer
pst=PorterStemmer()
pst.stem('having')
# In[4]:
words_to_stem=['give','giving','given','gave']
for words in words_to_stem:
print(words+":"+pst.stem(words))
# In[5]:
from nltk.stem import LancasterStemmer
lst=LancasterStemmer()
for words in words_to_stem:
print(words+":"+lst.stem(words))
# In[ ]:
#we can conclude LST is more aggresibe then PST
# In[6]:
from nltk.stem import SnowballStemmer
sbst=SnowballStemmer('english')
for words in words_to_stem:
print(words+":"+sbst.stem(words))
# In[ ]:
#lemmatization:-In case when steming dont results correctly for ex. Fish, Fishes, Fisherman. It uses
#morphological analysis of words
# In[ ]:
#What does Lemmmatization do?
#1.Groups Together different inflected forms of a word, called lemma
#2.Somehow similar to stemming, as it maps several word into a common root
#3.Output of Lemmatization is a proper word
#4.For example, a lemmatiser should map gone, going and went into go
# In[8]:
from nltk.stem import wordnet
from nltk.stem import WordNetLemmatizer
word_len=WordNetLemmatizer()
word_len.lemmatize('corpora')
# In[15]:
words_to_lem=['gone', 'Going']
for words in words_to_lem:
print(words+":"+word_len.lemmatize(words))
# In[ ]:
#Stop_Words
#several words in english such as I,at,for,begin,got,know,various
#which are usefull in making sentences but these are not usefull in NLP
#So these are called Stop Words
# In[16]:
from nltk.corpus import stopwords
stopwords.words('english')
# In[17]:
len(stopwords.words('english'))
# In[18]:
fdist_top10
# In[25]:
import re
punctuation=re.compile(r'[-.?!,:;()\'|0-9]')
#compile function from re module to form a list with any digit or special charater
# In[30]:
post_punctuation=[]
for words in AI_token:
word=punctuation.sub("/",words)
if len(word)>0:
post_punctuation.append(word)
# In[31]:
post_punctuation
# In[ ]:
AI_token
# In[ ]:
#POS: Parts of speech
#Generally speaking gramatical types of words like noun,verb,adverb, ajectives
#a word can have more then one parts of speech based on the context it is used
#Ex. "Google something on the internet", Here google is used as a verb although its a noun.
#these are some sort of ambiquities or difficulties which makes the NLU much more harder as compared to NLG
#Because once u understand the language Generation is quite easy
# In[ ]:
#POS tags are used to describe whether a word is noun, an adjective, a proper noun, sigular, plural, verb, adverb, symbol
# In[ ]:
sent='krishna is a natural when it comes to singing'
sent_tokens=word_tokenize(sent)
# In[ ]:
for token in sent_tokens:
print(nltk.pos_tag([token]))
# In[42]:
sent2='Vipul is walking through the park'
sent2_tokens=word_tokenize(sent2)
for token in sent2_tokens:
print(nltk.pos_tag([token]))
# In[ ]:
#named Entity Recognition
#naming such as--
#movie
#monetary value
#organization
#location
#Quantities
#person from a text
# In[ ]:
#Three phases of NAE
#1. Noun Pharase identification:-Extract all the noun phrases from a text using dependency passing and parts of speech tagging
#2. phase classification:- in this step all the extracted nouns and phrases are classified into respective categories such as location names and much more
#3. Validation layer to evalute if something goes wrong using knowledge graphs
#popular knowledge craft:- Google knowledge graph, IBM Watson, Wkipedia
#Google's CEO Sundar Pichai introduced the new pixel at Minnesota Roi Centre Event
#Google-Organization
#Sundar Pichai- person
#Minnesota-Location
#Roi centre event-location
# In[36]:
import nltk
from nltk import ne_chunk
NE_sent="The Indian Politicians shouts in the Parliament House"
# In[37]:
NE_tokens=word_tokenize(NE_sent)
NE_tags=nltk.pos_tag(NE_tokens)
# In[38]:
NE_NER=ne_chunk(NE_tags)
print(NE_NER)
# In[ ]:
#NER Entities LIST
#Facility
#Location
#Organization
#Person
#Geo-Socio-political Group
#Geo-Political Entity
#Facility
# In[ ]:
#syntax:- Linguistics syntax is a set of rules, priciples and the processes that govern the structure of a sentence in a given language
#The term syntax is also used to refer to the study of such principles and processes
#So we have certain rule regarding what part of sentence should come up on what positions
#with these rules we create a syntax tree whenever there is a sentence as an input
# In[ ]:
#Syntax tree is a tree representation of syntactic structure of sentences or strings
# In[ ]:
#chunking:- Picking up individual pieces of information and grouping them into bigger pieces
# In[39]:
#WE caught the pink Panther
#The big cat ate the little mouse who was after fresh cheese
new="The big cat ate the little mouse who was after fresh cheese"
new_tokens=nltk.pos_tag(word_tokenize(new))
new_tokens
# In[44]:
grammar_np=r"NP: {<DT><JJ><NN>}"
# In[45]:
chunk_parser=nltk.RegexpParser(grammar_np)
# In[46]:
chunk_result=chunk_parser.parse(new_tokens)
# In[47]:
chunk_result
# In[ ]: