HATSON0.11

126 days ago by PatrickHammer

A system for understanding natural language by Patrick Hammer.

Blue code is generated by the AI system, not written by me.

Implementation (Uno-example at the bottom)

from YP import * from Atom import * from Compiler import * from ListPair import * from Variable import * from cStringIO import StringIO import sys sys.setrecursionlimit(100000) def PROLOG(prologCode): prologCode="\n"+prologCode+"\n" code=StringIO(); YP.tell(code); YP.see(YP.StringReader(prologCode)) TermList = Variable(); PseudoCode = Variable() for l1 in parseInput(TermList): for l2 in makeFunctionPseudoCode(TermList, PseudoCode): convertFunctionPython(PseudoCode) YP.seen(); return code.getvalue() Value=lambda v: v.getValue().toQuotedString() AIString=lambda s: s.replace("?","").replace(".","").replace(",","").lower() SetOf=lambda s: AIString(s).split(" ") class _: def __eq__(x,y): return true _=_() #enables haskell like pattern matching in python 
       
Nouns=[];Generals=[] def question(s,bBegin=true): #execute prolog question global sv,T if bBegin: s=filter(None,s.replace(" ","").replace("(",",").replace(")",",").replace("?-","").replace(".","").split(",")) #Assume that we don't know much about sentence structure, except that the assumptions about the categorys verb, noun etc. hold #so let's permutate the elements in s and try: #P=permutations(s) #for p in P: # if question(p,false): # return true better more code than more trying if question(s,false): return true questionLi="" for x in s: vartype="not a function" try: exec "vartype=type("+x+").__name__" except: None #if it's a property then python knows it as function if vartype=="function": questionLi+=x+"(" #if it's a atom then it's in fact memory as atom elif x in Nouns or x in sv and x not in Generals: # questionLi+='Atom.a("'+x+'"),' #ah so it's a variable: else: exec "X"+x+"=Variable()" in globals() #make it known to python too questionLi+="X"+x+"," pythStr=(questionLi+")").replace(",)",")") print pythStr try: exec "ret=not "+pythStr+".next()" return ret except: return false 
       
from itertools import product sv="";code="" L=["wenn"] #if words def PermutationHelper(Str): #will maybe be obsolete when prol is rewritten if ":-" in Str: left=Str.split("(")[0] right=Str.split(":-")[1].split("(")[0] leftInner=Str.split("(")[1].split(")")[0].split(",") rightInner=Str.split("(")[2].split(")")[0].split(",") Li=list(product(permutations(leftInner),permutations(rightInner))) return ["c"+str(len(leftInner))+"ist("+",".join(Li[i][0])+")"+":-"+"c"+str(len(rightInner))+"ist("+",".join(Li[i][1])+")." for i in range(len(Li))] else: left=Str.split("(")[0] leftInner=Str.split("(")[1].split(")")[0].split(","); Li=permutations(leftInner) return ["c"+str(len(leftInner))+"ist("+",".join(Li[i])+")." for i in range(len(Li))] def AddToSv(x): #MUST be ordered else code generator has a problem global sv bAdded=false li=sv.split("\n") name=x.split("(")[0] for i in range(len(li)): if name==li[i].split("(")[0]: li[i]+="\n"+x bAdded=true break if not bAdded: li.append(x) sv="\n".join(li) def FACT(x): #tell prolog facts global sv,code for z in L: if len(x)==0 or z in x[0]: #it's a if then sentence!!!! return for x in PermutationHelper(x): print x if len(x)>=3 and x[0]=="c" and x[1] in ["0","1"] and x[2] not in ["0","1","2","3","4","5","6","7","8","9"]: return #not a useful fact AddToSv(x) def UPDATE(): global sv,code print "UPDATE" str=PROLOG(sv.replace(" ","")) code=str exec str in globals() 
       
