def __init__(self, domain, properties, binary_rels, valuation): self._domain = domain self._properties = properties self._binary_rels = binary_rels self._model = nltk.Model(set(domain), nltk.Valuation(valuation)) self._valuation = valuation self._preds = dict() self._pred_sats = dict() for (pred, sats) in valuation: self._preds[pred] = sats if pred in binary_rels: sat_map_0 = dict() sat_map_1 = dict() for sat in sats: if sat[0] not in sat_map_0: sat_map_0[sat[0]] = set([]) if sat[1] not in sat_map_1: sat_map_1[sat[1]] = set([]) sat_map_1[sat[1]].add(sat[0]) sat_map_0[sat[0]].add(sat[1]) self._pred_sats[pred] = (sat_map_0, sat_map_1) elif pred in properties: sat_set = set([]) for sat in sats: sat_set.add(sat) self._pred_sats[pred] = sat_set
def logic_parser(): lp = nltk.LogicParser() SnF = lp.parse('SnF') NotFnS = lp.parse('-FnS') R = lp.parse('SnF -> -FnS') # prover = nltk.Prover9() # print prover.prove(NotFnS, [SnF, R]) val = nltk.Valuation([('P', True), ('Q', True), ('R', False)]) dom = set([]) g = nltk.Assignment(dom) m = nltk.Model(dom, val) print "eval(P&Q)=", m.evaluate('(P & Q)', g) print "eval -(P&Q)=", m.evaluate('-(P & Q)', g) print "eval(P&R)=", m.evaluate('(P & R)', g) print "eval(-(P|R))=", m.evaluate('-(P | R)', g)
read_expr('P & Q') read_expr('P | (R -> Q)') read_expr('P <-> -- P') lp = nltk.sem.Expression.fromstring SnF = read_expr('SnF') NotFnS = read_expr('-FnS') R = read_expr('SnF -> -FnS') prover = nltk.Prover9() prover.config_prover9(r'D:\Program Files\LADR1007B-win\bin') prover.prove(NotFnS, [SnF, R]) val = nltk.Valuation([('P', True), ('Q', True), ('R', False)]) val['P'] dom = set() g = nltk.Assignment(dom) m = nltk.Model(dom, val) print(m.evaluate('(P & Q)', g)) print(m.evaluate('-(P & Q)', g)) print(m.evaluate('(P & R)', g)) print(m.evaluate('(P | R)', g)) ##一阶逻辑 read_expr = nltk.sem.Expression.fromstring expr = read_expr('walk(angus)', type_check=True)
import nltk value = nltk.Valuation([('X', True), ('Y', False), ('Z', True)]) print(value['Z']) domain = set() v = nltk.Assignment(domain) u = nltk.Model(domain, value) print(u.evaluate('(X & Y)', v)) print(u.evaluate('-(X & Y)', v)) print(u.evaluate('(X & Z)', v)) print(u.evaluate('(X | Y)', v))
propositon_Q = 'He will eat a sandwich' p_statuses = [False, False, True, True] q_statuses = [False, True, False, True] conjunction = '(P & Q)' disjunction = '(P | Q)' implication = '(P -> Q)' equivalence = '(P <-> Q)' expressions = [conjunction, disjunction, implication, equivalence] results = [] for status_p, status_q in zip(p_statuses, q_statuses): dom = set([]) val = nltk.Valuation([(symbol_P, status_p), (symbol_Q, status_q)]) assignments = nltk.Assignment(dom) model = nltk.Model(dom, val) row = [status_p, status_q] for expression in expressions: result = model.evaluate(expression, assignments) row.append(result) results.append(row) columns = [symbol_P, symbol_Q, conjunction, disjunction, implication, equivalence] result_frame = pd.DataFrame(results, columns=columns) print ('P:', proposition_P) print ('Q:', propositon_Q) print ()