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 parse_cooper_storage(tree): semantic_representation = cs.CooperStore(tree.label()['SEM']) 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()
def sem_parser(sents, syntax, verbose=False, is_cs=False): """ It parses sentences with an FDFG grammar and returns a dictionary of sentences to their semantic representations. Parameters: sents: a list of sentences to be parsed. fcfg_string: a string listing all fcfg rules with SEM for the FeatureGrammar. verbose: boolean value. default value is `False`. if verbose is True it prints results. is_cs: boolean value. Inicating if it is using Cooper Storage. Default value is `False`. Returns: dict: dictionary of sentences translated to a list of their semantic representaions. """ sents_reps = {sent: [] for sent in sents} for sent, results in zip(sents, nltk.interpret_sents(sents, syntax)): if verbose: display(Markdown(f"----\n{sent}: {len(results)} result(s)")) for j, (synrep, semrep) in enumerate(results): if is_cs: cs_semrep = cs.CooperStore(semrep) cs_semrep.s_retrieve(trace=False) for reading in cs_semrep.readings: sents_reps[sent].append(reading) if verbose: display_latex( reading) # prints the SEM feature of a tree if verbose: display_tree(synrep) # show the parse tree else: sents_reps[sent].append(semrep) if verbose: display_latex(semrep) # prints the SEM feature of a tree display_tree(synrep) # show the parse tree return sents_reps
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(drs1)