def prol(s,kw,bStart=true): #todo rewrite start="" if "?" in s: start="?-" s=s.replace(","," ") s=s.replace("?","") M=s.split(" "+L[0]+" ") ret="" ki=0 #keyword_index for m in M: N=m.split(" OBSOLETE") ret+="ist("+N[0].replace(" ",",") if len(N)>1 and N[1]!="": ret+=N[1].replace(" ",",") ret=ret+"):-" ki+=1 PreRet=(start+ret[0:len(ret)-2]+".").replace("(,","(").replace("(,","(") #python schlamperei ftw, scheiß stringfunktionen St=PreRet.split(":-") for i in range(len(St)): St[i]="c"+str(St[i].count(",")+1)+""+St[i] R=St[0] if len(St)>1: R+=":-"+St[1] return R.replace("),",")").replace("(,","(").replace(",,",",").replace(".)",")").replace("(.","(").replace(",,",",") 
       
bPrintCode=true #Python has serious bugs with default parameters def Work(s,SENTENCE="",bBegin=true,Len=-1,MaxExt=3,bUPDATE=true): #todo Len stuffing so that this param is not needed anymore #MaxExt=5 global Nouns,T,Mem; s=" "+s; Mem+=s i=0 Lu=s.split(" ") for u in Lu: if len(u)>0 and u[0].isupper(): Nouns.append(u.lower().replace("?","")) if "?" in s: while Len!=-1 and len(s.split(" "))<Len+1: if Work(s,"",false,Len=len(s.split(" ")),bUPDATE=false)==true: return true s+=" irgend"+str(i); i+=1 s="".join([a.lower() for a in s]).replace(".",",").replace("(","").replace(")","") s=s.replace("\n",",") kw=["OBSOLETE" for i in range(100)] if "?" in s: if question(prol(s,kw))==true: return true if Len!=-1 or MaxExt==0: return false return Work(s+" "+"irgendext"+str(MaxExt),"",false,Len=Len,MaxExt=MaxExt-1,bUPDATE=false) if bBegin: Work(s,"",false,Len=Len,bUPDATE=false) if not bBegin: FACT(prol(s,kw)) if bPrintCode: print code if bUPDATE and "?" not in s: UPDATE() 
       
sv="" #delete knowledge!! Mem=[]; Nouns=[] Len=6 def USE_TEXT(SENTENCE,Len=-1): global Nouns H=lambda M: [M[i:j+1] for i in range(len(M)) for j in range(i,len(M))] #hierachical set li=SENTENCE.replace("\n"," ").replace(","," ").replace("."," ").split(" ") for a in li: if len(a)>0 and a[0].upper(): Nouns+=a SENTS=SENTENCE.replace("\n"," ").replace(",",".").split(".") for x in SENTS: HT=H(x.split(" ")) lu=[" ".join(a) for a in HT if Len==-1 or len(a)==Len] for y in lu: bNo=false #if then sentence, never use as fact!! for z in L: if z in x and not z in y: bNo=true break if not bNo and len(y)>0 and " " not in y: #idea if there is a different element in lu where y is part of, skip! bNot=false for z in lu: if y in z and y!=z: bNot=true if not bNot: Work(y,bUPDATE=false) UPDATE() print code bPrintCode=false 
       

Uno

%time SENTENCE=""" Dies ist Uno. Man bekommt 7 Karten. Man legt eine Karte wenn Zahl/Farbe stimmt. Man hebt keine Karte wenn man eine Karte legt. Man sagt Uno wenn man eine Karte hat. Gewonnen hat man wenn man keine Karte hat. """ USE_TEXT(SENTENCE) #Let the action begin 
       
WARNING: Output truncated!  
full_output.txt



