def parse_sents(self, sents): os.environ['MALT_PARSER'] = config['MALT_WORKING_DIR'] + '/malt.jar' parser = NltkMaltParser(working_dir=config['MALT_WORKING_DIR'], mco=config['MALT_MCO'], additional_java_args=config['MALT_JAVA_ARGS'], tagger=self.tagger) graphs = [list(graph)[0] for graph in parser.parse_sents(sents)] # Sometimes, there is an empty graph at the end of the list. Delete it. if len(graphs) > 0 and len(graphs[-1].nodes) == 1: del graphs[-1] return graphs
# print(shortestPath(pt,'I','elephant')) # parsing many sentences tagged_sents= [ "The other day I went to the beach.".split(), "It was a hot day so I swimmed in the water.".split()] # feed all tagged_sents to Maltparser # deptrees = list(dparser.parse_tagged_sents( # tagged_sents)) # feed all tagged_sents to Maltparser deptrees = dparser.parse_sents(tagged_sents) print(deptrees) deptrees = [list(e) for e in deptrees ] # e = deptrees[0] # print(e[0]) # print(type(e[0])) # print(dir(e[0])) # print(e[0].root) # print(e[0].tree()) for e in deptrees: try: print(e[0].tree()) except: pass
# print(shortestPath(pt,'elephant','pajamas')) # print(shortestPath(pt,'I','elephant')) # parsing many sentences tagged_sents = [ "The other day I went to the beach.".split(), "It was a hot day so I swimmed in the water.".split() ] # feed all tagged_sents to Maltparser # deptrees = list(dparser.parse_tagged_sents( # tagged_sents)) # feed all tagged_sents to Maltparser deptrees = dparser.parse_sents(tagged_sents) print(deptrees) deptrees = [list(e) for e in deptrees] # e = deptrees[0] # print(e[0]) # print(type(e[0])) # print(dir(e[0])) # print(e[0].root) # print(e[0].tree()) for e in deptrees: try: print(e[0].tree()) except: pass