def reverse_premises(syllogism, first=True, second=True): additional_premises = [] if first: additional_premises.append(syllogism[0] + sylutil.term_order(syllogism[2])[0][::-1]) if second: additional_premises.append(syllogism[1] + sylutil.term_order(syllogism[2])[1][::-1]) return additional_premises
def reencode(self, syllogism, vm, conclusions): # sort rows (individuals) of VM by access time (most recent first) sorted_inds = sorted(vm, key=lambda x: x.t, reverse=True) # sort properties per individual by access time (most recent first), flatten and uniquify sorted_props = [sorted(ind.props, key=lambda p: p.t, reverse=True) for ind in sorted_inds] sorted_props = sylutil.uniquify_keep_order([p for l in sorted_props for p in l]) p1_terms, p2_terms = sylutil.term_order(syllogism[2]) for prop in sorted_props: for target_premise in [syllogism[0] + p1_terms, syllogism[1] + p2_terms]: if prop.name in target_premise: # reencode the target premise without additional information if not prop.neg: vm = self.extend_vm(vm, target_premise, reencoding=True) new_conclusions = self.conclude(vm) if any([c not in conclusions for c in new_conclusions]): return vm, new_conclusions # get indirect information encoded as additional premise prem, subj_neg = self.get_additional_premises(target_premise, prop) if prem is not None: # reencode using the additional premise vm = self.extend_vm(vm, prem, reencoding=True, subj_neg=subj_neg) new_conclusions = self.conclude(vm) if any([c not in conclusions for c in new_conclusions]): return vm, new_conclusions return vm, ["NVC"]
def encode(self, syllogism): """ Returns a VM by initially encoding a syllogism >>> vm = VerbalModels() >>> vm.params.update(dict.fromkeys(["p1", "p2", "p3", "p4", "p5", "p6"], "a")) >>> vm.encode("IA4") # p. 539 [[b'(0), a(0), c(1)](0), [b'(0), c(1)](0)] >>> vm.encode("IA3") # p. 542 [[a'(2), b(2)](2), [a'(2)](2), [c'(3), b(3)](3)] >>> vm.encode("AI1") # p. 542 [[a'(4), b'(4), c(5)](5), [a'(4), b'(4)](5)] >>> vm.encode("EI1") # own [[a'(6), -b(6)](6), [b'(7), c(7)](7), [b'(7)](7)] >>> vm.encode("AA1") # own [[a'(8), b'(8), c(9)](8)] >>> vm.encode("EE1") # own [[a'(10), -b(10)](10), [b'(11), -c(11)](11)] """ vm = [] premises = [syllogism[i] + sylutil.term_order(syllogism[2])[i] for i in [0, 1]] for premise in premises: vm = self.extend_vm(vm, premise, reencoding=False) return vm
def syllogism_combinations(syllogism, additional_premises): """ >>> syllogism_combinations("AA1", ["Aba"]) ['AA4'] """ for p in additional_premises: if p[0] not in syllogism: raise NotImplementedError new_syllogisms = [] for p in additional_premises: i = 0 if "a" in p and "b" in p else 1 i_other = (i + 1) % 2 premises = [None, None] premises[i] = p premises[i_other] = syllogism[i_other] + sylutil.term_order( syllogism[2])[i_other] if premises[0][1:] == "ab" and premises[1][1:] == "bc": figure = "1" elif premises[0][1:] == "ba" and premises[1][1:] == "cb": figure = "2" elif premises[0][1:] == "ab" and premises[1][1:] == "cb": figure = "3" elif premises[0][1:] == "ba" and premises[1][1:] == "bc": figure = "4" else: raise Exception new_syllogisms.append(premises[0][0] + premises[1][0] + figure) return new_syllogisms
def search_counterexample(self, conclusion, syllogism): """ Brute force search for counterexamples over generic models """ premises = [syllogism[i] + sylutil.term_order(syllogism[2])[i] for i in [0, 1]] for generic_model in self.generic_models(): # only models that satisfy the premises can be valid counterexamples if all([self.check_if_holds(generic_model, prem) for prem in premises]): if not self.check_if_holds(generic_model, conclusion): return generic_model return None
def encode_premises(self, syllogism, ex_implicatures=True, grice_implicatures=False): """ Encode premises as propositions, possibly adding implicatures """ to = sylutil.term_order(syllogism[2]) premises = [] pr = [] for i in [0, 1]: pr.append(syllogism[i] + to[i]) pr = sylutil.add_implicatures(pr, existential=ex_implicatures, gricean=grice_implicatures) for p in pr: premises.append(self.encode_proposition(p, True)) return premises
def encode(self, syllogism, size=4, deviation=0.0): if size < 2: size = 2 mm = [] (subj0, pred0), (subj1, pred1) = sylutil.term_order(syllogism[2]) ### Encode first premise ### # individuals that must be present in the model of the 1st premise required_inds = { "A": [[subj0, pred0]], "I": [[subj0, pred0]], "E": [[subj0, "-" + pred0], ["-" + subj0, pred0]], "O": [[subj0, "-" + pred0]] }[syllogism[0]] appendix = [] # O needs an additional requirement to make sure "b" is present (otherwise e.g. [a -b] [-a -b] would be allowed) while not all([ind in appendix for ind in required_inds]) or \ syllogism[0] == "O" and not any([prop == pred0 for ind in appendix for prop in ind]): appendix = [] for i in range(size): appendix.append( self.draw_individual(syllogism[0], subj0, pred0, random.random() < deviation)) mm.extend(appendix) i_b = [i for i, row in enumerate(mm) if "b" in row] ### Encode second premise ### if syllogism[1] == "A": for i in i_b: mm[i].append("c") elif syllogism[1] == "I": if subj1 == "b": for n, i in enumerate(i_b): mm[i].append("c") if n == 1: break elif subj1 == "c": mm[i_b[0]].append("c") mm.extend([["c"]]) elif syllogism[1] == "E": if subj1 == "b": for i in i_b: mm[i].append("-c") mm.append(["c"]) elif subj1 == "c": mm.extend([[subj1, "-" + pred1], [subj1, "-" + pred1], [subj1, "-" + pred1], [subj1, "-" + pred1]]) elif syllogism[1] == "O": if subj1 == "b": for n, i in enumerate(i_b): mm[i].append("-c") # augment max 2 individuals if n == 1: break mm.append(["c"]) elif subj1 == "c": mm.extend([["-b", "c"], ["c"]]) return [sorted(row, key=lambda e: e[-1]) for row in mm]
def encode(self, syllogism): """ Returns initial MM representation of a syllogism + list of exhausted terms >>> mm = MentalModels() >>> mm.encode("AA1") ([['a', 'b', 'c'], ['a', 'b', 'c']], ['a', 'b']) >>> mm.encode("AA2") ([['a', 'b', 'c'], ['a', 'b', 'c']], ['b', 'c']) >>> mm.encode("AA3") ([['a', 'b', 'c'], ['a', 'b', 'c']], ['a', 'c']) >>> mm.encode("AA4") ([['a', 'b', 'c'], ['a', 'b', 'c']], ['b']) >>> mm.encode("AE1") ([['a', 'b', '-c'], ['a', 'b', '-c'], ['c'], ['c']], ['a', 'b', 'c']) >>> mm.encode("AE2") ([['a', 'b'], ['a', 'b'], ['-b', 'c'], ['-b', 'c']], ['b', 'c']) >>> mm.encode("AE3") ([['a', 'b'], ['a', 'b'], ['-b', 'c'], ['-b', 'c']], ['a', 'b', 'c']) >>> mm.encode("AE4") ([['a', 'b', '-c'], ['a', 'b', '-c'], ['c'], ['c']], ['b', 'c']) >>> mm.encode("AI1") ([['a', 'b', 'c'], ['a', 'b'], ['c']], ['a']) >>> mm.encode("AI2") ([['a', 'b', 'c'], ['a', 'b'], ['c']], ['b']) >>> mm.encode("AI3") ([['a', 'b', 'c'], ['a', 'b'], ['c']], ['a']) >>> mm.encode("AI4") ([['a', 'b', 'c'], ['a', 'b'], ['c']], ['b']) >>> mm.encode("AO1") ([['a', 'b', '-c'], ['a', 'b', '-c'], ['c'], ['c']], ['a']) >>> mm.encode("AO2") ([['a', 'b'], ['a', 'b'], ['-b', 'c'], ['-b', 'c']], ['b']) >>> mm.encode("AO3") ([['a', 'b'], ['a', 'b'], ['-b', 'c'], ['-b', 'c']], ['a']) >>> mm.encode("AO4") ([['a', 'b', '-c'], ['a', 'b', '-c'], ['c'], ['c']], ['b']) >>> mm.encode("EA1") ([['a', '-b'], ['a', '-b'], ['b', 'c'], ['b', 'c']], ['a', 'b']) >>> mm.encode("EA2") ([['-a', 'b', 'c'], ['-a', 'b', 'c'], ['a'], ['a']], ['a', 'b', 'c']) >>> mm.encode("EA3") ([['a', '-b'], ['a', '-b'], ['b', 'c'], ['b', 'c']], ['a', 'b', 'c']) >>> mm.encode("EI1") ([['a', '-b'], ['a', '-b'], ['b', 'c'], ['b'], ['c']], ['a', 'b']) >>> mm.encode("EI3") ([['a', '-b'], ['a', '-b'], ['b', 'c'], ['b'], ['c']], ['a', 'b']) >>> mm.encode("IA1") ([['a', 'b', 'c'], ['a'], ['b', 'c']], ['b']) >>> mm.encode("IA3") ([['a', 'b', 'c'], ['a'], ['b', 'c']], ['c']) >>> mm.encode("IA4") ([['a', 'b', 'c'], ['b', 'c'], ['a']], ['b']) >>> mm.encode("IE1") ([['a', 'b', '-c'], ['a'], ['b', '-c'], ['c'], ['c']], ['b', 'c']) >>> mm.encode("II1") ([['a', 'b', 'c'], ['a'], ['b'], ['c']], []) >>> mm.encode("IO1") ([['a', 'b', '-c'], ['a'], ['b', '-c'], ['c'], ['c']], []) >>> mm.encode("OA4") ([['-a', 'b', 'c'], ['-a', 'b', 'c'], ['a'], ['a']], ['b']) >>> mm.encode("OE4") ([['-a', 'b', '-c'], ['-a', 'b', '-c'], ['a'], ['a'], ['c'], ['c']], ['b', 'c']) >>> mm.encode("OI4") ([['-a', 'b', 'c'], ['-a', 'b'], ['a'], ['a'], ['c']], []) """ mm = [] exhausted = set() to = sylutil.term_order(syllogism[2]) subj_0, pred_0 = to[0] subj_1, pred_1 = to[1] if syllogism[0] == "A": mm.extend(([subj_0, pred_0], [subj_0, pred_0])) exhausted.add(subj_0) if syllogism[0] == "I": mm.extend(([subj_0, pred_0], [subj_0], [pred_0])) if syllogism[0] == "E": mm.extend(([subj_0, "-" + pred_0], [subj_0, "-" + pred_0], [pred_0], [pred_0])) exhausted.update((subj_0, pred_0)) if syllogism[0] == "O": mm.extend(([subj_0, "-" + pred_0], [subj_0, "-" + pred_0], [pred_0], [pred_0])) i_b = [i for i, row in enumerate(mm) if "b" in row] if syllogism[1] == "A": for i in i_b: mm[i].append("c") exhausted.add(subj_1) if syllogism[1] == "I": mm[i_b[0]].append("c") mm.append(["c"]) if syllogism[1] == "E" or syllogism[1] == "O": if subj_1 == "b": for i in i_b: mm[i].append("-c") mm.extend((["c"], ["c"])) else: mm.extend((["-b", "c"], ["-b", "c"])) if syllogism[1] == "E": exhausted.update(("b", "c")) return [sorted(row, key=lambda e: e[-1]) for row in mm], sorted(list(exhausted))