summaryrefslogtreecommitdiff
path: root/stanford_parser/dependencies.py
blob: 4cda5814be546cd4be30892366b7bc0dc63a4985 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
stanford_dependency_hierarchy = {"dep":
                                 {"aux":{"auxpass":{},
                                         "cop":{}},
                                  "arg": {"agent":{},
                                          "comp":{"acomp":{},
                                                  "attr":{},
                                                  "ccomp":{},
                                                  "xcomp":{},
                                                  "compl":{},
                                                  "obj":{"dobj":{},
                                                         "iobj":{},
                                                         "pobj":{}},
                                                  "mark":{},
                                                  "rel":{}},
                                          "subj":{"nsubj":{"nsubjpass":{}},
                                                  "csubj":{}}},
                                  "cc":{},
                                  "conj":{},
                                  "expl":{},
                                  "mod":{"abbrev":{},
                                         "amod":{},
                                         "appos":{},
                                         "advcl":{},
                                         "purpcl":{},
                                         "det":{},
                                         "predet":{},
                                         "preconj":{},
                                         "infmod":{},
                                         "partmod":{},
                                         "advmod":{"neg":{}},
                                         "rcmod":{},
                                         "quantmod":{},
                                         "tmod":{},
                                         "measure":{},
                                         "nn":{},
                                         "num":{},
                                         "number":{},
                                         "prep":{},
                                         "poss":{},
                                         "possessive":{},
                                         "prt":{}},
                                  "parataxis":{},
                                  "punct":{},
                                  "ref":{},
                                  "sdep":{"xsubj":{}}
                                  }
                                 }
    

class StanfordDependencyHierarchy:
    """
    Class that encodes the types of dependencies.
    """


    def __init__(self, hierarchy=stanford_dependency_hierarchy):
        self.hierarchy=hierarchy

        self.flatMap = {}

        self.parentToChildren = {}

        activeSet = [self.hierarchy]

        while len(activeSet) != 0:
            newActiveSet = []
            for item in activeSet:
                for key, mapValue in item.iteritems():
                    self.flatMap[key] = mapValue
                    self.parentToChildren[key] = sorted(list(mapValue.keys()))
                    newActiveSet.append(mapValue)
                
            activeSet = newActiveSet

        self.ancestorToDescendents = {}
        
        for key, mapValue in self.flatMap.iteritems():
            descendents = []

            activeSet = [mapValue]
            while len(activeSet) != 0:
                newActiveSet = []
                for item in activeSet:
                    for childKey, mapValue in item.iteritems():
                        newActiveSet.extend(mapValue.values())
                        descendents.append(childKey)
                activeSet = newActiveSet
                
            self.ancestorToDescendents[key] = sorted(descendents)
    def isa(self, relation, ancestor):
        return relation in self.ancestorToDescendents[ancestor]