c3ist(dies,ist,uno).
c3ist(dies,uno,ist).
c3ist(ist,dies,uno).
c3ist(ist,uno,dies).
c3ist(uno,dies,ist).
c3ist(uno,ist,dies).
c4ist(man,bekommt,7,karten).
c4ist(man,bekommt,karten,7).
c4ist(man,7,bekommt,karten).
c4ist(man,7,karten,bekommt).
c4ist(man,karten,bekommt,7).
c4ist(man,karten,7,bekommt).
c4ist(bekommt,man,7,karten).
c4ist(bekommt,man,karten,7).
c4ist(bekommt,7,man,karten).
c4ist(bekommt,7,karten,man).
c4ist(bekommt,karten,man,7).
c4ist(bekommt,karten,7,man).
c4ist(7,man,bekommt,karten).
c4ist(7,man,karten,bekommt).
c4ist(7,bekommt,man,karten).
c4ist(7,bekommt,karten,man).
c4ist(7,karten,man,bekommt).
c4ist(7,karten,bekommt,man).
c4ist(karten,man,bekommt,7).
c4ist(karten,man,7,bekommt).
c4ist(karten,bekommt,man,7).
c4ist(karten,bekommt,7,man).
c4ist(karten,7,man,bekommt).
c4ist(karten,7,bekommt,man).
c4ist(man,legt,eine,karte):-c2ist(zahl/farbe,stimmt).
c4ist(man,legt,eine,karte):-c2ist(stimmt,zahl/farbe).
c4ist(man,legt,karte,eine):-c2ist(zahl/farbe,stimmt).
c4ist(man,legt,karte,eine):-c2ist(stimmt,zahl/farbe).
c4ist(man,eine,legt,karte):-c2ist(zahl/farbe,stimmt).
c4ist(man,eine,legt,karte):-c2ist(stimmt,zahl/farbe).
c4ist(man,eine,karte,legt):-c2ist(zahl/farbe,stimmt).
c4ist(man,eine,karte,legt):-c2ist(stimmt,zahl/farbe).
c4ist(man,karte,legt,eine):-c2ist(zahl/farbe,stimmt).
c4ist(man,karte,legt,eine):-c2ist(stimmt,zahl/farbe).
c4ist(man,karte,eine,legt):-c2ist(zahl/farbe,stimmt).
c4ist(man,karte,eine,legt):-c2ist(stimmt,zahl/farbe).
c4ist(legt,man,eine,karte):-c2ist(zahl/farbe,stimmt).
c4ist(legt,man,eine,karte):-c2ist(stimmt,zahl/farbe).
c4ist(legt,man,karte,eine):-c2ist(zahl/farbe,stimmt).
c4ist(legt,man,karte,eine):-c2ist(stimmt,zahl/farbe).
c4ist(legt,eine,man,karte):-c2ist(zahl/farbe,stimmt).
c4ist(legt,eine,man,karte):-c2ist(stimmt,zahl/farbe).
c4ist(legt,eine,karte,man):-c2ist(zahl/farbe,stimmt).
c4ist(legt,eine,karte,man):-c2ist(stimmt,zahl/farbe).
c4ist(legt,karte,man,eine):-c2ist(zahl/farbe,stimmt).
c4ist(legt,karte,man,eine):-c2ist(stimmt,zahl/farbe).
c4ist(legt,karte,eine,man):-c2ist(zahl/farbe,stimmt).
c4ist(legt,karte,eine,man):-c2ist(stimmt,zahl/farbe).
c4ist(eine,man,legt,karte):-c2ist(zahl/farbe,stimmt).
c4ist(eine,man,legt,karte):-c2ist(stimmt,zahl/farbe).
c4ist(eine,man,karte,legt):-c2ist(zahl/farbe,stimmt).
c4ist(eine,man,karte,legt):-c2ist(stimmt,zahl/farbe).
c4ist(eine,legt,man,karte):-c2ist(zahl/farbe,stimmt).

