diff options
Diffstat (limited to 'stanford_parser/dependencies.py')
| -rw-r--r-- | stanford_parser/dependencies.py | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/stanford_parser/dependencies.py b/stanford_parser/dependencies.py new file mode 100644 index 0000000..4cda581 --- /dev/null +++ b/stanford_parser/dependencies.py @@ -0,0 +1,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] + + + |
