SlideShare a Scribd company logo
Natural Language Processing and Machine LearningUsing PythonShankar AmbadyMicrosoft New England Research and Development Center, December 14, 2010
Example FilesHosted on Githubhttps://github.com/shanbady/NLTK-Boston-Python-Meetup
What is “Natural Language Processing”?Where is this stuff used?The Machine learning paradoxA look at a few key termsQuick start – creating NLP apps in Python
What is Natural Language Processing?	Computer aided text analysis of human language.
 The goal is to enable machines to understand human language and extract meaning from text.
 It is a field of study which falls under the category of machine learning and more specifically computational linguistics.
The “Natural Language Toolkit” is a python module that provides a variety of functionality that will aide us in processing text.Natural language processing is heavily used throughout all web technologies
Paradoxes in Machine Learning
ContextLittle sister: What’s your name?Me: Uhh….Shankar..?Sister: Can you spell it?Me: yes. S-H-A-N-K-A…..
Sister: WRONG! It’s spelled “I-T”
Ambiguity“I shot the man with ice cream.“A man with ice cream was shot
A man had ice cream shot at himLanguage translation is a complicated matter!Go to:  http://babel.mrfeinberg.com/The problem with communication is the illusion that it has occurred
NLTK - Natural Language Processing in Python
The problem with communication is the illusion that it has occurredThe problem with communication is the illusion, which developed itEPIC FAIL
The “Human Test” Turing testA test proposed to demonstrate that truly intelligent machines capable of understanding and comprehending human language should be indistinguishable from humans performing the same task.I am alsohumanI am ahuman
Key Terms
NLTK - Natural Language Processing in Python
NLTK - Natural Language Processing in Python
NLTK - Natural Language Processing in Python
Setting up NLTKSource downloads available for mac and linux as well as installable packages for windows.Currently only available for Python 2.5 – 2.6http://www.nltk.org/download`easy_install nltk`PrerequisitesNumPySciPy
First stepsNLTK comes with packages of corpora that are required for many modules. Open a python interpreter:importnltknltk.download() If you do not want to use the downloader with a gui (requires TKInter module)Run: python -m nltk.downloader <name of package or “all”>
You may individually select packages or download them in bulk.
Let’s dive into some code!
Part of Speech Taggingfromnltkimportpos_tag,word_tokenizesentence1='this is a demo that will show you how to detects parts of speech with little effort using NLTK!'tokenized_sent=word_tokenize(sentence1)printpos_tag(tokenized_sent)[('this', 'DT'), ('is', 'VBZ'), ('a', 'DT'), ('demo', 'NN'), ('that', 'WDT'), ('will', 'MD'), ('show', 'VB'), ('you', 'PRP'), ('how', 'WRB'), ('to', 'TO'), ('detects', 'NNS'), ('parts', 'NNS'), ('of', 'IN'), ('speech', 'NN'), ('with', 'IN'), ('little', 'JJ'), ('effort', 'NN'), ('using', 'VBG'), ('NLTK', 'NNP'),('!', '.')]
Penn Bank Part-of-Speech TagsSource: http://www.ai.mit.edu/courses/6.863/tagdef.html
NLTK Textnltk.clean_html(rawhtml)from nltk.corpus import brownfrom nltk import Textbrown_words = brown.words(categories='humor')brownText = Text(brown_words)brownText.collocations()brownText.count("car")brownText.concordance("oil")brownText.dispersion_plot(['car', 'document', 'funny', 'oil'])brownText.similar('humor')
Find similar terms (word definitions) using Wordnetimportnltkfromnltk.corpusimportwordnetaswnsynsets=wn.synsets('phone')print[str(syns.definition)forsynsinsynsets] 'electronic equipment that converts sound into electrical signals that can be transmitted over distances and then converts received signals back into sounds‘'(phonetics) an individual sound unit of speech without concern as to whether or not it is a phoneme of some language‘'electro-acoustic transducer for converting electric signals into sounds; it is held over or inserted into the ear‘'get or try to get into communication (with someone) by telephone'
Meronyms and Holonyms
Meronyms and Holonyms are better described in relation to computer science terms as:Meronym terms: 	“has a” relationshipHolonym terms: 	“part of” relationshipHyponym terms: “Is a” relationshipMeronyms and holonyms are oppositesHyponyms and hypernyms are opposites
Burger is a holonym of:
Cheese, beef, tomato, and bread are meronyms of burger
Going back to the previous example …from nltk.corpus import wordnet as wnsynsets = wn.synsets('phone')print [str(  syns.definition ) for syns in synsets]“syns.definition” can be modified to output hypernyms , meronyms, holonyms etc:
Source:http://www.sjsu.edu/faculty/hahn.koo/teaching/ling115/lecture_notes/ling115_wordnet.pdf
Source:http://www.sjsu.edu/faculty/hahn.koo/teaching/ling115/lecture_notes/ling115_wordnet.pdf
fromnltk.corpusimportwordnetaswnsynsets=wn.synsets('car')print[str(syns.part_meronyms())forsynsinsynsets][Synset('gasoline_engine.n.01'), Synset('car_mirror.n.01'), Synset('third_gear.n.01'), Synset('hood.n.09'), Synset('automobile_engine.n.01'), Synset('grille.n.02'),
fromnltk.corpusimportwordnetaswnsynsets=wn.synsets('wing')print[str(syns.part_holonyms())forsynsinsynsets][Synset('airplane.n.01')][Synset('division.n.09')][Synset('bird.n.02')][Synset('car.n.01')][Synset('building.n.01')]
importnltkfromnltk.corpusimportwordnetaswnsynsets=wn.synsets('trees')print[str(syns.part_meronyms())forsynsinsynsets]synset('burl.n.02')
synset('crown.n.07')
synset('stump.n.01')
synset('trunk.n.01')
synset('limb.n.02')fromnltk.corpusimportwordnetaswnforhypernyminwn.synsets('robot')[0].hypernym_paths()[0]:printhypernym.lemma_names['entity']['physical_entity']['object', 'physical_object']['whole', 'unit']		['artifact', 'artefact']		['instrumentality', 'instrumentation']['device']			['mechanism']			['automaton', 'robot', 'golem']
Fun things to Try
Feeling lonely?Eliza is there to talk to you all day! What human could ever do that for you??fromnltk.chatimportelizaeliza.eliza_chat()……starts the chatbotTherapist---------Talk to the program by typing in plain English, using normal upper-and lower-case letters and punctuation.  Enter "quit" when done.========================================================================Hello.  How are you feeling today?
Englisch to German to Englisch to German……fromnltk.bookimport*babelize_shell()Babel> the internet is a series of tubesBabel> germanBabel> run0> the internet is a series of tubes1> das Internet ist eine Reihe SchlSuche2> the Internet is a number of hoses3> das Internet ist einige SchlSuche4> the Internet is some hosesBabel>
Do you Speak Girbbhsi??Take the following meaningful textA new study in the journal Animal Behavior shows that dogs rely a great deal on face recognition to tell their own person from other people. Researchers describe how dogs in the study had difficulty recognizing their owners when two human subjects had their faces covered.Let’s have NLTK analyze and generate some gibberish!importnltkwords=‘text'tokens=nltk.word_tokenize(words)text=nltk.Text(tokens)printtext.generate()
Results May Vary but these were mineA new study in the study had difficulty recognizing their owners when two human subjects had their faces covered . their owners when two human subjects had their faces covered . on face recognition to tell their own person from other people. Researchers describe how dogs in the journal Animal Behavior shows that dogs rely a great deal on face recognition to tell their own person from other people. Researchers describe how dogs in the study had difficulty recognizingtheir owners when two human subjects had their faces covered . subjects had their faces covered . dogs rely a greatA new study in the journal Animal Behavior shows that dogs rely a great deal on face recognition to tell their own person from other people. Researchers describe how dogs in the study had difficulty recognizing their owners when two human subjects had their faces covered.
How Similar?#!/usr/bin/env pythonfromnltk.corpusimportwordnetaswnsimilars =[]Aword=‘language'Bword=‘barrier‘
“how similar” (continued)# grab synsets of each wordsynsetsA=wn.synsets(Aword)synsetsB=wn.synsets(Bword)groupA=[wn.synset(str(synset.name))forsynsetinsynsetsA]groupB=[wn.synset(str(synset.name))forsynsetinsynsetsB]
path_similarity()“Path Distance Similarity: Return a score denoting how similar two word senses are, based on the shortest path that connects the senses in the is-a (hypernym/hypnoym) taxonomy.”wup_similarity()“Wu-Palmer Similarity: Return a score denoting how similar two word senses are, based on the depth of the two senses in the taxonomy and that of their Least Common Subsumer (most specific ancestor node).”Source: http://nltk.googlecode.com/svn/trunk/doc/api/nltk.corpus.reader.wordnet.WordNetCorpusReader-class.html
“how similar” (continued)forssetaingroupA:forssetbingroupB:path_similarity=sseta.path_similarity(ssetb)wup_similarity=sseta.wup_similarity(ssetb)ifpath_similarityisnotNone:similars.append({'path':path_similarity,'wup':wup_similarity,'wordA':sseta,'wordB':ssetb,'wordA_definition':sseta.definition,'wordB_definition':ssetb.definition})
“how similar” (continued)similars=sorted( similars,key=\lambdaitem: item['path'] , reverse=True)foriteminsimilars:printitem['wordA'], "- “,  item['wordA_definition']printitem['wordB'],  "-",   item['wordB_definition']print'Path similarity - ',item['path'],"\n“
Languagethe cognitive processes involved in producing and understanding linguistic communicationSimilarity: 0.111~Barrierany condition that makes it difficult to make progress or to achieve an objective
“how similar” (continued)It trickles down from thereSynset('linguistic_process.n.02') the cognitive processes involved in producing and understanding linguistic communicationSynset('barrier.n.02') any condition that makes it difficult to make progress or to achieve an objectivePath similarity -  0.111111111111 Synset('language.n.05') the mental faculty or power of vocal communicationSynset('barrier.n.02') any condition that makes it difficult to make progress or to achieve an objectivePath similarity -  0.111111111111 Synset('language.n.01') a systematic means of communicating by the use of sounds or conventional symbolsSynset('barrier.n.02') any condition that makes it difficult to make progress or to achieve an objectivePath similarity -  0.1 Synset('language.n.01') a systematic means of communicating by the use of sounds or conventional symbolsSynset('barrier.n.03') anything serving to maintain separation by obstructing vision or accessPath similarity -  0.1
Poetic ProgrammingWe will create a program to extract “Haikus” from any given English text.A haiku is a poem in which each stanza consists of three lines. The first line has 5 syllables, the second has 7 and the last line has 5.
Inspired by a GitHub project “Haiku Finder” :https://github.com/jdf/haikufinderWe will be re-implementing this program and adding a few other little features.You will need  The nltk_contrib package from Google Code:	http://code.google.com/p/nltk/downloads/list
  The following corpora:
 Wordnet
 Cmudict
  A few paragraphs of text that we will use to create haikus from“poetic programming” (continued)fromnltk_contrib.readability.textanalyzerimportsyllables_enfromnltk.corpusimportcmudict,wordnetaswnfromnltkimportword_tokenizeimportretextchunk=''‘ # we will make Ted Stevens sound more poeticThey want to deliver vast amounts of information over the Internet.And again, the Internet is not something that you just dump somethingon.It's not a big truck. It's a series of tubes. And if you don't understand, those tubes can be filled and if they are filled, when you put your message in, it gets in line and it's going to be delayed by anyone that puts into that tube enormous amounts of material, enormous amounts of material       '''
“poetic programming” (continued)textchunk +='‘‘# throw in a few “bush-isms”I want to share with you an interesting program for two reasons, one, it's interesting, and two, my wife thought of it or has actually been involved with it; she didn't think of it. But she thought of it for this speech.This is my maiden voyage.My first speech since I was the president of the United States and I couldn't think of a betterplace to give it than Calgary, Canada.''‘
“poetic programming” (continued)poem=''wordmap=[]words=word_tokenize(textchunk)foriter,wordinenumerate(words):       # if it is a word, add a append a space to itifword.isalpha():word+=" "syls=syllables_en.count(word) wordmap.append((word,syls))Tokenize the wordsNLTK function to count syllables
“poetic programming” (continued)Define a function to provide a fallback word in case we end up with lines that do not have the syllable count we need.deffindSyllableWord(word,syllableSize):synsets=wn.synsets(word)forsynsinsynsets:name=syns.namelemmas=syns.lemma_namesforwordstringinlemmas:if(syllables_en.count(wordstring)==syllableSizeandwordstring!=word):return{'word':word,'syllable':syllableSize}return{'word':word,'syllable':syllables_en.count(word)}Given a word , this function tries to find similar words from WordNet that match the required syllable size
“poetic programming” (continued)We loop through the each word keeping tally of syllables and breaking each line when it reaches the appropriate thresholdlineNo=1charNo=0tally=0forsyllabicwordinwordmap:s=syllabicword[1]wordtoAdd=syllabicword[0]iflineNo==1:iftally<5:iftally+int(s)>5andwordtoAdd.isalpha():num=5-tallysimilarterm=findSyllableWord(wordtoAdd,num)wordtoAdd=similarterm['word']s=similarterm['syllable']tally+=int(s)poem+=wordtoAddelse:poem+=" ---"+str(tally)+"\n"ifwordtoAdd.isalpha():poem+=wordtoAddtally=slineNo=2iflineNo==2:…. Abridged
“poetic programming” (continued)printpoemIts not perfect but its still pretty funny!I want to share with  ---5you an interesting program  ---8for two reasons ,one  ---5it 's interesting  ---5and two ,my wife thought of it  ---7or has actually  ---5been involved with  ---5it ;she didn't think of it. But she thought  ---7of it for this speech.  ---5This is my maiden  ---5voyage. My first speech since I  ---7was the president of  ---5 …. Abridged
Let’s build something even cooler
Lets write a spam filter!	A program that analyzes legitimate emails “Ham” as well as “spam” and learns the features that are associated with each.	Once trained, we should be able to run this program on incoming mail and have it reliably label each one with the appropriate category.
What you will needNLTK (of course) as well as the “stopwords” corpusA good dataset of emails; Both spam and hamPatience and a cup of coffee 	(these programs tend to take a while to complete)
Finding Great Data: The Enron EmailsA dataset of 200,000+ emails made publicly available in 2003 after the Enron scandal.Contains both spam and actual corporate ham mail.For this reason it is one of the most popular datasets used for testing and developing anti-spam software.The dataset we will use is located at the following url:http://labs-repos.iit.demokritos.gr/skel/i-config/downloads/enron-spam/preprocessed/It contains a list of archived files that contain plaintext emails in two folders , Spam and Ham.
Extract one of the archives from the site into your working directory. Create a python script, lets call it “spambot.py”.Your working directory should contain the “spambot” script and the folders “spam” and “ham”.“Spambot.py”fromnltkimportword_tokenize,\ WordNetLemmatizer,NaiveBayesClassifier\,classify,MaxentClassifierfromnltk.corpusimportstopwordsimportrandomimportos,glob,re
“Spambot.py” (continued)wordlemmatizer = WordNetLemmatizer()commonwords = stopwords.words('english') hamtexts=[]spamtexts=[]forinfileinglob.glob(os.path.join('ham/','*.txt')):text_file=open(infile,"r")hamtexts.append(text_file.read())text_file.close()forinfileinglob.glob(os.path.join('spam/','*.txt')):text_file=open(infile,"r")spamtexts.append(text_file.read())text_file.close()load common English words into liststart globbing the files into the appropriate lists
“Spambot.py” (continued)mixedemails=([(email,'spam')foremailinspamtexts]mixedemails+=   [(email,'ham')foremailinhamtexts])random.shuffle(mixedemails)label each item with the appropriate label and store them as a list of tuplesFrom this list of random but labeled emails, we will defined a “feature extractor” which outputs a feature set that our program can use to statistically compare spam and ham. lets give them a nice shuffle
“Spambot.py” (continued)defemail_features(sent):features={}wordtokens=[wordlemmatizer.lemmatize(word.lower())forwordinword_tokenize(sent)]forwordinwordtokens:ifwordnotincommonwords:features[word]=Truereturnfeaturesfeaturesets=[(email_features(n),g)for(n,g)inmixedemails]Normalize wordsIf the word is not a stop-word then lets consider it a “feature”Let’s run each email through the feature extractor  and collect it in a “featureset” list
The features you select must be binary features such as the existence of words or part of speech tags (True or False).
To use features that are non-binary such as number values, you must convert it to a binary feature. This process is called “binning”.
If the feature is the number 12 the feature is: (“11<x<13”, True)“Spambot.py” (continued)Lets grab a sampling of our featureset. Changing this number will affect the accuracy of the classifier. It will be a different number for every classifier, find the most effective threshold for your dataset through experimentation.size=int(len(featuresets)*0.7)train_set,test_set=featuresets[size:],featuresets[:size]classifier=NaiveBayesClassifier.train(train_set)Using this threshold grab the first n elements of our featureset and the last n elements to populate our “training” and “test”  featuresetsHere we start training the classifier using NLTK’s built in Naïve Bayes classifier
“Spambot.py” (continued)print classifier.labels()This will output the labels that our classifier will use to tag new data['ham', 'spam']The purpose of create a “training set” and a “test set” is to test the accuracy of our classifier on a separate sample from the same data source.printclassify.accuracy(classifier,test_set)0.983589566419
classifier.show_most_informative_features(20)SpamHam
“Spambot.py” (continued)WhileTrue:featset=email_features(raw_input("Enter text to classify: "))printclassifier.classify(featset)We can now directly input new email and have it classified as either Spam or Ham
A few notes:The quality of your input data will affect the accuracy of your classifier.
 The threshold value that determines the sample size of the feature set will need to be refined until it reaches its maximum accuracy. This will need to be adjusted if training data is added, changed or removed.A few notes:The accuracy of this dataset can be misleading; In fact our spambot has an accuracy of 98% - but this only applies to Enron emails. This is known as “over-fitting” .
Try classifying your own emails using this trained classifier and you will notice a sharp decline in accuracy.Chunking
Complete sentences are composed of two or more “phrases”. Noun phrase:Jack and Jill went up the hill Prepositional phrase:Contains a noun, preposition and in most cases an adjectiveThe NLTK book is on the table but perhaps it is best kept in a bookshelf Gerund Phrase:Phrases that contain “–ing” verbsJack fell down and broke his crown and Jill came tumbling after
Take the following sentence …..Jack and Jillwent up the hillNoun phraseNoun Phrase
Chunkers will get us this far:[ Jack and Jill ] went up [ the hill ]Chunk tokens are non-recursive – meaning, there is no overlap when chunkingThe recursive form for the same sentence is:( Jack and Jill went up (the hill ) )

More Related Content

PPTX
NLTK
Girish Khanzode
 
PPTX
NLP
guestff64339
 
PDF
Natural Language Processing (NLP) & Text Mining Tutorial Using NLTK | NLP Tra...
Edureka!
 
KEY
NLTK in 20 minutes
Jacob Perkins
 
PPTX
Thomas Wolf "Transfer learning in NLP"
Fwdays
 
PPTX
Introduction to Named Entity Recognition
Tomer Lieber
 
PDF
Natural Language Toolkit (NLTK), Basics
Prakash Pimpale
 
PPTX
NLP State of the Art | BERT
shaurya uppal
 
Natural Language Processing (NLP) & Text Mining Tutorial Using NLTK | NLP Tra...
Edureka!
 
NLTK in 20 minutes
Jacob Perkins
 
Thomas Wolf "Transfer learning in NLP"
Fwdays
 
Introduction to Named Entity Recognition
Tomer Lieber
 
Natural Language Toolkit (NLTK), Basics
Prakash Pimpale
 
NLP State of the Art | BERT
shaurya uppal
 

What's hot (20)

PPT
NLTK: Natural Language Processing made easy
outsider2
 
PPTX
Language models
Maryam Khordad
 
PPTX
Bert
Abdallah Bashir
 
PDF
Natural Language Processing
CloudxLab
 
PDF
Natural Language Processing with Python
Benjamin Bengfort
 
PDF
BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding
Minh Pham
 
PDF
An introduction to the Transformers architecture and BERT
Suman Debnath
 
PDF
Natural language processing (NLP) introduction
Robert Lujo
 
PPTX
Natural language processing
Saurav Aryal
 
PPTX
Introduction to natural language processing, history and origin
Shubhankar Mohan
 
PDF
Introduction to Natural Language Processing (NLP)
VenkateshMurugadas
 
PDF
Neural Machine Translation (D3L4 Deep Learning for Speech and Language UPC 2017)
Universitat Politècnica de Catalunya
 
PPTX
Python for loop
Aishwarya Deshmukh
 
PPTX
NLP Bootcamp
Anuj Gupta
 
PPTX
Generators In Python
Simplilearn
 
PPT
Introduction to Natural Language Processing
Pranav Gupta
 
NLTK: Natural Language Processing made easy
outsider2
 
Language models
Maryam Khordad
 
Natural Language Processing
CloudxLab
 
Natural Language Processing with Python
Benjamin Bengfort
 
BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding
Minh Pham
 
An introduction to the Transformers architecture and BERT
Suman Debnath
 
Natural language processing (NLP) introduction
Robert Lujo
 
Natural language processing
Saurav Aryal
 
Introduction to natural language processing, history and origin
Shubhankar Mohan
 
Introduction to Natural Language Processing (NLP)
VenkateshMurugadas
 
Neural Machine Translation (D3L4 Deep Learning for Speech and Language UPC 2017)
Universitat Politècnica de Catalunya
 
Python for loop
Aishwarya Deshmukh
 
NLP Bootcamp
Anuj Gupta
 
Generators In Python
Simplilearn
 
Introduction to Natural Language Processing
Pranav Gupta
 
Ad

Similar to NLTK - Natural Language Processing in Python (20)

PDF
Nltk:a tool for_nlp - py_con-dhaka-2014
Fasihul Kabir
 
PPTX
Nltk
Anirudh
 
PPTX
Pycon ke word vectors
Osebe Sammi
 
PDF
Natural Language Processing
punedevscom
 
PPTX
NLP Introduction and basics of natural language processing
mailtoahmedhassan
 
PDF
NLTK introduction
Prakash Pimpale
 
PDF
NLP for Everyday People
Rebecca Bilbro
 
PPT
week7.ppt
GiannisPagges
 
PPT
NLTK Python Basic Natural Language Processing.ppt
abdul623429
 
PDF
MACHINE-DRIVEN TEXT ANALYSIS
Massimo Schenone
 
PPTX
Cork AI Meetup Number 3
Nick Grattan
 
PPTX
AI UNIT 3 - SRCAS JOC.pptx enjoy this ppt
pavankalyanadroittec
 
PPSX
Nltk - Boston Text Analytics
shanbady
 
PDF
HackYale - Natural Language Processing (All Slides)
Nick Hathaway
 
PDF
Pycon India 2018 Natural Language Processing Workshop
Lakshya Sivaramakrishnan
 
PPT
NLP 2020: What Works and What's Next
Seth Grimes
 
PDF
AM4TM_WS22_Practice_01_NLP_Basics.pdf
mewajok782
 
PPTX
Natural Language processing using nltk.pptx
Ramakrishna Reddy Bijjam
 
PPTX
Text Analysis Operations using NLTK.pptx
devamrana27
 
PPTX
Natural Language Processing: Comparing NLTK and OpenNLP
CodeOps Technologies LLP
 
Nltk:a tool for_nlp - py_con-dhaka-2014
Fasihul Kabir
 
Nltk
Anirudh
 
Pycon ke word vectors
Osebe Sammi
 
Natural Language Processing
punedevscom
 
NLP Introduction and basics of natural language processing
mailtoahmedhassan
 
NLTK introduction
Prakash Pimpale
 
NLP for Everyday People
Rebecca Bilbro
 
week7.ppt
GiannisPagges
 
NLTK Python Basic Natural Language Processing.ppt
abdul623429
 
MACHINE-DRIVEN TEXT ANALYSIS
Massimo Schenone
 
Cork AI Meetup Number 3
Nick Grattan
 
AI UNIT 3 - SRCAS JOC.pptx enjoy this ppt
pavankalyanadroittec
 
Nltk - Boston Text Analytics
shanbady
 
HackYale - Natural Language Processing (All Slides)
Nick Hathaway
 
Pycon India 2018 Natural Language Processing Workshop
Lakshya Sivaramakrishnan
 
NLP 2020: What Works and What's Next
Seth Grimes
 
AM4TM_WS22_Practice_01_NLP_Basics.pdf
mewajok782
 
Natural Language processing using nltk.pptx
Ramakrishna Reddy Bijjam
 
Text Analysis Operations using NLTK.pptx
devamrana27
 
Natural Language Processing: Comparing NLTK and OpenNLP
CodeOps Technologies LLP
 
Ad

Recently uploaded (20)

PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
PDF
Enable Enterprise-Ready Security on IBM i Systems.pdf
Precisely
 
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
georgschmitzdoerner
 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
Doc9.....................................
SofiaCollazos
 
PPT
L2 Rules of Netiquette in Empowerment technology
Archibal2
 
PDF
Software Development Company | KodekX
KodekX
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
PDF
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
Google’s NotebookLM Unveils Video Overviews
SOFTTECHHUB
 
PDF
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
PPTX
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
This slide provides an overview Technology
mineshkharadi333
 
PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PDF
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
Enable Enterprise-Ready Security on IBM i Systems.pdf
Precisely
 
madgavkar20181017ppt McKinsey Presentation.pdf
georgschmitzdoerner
 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
Software Development Methodologies in 2025
KodekX
 
Doc9.....................................
SofiaCollazos
 
L2 Rules of Netiquette in Empowerment technology
Archibal2
 
Software Development Company | KodekX
KodekX
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Google’s NotebookLM Unveils Video Overviews
SOFTTECHHUB
 
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
This slide provides an overview Technology
mineshkharadi333
 
REPORT: Heating appliances market in Poland 2024
SPIUG
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 

NLTK - Natural Language Processing in Python

  • 1. Natural Language Processing and Machine LearningUsing PythonShankar AmbadyMicrosoft New England Research and Development Center, December 14, 2010
  • 2. Example FilesHosted on Githubhttps://github.com/shanbady/NLTK-Boston-Python-Meetup
  • 3. What is “Natural Language Processing”?Where is this stuff used?The Machine learning paradoxA look at a few key termsQuick start – creating NLP apps in Python
  • 4. What is Natural Language Processing? Computer aided text analysis of human language.
  • 5. The goal is to enable machines to understand human language and extract meaning from text.
  • 6. It is a field of study which falls under the category of machine learning and more specifically computational linguistics.
  • 7. The “Natural Language Toolkit” is a python module that provides a variety of functionality that will aide us in processing text.Natural language processing is heavily used throughout all web technologies
  • 9. ContextLittle sister: What’s your name?Me: Uhh….Shankar..?Sister: Can you spell it?Me: yes. S-H-A-N-K-A…..
  • 10. Sister: WRONG! It’s spelled “I-T”
  • 11. Ambiguity“I shot the man with ice cream.“A man with ice cream was shot
  • 12. A man had ice cream shot at himLanguage translation is a complicated matter!Go to: http://babel.mrfeinberg.com/The problem with communication is the illusion that it has occurred
  • 14. The problem with communication is the illusion that it has occurredThe problem with communication is the illusion, which developed itEPIC FAIL
  • 15. The “Human Test” Turing testA test proposed to demonstrate that truly intelligent machines capable of understanding and comprehending human language should be indistinguishable from humans performing the same task.I am alsohumanI am ahuman
  • 20. Setting up NLTKSource downloads available for mac and linux as well as installable packages for windows.Currently only available for Python 2.5 – 2.6http://www.nltk.org/download`easy_install nltk`PrerequisitesNumPySciPy
  • 21. First stepsNLTK comes with packages of corpora that are required for many modules. Open a python interpreter:importnltknltk.download() If you do not want to use the downloader with a gui (requires TKInter module)Run: python -m nltk.downloader <name of package or “all”>
  • 22. You may individually select packages or download them in bulk.
  • 23. Let’s dive into some code!
  • 24. Part of Speech Taggingfromnltkimportpos_tag,word_tokenizesentence1='this is a demo that will show you how to detects parts of speech with little effort using NLTK!'tokenized_sent=word_tokenize(sentence1)printpos_tag(tokenized_sent)[('this', 'DT'), ('is', 'VBZ'), ('a', 'DT'), ('demo', 'NN'), ('that', 'WDT'), ('will', 'MD'), ('show', 'VB'), ('you', 'PRP'), ('how', 'WRB'), ('to', 'TO'), ('detects', 'NNS'), ('parts', 'NNS'), ('of', 'IN'), ('speech', 'NN'), ('with', 'IN'), ('little', 'JJ'), ('effort', 'NN'), ('using', 'VBG'), ('NLTK', 'NNP'),('!', '.')]
  • 25. Penn Bank Part-of-Speech TagsSource: http://www.ai.mit.edu/courses/6.863/tagdef.html
  • 26. NLTK Textnltk.clean_html(rawhtml)from nltk.corpus import brownfrom nltk import Textbrown_words = brown.words(categories='humor')brownText = Text(brown_words)brownText.collocations()brownText.count("car")brownText.concordance("oil")brownText.dispersion_plot(['car', 'document', 'funny', 'oil'])brownText.similar('humor')
  • 27. Find similar terms (word definitions) using Wordnetimportnltkfromnltk.corpusimportwordnetaswnsynsets=wn.synsets('phone')print[str(syns.definition)forsynsinsynsets] 'electronic equipment that converts sound into electrical signals that can be transmitted over distances and then converts received signals back into sounds‘'(phonetics) an individual sound unit of speech without concern as to whether or not it is a phoneme of some language‘'electro-acoustic transducer for converting electric signals into sounds; it is held over or inserted into the ear‘'get or try to get into communication (with someone) by telephone'
  • 29. Meronyms and Holonyms are better described in relation to computer science terms as:Meronym terms: “has a” relationshipHolonym terms: “part of” relationshipHyponym terms: “Is a” relationshipMeronyms and holonyms are oppositesHyponyms and hypernyms are opposites
  • 30. Burger is a holonym of:
  • 31. Cheese, beef, tomato, and bread are meronyms of burger
  • 32. Going back to the previous example …from nltk.corpus import wordnet as wnsynsets = wn.synsets('phone')print [str( syns.definition ) for syns in synsets]“syns.definition” can be modified to output hypernyms , meronyms, holonyms etc:
  • 43. Feeling lonely?Eliza is there to talk to you all day! What human could ever do that for you??fromnltk.chatimportelizaeliza.eliza_chat()……starts the chatbotTherapist---------Talk to the program by typing in plain English, using normal upper-and lower-case letters and punctuation. Enter "quit" when done.========================================================================Hello. How are you feeling today?
  • 44. Englisch to German to Englisch to German……fromnltk.bookimport*babelize_shell()Babel> the internet is a series of tubesBabel> germanBabel> run0> the internet is a series of tubes1> das Internet ist eine Reihe SchlSuche2> the Internet is a number of hoses3> das Internet ist einige SchlSuche4> the Internet is some hosesBabel>
  • 45. Do you Speak Girbbhsi??Take the following meaningful textA new study in the journal Animal Behavior shows that dogs rely a great deal on face recognition to tell their own person from other people. Researchers describe how dogs in the study had difficulty recognizing their owners when two human subjects had their faces covered.Let’s have NLTK analyze and generate some gibberish!importnltkwords=‘text'tokens=nltk.word_tokenize(words)text=nltk.Text(tokens)printtext.generate()
  • 46. Results May Vary but these were mineA new study in the study had difficulty recognizing their owners when two human subjects had their faces covered . their owners when two human subjects had their faces covered . on face recognition to tell their own person from other people. Researchers describe how dogs in the journal Animal Behavior shows that dogs rely a great deal on face recognition to tell their own person from other people. Researchers describe how dogs in the study had difficulty recognizingtheir owners when two human subjects had their faces covered . subjects had their faces covered . dogs rely a greatA new study in the journal Animal Behavior shows that dogs rely a great deal on face recognition to tell their own person from other people. Researchers describe how dogs in the study had difficulty recognizing their owners when two human subjects had their faces covered.
  • 48. “how similar” (continued)# grab synsets of each wordsynsetsA=wn.synsets(Aword)synsetsB=wn.synsets(Bword)groupA=[wn.synset(str(synset.name))forsynsetinsynsetsA]groupB=[wn.synset(str(synset.name))forsynsetinsynsetsB]
  • 49. path_similarity()“Path Distance Similarity: Return a score denoting how similar two word senses are, based on the shortest path that connects the senses in the is-a (hypernym/hypnoym) taxonomy.”wup_similarity()“Wu-Palmer Similarity: Return a score denoting how similar two word senses are, based on the depth of the two senses in the taxonomy and that of their Least Common Subsumer (most specific ancestor node).”Source: http://nltk.googlecode.com/svn/trunk/doc/api/nltk.corpus.reader.wordnet.WordNetCorpusReader-class.html
  • 51. “how similar” (continued)similars=sorted( similars,key=\lambdaitem: item['path'] , reverse=True)foriteminsimilars:printitem['wordA'], "- “, item['wordA_definition']printitem['wordB'], "-", item['wordB_definition']print'Path similarity - ',item['path'],"\n“
  • 52. Languagethe cognitive processes involved in producing and understanding linguistic communicationSimilarity: 0.111~Barrierany condition that makes it difficult to make progress or to achieve an objective
  • 53. “how similar” (continued)It trickles down from thereSynset('linguistic_process.n.02') the cognitive processes involved in producing and understanding linguistic communicationSynset('barrier.n.02') any condition that makes it difficult to make progress or to achieve an objectivePath similarity - 0.111111111111 Synset('language.n.05') the mental faculty or power of vocal communicationSynset('barrier.n.02') any condition that makes it difficult to make progress or to achieve an objectivePath similarity - 0.111111111111 Synset('language.n.01') a systematic means of communicating by the use of sounds or conventional symbolsSynset('barrier.n.02') any condition that makes it difficult to make progress or to achieve an objectivePath similarity - 0.1 Synset('language.n.01') a systematic means of communicating by the use of sounds or conventional symbolsSynset('barrier.n.03') anything serving to maintain separation by obstructing vision or accessPath similarity - 0.1
  • 54. Poetic ProgrammingWe will create a program to extract “Haikus” from any given English text.A haiku is a poem in which each stanza consists of three lines. The first line has 5 syllables, the second has 7 and the last line has 5.
  • 55. Inspired by a GitHub project “Haiku Finder” :https://github.com/jdf/haikufinderWe will be re-implementing this program and adding a few other little features.You will need The nltk_contrib package from Google Code: http://code.google.com/p/nltk/downloads/list
  • 56. The following corpora:
  • 59. A few paragraphs of text that we will use to create haikus from“poetic programming” (continued)fromnltk_contrib.readability.textanalyzerimportsyllables_enfromnltk.corpusimportcmudict,wordnetaswnfromnltkimportword_tokenizeimportretextchunk=''‘ # we will make Ted Stevens sound more poeticThey want to deliver vast amounts of information over the Internet.And again, the Internet is not something that you just dump somethingon.It's not a big truck. It's a series of tubes. And if you don't understand, those tubes can be filled and if they are filled, when you put your message in, it gets in line and it's going to be delayed by anyone that puts into that tube enormous amounts of material, enormous amounts of material '''
  • 60. “poetic programming” (continued)textchunk +='‘‘# throw in a few “bush-isms”I want to share with you an interesting program for two reasons, one, it's interesting, and two, my wife thought of it or has actually been involved with it; she didn't think of it. But she thought of it for this speech.This is my maiden voyage.My first speech since I was the president of the United States and I couldn't think of a betterplace to give it than Calgary, Canada.''‘
  • 61. “poetic programming” (continued)poem=''wordmap=[]words=word_tokenize(textchunk)foriter,wordinenumerate(words): # if it is a word, add a append a space to itifword.isalpha():word+=" "syls=syllables_en.count(word) wordmap.append((word,syls))Tokenize the wordsNLTK function to count syllables
  • 62. “poetic programming” (continued)Define a function to provide a fallback word in case we end up with lines that do not have the syllable count we need.deffindSyllableWord(word,syllableSize):synsets=wn.synsets(word)forsynsinsynsets:name=syns.namelemmas=syns.lemma_namesforwordstringinlemmas:if(syllables_en.count(wordstring)==syllableSizeandwordstring!=word):return{'word':word,'syllable':syllableSize}return{'word':word,'syllable':syllables_en.count(word)}Given a word , this function tries to find similar words from WordNet that match the required syllable size
  • 63. “poetic programming” (continued)We loop through the each word keeping tally of syllables and breaking each line when it reaches the appropriate thresholdlineNo=1charNo=0tally=0forsyllabicwordinwordmap:s=syllabicword[1]wordtoAdd=syllabicword[0]iflineNo==1:iftally<5:iftally+int(s)>5andwordtoAdd.isalpha():num=5-tallysimilarterm=findSyllableWord(wordtoAdd,num)wordtoAdd=similarterm['word']s=similarterm['syllable']tally+=int(s)poem+=wordtoAddelse:poem+=" ---"+str(tally)+"\n"ifwordtoAdd.isalpha():poem+=wordtoAddtally=slineNo=2iflineNo==2:…. Abridged
  • 64. “poetic programming” (continued)printpoemIts not perfect but its still pretty funny!I want to share with ---5you an interesting program ---8for two reasons ,one ---5it 's interesting ---5and two ,my wife thought of it ---7or has actually ---5been involved with ---5it ;she didn't think of it. But she thought ---7of it for this speech. ---5This is my maiden ---5voyage. My first speech since I ---7was the president of ---5 …. Abridged
  • 66. Lets write a spam filter! A program that analyzes legitimate emails “Ham” as well as “spam” and learns the features that are associated with each. Once trained, we should be able to run this program on incoming mail and have it reliably label each one with the appropriate category.
  • 67. What you will needNLTK (of course) as well as the “stopwords” corpusA good dataset of emails; Both spam and hamPatience and a cup of coffee (these programs tend to take a while to complete)
  • 68. Finding Great Data: The Enron EmailsA dataset of 200,000+ emails made publicly available in 2003 after the Enron scandal.Contains both spam and actual corporate ham mail.For this reason it is one of the most popular datasets used for testing and developing anti-spam software.The dataset we will use is located at the following url:http://labs-repos.iit.demokritos.gr/skel/i-config/downloads/enron-spam/preprocessed/It contains a list of archived files that contain plaintext emails in two folders , Spam and Ham.
  • 69. Extract one of the archives from the site into your working directory. Create a python script, lets call it “spambot.py”.Your working directory should contain the “spambot” script and the folders “spam” and “ham”.“Spambot.py”fromnltkimportword_tokenize,\ WordNetLemmatizer,NaiveBayesClassifier\,classify,MaxentClassifierfromnltk.corpusimportstopwordsimportrandomimportos,glob,re
  • 70. “Spambot.py” (continued)wordlemmatizer = WordNetLemmatizer()commonwords = stopwords.words('english') hamtexts=[]spamtexts=[]forinfileinglob.glob(os.path.join('ham/','*.txt')):text_file=open(infile,"r")hamtexts.append(text_file.read())text_file.close()forinfileinglob.glob(os.path.join('spam/','*.txt')):text_file=open(infile,"r")spamtexts.append(text_file.read())text_file.close()load common English words into liststart globbing the files into the appropriate lists
  • 71. “Spambot.py” (continued)mixedemails=([(email,'spam')foremailinspamtexts]mixedemails+= [(email,'ham')foremailinhamtexts])random.shuffle(mixedemails)label each item with the appropriate label and store them as a list of tuplesFrom this list of random but labeled emails, we will defined a “feature extractor” which outputs a feature set that our program can use to statistically compare spam and ham. lets give them a nice shuffle
  • 73. The features you select must be binary features such as the existence of words or part of speech tags (True or False).
  • 74. To use features that are non-binary such as number values, you must convert it to a binary feature. This process is called “binning”.
  • 75. If the feature is the number 12 the feature is: (“11<x<13”, True)“Spambot.py” (continued)Lets grab a sampling of our featureset. Changing this number will affect the accuracy of the classifier. It will be a different number for every classifier, find the most effective threshold for your dataset through experimentation.size=int(len(featuresets)*0.7)train_set,test_set=featuresets[size:],featuresets[:size]classifier=NaiveBayesClassifier.train(train_set)Using this threshold grab the first n elements of our featureset and the last n elements to populate our “training” and “test” featuresetsHere we start training the classifier using NLTK’s built in Naïve Bayes classifier
  • 76. “Spambot.py” (continued)print classifier.labels()This will output the labels that our classifier will use to tag new data['ham', 'spam']The purpose of create a “training set” and a “test set” is to test the accuracy of our classifier on a separate sample from the same data source.printclassify.accuracy(classifier,test_set)0.983589566419
  • 78. “Spambot.py” (continued)WhileTrue:featset=email_features(raw_input("Enter text to classify: "))printclassifier.classify(featset)We can now directly input new email and have it classified as either Spam or Ham
  • 79. A few notes:The quality of your input data will affect the accuracy of your classifier.
  • 80. The threshold value that determines the sample size of the feature set will need to be refined until it reaches its maximum accuracy. This will need to be adjusted if training data is added, changed or removed.A few notes:The accuracy of this dataset can be misleading; In fact our spambot has an accuracy of 98% - but this only applies to Enron emails. This is known as “over-fitting” .
  • 81. Try classifying your own emails using this trained classifier and you will notice a sharp decline in accuracy.Chunking
  • 82. Complete sentences are composed of two or more “phrases”. Noun phrase:Jack and Jill went up the hill Prepositional phrase:Contains a noun, preposition and in most cases an adjectiveThe NLTK book is on the table but perhaps it is best kept in a bookshelf Gerund Phrase:Phrases that contain “–ing” verbsJack fell down and broke his crown and Jill came tumbling after
  • 83. Take the following sentence …..Jack and Jillwent up the hillNoun phraseNoun Phrase
  • 84. Chunkers will get us this far:[ Jack and Jill ] went up [ the hill ]Chunk tokens are non-recursive – meaning, there is no overlap when chunkingThe recursive form for the same sentence is:( Jack and Jill went up (the hill ) )
  • 85. Verb phrase chunkingJack and Jill went up the hill to fetch a pail of waterVerb PhraseVerb Phrase
  • 86. fromnltk.chunkimport*fromnltk.chunk.utilimport*fromnltk.chunk.regexpimport*fromnltkimportword_tokenize,pos_tagtext='''Jack and Jill went up the hill to fetch a pail of water'''tokens=pos_tag(word_tokenize(text))chunk=ChunkRule("<.*>+","Chunk all the text")chink=ChinkRule("<VBD|IN|\.>",“Verbs/Props")split=SplitRule("<DT><NN>","<DT><NN>","determiner+noun")chunker=RegexpChunkParser([chunk,chink,split],chunk_node='NP')chunked=chunker.parse(tokens)chunked.draw()
  • 87. Chunkers and Parsers ignore the words and instead use part of speech tags to create chunks.
  • 88. Chunking and Named Entity Recognitionfromnltkimportne_chunk,pos_tagfromnltk.tokenize.punktimportPunktSentenceTokenizerfromnltk.tokenize.treebankimportTreebankWordTokenizerTreeBankTokenizer=TreebankWordTokenizer()PunktTokenizer=PunktSentenceTokenizer()text='''text on next slide''‘sentences=PunktTokenizer.tokenize(text)tokens=[TreeBankTokenizer.tokenize(sentence)forsentenceinsentences]tagged=[pos_tag(token)fortokenintokens]chunked=[ne_chunk(taggedToken)fortaggedTokenintagged]
  • 89. text='''The Boston Celtics are a National Basketball Association (NBA) team based in Boston, MA. They play in the Atlantic Division of the Eastern Conference. Founded in 1946, the team is currently owned by Boston Basketball Partners LLC. The Celtics play their home games at the TD Garden, which they share with the Boston Blazers (NLL), and the Boston Bruins of the NHL. The Celtics have dominated the league during the late 50's and through the mid 80's, with the help of many Hall of Famers which include Bill Russell, Bob Cousy, John Havlicek, Larry Bird and legendary Celtics coach Red Auerbach, combined for a 795 - 397 record that helped the Celtics win 16 Championships.
  • 90. print chunked(S The/DT (ORGANIZATION Boston/NNP Celtics/NNP) are/VBP a/DT (ORGANIZATION National/NNP Basketball/ (/NNP(ORGANIZATION NBA/NNP) )/NNP team/NN based/VBN in/IN (GPE Boston/NNP) ,/, MA./NNP They/NNP play/VBP in/IN the/DT (ORGANIZATION Atlantic/NNP Division/NN of/IN the/DT (LOCATION Eastern/NNP) Conference./NNP Founded/NNP in/IN 1946/CD ,/, the/DT team/NN is/VBZ currently/RB owned/VBN by/IN (PERSON Boston/NNP Basketball/NNP) (ORGANIZATION Partners/NNPS) which/WDT include/VBP (PERSON Bill/NNP Russell/NNP) ,/, (PERSON Bob/NNP Cousy/NNP) ,/, (PERSON John/NNP Havlicek/NNP) ,/, (PERSON Larry/NNP Bird/NNP) and/CC legendary/JJ Celtics/NNP coach/NN (PERSON Red/NNP Auerbach/NNP)
  • 92. Thank you for coming!Special thanksNed Batchelder and MicrosoftMy latest creation.Check out weatherzombie.com on your iphone or android!
  • 93. Further Resources:I will be uploading this presentation to my site:http://www.shankarambady.com“Natural Language Processing with Python” by Steven Bird, Ewan Klein, and Edward Loperhttp://www.nltk.org/bookAPI reference : http://nltk.googlecode.com/svn/trunk/doc/api/index.htmlGreat NLTK blog:http://streamhacker.com/

Editor's Notes

  • #26: You may also do #print [str(syns.examples) for syns in synsets] for usage examples of each definition