...

        for l4 in YP.unify(arg4, Atom.a("karten")):
          yield False
  for l1 in YP.unify(arg1, Atom.a("bekommt")):
    for l2 in YP.unify(arg2, Atom.a("karten")):
      for l3 in YP.unify(arg3, 7):
        for l4 in YP.unify(arg4, Atom.a("man")):
          yield False
  for l1 in YP.unify(arg1, Atom.a("bekommt")):
    for l2 in YP.unify(arg2, Atom.a("karten")):
      for l3 in YP.unify(arg3, Atom.a("man")):
        for l4 in YP.unify(arg4, 7):
          yield False
  for l1 in YP.unify(arg1, Atom.a("bekommt")):
    for l2 in YP.unify(arg2, 7):
      for l3 in YP.unify(arg3, Atom.a("karten")):
        for l4 in YP.unify(arg4, Atom.a("man")):
          yield False
  for l1 in YP.unify(arg1, Atom.a("bekommt")):
    for l2 in YP.unify(arg2, 7):
      for l3 in YP.unify(arg3, Atom.a("man")):
        for l4 in YP.unify(arg4, Atom.a("karten")):
          yield False
  for l1 in YP.unify(arg1, Atom.a("bekommt")):
    for l2 in YP.unify(arg2, Atom.a("man")):
      for l3 in YP.unify(arg3, Atom.a("karten")):
        for l4 in YP.unify(arg4, 7):
          yield False
  for l1 in YP.unify(arg1, Atom.a("bekommt")):
    for l2 in YP.unify(arg2, Atom.a("man")):
      for l3 in YP.unify(arg3, 7):
        for l4 in YP.unify(arg4, Atom.a("karten")):
          yield False
  for l1 in YP.unify(arg1, Atom.a("man")):
    for l2 in YP.unify(arg2, Atom.a("karten")):
      for l3 in YP.unify(arg3, 7):
        for l4 in YP.unify(arg4, Atom.a("bekommt")):
          yield False
  for l1 in YP.unify(arg1, Atom.a("man")):
    for l2 in YP.unify(arg2, Atom.a("karten")):
      for l3 in YP.unify(arg3, Atom.a("bekommt")):
        for l4 in YP.unify(arg4, 7):
          yield False
  for l1 in YP.unify(arg1, Atom.a("man")):
    for l2 in YP.unify(arg2, 7):
      for l3 in YP.unify(arg3, Atom.a("karten")):
        for l4 in YP.unify(arg4, Atom.a("bekommt")):
          yield False
  for l1 in YP.unify(arg1, Atom.a("man")):
    for l2 in YP.unify(arg2, 7):
      for l3 in YP.unify(arg3, Atom.a("bekommt")):
        for l4 in YP.unify(arg4, Atom.a("karten")):
          yield False
  for l1 in YP.unify(arg1, Atom.a("man")):
    for l2 in YP.unify(arg2, Atom.a("bekommt")):
      for l3 in YP.unify(arg3, Atom.a("karten")):
        for l4 in YP.unify(arg4, 7):
          yield False


CPU time: 41.67 s,  Wall time: 41.67 s
WARNING: Output truncated!  
full_output.txt



c3ist(dies,ist,uno).
c3ist(dies,uno,ist).
c3ist(ist,dies,uno).
c3ist(ist,uno,dies).
c3ist(uno,dies,ist).
c3ist(uno,ist,dies).
c4ist(man,bekommt,7,karten).
c4ist(man,bekommt,karten,7).
c4ist(man,7,bekommt,karten).
c4ist(man,7,karten,bekommt).
c4ist(man,karten,bekommt,7).
c4ist(man,karten,7,bekommt).
c4ist(bekommt,man,7,karten).
c4ist(bekommt,man,karten,7).
c4ist(bekommt,7,man,karten).
c4ist(bekommt,7,karten,man).
c4ist(bekommt,karten,man,7).
c4ist(bekommt,karten,7,man).
c4ist(7,man,bekommt,karten).
c4ist(7,man,karten,bekommt).
c4ist(7,bekommt,man,karten).
c4ist(7,bekommt,karten,man).
c4ist(7,karten,man,bekommt).
c4ist(7,karten,bekommt,man).
c4ist(karten,man,bekommt,7).
c4ist(karten,man,7,bekommt).
c4ist(karten,bekommt,man,7).
c4ist(karten,bekommt,7,man).
c4ist(karten,7,man,bekommt).
c4ist(karten,7,bekommt,man).
c4ist(man,legt,eine,karte):-c2ist(zahl/farbe,stimmt).
c4ist(man,legt,eine,karte):-c2ist(stimmt,zahl/farbe).
c4ist(man,legt,karte,eine):-c2ist(zahl/farbe,stimmt).
c4ist(man,legt,karte,eine):-c2ist(stimmt,zahl/farbe).
c4ist(man,eine,legt,karte):-c2ist(zahl/farbe,stimmt).
c4ist(man,eine,legt,karte):-c2ist(stimmt,zahl/farbe).
c4ist(man,eine,karte,legt):-c2ist(zahl/farbe,stimmt).
c4ist(man,eine,karte,legt):-c2ist(stimmt,zahl/farbe).
c4ist(man,karte,legt,eine):-c2ist(zahl/farbe,stimmt).
c4ist(man,karte,legt,eine):-c2ist(stimmt,zahl/farbe).
c4ist(man,karte,eine,legt):-c2ist(zahl/farbe,stimmt).
c4ist(man,karte,eine,legt):-c2ist(stimmt,zahl/farbe).
c4ist(legt,man,eine,karte):-c2ist(zahl/farbe,stimmt).
c4ist(legt,man,eine,karte):-c2ist(stimmt,zahl/farbe).
c4ist(legt,man,karte,eine):-c2ist(zahl/farbe,stimmt).
c4ist(legt,man,karte,eine):-c2ist(stimmt,zahl/farbe).
c4ist(legt,eine,man,karte):-c2ist(zahl/farbe,stimmt).
c4ist(legt,eine,man,karte):-c2ist(stimmt,zahl/farbe).
c4ist(legt,eine,karte,man):-c2ist(zahl/farbe,stimmt).
c4ist(legt,eine,karte,man):-c2ist(stimmt,zahl/farbe).
c4ist(legt,karte,man,eine):-c2ist(zahl/farbe,stimmt).
c4ist(legt,karte,man,eine):-c2ist(stimmt,zahl/farbe).
c4ist(legt,karte,eine,man):-c2ist(zahl/farbe,stimmt).
c4ist(legt,karte,eine,man):-c2ist(stimmt,zahl/farbe).
c4ist(eine,man,legt,karte):-c2ist(zahl/farbe,stimmt).
c4ist(eine,man,legt,karte):-c2ist(stimmt,zahl/farbe).
c4ist(eine,man,karte,legt):-c2ist(zahl/farbe,stimmt).
c4ist(eine,man,karte,legt):-c2ist(stimmt,zahl/farbe).
c4ist(eine,legt,man,karte):-c2ist(zahl/farbe,stimmt).

