def demo(): from nltk.sem import cooper_storage as cs sentence = "every girl chases a dog" #sentence = "a man gives a bone to every dog" print() print("Analyis of sentence '%s'" % sentence) print("=" * 50) trees = cs.parse_with_bindops(sentence, trace=0) for tree in trees: semrep = cs.CooperStore(tree.label()['SEM']) print() print("Binding operators:") print("-" * 15) for s in semrep.store: print(s) print() print("Core:") print("-" * 15) print(semrep.core) print() print("S-Retrieval:") print("-" * 15) semrep.s_retrieve(trace=True) print("Readings:") print("-" * 15) for i, reading in enumerate(semrep.readings): print("%s: %s" % (i+1, reading))
def demo(): from nltk.sem import cooper_storage as cs sentence = "every girl chases a dog" #sentence = "a man gives a bone to every dog" print() print("Analyis of sentence '%s'" % sentence) print("=" * 50) trees = cs.parse_with_bindops(sentence, trace=0) for tree in trees: semrep = cs.CooperStore(tree.node['SEM']) print() print("Binding operators:") print("-" * 15) for s in semrep.store: print(s) print() print("Core:") print("-" * 15) print(semrep.core) print() print("S-Retrieval:") print("-" * 15) semrep.s_retrieve(trace=True) print("Readings:") print("-" * 15) for i, reading in enumerate(semrep.readings): print("%s: %s" % (i + 1, reading))
def demo(): from nltk.sem import cooper_storage as cs sentence = "every girl chases a dog" # sentence = "a man gives a bone to every dog" print print "Analyis of sentence '%s'" % sentence print "=" * 50 trees = cs.parse_with_bindops(sentence, trace=0) for tree in trees: semrep = cs.CooperStore(tree.node["SEM"]) print print "Binding operators:" print "-" * 15 for s in semrep.store: print s print print "Core:" print "-" * 15 print semrep.core print print "S-Retrieval:" print "-" * 15 semrep.s_retrieve(trace=True) print "Readings:" print "-" * 15 for i, reading in enumerate(semrep.readings): print "%s: %s" % (i + 1, reading)
print semantic_representation.s_retrieve,'k' semantic_representation.s_retrieve() for reading in semantic_representation.readings: print 'Reading %s'%reading print 'Simplified Reading %s'%reading.simplify() print 'Predicates: ', reading.predicates() print 'Variables: ',reading.variables() hpi = "33_year_old man with coronary_artery_disease complicated_by coronary_artery_bypass_graft in 2010" #hpi = '33_year_old man' #hpi = "stroke and coronary_artery_disease in 2010" #hpi = "33_year_old man with coronary_artery_disease and stroke in 2010" #hpi = "coronary_artery_disease complicated_by coronary_artery_bypass_graft" #parser = nltk.parse.FeatureEarleyChartParser(grammar) #trees = cs.parse_with_bindops(hpi, grammar=r'./with.fcfg') trees = cs.parse_with_bindops(hpi, grammar=r'./many-quantified-nps.fcfg') #trees = [tree for tree in parser.parse(hpi)] if len(trees) == 1: trees[0].draw() parse_cooper_storage(trees[0]) else: print '%d trees. Have to choose'%len(trees) print 'No choice function defined' for tree in trees: tree.draw() parse_cooper_storage(tree)
print(expr1) expr2 = expr1.alpha_convert(nltk.sem.Variable('z')) print(expr2) expr1 == expr2 expr3 = read_expr('\P.(exists x.P(x))(\y.see(y, x))') print(expr3) print(expr3.simplify()) ##量词歧义 from nltk.sem import cooper_storage as cs sentence = 'every girl chases a dog' trees = cs.parse_with_bindops(sentence, grammar='grammars/book_grammars/storage.fcfg') semrep = trees[0].label()['SEM'] cs_semrep = cs.CooperStore(semrep) print(cs_semrep.core) for bo in cs_semrep.store: print(bo) cs_semrep.s_retrieve(trace=True) for reading in cs_semrep.readings: print(reading) #段落语义理解 read_dexpr = nltk.sem.DrtExpression.fromstring drs1 = read_dexpr('([x, y], [angus(x), dog(y), own(x, y)])')
print semantic_representation.s_retrieve, 'k' semantic_representation.s_retrieve() for reading in semantic_representation.readings: print 'Reading %s' % reading print 'Simplified Reading %s' % reading.simplify() print 'Predicates: ', reading.predicates() print 'Variables: ', reading.variables() hpi = "33_year_old man with coronary_artery_disease complicated_by coronary_artery_bypass_graft in 2010" #hpi = '33_year_old man' #hpi = "stroke and coronary_artery_disease in 2010" #hpi = "33_year_old man with coronary_artery_disease and stroke in 2010" #hpi = "coronary_artery_disease complicated_by coronary_artery_bypass_graft" #parser = nltk.parse.FeatureEarleyChartParser(grammar) #trees = cs.parse_with_bindops(hpi, grammar=r'./with.fcfg') trees = cs.parse_with_bindops(hpi, grammar=r'./many-quantified-nps.fcfg') #trees = [tree for tree in parser.parse(hpi)] if len(trees) == 1: trees[0].draw() parse_cooper_storage(trees[0]) else: print '%d trees. Have to choose' % len(trees) print 'No choice function defined' for tree in trees: tree.draw() parse_cooper_storage(tree)
from nltk.sem import cooper_storage as cs from nltk.draw.util import CanvasFrame from nltk.draw import TreeWidget sentence = 'a girl chases a dog' trees = cs.parse_with_bindops(sentence, grammar=r'./storage.fcfg') trees[0].draw()