class MaltParser(DependencyParserWrapper): # currently this can only be run from the malt parser directory # TODO: make more general/easy to run def __init__(self, version = "maltparser-1.9.2", model = "engmalt.linear-1.7.mco"): super().__init__() root = os.getcwd() version_path = os.path.join(root, "schemata", "parse", "maltparser", version ) model_path =os.path.join(root, "schemata", "parse", "maltparser", model ) self.base = MP(version_path, model_path) def get_spans(self, sent): dparse = self.base.parse_one(sent.split()) heads = [node['head'] for _, node in sorted(dparse.nodes.items())][1:] tree = DependencyParserWrapper.head_to_tree(heads) non_singletons = DependencyParserWrapper.compute_spans(tree) singletons = [(n, n+1) for n in range(len(heads))] return set(non_singletons) | set(singletons)
sublist1 = path1[j:] #print("sublist1",sublist1) if j< len(path2)-1: j=j+1 sublist2 = path2[j:] #print("sublist2",sublist2) sublist2.reverse() #print("sublist2",sublist2) shortestpath = sublist2 + sublist1 return shortestpath dparser = MaltParser('../data/grammars/maltparser-1.8.1/', 'engmalt.linear-1.7.mco') pt = dparser.parse_one('I shot an elephant in my pajamas .'.split()).tree() # print(pt) # print(pt) # print(shortestPath(pt,'I','pajamas')) # print(shortestPath(pt,'I','pajamas')) # print(shortestPath(pt,'elephant','pajamas')) # print(shortestPath(pt,'I','elephant')) # parsing many sentences tagged_sents= [ "The other day I went to the beach.".split(),
sublist1 = path1[j:] #print("sublist1",sublist1) if j < len(path2) - 1: j = j + 1 sublist2 = path2[j:] #print("sublist2",sublist2) sublist2.reverse() #print("sublist2",sublist2) shortestpath = sublist2 + sublist1 return shortestpath dparser = MaltParser('../data/grammars/maltparser-1.8.1/', 'engmalt.linear-1.7.mco') pt = dparser.parse_one('I shot an elephant in my pajamas .'.split()).tree() # print(pt) # print(pt) # print(shortestPath(pt,'I','pajamas')) # print(shortestPath(pt,'I','pajamas')) # 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() ]
import sys from nltk.parse.malt import MaltParser PATH_TO_MALTPARSER = "maltparser-1.9.2" PATH_TO_MODEL = "kaist-conll.mco" def print_usage(): print("usage: $ python3 parser.py <input text>") if __name__ == '__main__': argv = sys.argv[1:] argc = len(sys.argv) if (argc != 2): print_usage() sys.exit() user_input = argv[0] tokens = user_input.split() mp = MaltParser(PATH_TO_MALTPARSER, PATH_TO_MODEL) graph = mp.parse_one(tokens).tree() print(graph)