A very simple chatbot program, needs improving and cleaned up a bit. Seems to work well though.
` Make a brain file if none exists
if file exist("brain.txt")=0 then make file "brain.txt"
INK RGB(0,0,0),RGB(255,255,255) : cls rgb(255,255,255)
` Start of main loop
do
` Get human input
input ": ", human$
cls rgb(255,255,255)
` Convert human input to lowercase
human$=LOWER$(human$)
` Search brain file for a response.
open to read 1, "brain.txt"
found = 0
while (file end(1) = 0) and (found = 0)
read string 1, find$
dash$="-"
a$=find$
b$=first token$(a$,"-")
b$=next token$("-")
b$=dash$+b$
word2$ = REMOVE$( find$, b$)
removedhuman$=human$+dash$
word$ = REMOVE$( find$, removedhuman$)
if human$ = word2$
print word$
gosub TTS
found = 1
endif
endwhile
close file 1
` If no response is found then we can creating one. This is how the computer learns what to say.
` Type "no" if you do not what to create a response.
` Responses can be changed by editing the brain.txt file.
if found=0
print "Please type in a response for me or type "no" to skip."
input ": ",response$
cls rgb(255,255,255)
if response$="no" then goto skip
response$=human$+dash$+response$
OPEN DATAFILE TO Append 1, "brain.txt"
WRITE DATAFILE STRING 1, response$
close datafile 1
endif
skip:
loop
// This downloads a very small mp3 file of the text to speech voice and plays it.
TTS:
w$="http://translate.google.com/translate_tts?ie=utf-8&tl=en&q=" + word$
f$="tts.mp3"
if file exist(f$) then delete file f$
load dll "urlmon",1
a=CALL DLL(1,"URLDownloadToFileA",0,w$,f$,0,0)
delete dll 1
if file exist("tts.mp3")=1
LOAD music "tts.mp3",1
play music 1
While music playing(1)=1
endwhile
delete music 1
endif
return