|
| 1 | +stanford_dependency_hierarchy = {"dep": |
| 2 | + {"aux":{"auxpass":{}, |
| 3 | + "cop":{}}, |
| 4 | + "arg": {"agent":{}, |
| 5 | + "comp":{"acomp":{}, |
| 6 | + "attr":{}, |
| 7 | + "ccomp":{}, |
| 8 | + "xcomp":{}, |
| 9 | + "compl":{}, |
| 10 | + "obj":{"dobj":{}, |
| 11 | + "iobj":{}, |
| 12 | + "pobj":{}}, |
| 13 | + "mark":{}, |
| 14 | + "rel":{}}, |
| 15 | + "subj":{"nsubj":{"nsubjpass":{}}, |
| 16 | + "csubj":{}}}, |
| 17 | + "cc":{}, |
| 18 | + "conj":{}, |
| 19 | + "expl":{}, |
| 20 | + "mod":{"abbrev":{}, |
| 21 | + "amod":{}, |
| 22 | + "appos":{}, |
| 23 | + "advcl":{}, |
| 24 | + "purpcl":{}, |
| 25 | + "det":{}, |
| 26 | + "predet":{}, |
| 27 | + "preconj":{}, |
| 28 | + "infmod":{}, |
| 29 | + "partmod":{}, |
| 30 | + "advmod":{"neg":{}}, |
| 31 | + "rcmod":{}, |
| 32 | + "quantmod":{}, |
| 33 | + "tmod":{}, |
| 34 | + "measure":{}, |
| 35 | + "nn":{}, |
| 36 | + "num":{}, |
| 37 | + "number":{}, |
| 38 | + "prep":{}, |
| 39 | + "poss":{}, |
| 40 | + "possessive":{}, |
| 41 | + "prt":{}}, |
| 42 | + "parataxis":{}, |
| 43 | + "punct":{}, |
| 44 | + "ref":{}, |
| 45 | + "sdep":{"xsubj":{}} |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | +class StanfordDependencyHierarchy: |
| 51 | + """ |
| 52 | + Class that encodes the types of dependencies. |
| 53 | + """ |
| 54 | + |
| 55 | + |
| 56 | + def __init__(self, hierarchy=stanford_dependency_hierarchy): |
| 57 | + self.hierarchy=hierarchy |
| 58 | + |
| 59 | + self.flatMap = {} |
| 60 | + |
| 61 | + self.parentToChildren = {} |
| 62 | + |
| 63 | + activeSet = [self.hierarchy] |
| 64 | + |
| 65 | + while len(activeSet) != 0: |
| 66 | + newActiveSet = [] |
| 67 | + for item in activeSet: |
| 68 | + for key, mapValue in item.iteritems(): |
| 69 | + self.flatMap[key] = mapValue |
| 70 | + self.parentToChildren[key] = sorted(list(mapValue.keys())) |
| 71 | + newActiveSet.append(mapValue) |
| 72 | + |
| 73 | + activeSet = newActiveSet |
| 74 | + |
| 75 | + self.ancestorToDescendents = {} |
| 76 | + |
| 77 | + for key, mapValue in self.flatMap.iteritems(): |
| 78 | + descendents = [] |
| 79 | + |
| 80 | + activeSet = [mapValue] |
| 81 | + while len(activeSet) != 0: |
| 82 | + newActiveSet = [] |
| 83 | + for item in activeSet: |
| 84 | + for childKey, mapValue in item.iteritems(): |
| 85 | + newActiveSet.extend(mapValue.values()) |
| 86 | + descendents.append(childKey) |
| 87 | + activeSet = newActiveSet |
| 88 | + |
| 89 | + self.ancestorToDescendents[key] = sorted(descendents) |
| 90 | + def isa(self, relation, ancestor): |
| 91 | + return relation in self.ancestorToDescendents[ancestor] |
| 92 | + |
| 93 | + |
| 94 | + |
0 commit comments