Jim's
Tutorials

Fall 2011
course
navigation

2011-11-29

Paper

Will be uploading written portion of Final Paper (without questions from NLTK...yet) soon.
So far I have most of the paper.

NLTK Chapter 8

Syntax/ sentence structure - defining grammars:
Symbol Meaning Example S sentence the man walked NP noun phrase a dog VP verb phrase saw a park PP prepositional phrase with a telescope Det determiner the N noun dog V verb walked P preposition in
Context free grammar (CFG) grammar1 = nltk.parse_cfg(""" S -> NP VP VP -> V NP | V NP PP PP -> P NP V -> "saw" | "ate" | "walked" NP -> "John" | "Mary" | "Bob" | Det N | Det N PP Det -> "a" | "an" | "the" | "my" N -> "man" | "dog" | "cat" | "telescope" | "park" P -> "in" | "on" | "by" | "with" """) sent = "the man saw Bob with the telescope".split() rd_parser = nltk.RecursiveDescentParser(grammar1) for tree in rd_parser.nbest_parse(sent): print tree # Output: # (S # (NP (Det the) (N man)) # (VP (V saw) (NP Bob) (PP (P with) (NP (Det the) (N telescope))))) tree1 = nltk.Tree('S', [nltk.Tree('NP', [nltk.Tree('Det', ['the']), nltk.Tree('N', ['man'])]), nltk.Tree('VP', [nltk.Tree('V', ['saw']), nltk.Tree('NP', ['Bob']), nltk.Tree('PP', [nltk.Tree('P', ['with']), nltk.Tree('NP', [nltk.Tree('Det', ['the']), nltk.Tree('N', ['telescope'])])])])]) tree1.draw() # tree1.png
If I wanted to write my own grammar, I could make a mygrammar.cfg file and use that above.

Chapter 9

Chapter 10

dp = nltk.DrtParser() drs1 = dp.parse('([x, y], [lauren(x), snake(y), own(x, y)])') print drs1 # ([x,y],[lauren(x), snake(y), own(x,y)]) drs1.draw() # DRT.png
First order logic print drs1.fol() # exists x y.(lauren(x) & snake(y) & own(x,y)) # There exists an x and a y such that: # x is lauren, # y is a snake, and # x owns y. # (Lauren owns a snake) # - EZ - 12/5 drs2 = dp.parse('([u, z], [PRO(u), mouse(z), eat(u, z)])') # PRO(u) is a pronoun (he, she, it,...) - EZ -12/5 drs3 = drs1 + drs2 print drs3.simplify() # ([u,x,y,z],[lauren(x), snake(y), own(x,y), PRO(u), mouse(z), eat(u,z)]) print drs3.simplify().resolve_anaphora() # ([u,x,y,z],[lauren(x), snake(y), own(x,y), (u = [x,y,z]), mouse(z), eat(u,z)]) drs3.simplify().resolve_anaphora().draw() # In linguistics, anaphora is an instance of an expression referring to another # e.g. pronouns. - EZ -12/5 # DRT2.png
http://cs.marlboro.edu/ courses/ fall2011/jims_tutorials/ elias/ 2011-11-29
last modified Tuesday December 6 2011 3:02 am EST

attachments [paper clip]

     name last modified size
[IMG]DRT.png Nov 29 2011 1:30 am 19.8kB [IMG]DRT2.png Nov 29 2011 1:41 am 22.5kB [DOC]NLPFinal.doc Nov 29 2011 1:48 am 81.4kB [IMG]tree1.png Nov 28 2011 10:45 pm 16.0kB