<jupyter_start><jupyter_text>__Exercise 1__<jupyter_code># Today is Saturday, the 5th of May 2018 and my name is Julia, living in [address],
# and I'm currently fullfilling an nltk book assignment.<jupyter_output><empty_output><jupyter_text>__Exercise 2__<jupyter_code># used in sense of 'no matter how': 
#    “However beautiful the strategy, you should occasionally look at the results.”
#    “However bad you think you’re going to be in that room, not being there is worse.”
# used as connector:
#    "However, they kept on, with unabated perseverance."<jupyter_output><empty_output><jupyter_text>__Exercise 3__<jupyter_code># (Kim arrived) or (Dana left and everyone cheered).
# (Kim arrived or Dana left) and everyone cheered.

import nltk, pprint, re
grammar = nltk.CFG.fromstring("""
S -> NP VP
S -> S Conj S
VP -> "arrived" | "left" | "cheered"
NP -> "Kim" | "Dana" | "everyone"
Conj -> "and" | "or"
""")

sr_parse = nltk.ShiftReduceParser(grammar, trace=2)
sent = 'Kim arrived or Dana left and everyone cheered'.split()
for tree in sr_parse.parse(sent):
    print tree
grammar = nltk.PCFG.fromstring("""
S -> NP VP [0.6]
S -> S Conj S [0.4]
VP -> "arrived" | "left" | "cheered" [1.0]
NP -> "Kim" | "Dana" | "everyone" [1.0]
Conj -> "and" | "or" [1.0]
""")

viterbi_parse = nltk.ViterbiParser(grammar, trace=2)
sent = 'Kim arrived or Dana left and everyone cheered'.split()
for tree in viterbi_parse.parse(sent):
    print tree<jupyter_output>Inserting tokens into the most likely constituents table...
   Insert: |=.......| Kim
   Insert: |.=......| arrived
   Insert: |..=.....| or
   Insert: |...=....| Dana
   Insert: |....=...| left
   Insert: |.....=..| and
   Insert: |......=.| everyone
   Insert: |.......=| cheered
Finding the most likely constituents spanning 1 text elements...
   Insert: |=.......| NP -> 'Kim' [0]
   Insert: |.=......| VP -> 'arrived' [0]
   Insert: |..=.....| Conj -> 'or' [1.0]
   Insert: |...=....| NP -> 'Dana' [0]
   Insert: |....=...| VP -> 'left' [0]
   Insert: |.....=..| Conj -> 'and' [0]
   Insert: |......=.| NP -> 'everyone' [1.0]
   Insert: |.......=| VP -> 'cheered' [1.0]
Finding the most likely constituents spanning 2 text elements...
   Insert: |==......| S -> NP VP [0.6]
   Insert: |...==...| S -> NP VP [0.6]
   Insert: |......==| S -> NP VP [0.6]
Finding the most likely constituents spanning 3 text elements...
Finding the most likely constituents spanning 4 text elements...
Finding the m[...]<jupyter_text>__Exercise 4__<jupyter_code>from nltk import Tree
help(Tree)<jupyter_output>Help on class Tree in module nltk.tree:

class Tree(__builtin__.list)
 |  A Tree represents a hierarchical grouping of leaves and subtrees.
 |  For example, each constituent in a syntax tree is represented by a single Tree.
 |  
 |  A tree's children are encoded as a list of leaves and subtrees,
 |  where a leaf is a basic (non-tree) value; and a subtree is a
 |  nested Tree.
 |  
 |      >>> from nltk.tree import Tree
 |      >>> print(Tree(1, [2, Tree(3, [4]), 5]))
 |      (1 2 (3 4) 5)
 |      >>> vp = Tree('VP', [Tree('V', ['saw']),
 |      ...                  Tree('NP', ['him'])])
 |      >>> s = Tree('S', [Tree('NP', ['I']), vp])
 |      >>> print(s)
 |      (S (NP I) (VP (V saw) (NP him)))
 |      >>> print(s[1])
 |      (VP (V saw) (NP him))
 |      >>> print(s[1,1])
 |      (NP him)
 |      >>> t = Tree.fromstring("(S (NP I) (VP (V saw) (NP him)))")
 |      >>> s == t
 |      True
 |      >>> t[1][1].set_label('X')
 |      >>> t[1][1].label()
 |      'X'
 |      >>> print(t)
[...]<jupyter_text>__Exercise 5__<jupyter_code># a
tree1 = Tree('NP', 
             [Tree('JJ', ['old']), 
              Tree('NP', 
                   [Tree('N', ['men']), Tree('Conj', ['and']), Tree('N', ['women'])])])
print(tree1)
tree2 = Tree('NP', 
             [Tree('NP', 
                   [Tree('JJ', ['old']), Tree('N', ['men'])]), 
              Tree('Conj', ['and']), 
              Tree('NP', ['women'])])
print(tree2)
# b
tree3 = Tree.fromstring("((S (NP I) (VP (VP (V shot) (NP (Det an) (N elephant))) (PP (P in) (NP (Det my) (N pajamas))))))")
tree3.draw()
# c
tree4 = Tree('S', 
             [Tree('NP', 
                   [Tree('Det', ['The']), Tree('N', ['woman'])]), 
              Tree('VP', 
                   [Tree('V', ['saw']), 
                    Tree('NP', 
                         [Tree('Det', ['a']), Tree('N', ['man'])]),
                    Tree('NP',
                        [Tree('JJ', ['last']), Tree('N', ['Thursday'])])])])
print(tree4)
tree4.draw()<jupyter_output>(S
  (NP (Det The) (N woman))
  (VP (V saw) (NP (Det a) (N man)) (NP (JJ last) (N Thursday))))
StarcoderdataJupyterStructuredCleanDedup