1

I am struggling to go over the results of Stanford Dependency Parser. It prints the information successfully but I can't access it. This is what I see when I print the results.

defaultdict(<function DependencyGraph.__init__.<locals>.<lambda> at 0x075078A0>,
                        {0: {'address': 0,
                             'ctag': 'TOP',
                             'deps': defaultdict(<class 'list'>, {'root': [2]}),
                             'feats': None,
                             'head': None,
                             'lemma': None,
                             'rel': None,
                             'tag': 'TOP',
                             'word': None},
                         1: {'address': 1,
                             'ctag': 'CD',
                             'deps': defaultdict(<class 'list'>, {}),
                             'feats': '_',
                             'head': 2,
                             'lemma': '_',
                             'rel': 'nummod',
                             'tag': 'CD',
                             'word': 'seven'},
                    

How can I acess these items?

Abu Shoeb
  • 4,747
  • 2
  • 40
  • 45
user970155
  • 23
  • 4

1 Answers1

1

The output of the dependency graph is an object of DependencyGraph class. If you run

dir(result) # assuming result contains the parser output

You would be able to see all the properties and methods of the result object.

['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__unicode__',
 '__weakref__',
 '_hd',
 '_parse',
 '_rel',
 '_repr_svg_',
 '_tree',
 '_word',
 'add_arc',
 'add_node',
 'connect_graph',
 'contains_address',
 'contains_cycle',
 'get_by_address',
 'get_cycle_path',
 'left_children',
 'load',
 'nodes',
 'nx_graph',
 'redirect_arcs',
 'remove_by_address',
 'right_children',
 'root',
 'to_conll',
 'to_dot',
 'top_relation_label',
 'tree',
 'triples',
 'unicode_repr']

Then you can iterate over all nodes to access your desired items.

for i in range(len(result.nodes)):
    print(f"Word: {result.nodes[i]['word']}\tLemma: {result.nodes[i]['lemma']}\tTag: {result.nodes[i]['tag']}\tHead: {result.nodes[i]['head']}\tRel: {result.nodes[i]['rel']}")
Abu Shoeb
  • 4,747
  • 2
  • 40
  • 45