from nltk.corpus import propbank

pb_instances = propbank.instances()
print(pb_instances)

"""
Explanation: PropBank in NLTK
(C) 2019 by Damir Cavar
The material in this notebook is based on:
- The NLKT Howto on Propbank
- The Proposition Bank Website
- The Propbank GitHub repo
- The Google Propbank Archive
The PropBank Corpus augments the Penn Treebank syntactic trees with predicate-argument annotation. PropBank provides a specific annotation about verbs and arguments for every single tree in the Penn Treebank.
End of explanation
"""


inst = pb_instances[103]

print("File ID:", inst.fileid)

print("Sentence Number:", inst.sentnum)

print("Word Number:", inst.wordnum)

inst.tagger

inst.inflection

infl = inst.inflection

infl.form, infl.tense, infl.aspect, infl.person, infl.voice

inst.roleset

inst.predicate

inst.arguments

"""
Explanation: Each propbank instance defines the following member variables:
- Location information: fileid, sentnum, wordnum
- Annotator information: tagger
- Inflection information: inflection
- Roleset identifier: roleset
- Verb (aka predicate) location: predicate
- Argument locations and types: arguments
End of explanation
"""


print(inst.predicate.wordnum, inst.predicate.height)

"""
Explanation: The location of the predicate and of the arguments are encoded using PropbankTreePointer objects, as well as PropbankChainTreePointer objects and PropbankSplitTreePointer objects. A PropbankTreePointer consists of a wordnum and a height:
End of explanation
"""


tree = inst.tree

from nltk.corpus import treebank

assert tree == treebank.parsed_sents(inst.fileid)[inst.sentnum]

inst.predicate.select(tree)

for (argloc, argid) in inst.arguments:
    print('%-10s %s' % (argid, argloc.select(tree).pformat(500)[:50]))

"""
Explanation: This identifies the tree constituent that is headed by the word that is the wordnum'th token in the sentence, and whose span is found by going height nodes up in the tree. This type of pointer is only useful if we also have the corresponding tree structure, since it includes empty elements such as traces in the word number count. The trees for 10% of the standard PropBank Corpus are contained in the treebank corpus:
End of explanation
"""


treepos = inst.predicate.treepos(tree)

print (treepos, tree[treepos])

"""
Explanation: Propbank tree pointers can be converted to standard tree locations, which are usually easier to work with, using the treepos() method:
End of explanation
"""


inst = pb_instances[6]

inst.roleset

argloc, argid = inst.arguments[2]
argloc

argloc.pieces

argloc.pieces[0].pieces

print(argloc.select(inst.tree))

"""
Explanation: In some cases, argument locations will be encoded using PropbankChainTreePointers (for trace chains) or PropbankSplitTreePointers (for discontinuous constituents). Both of these objects contain a single member variable, pieces, containing a list of the constituent pieces. They also define the method select(), which will return a tree containing all the elements of the argument. (A new head node is created, labeled "CHAIN" or "SPLIT", since the argument is not a single constituent in the original tree). Sentence #6 contains an example of an argument that is both discontinuous and contains a chain:
End of explanation
"""


expose_01 = propbank.roleset('expose.01')

turn_01 = propbank.roleset('turn.01')

print(turn_01)

for role in turn_01.findall("roles/role"):
    print(role.attrib['n'], role.attrib['descr'])

from xml.etree import ElementTree

print(ElementTree.tostring(turn_01.find('example')).decode('utf8').strip())

"""
Explanation: The PropBank Corpus also provides access to the frameset files, which define the argument labels used by the annotations, on a per-verb basis. Each frameset file contains one or more predicates, such as 'turn' or 'turn_on', each of which is divided into coarse-grained word senses called rolesets. For each roleset, the frameset file provides descriptions of the argument roles, along with examples.
End of explanation
"""


inst = pb_instances[9352]

inst.fileid

print(inst.tree)

print(inst.predicate.select(inst.tree))

inst = pb_instances[9353]

inst.fileid

print(inst.tree)

print(inst.predicate.select(inst.tree))

"""
Explanation: Note that the standard corpus distribution only contains 10% of the treebank, so the parse trees are not available for instances starting at 9353:
End of explanation
"""


propbank.verbs()

"""
Explanation: However, if you supply your own version of the treebank corpus (by putting it before the nltk-provided version on nltk.data.path, or by creating a ptb directory as described above and using the propbank_ptb module), then you can access the trees for all instances.
A list of the verb lemmas contained in PropBank is returned by the propbank.verbs() method:
End of explanation
"""