...

        for l4 in YP.unify(arg4, Atom.a("karten")):
          yield False
  for l1 in YP.unify(arg1, Atom.a("bekommt")):
    for l2 in YP.unify(arg2, Atom.a("karten")):
      for l3 in YP.unify(arg3, 7):
        for l4 in YP.unify(arg4, Atom.a("man")):
          yield False
  for l1 in YP.unify(arg1, Atom.a("bekommt")):
    for l2 in YP.unify(arg2, Atom.a("karten")):
      for l3 in YP.unify(arg3, Atom.a("man")):
        for l4 in YP.unify(arg4, 7):
          yield False
  for l1 in YP.unify(arg1, Atom.a("bekommt")):
    for l2 in YP.unify(arg2, 7):
      for l3 in YP.unify(arg3, Atom.a("karten")):
        for l4 in YP.unify(arg4, Atom.a("man")):
          yield False
  for l1 in YP.unify(arg1, Atom.a("bekommt")):
    for l2 in YP.unify(arg2, 7):
      for l3 in YP.unify(arg3, Atom.a("man")):
        for l4 in YP.unify(arg4, Atom.a("karten")):
          yield False
  for l1 in YP.unify(arg1, Atom.a("bekommt")):
    for l2 in YP.unify(arg2, Atom.a("man")):
      for l3 in YP.unify(arg3, Atom.a("karten")):
        for l4 in YP.unify(arg4, 7):
          yield False
  for l1 in YP.unify(arg1, Atom.a("bekommt")):
    for l2 in YP.unify(arg2, Atom.a("man")):
      for l3 in YP.unify(arg3, 7):
        for l4 in YP.unify(arg4, Atom.a("karten")):
          yield False
  for l1 in YP.unify(arg1, Atom.a("man")):
    for l2 in YP.unify(arg2, Atom.a("karten")):
      for l3 in YP.unify(arg3, 7):
        for l4 in YP.unify(arg4, Atom.a("bekommt")):
          yield False
  for l1 in YP.unify(arg1, Atom.a("man")):
    for l2 in YP.unify(arg2, Atom.a("karten")):
      for l3 in YP.unify(arg3, Atom.a("bekommt")):
        for l4 in YP.unify(arg4, 7):
          yield False
  for l1 in YP.unify(arg1, Atom.a("man")):
    for l2 in YP.unify(arg2, 7):
      for l3 in YP.unify(arg3, Atom.a("karten")):
        for l4 in YP.unify(arg4, Atom.a("bekommt")):
          yield False
  for l1 in YP.unify(arg1, Atom.a("man")):
    for l2 in YP.unify(arg2, 7):
      for l3 in YP.unify(arg3, Atom.a("bekommt")):
        for l4 in YP.unify(arg4, Atom.a("karten")):
          yield False
  for l1 in YP.unify(arg1, Atom.a("man")):
    for l2 in YP.unify(arg2, Atom.a("bekommt")):
      for l3 in YP.unify(arg3, Atom.a("karten")):
        for l4 in YP.unify(arg4, 7):
          yield False


