本文整理汇总了Python中nltk.sem.glue.DrtGlue类的典型用法代码示例。如果您正苦于以下问题:Python DrtGlue类的具体用法?Python DrtGlue怎么用?Python DrtGlue使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DrtGlue类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: tag_sentences
def tag_sentences(self, text, hyp, verbose=False):
"""
Tag a RTEPair as to whether the hypothesis can be inferred from the text.
"""
glueclass = DrtGlue()
text_drs_list = glueclass.parse_to_meaning(text)
if text_drs_list:
text_ex = text_drs_list[0].simplify().toFol()
else:
if verbose: print 'ERROR: No readings were generated for the Text'
hyp_drs_list = glueclass.parse_to_meaning(hyp)
if hyp_drs_list:
hyp_ex = hyp_drs_list[0].simplify().toFol()
else:
if verbose: print 'ERROR: No readings were generated for the Hypothesis'
#1. proof T -> H
#2. proof (BK & T) -> H
#3. proof :(BK & T)
#4. proof :(BK & T & H)
#5. satisfy BK & T
#6. satisfy BK & T & H
result = inference.Prover9().prove(hyp_ex, [text_ex])
if verbose: print 'prove: T -> H: %s' % result
if not result:
bk = self._generate_BK(text, hyp, verbose)
bk_exs = [bk_pair[0] for bk_pair in bk]
if verbose:
print 'Generated Background Knowledge:'
for bk_ex in bk_exs:
print bk_ex
result = inference.Prover9().prove(hyp_ex, [text_ex]+bk_exs)
if verbose: print 'prove: (T & BK) -> H: %s' % result
if not result:
consistent = self.check_consistency(bk_exs+[text_ex])
if verbose: print 'consistency check: (BK & T): %s' % consistent
if consistent:
consistent = self.check_consistency(bk_exs+[text_ex, hyp_ex])
if verbose: print 'consistency check: (BK & T & H): %s' % consistent
return result
示例2: DrtGlueReadingCommand
class DrtGlueReadingCommand(ReadingCommand):
def __init__(self, semtype_file=None, remove_duplicates=False,
depparser=None):
"""
:param semtype_file: name of file where grammar can be loaded
:param remove_duplicates: should duplicates be removed?
:param depparser: the dependency parser
"""
if semtype_file is None:
semtype_file = 'drt_glue.semtype'
self._glue = DrtGlue(semtype_file=semtype_file,
remove_duplicates=remove_duplicates,
depparser=depparser)
def parse_to_readings(self, sentence):
""":see: ReadingCommand.parse_to_readings()"""
return self._glue.parse_to_meaning(sentence)
def process_thread(self, sentence_readings):
""":see: ReadingCommand.process_thread()"""
try:
return [self.combine_readings(sentence_readings)]
except AnaphoraResolutionException:
return []
def combine_readings(self, readings):
""":see: ReadingCommand.combine_readings()"""
thread_reading = reduce(add, readings)
return resolve_anaphora(thread_reading.simplify())
def to_fol(self, expression):
""":see: ReadingCommand.to_fol()"""
return expression.fol()
示例3: __init__
def __init__(self, semtype_file=None, remove_duplicates=False, depparser=None):
"""
:param semtype_file: name of file where grammar can be loaded
:param remove_duplicates: should duplicates be removed?
:param depparser: the dependency parser
"""
if semtype_file is None:
semtype_file = "drt_glue.semtype"
self._glue = DrtGlue(semtype_file=semtype_file, remove_duplicates=remove_duplicates, depparser=depparser)
示例4: _init_glue
def _init_glue(self):
tagger = RegexpTagger(
[('^(David|Mary|John)$', 'NNP'),
('^(walks|sees|eats|chases|believes|gives|sleeps|chases|persuades|tries|seems|leaves)$', 'VB'),
('^(go|order|vanish|find|approach)$', 'VB'),
('^(a)$', 'ex_quant'),
('^(every)$', 'univ_quant'),
('^(sandwich|man|dog|pizza|unicorn|cat|senator)$', 'NN'),
('^(big|gray|former)$', 'JJ'),
('^(him|himself)$', 'PRP')
])
depparser = MaltParser(tagger=tagger)
self._glue = DrtGlue(depparser=depparser, remove_duplicates=False)
示例5: DrtGlueDemo
class DrtGlueDemo(object):
def __init__(self, examples):
# Set up the main window.
self._top = Tk()
self._top.title('DRT Glue Demo')
# Set up key bindings.
self._init_bindings()
# Initialize the fonts.self._error = None
self._init_fonts(self._top)
self._examples = examples
self._readingCache = [None for example in examples]
# The user can hide the grammar.
self._show_grammar = IntVar(self._top)
self._show_grammar.set(1)
# Set the data to None
self._curExample = -1
self._readings = []
self._drs = None
self._drsWidget = None
self._error = None
self._init_glue()
# Create the basic frames.
self._init_menubar(self._top)
self._init_buttons(self._top)
self._init_exampleListbox(self._top)
self._init_readingListbox(self._top)
self._init_canvas(self._top)
# Resize callback
self._canvas.bind('<Configure>', self._configure)
#########################################
## Initialization Helpers
#########################################
def _init_glue(self):
tagger = RegexpTagger(
[('^(David|Mary|John)$', 'NNP'),
('^(walks|sees|eats|chases|believes|gives|sleeps|chases|persuades|tries|seems|leaves)$', 'VB'),
('^(go|order|vanish|find|approach)$', 'VB'),
('^(a)$', 'ex_quant'),
('^(every)$', 'univ_quant'),
('^(sandwich|man|dog|pizza|unicorn|cat|senator)$', 'NN'),
('^(big|gray|former)$', 'JJ'),
('^(him|himself)$', 'PRP')
])
depparser = MaltParser(tagger=tagger)
self._glue = DrtGlue(depparser=depparser, remove_duplicates=False)
def _init_fonts(self, root):
# See: <http://www.astro.washington.edu/owen/ROTKFolklore.html>
self._sysfont = Font(font=Button()["font"])
root.option_add("*Font", self._sysfont)
# TWhat's our font size (default=same as sysfont)
self._size = IntVar(root)
self._size.set(self._sysfont.cget('size'))
self._boldfont = Font(family='helvetica', weight='bold',
size=self._size.get())
self._font = Font(family='helvetica',
size=self._size.get())
if self._size.get() < 0: big = self._size.get()-2
else: big = self._size.get()+2
self._bigfont = Font(family='helvetica', weight='bold',
size=big)
def _init_exampleListbox(self, parent):
self._exampleFrame = listframe = Frame(parent)
self._exampleFrame.pack(fill='both', side='left', padx=2)
self._exampleList_label = Label(self._exampleFrame, font=self._boldfont,
text='Examples')
self._exampleList_label.pack()
self._exampleList = Listbox(self._exampleFrame, selectmode='single',
relief='groove', background='white',
foreground='#909090', font=self._font,
selectforeground='#004040',
selectbackground='#c0f0c0')
self._exampleList.pack(side='right', fill='both', expand=1)
for example in self._examples:
self._exampleList.insert('end', (' %s' % example))
self._exampleList.config(height=min(len(self._examples), 25), width=40)
# Add a scrollbar if there are more than 25 examples.
if len(self._examples) > 25:
listscroll = Scrollbar(self._exampleFrame,
orient='vertical')
self._exampleList.config(yscrollcommand = listscroll.set)
listscroll.config(command=self._exampleList.yview)
listscroll.pack(side='left', fill='y')
#.........这里部分代码省略.........
示例6: demo_inference_tagger
def demo_inference_tagger(verbose=False):
tagger = RTEInferenceTagger()
text = 'John see a car'
print 'Text: ', text
hyp = 'John watch an auto'
print 'Hyp: ', hyp
# text_ex = LogicParser().parse('exists e x y.(david(x) & own(e) & subj(e,x) & obj(e,y) & car(y))')
# hyp_ex = LogicParser().parse('exists e x y.(david(x) & have(e) & subj(e,x) & obj(e,y) & auto(y))')
glueclass = DrtGlue(verbose=verbose)
text_drs_list = glueclass.parse_to_meaning(text)
if text_drs_list:
text_ex = text_drs_list[0].simplify().toFol()
else:
print 'ERROR: No readings were be generated for the Text'
hyp_drs_list = glueclass.parse_to_meaning(hyp)
if hyp_drs_list:
hyp_ex = hyp_drs_list[0].simplify().toFol()
else:
print 'ERROR: No readings were be generated for the Hypothesis'
print 'Text: ', text_ex
print 'Hyp: ', hyp_ex
print ''
#1. proof T -> H
#2. proof (BK & T) -> H
#3. proof :(BK & T)
#4. proof :(BK & T & H)
#5. satisfy BK & T
#6. satisfy BK & T & H
result = inference.Prover9().prove(hyp_ex, [text_ex])
print 'prove: T -> H: %s' % result
if result:
print 'Logical entailment\n'
else:
print 'No logical entailment\n'
bk = tagger._generate_BK(text, hyp, verbose)
bk_exs = [bk_pair[0] for bk_pair in bk]
print 'Generated Background Knowledge:'
for bk_ex in bk_exs:
print bk_ex
print ''
result = inference.Prover9().prove(hyp_ex, [text_ex]+bk_exs)
print 'prove: (T & BK) -> H: %s' % result
if result:
print 'Logical entailment\n'
else:
print 'No logical entailment\n'
# Check if the background knowledge axioms are inconsistent
result = inference.Prover9().prove(assumptions=bk_exs+[text_ex]).prove()
print 'prove: (BK & T): %s' % result
if result:
print 'Inconsistency -> Entailment unknown\n'
else:
print 'No inconsistency\n'
result = inference.Prover9().prove(assumptions=bk_exs+[text_ex, hyp_ex])
print 'prove: (BK & T & H): %s' % result
if result:
print 'Inconsistency -> Entailment unknown\n'
else:
print 'No inconsistency\n'
result = inference.Mace().build_model(assumptions=bk_exs+[text_ex])
print 'satisfy: (BK & T): %s' % result
if result:
print 'No inconsistency\n'
else:
print 'Inconsistency -> Entailment unknown\n'
result = inference.Mace().build_model(assumptions=bk_exs+[text_ex, hyp_ex]).build_model()
print 'satisfy: (BK & T & H): %s' % result
if result:
print 'No inconsistency\n'
else:
print 'Inconsistency -> Entailment unknown\n'