import nltk
text = "On Wednesday, the Association for Computing Machinery, the world’s largest society of computing professionals, announced that Hinton, LeCun and Bengio had won this year’s Turing Award for their work on neural networks. The Turing Award, which was introduced in 1966, is often called the Nobel Prize of computing, and it includes a $1 million prize, which the three scientists will share."
nltk.download('punkt')
[nltk_data] Downloading package punkt to [nltk_data] C:\Users\Mark\AppData\Roaming\nltk_data... [nltk_data] Unzipping tokenizers\punkt.zip.
True
from nltk.tokenize import sent_tokenize
sent_tk = sent_tokenize(text)
print('Sentence tokenizing the text: \n')
print(sent_tk)
Sentence tokenizing the text: ['On Wednesday, the Association for Computing Machinery, the world’s largest society of computing professionals, announced that Hinton, LeCun and Bengio had won this year’s Turing Award for their work on neural networks.', 'The Turing Award, which was introduced in 1966, is often called the Nobel Prize of computing, and it includes a $1 million prize, which the three scientists will share.']
from nltk.tokenize import word_tokenize
word_tk = word_tokenize(text)
print('Word tokenizing the text: \n')
print(word_tk)
Word tokenizing the text: ['On', 'Wednesday', ',', 'the', 'Association', 'for', 'Computing', 'Machinery', ',', 'the', 'world', '’', 's', 'largest', 'society', 'of', 'computing', 'professionals', ',', 'announced', 'that', 'Hinton', ',', 'LeCun', 'and', 'Bengio', 'had', 'won', 'this', 'year', '’', 's', 'Turing', 'Award', 'for', 'their', 'work', 'on', 'neural', 'networks', '.', 'The', 'Turing', 'Award', ',', 'which', 'was', 'introduced', 'in', '1966', ',', 'is', 'often', 'called', 'the', 'Nobel', 'Prize', 'of', 'computing', ',', 'and', 'it', 'includes', 'a', '$', '1', 'million', 'prize', ',', 'which', 'the', 'three', 'scientists', 'will', 'share', '.']
nltk.download('stopwords')
[nltk_data] Downloading package stopwords to [nltk_data] C:\Users\Mark\AppData\Roaming\nltk_data... [nltk_data] Unzipping corpora\stopwords.zip.
True
from nltk.corpus import stopwords
sw = set(stopwords.words("english"))
print("Stop words: \n")
print(sw)
Stop words:
{'yours', "won't", 'both', 'hers', 'ain', 'are', 'before', 'again', "don't", 'couldn', "hadn't", 'had', 'himself', 'down', 'between', 'haven', 'any', 'that', 'all', "aren't", 'she', 'o', 'where', 'wouldn', 'aren', 'there', 'their', 'after', "mustn't", 'which', 'other', 'under', 'about', 'doesn', 'when', 'hadn', 'now', 'so', 'he', 'me', 'our', 'until', "isn't", 'they', 'an', 'as', 'from', 'theirs', 'most', 'myself', 'off', 'once', 'or', 'whom', 'over', "wouldn't", 'but', 'themselves', "shan't", 'we', 'a', 'the', 'wasn', 'against', "doesn't", "haven't", 'shan', 'because', 'mightn', 'yourself', 'into', 'if', "wasn't", 'hasn', 'not', "that'll", 'been', 'has', "hasn't", 'be', 'didn', "she's", "shouldn't", 've', 'you', 'doing', 'don', 'm', 'needn', 'them', 'i', 'by', 'these', 'through', 't', 'herself', 'above', 'won', 'same', "should've", 'at', 'while', 'here', 'each', 'ourselves', 'yourselves', 'mustn', "you'll", 'am', 'ours', 'him', 'does', 'than', 'who', 'such', "needn't", "didn't", "couldn't", 'is', 're', 'this', 'shouldn', 'did', 'll', 'its', 'very', 'just', 'd', 'for', "mightn't", 'weren', 'nor', 'do', 'having', 'will', 'why', 'his', "weren't", 'more', 'with', 'some', 'how', 'too', 'isn', 'my', 'out', 'your', "you'd", 'itself', "you're", "it's", "you've", 'in', 'few', 'were', 'can', 'have', 'ma', 'of', 'her', 'should', 'and', 'those', 's', 'on', 'was', 'being', 'then', 'it', 'during', 'up', 'below', 'what', 'only', 'y', 'no', 'further', 'own', 'to'}
filtered_words = [w for w in word_tk if not w in sw]
print('Filtered text after stopwords removed: \n')
print(filtered_words)
Filtered text after stopwords removed: ['On', 'Wednesday', ',', 'Association', 'Computing', 'Machinery', ',', 'world', '’', 'largest', 'society', 'computing', 'professionals', ',', 'announced', 'Hinton', ',', 'LeCun', 'Bengio', 'year', '’', 'Turing', 'Award', 'work', 'neural', 'networks', '.', 'The', 'Turing', 'Award', ',', 'introduced', '1966', ',', 'often', 'called', 'Nobel', 'Prize', 'computing', ',', 'includes', '$', '1', 'million', 'prize', ',', 'three', 'scientists', 'share', '.']
from nltk.stem import PorterStemmer
from nltk.tokenize import sent_tokenize, word_tokenize
port_stem = PorterStemmer()
stemmed_words = []
for w in filtered_words:
stemmed_words.append(port_stem.stem(w))
print("Filtered sentence: \n", filtered_words, "\n")
print("Stemmed sentence: ", stemmed_words)
Filtered sentence: ['On', 'Wednesday', ',', 'Association', 'Computing', 'Machinery', ',', 'world', '’', 'largest', 'society', 'computing', 'professionals', ',', 'announced', 'Hinton', ',', 'LeCun', 'Bengio', 'year', '’', 'Turing', 'Award', 'work', 'neural', 'networks', '.', 'The', 'Turing', 'Award', ',', 'introduced', '1966', ',', 'often', 'called', 'Nobel', 'Prize', 'computing', ',', 'includes', '$', '1', 'million', 'prize', ',', 'three', 'scientists', 'share', '.'] Stemmed sentence: ['on', 'wednesday', ',', 'associ', 'comput', 'machineri', ',', 'world', '’', 'largest', 'societi', 'comput', 'profession', ',', 'announc', 'hinton', ',', 'lecun', 'bengio', 'year', '’', 'ture', 'award', 'work', 'neural', 'network', '.', 'the', 'ture', 'award', ',', 'introduc', '1966', ',', 'often', 'call', 'nobel', 'prize', 'comput', ',', 'includ', '$', '1', 'million', 'prize', ',', 'three', 'scientist', 'share', '.']
nltk.download('wordnet')
[nltk_data] Downloading package wordnet to [nltk_data] C:\Users\Mark\AppData\Roaming\nltk_data... [nltk_data] Unzipping corpora\wordnet.zip.
True
from nltk.stem.wordnet import WordNetLemmatizer
from nltk.stem.porter import PorterStemmer
lem = WordNetLemmatizer()
stem = PorterStemmer()
lemm_words = []
for i in range(len(filtered_words)):
lemm_words.append(lem.lemmatize(filtered_words[i]))
print(lemm_words)
['On', 'Wednesday', ',', 'Association', 'Computing', 'Machinery', ',', 'world', '’', 'largest', 'society', 'computing', 'professional', ',', 'announced', 'Hinton', ',', 'LeCun', 'Bengio', 'year', '’', 'Turing', 'Award', 'work', 'neural', 'network', '.', 'The', 'Turing', 'Award', ',', 'introduced', '1966', ',', 'often', 'called', 'Nobel', 'Prize', 'computing', ',', 'includes', '$', '1', 'million', 'prize', ',', 'three', 'scientist', 'share', '.']
nltk.download('averaged_perceptron_tagger')
[nltk_data] Downloading package averaged_perceptron_tagger to [nltk_data] C:\Users\Mark\AppData\Roaming\nltk_data... [nltk_data] Unzipping taggers\averaged_perceptron_tagger.zip.
True
from nltk import pos_tag
pos_tagged_words = pos_tag(word_tk)
print(pos_tagged_words)
[('On', 'IN'), ('Wednesday', 'NNP'), (',', ','), ('the', 'DT'), ('Association', 'NNP'), ('for', 'IN'), ('Computing', 'VBG'), ('Machinery', 'NNP'), (',', ','), ('the', 'DT'), ('world', 'NN'), ('’', 'NNP'), ('s', 'RB'), ('largest', 'JJS'), ('society', 'NN'), ('of', 'IN'), ('computing', 'VBG'), ('professionals', 'NNS'), (',', ','), ('announced', 'VBD'), ('that', 'IN'), ('Hinton', 'NNP'), (',', ','), ('LeCun', 'NNP'), ('and', 'CC'), ('Bengio', 'NNP'), ('had', 'VBD'), ('won', 'VBN'), ('this', 'DT'), ('year', 'NN'), ('’', 'VBZ'), ('s', 'JJ'), ('Turing', 'NNP'), ('Award', 'NNP'), ('for', 'IN'), ('their', 'PRP$'), ('work', 'NN'), ('on', 'IN'), ('neural', 'JJ'), ('networks', 'NNS'), ('.', '.'), ('The', 'DT'), ('Turing', 'NNP'), ('Award', 'NNP'), (',', ','), ('which', 'WDT'), ('was', 'VBD'), ('introduced', 'VBN'), ('in', 'IN'), ('1966', 'CD'), (',', ','), ('is', 'VBZ'), ('often', 'RB'), ('called', 'VBN'), ('the', 'DT'), ('Nobel', 'NNP'), ('Prize', 'NNP'), ('of', 'IN'), ('computing', 'NN'), (',', ','), ('and', 'CC'), ('it', 'PRP'), ('includes', 'VBZ'), ('a', 'DT'), ('$', '$'), ('1', 'CD'), ('million', 'CD'), ('prize', 'NN'), (',', ','), ('which', 'WDT'), ('the', 'DT'), ('three', 'CD'), ('scientists', 'NNS'), ('will', 'MD'), ('share', 'NN'), ('.', '.')]
from nltk import FreqDist
fd = FreqDist(word_tk)
print(fd)
<FreqDist with 56 samples and 76 outcomes>
import matplotlib.pyplot as plt
fd.plot(30, cumulative=False)
plt.show()
fd_alpha = FreqDist(text)
fd_alpha.plot(30, cumulative=False)
<AxesSubplot:xlabel='Samples', ylabel='Counts'>
import os