CPU time: 41.67 s,  Wall time: 41.67 s
print len(code.split("\n"))+1," code-lines where generated for Uno" print len(sv.split("\n"))+1, "definitions and rules generated" 
       
5337  code-lines where generated for Uno
944 definitions and rules generated
5337  code-lines where generated for Uno
944 definitions and rules generated
Work("Man hebt keine Karte?") 
       
c4ist(Atom.a("man"),Atom.a("hebt"),Atom.a("keine"),Atom.a("karte"))
c4ist(Atom.a("man"),Atom.a("hebt"),Atom.a("keine"),Atom.a("karte"))
Xc5ist,Atom.a("man"),Atom.a("hebt"),Atom.a("keine"),Atom.a("karte"),Xirg\
endext3)
Xc5ist,Atom.a("man"),Atom.a("hebt"),Atom.a("keine"),Atom.a("karte"),Xirg\
endext3)
Xc7ist,Atom.a("man"),Atom.a("hebt"),Atom.a("keine"),Atom.a("karte"),Xirg\
endext3,Xirgendext2)
Xc7ist,Atom.a("man"),Atom.a("hebt"),Atom.a("keine"),Atom.a("karte"),Xirg\
endext3,Xirgendext2)
Xc9ist,Atom.a("man"),Atom.a("hebt"),Atom.a("keine"),Atom.a("karte"),Xirg\
endext3,Xirgendext2,Xirgendext1)
Xc9ist,Atom.a("man"),Atom.a("hebt"),Atom.a("keine"),Atom.a("karte"),Xirg\
endext3,Xirgendext2,Xirgendext1)
False
c4ist(Atom.a("man"),Atom.a("hebt"),Atom.a("keine"),Atom.a("karte"))
c4ist(Atom.a("man"),Atom.a("hebt"),Atom.a("keine"),Atom.a("karte"))
Xc5ist,Atom.a("man"),Atom.a("hebt"),Atom.a("keine"),Atom.a("karte"),Xirgendext3)
Xc5ist,Atom.a("man"),Atom.a("hebt"),Atom.a("keine"),Atom.a("karte"),Xirgendext3)
Xc7ist,Atom.a("man"),Atom.a("hebt"),Atom.a("keine"),Atom.a("karte"),Xirgendext3,Xirgendext2)
Xc7ist,Atom.a("man"),Atom.a("hebt"),Atom.a("keine"),Atom.a("karte"),Xirgendext3,Xirgendext2)
Xc9ist,Atom.a("man"),Atom.a("hebt"),Atom.a("keine"),Atom.a("karte"),Xirgendext3,Xirgendext2,Xirgendext1)
Xc9ist,Atom.a("man"),Atom.a("hebt"),Atom.a("keine"),Atom.a("karte"),Xirgendext3,Xirgendext2,Xirgendext1)
False
Work("Zahl/Farbe stimmt") 
       
c2ist(zahl/farbe,stimmt).
c2ist(stimmt,zahl/farbe).
UPDATE
c2ist(zahl/farbe,stimmt).
c2ist(stimmt,zahl/farbe).
UPDATE
%time Work("Keine Karte hebt man?") 
       
c4ist(Atom.a("keine"),Atom.a("karte"),Atom.a("hebt"),Atom.a("man"))
True
CPU time: 0.01 s,  Wall time: 0.01 s
c4ist(Atom.a("keine"),Atom.a("karte"),Atom.a("hebt"),Atom.a("man"))
True
CPU time: 0.01 s,  Wall time: 0.01 s