This is a snippet that is an implementation of an idea I had. I think, to have a believable and realistic chatbot of any kind the program would need to know relations between various ideas, concepts, words, and other things.
This program makes links between various things and you can tell it statements in the form "{noun} {verb} {noun}" and can ask it to tell you what it knows about something by saying "what is {noun}?" A more complete implementation would have modifiers in the links, so you could say things like "a week has seven days". Don't mix plural and singular, it's not that smart at language parsing.
Instead of trying to make the program appear to chat realisticly by parsing complicated grammar structures and responding with witty responses, I've tried to make a chatbot that can make the connections between various ideas and, in a way, think.
A big thing missing from this snippet that would be tricky to program is the idea of instances of various concepts. For example, it knows that "self" has a "name", but since "self" is defined as a concept (like entity and time, etc) it is not an instance definition, and I cannot give it, the program, a particular name.
If the program had instance support, you'd be able to say things like "I have an animal named spot" and the program would be able to ask "is spot a specific type of animal?",and you could say "he is a dog".
I've given it a good 120 or so definitions so it already knows some stuff such as word, number, image, state, time, day, computer, network, sum, set, entity, and lots of other things.
global max_class_amount=1000
global max_link_amount=50
global knowledge_already_exists$="I already know that"
global immediate_subject$=""
type class_type
name as string
exist as boolean
endtype
set display mode 1024,768,32
dim class(max_class_amount) as class_type
type link_type
verb as string
percent as float
class
endtype
dim class_link(max_class_amount,max_link_amount) as link_type
global max_word_amount=30
dim word$(max_word_amount)
global prompt=0
rem first learn things that can't really be defined
learn("device","is","invention"):learn("invention","is","device")
learn("predicate","is","relation"):learn("relation","is","predicate")
learn("property","is","possession"):learn("possession","is","property")
learn("picture","is","image"):learn("image","is","picture")
learn("material","is","substance"):learn("substance","is","material")
learn("data","is","information"):learn("information","is","data")
learn("sum","is","total"):learn("total","is","sum")
learn("process","is","program"):learn("program","is","process")
learn("ability","is","skill"):learn("skill","is","ability")
learn("condition","is","mode"):learn("mode","is","condition")
learn("condition","is","state"):learn("state","is","condition")
learn("mode","is","state"):learn("state","is","mode")
learn("existence","has-no","definition")
rem next learn basic definitions
learn("symbol","is","picture")
learn("character","is","symbol")
learn("number","is","symbol")
learn("tool","is","invention")
learn("machine","is","device")
learn("relation","is","property")
learn("shape","is","information")
learn("act","is","process")
learn("activity","is","state")
rem next learn about stuff that builds upon these basic definitions
learn("computer","is","machine")
learn("computer","has","keyboard")
learn("computer","has","screen")
learn("computer","has","hard-drive")
learn("amount","is","number")
learn("amount","is","sum")
learn("interval","is","amount")
learn("period","is","interval")
learn("time","is","interval")
learn("time","is","number")
learn("time","is","system")
learn("second","is","period")
learn("minute","is","period")
learn("minute","has","second")
learn("hour","is","period")
learn("hour","has","minute")
learn("day","is","period")
learn("day","has","hour")
learn("week","is","period")
learn("week","has","day")
learn("month","is","period")
learn("month","has","day")
learn("year","is","period")
learn("year","has","day")
learn("year","has","month")
learn("decade","is","period")
learn("decade","has","year")
learn("century","is","period")
learn("century","has","year")
learn("century","has","decade")
learn("quantity","is","amount")
learn("rate","is","quantity")
learn("power","is","rate")
learn("power","is","ability")
learn("computer","needs","power")
learn("process","runs-in","computer")
learn("entertainment","is","act")
learn("game","is","activity")
learn("game","provides","entertainment")
learn("computer-game","is","game")
learn("computer-game","runs-in","computer")
learn("starcraft","is","computer-game")
learn("warcraft","is","computer-game")
learn("consequence","is","relation")
learn("result","is","consequence")
learn("information","is","result")
learn("form","is","shape")
learn("definition","is","form")
learn("entity","has","existence")
learn("system","has","entity")
learn("system","is","set")
learn("language","is","system")
learn("artificial-language","is","language")
learn("programming-language","is","artificial-language")
learn("darkbasic","is","programming-language")
learn("variation","is","act")
learn("disturbance","is","variation")
learn("wave","is","disturbance")
learn("sound","is","wave")
learn("word","is","sound")
learn("word","has","character")
learn("name","is","word")
learn("other","is","entity")
learn("name","distinguishes-from","other")
learn("animal","is","entity")
learn("insect","is","entity")
learn("human","is","animal")
learn("person","is","human")
learn("dog","is","animal")
learn("cat","is","animal")
learn("mouse","is","animal")
learn("device","is","tool")
learn("pencil","is","tool")
learn("pen","is","tool")
learn("paper","is","material")
learn("wood","is","material")
learn("metal","is","material")
learn("network","is","system")
learn("computer-network","is","network")
learn("computer-network","has","computer")
rem now learn about self
learn("self","has","name")
learn("self","is","entity")
learn("self","is-written-in","darkbasic")
learn("self","is","program")
immediate_subject$=""
prompt=1
do
text$=""
print
input ">",text$
parse(text$)
sync
loop
end
function print_classes()
for c=0 to max_class_amount
if class(c).exist=1
print class(c).name
for l=0 to max_link_amount
if class_link(c,l).percent>0
print " ",class_link(c,l).verb," ",class(class_link(c,l).class).name
endif
next l
endif
next c
endfunction
function parse(strg$)
print "Parsing ",strg$
sync
while mid$(strg$,1)=" ":strg$=right$(strg$,len(strg$)-1):endwhile
while mid$(strg$,len(strg$))=" ":strg$=left$(strg$,len(strg$)-1):endwhile
if len(strg$)=0 then exitfunction
if strg$="list classes"
print_classes()
exitfunction
endif
type$="statement"
if right$(strg$,1)="?"
type$="question"
strg$=left$(strg$,len(strg$)-1)
endif
for c=1 to len(strg$)
if mid$(strg$,c)="." or mid$(strg$,c)="," or mid$(strg$,c)=";"
strg$=left$(strg$,c-1)+right$(strg$,len(strg$)-c)
dec c
endif
next c
w=0
for c=1 to len(strg$)
if mid$(strg$,c)=" "
word$(w)=left$(strg$,c-1)
inc w
strg$=right$(strg$,len(strg$)-c)
c=0
endif
next c
word$(w)=strg$
word$(w+1)=""
for w0=0 to w
if upper$(word$(w0))="A"
delete_word(w0)
dec w0
dec w
else
if upper$(word$(w0))="AN"
delete_word(w0)
dec w0
dec w
else
if upper$(word$(w0))="THE"
delete_word(w0)
dec w0
dec w
else
if upper$(word$(w0))="IT"
word$(w0)=immediate_subject$
endif
endif
endif
endif
next w0
if type$="statement"
if w=2
learn(word$(0),word$(1),word$(2))
else
if w>2
respond("You use too many words!")
else
respond("I see.")
endif
endif
endif
if type$="question"
define(word$(2))
endif
endfunction
function define(class$)
if class$="" then exitfunction
print "Defining ",class$
c=get_class(class$)
counter=0
while c=-1
inc counter
if prompt=1 then print "I don't know."
c=get_class(class$)
if counter>1 then exitfunction
endwhile
defined=0
for l=0 to max_link_amount
if class_link(c,l).percent>0
print class$," ",class_link(c,l).verb," ",class(class_link(c,l).class).name
defined=1
endif
next l
found=0
for c2=0 to max_class_amount
if class(c2).exist=1
for l=0 to max_link_amount
if class_link(c2,l).percent>0
if class_link(c2,l).class=c
found=1
exit
endif
endif
if found=1 then exit
next l
endif
next c2
if found=1
if defined=0
print "I don't know, but:"
else
print "Also note:"
endif
for c2=0 to max_class_amount
if class(c2).exist=1
for l=0 to max_link_amount
if class_link(c2,l).percent>0
if class_link(c2,l).class=c
print class(c2).name," ",class_link(c2,l).verb," ",class(class_link(c2,l).class).name
endif
endif
next l
endif
next l
endif
endfunction
function delete_word(w)
for n=w to max_word_amount-1
word$(n)=word$(n+1)
next n
endfunction
function learn(class1$,verb$,class2$)
if class1$="" or verb$="" or class2$="" then exitfunction
c1=get_class(class1$)
if c1=-1
c1=make_class(class1$)
endif
c2=get_class(class2$)
if c2=-1
c2=make_class(class2$)
if prompt=1 then ask(class2$)
endif
for n=0 to max_link_amount
if class_link(c1,n).verb=verb$ and class_link(c1,n).percent>0
if prompt=1 then respond(knowledge_already_exists$)
exit
endif
next n
for n=0 to max_link_amount
if class_link(c1,n).percent=0
class_link(c1,n).percent=0.8
class_link(c1,n).verb=verb$
class_link(c1,n).class=c2
`respond("I see. So a "+class1$+" "+verb$+" "+class2$+"?")
exit
endif
next n
endfunction
function get_class(name$)
for c=0 to max_class_amount
if class(c).exist=1
if upper$(class(c).name)=upper$(name$)
exitfunction c
endif
endif
next c
endfunction -1
function make_class(name$)
for c=0 to max_class_amount
if class(c).exist=0
class(c).exist=1
class(c).name=name$
exitfunction c
endif
next c
endfunction -1
function respond(strg$)
print strg$
sync
endfunction
function ask(class$)
print "What is a '",class$,"'?"
immediate_subject$=class$
sync
endfunction
Here's an example of it responding to questions and learning what a keyboard is. I only type the lines that start with ">".
The ultimate goal would be to get the program to use its knowledge database to converse in a natural way. Also it could be much smarter if it could read a dictionary to expand its knowledge.