def sr_parse(self, texts): """ Shift-reduce RST parsing based on model prediction :type texts: list of string :param texts: list of EDUs for parsing """ # Initialize parser srparser = SRParser([],[]) srparser.init(texts) # Parsing while not srparser.endparsing(): # Generate features stack, queue = srparser.getstatus() # Make sure call the generator with # same arguments as in data generation part fg = FeatureGenerator(stack, queue) features = fg.features() labels = self.predict(features) # Enumerate through all possible actions ranked based on predcition scores for i,label in enumerate(labels): action = label2action(label) try: srparser.operate(action) break # if legal action, end the loop except ActionError: if i < len(labels): # if not a legal action, try the next possible action continue else: print "Parsing action error with {}".format(action) sys.exit() tree = srparser.getparsetree() rst = RSTTree(tree=tree) return rst
def sr_parse(self, texts, d_pos, d_dep): """ Shift-reduce RST parsing based on model prediction :type texts: list of string :param texts: list of EDUs for parsing """ # Initialize parser srparser = SRParser([],[]) srparser.init(texts, d_pos, d_dep) # Parsing while not srparser.endparsing(): # Generate features stack, queue = srparser.getstatus() # Make sure call the generator with # same arguments as in data generation part fg = FeatureGenerator(stack, queue) features = fg.features() label = self.predict(features) action = label2action(label) # The best choice here is to choose the first # legal action try: srparser.operate(action) except ActionError: print "Parsing action error with {}".format(action) sys.exit() tree = srparser.getparsetree() rst = RSTTree(tree=tree) return rst
def sr_parse(self, doc, bcvocab=None): """ Shift-reduce RST parsing based on model prediction :type texts: list of string :param texts: list of EDUs for parsing :type bcvocab: dict :param bcvocab: brown clusters """ # raise NotImplementedError("Not finished yet") # Initialize parser srparser = SRParser([], []) srparser.init(doc) # Parsing while not srparser.endparsing(): # Generate features stack, queue = srparser.getstatus() # Make sure call the generator with # same arguments as in data generation part fg = FeatureGenerator(stack, queue, doc, bcvocab) feat = fg.features() # label = self.predict(feat) labels = self.rank_labels(feat) for label in labels: action = label2action(label) try: srparser.operate(action) break except ActionError: # print "Parsing action error with {}".format(action) pass tree = srparser.getparsetree() rst = RSTTree() rst.asign_tree(tree) return rst
def sr_parse(self, texts): """ Shift-reduce RST parsing based on model prediction :type texts: list of string :param texts: list of EDUs for parsing """ # Initialize parser srparser = SRParser([], []) srparser.init(texts) # Parsing while not srparser.endparsing(): # Generate features stack, queue = srparser.getstatus() # Make sure call the generator with # same arguments as in data generation part fg = FeatureGenerator(stack, queue) features = fg.features() label = self.predict(features) action = label2action(label) # The best choice here is to choose the first # legal action try: srparser.operate(action) except ActionError: print "Parsing action error with {}".format(action) sys.exit() tree = srparser.getparsetree() rst = RSTTree(tree=tree) return rst
def generate_samples(self): """ Generate samples from an binary RST tree """ # Sample list samplelist = [] # Parsing action actionlist = decodeSRaction(self.tree) # Initialize queue and stack queue = getedunode(self.tree) stack = [] # Start simulating the shift-reduce parsing for action in actionlist: # Generate features fg = FeatureGenerator(stack, queue) features = fg.features() samplelist.append(features) # Change status of stack/queue sr = SRParser(stack, queue) sr.operate(action) # stack, queue = sr.getstatus() return (actionlist, samplelist)
def sr_parse(self, texts,fname): """ Shift-reduce RST parsing based on model prediction :type texts: list of string :param texts: list of EDUs for parsing """ # Initialize parser srparser = SRParser([],[]) dep = defaultdict() pos = defaultdict() lines =defaultdict() # print fname.split(".dis")[0]+'.dep' dir = fname.split s =fname.split(".edus") # print fname # st= fname if fname.endswith(".out.edus"): # print "yes" s= fname.split(".out.edus") f= open(s[0]+'.dep',"r") data = f.read().splitlines() for line in data: # print line l = line.split('@#%^&*') dep[l[0]] = l[1] f= open(s[0]+'.pos',"r") data = f.read().splitlines() for line in data: # print line l = line.split('@#%^&*') pos[l[0]] = l[1].strip() f= open(s[0]+'.line',"r") data = f.read().splitlines() for line in data: # print line l = line.split('@#%^&*') lines[l[0]] = l[1] srparser.init(texts,pos,dep,lines) # Parsing while not srparser.endparsing(): # Generate features stack, queue = srparser.getstatus() # Make sure call the generator with # same arguments as in data generation part fg = FeatureGenerator(stack, queue) features = fg.features() labels = self.predict(features) # Enumerate through all possible actions ranked based on predcition scores for i,label in enumerate(labels): action = label2action(label) try: srparser.operate(action) break # if legal action, end the loop except ActionError: if i < len(labels): # if not a legal action, try the next possible action continue else: print "Parsing action error with {}".format(action) sys.exit() tree = srparser.getparsetree() rst = RSTTree(tree=tree) return rst