I worked in meta data and meta structure back in the 1990s for search engines,but I have been programming since I was a kid. This is just for fun but the concept can be taken quite far. Please enjoy BASIC AI.
Since BASIC is a very old language, it doesnât natively support modern AI concepts like machine learning or neural networks. However, you can still simulate some AI behaviors, such as decision-making or learning, using basic algorithms and data structures. Below is an example of how you could write a simple AI that learns from user input using a decision-making approach in BASIC:
10 PRINT âWelcome to the Simple AI in BASIC!â
20 PRINT âWhat is your name?â
30 INPUT N$
40 PRINT "Hello, "; N$; â! Letâs chat.â
50 PRINT âAsk me something, or say âbyeâ to exit.â
60 INPUT Q$
70 IF Q$ = âbyeâ THEN GOTO 200
â Decision-making process based on keywords
80 IF INSTR(Q$, âweatherâ) THEN PRINT âThe weather is always nice in the digital world!â
90 IF INSTR(Q$, ânameâ) THEN PRINT âYou can call me BASIC-BOT.â
â Learning process: store questions and responses
100 PRINT âWhat should I reply next time someone asks this?â
110 INPUT A$
120 OPEN âdata.txtâ FOR APPEND AS #1
130 PRINT #1, Q$; â:â; A$
140 CLOSE #1
150 PRINT âGot it! Iâll remember that.â
160 GOTO 50
â Search for previously learned responses
170 OPEN âdata.txtâ FOR INPUT AS #2
180 WHILE NOT EOF(2)
190 LINE INPUT #2, DATA$
200 IF INSTR(DATA$, Q$) THEN PRINT MID$(DATA$, INSTR(DATA$, â:â) + 1)
210 WEND
220 CLOSE #2
230 GOTO 50
200 PRINT "Goodbye, "; N$; â!â
210 END
Explanation:
- Line 10-60: This is the basic interaction with the user. The AI asks for a name, initiates a conversation, and waits for input.
- Line 70-80: Basic keyword-based decision-making. It checks for words like âweatherâ or ânameâ in the input and responds accordingly.
- Line 100-140: This simulates the AI âlearningâ by asking the user for an appropriate response to store for future use. It then writes the question and the userâs response to a file (data.txt).
- Line 170-220: When the AI doesnât have a pre-programmed response, it searches through the data.txt file for any previously stored questions and replies accordingly.
- Line 200-210: Exits the program when the user types âbye.â
This simple AI grows by learning responses based on user input and storing them for future use. The AI itself doesnât have âintelligenceâ but mimics learning behavior by saving and recalling previous interactions.
This example represents basic AI behavior, but more advanced techniques would require additional libraries and capabilities beyond the limitations of BASIC.
Thank you all
Modified code.
10 PRINT âWelcome to the Simple AI in BASIC!â
20 PRINT âWhat is your name?â
30 INPUT N$
40 PRINT "Hello, "; N$; â! Letâs chat.â
50 PRINT âAsk me something, or say âbyeâ to exit.â
60 INPUT Q$
â Exit condition
70 IF Q$ = âbyeâ THEN GOTO 200
â Check if response is already learned
80 OPEN âdata.txtâ FOR INPUT AS #2
90 ResponseFound = 0 â Flag to check if the response is found
100 WHILE NOT EOF(2)
110 LINE INPUT #2, DATA$
120 IF INSTR(DATA$, Q$) THEN â Check if the question is in the file
130 PRINT "I remember! "; MID$(DATA$, INSTR(DATA$, â:â) + 1)
140 ResponseFound = 1
150 EXIT WHILE
160 END IF
170 WEND
180 CLOSE #2
â If no response is found, ask the user for a new response
190 IF ResponseFound = 0 THEN
200 PRINT âI donât know the answer to that. What should I say next time?â
210 INPUT A$ â User provides the response
220 OPEN âdata.txtâ FOR APPEND AS #1 â Open the file to append
230 PRINT #1, Q$; â:â; A$ â Record the question and answer in the file
240 CLOSE #1
250 PRINT âGot it! Iâll remember that.â
260 END IF
â Loop back to ask another question
270 GOTO 50
200 PRINT "Goodbye, "; N$; â!â
210 END
Explanation of Changes:
Lines 80-180: The program now searches the data.txt file for a previously learned response using INSTR to check if the userâs question matches any stored questions. If it finds a match, it prints the stored response. Otherwise, it sets ResponseFound to 0, meaning no response was found.
Lines 190-260: If no matching question is found (ResponseFound = 0), the program asks the user to provide a response. It then stores both the question and response in data.txt for future use.
Lines 230-240: The question and response are written to data.txt in the format: Question:Response. This ensures that when the user asks the same question later, the program can retrieve and display the correct answer.
Loop Back: The program loops back to ask for new input after each interaction, ensuring continuous learning.