def make(v, query, parse, dt): l = nltk.LogicParser() var = """ India => i New Zealand=> n Match1 => m1 Match2 => m2 Match3 => m3 team => {i,n} match => {m1,m2,m3} pomteam => {(m1,i),(m2,n),(m3,i)} winteam => {(m1,i),(m2,n),(m3,i)} """ val = nltk.parse_valuation(v) #print val dom = val.domain m = nltk.Model(dom, val) dom = m.domain #print dom g = nltk.Assignment(dom, []) #print g print "************************" #print m.evaluate('all x.( (match(x) & exists y.(team(y) and (pomteam(x,y) and winteam(x,y)))) | (-match(x)) )', g) print m.evaluate(query, g) print "************************" fmla1 = l.parse(parse) varnames = m.satisfiers(fmla1, 'x', g) for i in varnames: for p, q in dt.iteritems(): if q == i: print p
def var_pair18(middle, count): string1 = 'Batsmen => {' for k in middle: string1 += k + ',' string1 = string1[:-1] string1 += '}\n' v = string1 query = 'Batsmen(x)' l = nltk.LogicParser() val = nltk.parse_valuation(v) dom = val.domain m = nltk.Model(dom, val) dom = m.domain g = nltk.Assignment(dom, []) fmla1 = l.parse(query) varnames = m.satisfiers(fmla1, 'x', g) le = len(varnames) le = le / count if le >= 0.8: print True else: print False
def var_pair13(highrun): name_to_var = {} temp_strin3 = 'highrun => {' for k, v in highrun.iteritems(): for j in v: temp_strin3 += '(' temp_strin3 += k + ',' temp_strin3 += j + '),' temp_strin3 = temp_strin3[:-1] temp_strin3 += '} \n' v = temp_strin3 query = 'highrun(x,y)' #parse = 'highrun(Ryder,Southee)' #return v,query,name_to_var,parse l = nltk.LogicParser() val = nltk.parse_valuation(v) dom = val.domain m = nltk.Model(dom, val) dom = m.domain g = nltk.Assignment(dom, [('x', 'Ryder'), ('y', 'Southee')]) print "******************************" print m.evaluate(query, g) print "**************************"
def var_pair16(arr): name_to_var = {} count = 0 for i in arr: if i not in name_to_var: name_to_var[i] = 'p' + str(count) count = count + 1 st = 'MS Dhoni' temp_strin1 = '' temp_strin1 += 'player => {' for i in name_to_var: temp_strin1 += name_to_var[i] temp_strin1 += ',' temp_strin1 = temp_strin1[:-1] temp_strin1 += '} \n' if st not in name_to_var: name_to_var[st] = 'p' + str(count) count = count + 1 s = name_to_var['MS Dhoni'] v = temp_strin1 query = 'player(s)' l = nltk.LogicParser() val = nltk.parse_valuation(v) dom = val.domain m = nltk.Model(dom, val) dom = m.domain g = nltk.Assignment(dom, [('s', name_to_var['MS Dhoni'])]) print "******************************" print m.evaluate(query, g) print "******************************"
def var_pair15(inn1, inn2): name_to_var = {} count = 0 for i in inn1: if i not in name_to_var: name_to_var[i] = 'p' + str(count) count += 1 for i in inn2: if i not in name_to_var: name_to_var[i] = 'p' + str(count) count += 1 temp_strin1 = '' for i in name_to_var: temp_strin1 += i + ' => ' + name_to_var[i] + '\n' temp_strin4 = 'player =>{' for c in range(count): temp_strin4 += 'p' + str(c) + ',' temp_strin4 = temp_strin4[:-1] temp_strin4 += '}\n' temp_strin2 = 'inn1 => {' for i in inn1: temp_strin2 += name_to_var[i] + ',' temp_strin2 = temp_strin2[:-1] temp_strin2 += '}\n' temp_strin3 = 'inn2 => {' for i in inn2: temp_strin3 += name_to_var[i] + ',' temp_strin3 = temp_strin3[:-1] temp_strin3 += '}\n' v = temp_strin1 + temp_strin4 + temp_strin2 + temp_strin3 l = nltk.LogicParser() val = nltk.parse_valuation(v) #print val dom = val.domain m = nltk.Model(dom, val) dom = m.domain #print dom bowler = name_to_var['RA Jadeja'] g = nltk.Assignment(dom, [('x', bowler)]) #print g print "************************" #print m.evaluate('all x.( (match(x) & exists y.(team(y) and (pomteam(x,y) and winteam(x,y)))) | (-match(x)) )', g) flag1 = m.evaluate(' player(x) and inn1(x) ', g) flag2 = m.evaluate(' player(x) and inn2(x) ', g) if (flag1 == True): print 'Inning 1' else: print ' Inning 2' print "************************"
def make_model_and_answer(v,query): global quantifier global quantified global predicate1 global predicate2 global connector l = nltk.LogicParser() val = nltk.parse_valuation(v) #print 'val:' #print val #print 'end val:' dom = val.domain #print 'dom:' #print dom #print 'end dom:' model = nltk.Model(dom, val) #print 'model:' #print m #print 'end m:' g = nltk.Assignment(dom, []) temp = l.parse('match(x)') m = model.satisfiers(temp, 'x', g) temp = l.parse('match(x)') m1 = model.satisfiers(temp, 'x', g) temp = l.parse('match(x)') m2 = model.satisfiers(temp, 'x', g) #print match temp = l.parse('country(x)') c = model.satisfiers(temp, 'x', g) temp = l.parse('country(x)') c1 = model.satisfiers(temp, 'x', g) temp = l.parse('country(x)') c2 = model.satisfiers(temp, 'x', g) #print country temp = l.parse('player(x)') p = model.satisfiers(temp, 'x', g) temp = l.parse('player(x)') p1 = model.satisfiers(temp, 'x', g) temp = l.parse('player(x)') p2 = model.satisfiers(temp, 'x', g) #print player #query formation #query = 'all m.exists c1.exists c2.exists p1.exists p2.(mom(m,c1,p1) => win(m,c2))' print "The anwer for the query is : ", print model.evaluate(query, g)
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)
def var_pair11(match1): string1 = 'play1 => {' for i in match1['play1']: string1 += i + ',' string1 = string1[:-1] string1 += '}\n' string1 += 'play2 => {' for i in match1['play2']: string1 += i + ',' string1 = string1[:-1] string1 += '}\n' string1 += 'play3 => {' for i in match1['play3']: string1 += i + ',' string1 = string1[:-1] string1 += '}\n' string1 += 'play4 => {' for i in match1['play4']: string1 += i + ',' string1 = string1[:-1] string1 += '}\n' string1 += 'play5 => {' for i in match1['play5']: string1 += i + ',' string1 = string1[:-1] string1 += '}\n' query = 'play1(x) and play2(x) and play3(x) and play4(x) and play5(x)' l = nltk.LogicParser() val = nltk.parse_valuation(string1) dom = val.domain m = nltk.Model(dom, val) dom = m.domain g = nltk.Assignment(dom, []) fmla1 = l.parse(query) print m.satisfiers(fmla1, 'x', g)
def var_pair20(winteam): name_to_var = {} name_to_var['Match1'] = 'm1' name_to_var['Match2'] = 'm2' name_to_var['Match3'] = 'm3' name_to_var['Match4'] = 'm4' name_to_var['Match5'] = 'm5' temp_strin2 = '' for i in name_to_var: temp_strin2 += i + ' => ' + name_to_var[i] + '\n' temp_strin3 = 'winteam => {' for k, v in winteam.iteritems(): temp_strin3 += '(' temp_strin3 += k + ',' temp_strin3 += v + '),' temp_strin3 = temp_strin3[:-1] temp_strin3 += '} \n' temp_strin4 = 'Match1 => m1\n' temp_strin4 += 'Match2 => m2\n' temp_strin4 += 'Match3 => m3\n' temp_strin4 += 'Match4 => m4\n' temp_strin4 += 'Match5 => m5\n' temp_strin4 += 'match => {m1,m2,m3,m4,m5}\n' v = temp_strin3 + temp_strin4 + temp_strin2 parse1 = 'match(x) & winteam(x,i)' parse2 = 'match(x) & winteam(x,n) | -match(x)' #return v,query,name_to_var,parse l = nltk.LogicParser() val = nltk.parse_valuation(v) dom = val.domain m = nltk.Model(dom, val) dom = m.domain g = nltk.Assignment(dom, [('i', 'India'), ('n', 'New Zealand')]) print "******************************" fmla1 = l.parse(parse1) varnames1 = m.satisfiers(fmla1, 'x', g) fmla2 = l.parse(parse2) varnames2 = m.satisfiers(fmla2, 'x', g) #print varnames1 #print varnames2 count1 = 0 count2 = 0 for i in varnames1: for p, q in winteam.iteritems(): if p == i: count1 = count1 + 1 for i in varnames2: for p, q in winteam.iteritems(): if p == i: count2 = count2 + 1 if count1 > count2: print 'India' else: print 'New Zealand' print "**************************"
def first_order_logic(): tlp = nltk.LogicParser(type_check=True) sig = {"walk": "<e,t>"} parsed = tlp.parse("walk(angus)", sig) print "parsed_arg(value,type)=", parsed.argument, parsed.argument.type print "parsed_func(value,type)=", parsed.function, parsed.function.type