Ejemplo n.º 1
0
def solve_fol_inference(prem_fol, hypo_fol, str_axioms=[], prover=Prover9(timeout=5), p_number=0):
    '''Given FOL formulas of a premise and a hypothesis,
       extract relevant knowledge axioms from WordNet, and solve the inference
    '''
    # if one of the formulas is not derived, return neutral
    if not prem_fol or not hypo_fol:
        return 'NEUTRAL', 'No closed formulas'
    # retrieve knowledge from WordNet based on the predicates found in the formulas
    axioms = [ semexp.fromstring(a) for a in str_axioms ]
    axioms = axioms + wn_axioms([prem_fol, hypo_fol], p_number)
    if axioms:
        info("Extracted axioms: {}".format(axioms))
    info("Premise Formula: {}".format(prem_fol))
    info("Hypothesis Formula: {}".format(hypo_fol))
    # run prover two times, for checking entailment and contradiction relations
    try:
        Entails = prover.prove(hypo_fol, [prem_fol] + axioms)
        Contradicts = prover.prove(hypo_fol.negate(), [prem_fol] + axioms)
    except Exception as e:
        warning(e)
        return 'NEUTRAL', e
    # if only one of Entails and Contradicts is true, then return that label
    # in all other cases return NEUTRAL,
    # but give a warning if both Entails and Contradicts are true
    if Entails:
        if not Contradicts:
            return 'ENTAILMENT', 'Definite answer'
        warning('Both ENTAILMENT and CONTRADICTION')
        return 'NEUTRAL', 'Mixed answers'
    if Contradicts:
        return 'CONTRADICTION', 'Definite answer'
    return 'NEUTRAL', 'Definite answer'
Ejemplo n.º 2
0
    def __init__(self, semtype_file=None, remove_duplicates=False,
                 depparser=None, verbose=False):
        self.verbose = verbose
        self.remove_duplicates = remove_duplicates
        self.depparser = depparser

        from nltk import Prover9
        self.prover = Prover9()

        if semtype_file:
            self.semtype_file = semtype_file
        else:
            self.semtype_file = os.path.join('grammars', 'sample_grammars','glue.semtype')
Ejemplo n.º 3
0
    def __init__(self,
                 semtype_file=None,
                 remove_duplicates=False,
                 depparser=None,
                 verbose=False):
        self.verbose = verbose
        self.remove_duplicates = remove_duplicates
        self.depparser = depparser

        from nltk import Prover9
        self.prover = Prover9()

        if semtype_file:
            self.semtype_file = semtype_file
        else:
            self.semtype_file = 'glue.semtype'
# %%
from nltk import Prover9, Prover9Command
from nltk.sem import Expression

read_expr = Expression.fromstring

p = Prover9()
p._binary_location = r"C:\Program Files (x86)\Prover9-Mace4\bin-win32"

# %%
assumptions = [
    read_expr(p) for p in [
        # Every child loves Santa.
        """
        all x.( child(x) -> loves(x,Santa) )
        """,
        # Everyone who loves Santa loves any reindeer.
        """
        all x.(
            loves(x,Santa) -> (
                all y.(reindeer(y)-> loves(x,y))
            )
        )
        """,
        # Rudolph is a reindeer, and Rudolph has a red nose.
        """
        reindeer(Rudolph) & rednose(Rudolph)
        """,
        # Anything which has a red nose is weird or is a clown.
        """
        all x.(rednose(x) ->  weird(x) | clown(x))
Ejemplo n.º 5
0
import json
from nltk.sem.logic import *
from nltk.inference import *
from nltk import Prover9
from joblib import Parallel, delayed
from data_util import sentence

prover = Prover9()
prover.config_prover9(r"C:\Program Files (x86)\Prover9-Mace4\bin-win32")


def get_label(premise, hypothesis):
    #returns a label that is determined from using Prover9 on the first order logic representations
    premise_assumptions = Expression.fromstring(premise.assumptions)
    hypothesis_assumptions = Expression.fromstring(hypothesis.assumptions)
    premise_logical_form = Expression.fromstring(premise.logical_form)
    hypothesis_logical_form = Expression.fromstring(hypothesis.logical_form)
    negated_hypothesis_logical_form = Expression.fromstring(
        "-" + "(" + hypothesis.logical_form + ")")
    if prover.prove(
            hypothesis_logical_form,
        [premise_logical_form, premise_assumptions, hypothesis_assumptions]):
        return "entailment"
    if prover.prove(
            negated_hypothesis_logical_form,
        [premise_logical_form, premise_assumptions, hypothesis_assumptions]):
        return "contradiction"
    return "neutral"


def build_simple_file(name):