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
- Context Free Grammars
- X̄ (X-Bar, X') Syntax
- S[+INT]...
- Other languages.
Chapter 10
- Sentence analysis
- NLTK has a Python-to-SQL-like-syntax method.
- Logic
- Propositional and First Order
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