<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Dependency Grammar in NLTK
(C) 2019 by Damir Cavar
Based on the NLTK HOWTO Dependency.
We load the DependencyGrammar module from NLTK Grammar
Step2:  We can load different Dependency Grammar parsers from NLTK
Step3:  Dependency Version of the Penn Treebank
Step5:  "Using the output of zpar (like Malt-TAB but with zero-based indexing)"
Step7:  Projective Dependency Parsing
Step9:  Non-Projective Dependency Parsing
<ASSISTANT_TASK:>
Python Code:
from nltk.grammar import DependencyGrammar
Explanation: Dependency Grammar in NLTK
(C) 2019 by Damir Cavar
Based on the NLTK HOWTO Dependency.
We load the DependencyGrammar module from NLTK Grammar:
End of explanation
from nltk.parse import (
    DependencyGraph,
    ProjectiveDependencyParser,
    NonprojectiveDependencyParser,
)
treebank_data = Pierre  NNP     2       NMOD
Vinken  NNP     8       SUB
,       ,       2       P
61      CD      5       NMOD
years   NNS     6       AMOD
old     JJ      2       NMOD
,       ,       2       P
will    MD      0       ROOT
join    VB      8       VC
the     DT      11      NMOD
board   NN      9       OBJ
as      IN      9       VMOD
a       DT      15      NMOD
nonexecutive    JJ      15      NMOD
director        NN      12      PMOD
Nov.    NNP     9       VMOD
29      CD      16      NMOD
.       .       9       VMOD
dg = DependencyGraph(treebank_data)
dg.tree().pprint()
for head, rel, dep in dg.triples():
    print(
        '({h[0]}, {h[1]}), {r}, ({d[0]}, {d[1]})'
        .format(h=head, r=rel, d=dep)
    )
Explanation: We can load different Dependency Grammar parsers from NLTK:
End of explanation
from nltk.corpus import dependency_treebank
t = dependency_treebank.parsed_sents()[0]
print(t.to_conll(3))  # doctest: +NORMALIZE_WHITESPACE
Explanation: Dependency Version of the Penn Treebank
End of explanation
zpar_data = 
Pierre  NNP     1       NMOD
Vinken  NNP     7       SUB
,       ,       1       P
61      CD      4       NMOD
years   NNS     5       AMOD
old     JJ      1       NMOD
,       ,       1       P
will    MD      -1      ROOT
join    VB      7       VC
the     DT      10      NMOD
board   NN      8       OBJ
as      IN      8       VMOD
a       DT      14      NMOD
nonexecutive    JJ      14      NMOD
director        NN      11      PMOD
Nov.    NNP     8       VMOD
29      CD      15      NMOD
.       .       7       P
zdg = DependencyGraph(zpar_data, zero_based=True)
print(zdg.tree())
Explanation: "Using the output of zpar (like Malt-TAB but with zero-based indexing)":
End of explanation
grammar = DependencyGrammar.fromstring(
'fell' -> 'price' | 'stock'
'price' -> 'of' 'the'
'of' -> 'stock'
'stock' -> 'the'
)
print(grammar)
dp = ProjectiveDependencyParser(grammar)
for t in sorted(dp.parse(['the', 'price', 'of', 'the', 'stock', 'fell'])):
    print(t)
Explanation: Projective Dependency Parsing
End of explanation
grammar = DependencyGrammar.fromstring(
'taught' -> 'play' | 'man'
'man' -> 'the'
'play' -> 'golf' | 'dog' | 'to'
'dog' -> 'his'
)
print(grammar)
dp = NonprojectiveDependencyParser(grammar)
g, = dp.parse(['the', 'man', 'taught', 'his', 'dog', 'to', 'play', 'golf'])
print(g.root['word'])
print(g)
x = dp.parse(['the', 'man', 'taught', 'his', 'dog', 'to', 'play', 'golf'])
for i in x:
    print(i)
for _, node in sorted(g.nodes.items()):
    if node['word'] is not None:
        print('{address} {word}: {d}'.format(d=node['deps'][''], **node))
print(g.tree())
Explanation: Non-Projective Dependency Parsing
End of explanation
<END_TASK>