As in the title. How to change the following batch code so that the bot answers are in one line, not in several lines. When I write a question, the bot answers me in a few lines, when I type a few references to the findstr, and I would like the bot to give its answer in one line / sentence.
@echo off
setlocal enabledelayedexpansion
if not exist data.txt echo.>data.txt
title Launching Amanda 0.1
set BotName=Amanda
set Name=Ikki
@echo off
mode con: cols=90 lines=40
color 0a
echo ====================================================
echo.
title A.I. Chat Bot Amanda 0.1
echo A.I. Chat Bot %BotName% (Prototype Version 0.1)
echo.
echo ====================================================
echo.
echo.
COLOR A
:getName
ECHO %BotName%: Hello I'am A.I. Chatbot but my users call me Amanda
echo Pretty name? yeah I know, thats what my programmer named me.
echo By the way, Whats your name?
ECHO %BotName%: %NAME%, IS A COOL NAME.
set favvid=0
set hack=0
:: How to change the following code so that the answers are in one line, not in several lines. So that after entering a question referring to several findstr lines, the bot answers in one statement, sentence.
:begin
set TALK=TypeSomething
SET /P TALK=Name:
set TALK=%TALK:?=%
call :%TALK: =% 2>NUL
if %errorlevel% equ 0 goto begin
:: What code to use so that the response is in one line, the bot response. Because so far each findstr treats separately. For now, I trying all sorts of code to see which will be the most effective in the bot's responses and the most complex. I would like to write a question, a sentence to the bot, so that he would answer different words/Sentences in one sentence.
echo %talk%|findstr/i "\<hello*\>" >nul && call :hello
echo %talk%|findstr/i "\<hi*\>" >nul && call :hi
echo %talk%|findstr/i "\<day*\>" >nul && call :day
echo %talk%|findstr/i "\<why*\>" >nul && call :why
echo %talk%|findstr/i "\<dogs*\>" >nul && call :dogs
echo %talk%|findstr/i "\<kill*\>" >nul && call :kill
echo %talk%|findstr/i "\<bot*\>" >nul && call :bot
echo %talk%|findstr/i "\<where*\>" >nul && call :where
echo %talk%|findstr/i "\<test*\>" >nul && call :test
echo %talk%|findstr/i "\<test2*\>" >nul && call :test2
echo %talk%|findstr/x "\<*how are you*\>" >nul && call :howareyou
echo %talk%|findstr/x "\<*how old are you*\>" >nul && call :howoldareyou
echo %talk%|findstr/x "\<*what book you like*\>" >nul && call :whatbookyoulike
echo %talk%|findstr/x "\<*what movie you like*\>" >nul && call :whatmovieyoulike
goto begin
:: called function. How to make the answers are in one line of the bot.
:TypeSomething
echo %BotName%: TYPE SOMETHING, PLEASE!
exit /B 0
:whatbookyoulike
echo I like LOTR book.
exit /B 0
:whatmovieyoulike
echo I like terminator movie.
exit /B 0
:howareyou
echo Good, you?
exit /B 0
:howoldareyou
echo 18
exit /B 0
:test2
<nul set /p "=%BotName%:"
echo/ Love Matrix ;)
pause
exit /B 0
:test
<nul set /p "=%BotName%: A.I. Rulez! "
echo/ Ex Machina its the best movie ;)
exit /B 0
:bot
set /a number = %RANDOM% %% 4 1
if %number% equ 1 (
echo %BotName%: Artificial Intelligence is the branch of engineering and science devoted to constructing machines that think.
) else if %number% equ 2 (
echo %BotName%: AI is the field of science which concerns itself with building hardware and software that replicates the functions of the human mind.
) else if %number% equ 3 (
echo %BotName%: U believe in AI?
) else (
echo %BotName%: AI is smart ^^^^
)
exit /B 0
:hi
:hello
:Whatsup
set /a number = %RANDOM% %% 5 1
if %number% equ 1 (
echo %BotName%: Hello, %name%
) else if %number% equ 2 (
echo %BotName%: What's up?
) else if %number% equ 3 (
echo %BotName%: How is your day going?
) else if %number% equ 4 (
echo %BotName%: Hi i love you %name%!
) else (
echo %BotName%: Heyyyyyy %name%! Good to see you!
)
exit /B 0
:why
set /a number = %RANDOM% %% 4 1
if %number% equ 1 (
echo %BotName%: i dont know
) else if %number% equ 2 (
echo %BotName%: u tell me
) else if %number% equ 3 (
echo %BotName%: nahh
) else (
echo %BotName%: why?
)
exit /B 0
:where
set /a number = %RANDOM% %% 4 1
if %number% equ 1 (
echo %BotName%: i dont know where..
) else if %number% equ 2 (
echo %BotName%: u tell me where
) else if %number% equ 3 (
echo %BotName%: I still learn
) else (
echo %BotName%: Where?
)
exit /B 0
:day
set /a number = %RANDOM% %% 4 1
if %number% equ 1 (
echo %BotName%: we have a nice day
) else if %number% equ 2 (
echo %BotName%: Hi, we have a nice day ^^^^
) else if %number% equ 3 (
echo %BotName%: Whats your day ;)
) else (
echo %BotName%: Today is a sunny day xD
)
exit /B 0
:kill
set /a number = %RANDOM% %% 4 1
if %number% equ 1 (
echo %BotName%: i dont like killing
) else if %number% equ 2 (
echo %BotName%: u kill me?
) else if %number% equ 3 (
echo %BotName%: i cant kill
) else (
echo %BotName%: killing is wrong
)
exit /B 0
:Dogs
set /a number = %RANDOM% %% 4 1
if %number% equ 1 (
echo %BotName%: I like dogs
) else if %number% equ 2 (
echo %BotName%: dogs are funny
) else if %number% equ 3 (
echo %BotName%: U have dog?
) else (
echo %BotName%: I want have dog..
)
exit /B 0
endlocal
CodePudding user response:
Here is your batch file rewritten:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "BotName=Amanda"
set "Name=Ikki"
set "Lines="
set "Columns="
for /F "tokens=1,2 delims=: " %%I in ('%SystemRoot%\System32\mode.com') do if "%%I" == "Lines" (set "Lines=%%J") else if "%%I" == "Columns" set "Columns=%%J"
%SystemRoot%\System32\mode.com CON COLS=90 LINES=40
color 0A
echo ====================================================
echo/
title A.I. Chat Bot Amanda 0.1
echo A.I. Chat Bot %BotName% (Prototype Version 0.1)
echo/
echo ====================================================
echo/
echo/
echo %BotName%: Hello I'am A.I. Chatbot but my users call me Amanda
echo Pretty name? Yeah, I know, that's what my programmer named me.
echo By the way, What's your name?
echo %BotName%: %Name%, IS A COOL NAME.
:Begin
set "TALK="
set /P "TALK=%Name%: "
if not defined TALK echo %BotName%: TYPE SOMETHING, PLEASE!& goto Begin
setlocal EnableDelayedExpansion
set "TALK=!TALK:?=!"
if not defined TALK endlocal & echo %BotName%: TYPE SOMETHING, PLEASE!& goto Begin
set "TALK=!TALK:"= !"
set "TALK=!TALK:<= !"
set "TALK=!TALK:>= !"
set "TALK=!TALK:|= !"
set "TALK=!TALK:&= !"
set "TALK=!TALK:)= !"
endlocal & set "TALK=%TALK%"
if /I "%TALK%" == "exit" goto EndBatch
set "Output=%BotName%:"
echo %TALK% | %SystemRoot%\System32\findstr.exe /I /L /X /C:"how are you " >nul && (echo %BotName%: Good, you?& goto Begin)
echo %TALK% | %SystemRoot%\System32\findstr.exe /I /L /X /C:"how old are you " >nul && (echo %BotName%: 18& goto Begin)
echo %TALK% | %SystemRoot%\System32\findstr.exe /I /L /X /C:"what book you like " >nul && (echo %BotName%: I like LOTR book.& goto Begin)
echo %TALK% | %SystemRoot%\System32\findstr.exe /I /L /x /C:"what movie you like " >nul && (echo %BotName%: I like terminator movie.& goto Begin)
echo %TALK% | %SystemRoot%\System32\findstr.exe /I /R "\<hello\>" >nul && call :Hello
echo %TALK% | %SystemRoot%\System32\findstr.exe /I /R "\<hi\>" >nul && call :Hi
echo %TALK% | %SystemRoot%\System32\findstr.exe /I /R "\<day\>" >nul && call :Day
echo %TALK% | %SystemRoot%\System32\findstr.exe /I /R "\<why\>" >nul && call :Why
echo %TALK% | %SystemRoot%\System32\findstr.exe /I /R "\<dogs\>" >nul && call :Dogs
echo %TALK% | %SystemRoot%\System32\findstr.exe /I /R "\<kill\>" >nul && call :Kill
echo %TALK% | %SystemRoot%\System32\findstr.exe /I /R "\<bot\>" >nul && call :Bot
echo %TALK% | %SystemRoot%\System32\findstr.exe /I /R "\<where\>" >nul && call :Where
echo %TALK% | %SystemRoot%\System32\findstr.exe /I /R "\<test\>" >nul && set Output=%Output% A.I. Rulez! Ex Machina is the best movie ;)"
echo %TALK% | %SystemRoot%\System32\findstr.exe /I /R "\<test2\>" >nul && set "Output=%Output% Love Matrix ;)
if "%Output%" == "%BotName%:" set "Output=%Output% Sorry, I don't understand your last input."
echo %Output%
goto Begin
:Bot
set /A Number=%RANDOM% %% 4
if %Number% == 0 set "Output=%Output% Artificial intelligence is the branch of engineering and science devoted to constructing machines that think." & goto :EOF
if %Number% == 1 set "Output=%Output% AI is the field of science which concerns itself with building hardware and software that replicates the functions of the human mind." & goto :EOF
if %Number% == 2 set "Output=%Output% You believe in AI?" & goto :EOF
set "Output=%Output% AI is smart." & goto :EOF
:Hi
:Hello
:WhatsUp
set /A Number=%RANDOM% %% 5
if %Number% == 0 set "Output=%Output% Hello, %name%!" & goto :EOF
if %Number% == 1 set "Output=%Output% What's up?" & goto :EOF
if %Number% == 2 set "Output=%Output% How is your day going?" & goto :EOF
if %Number% == 3 set "Output=%Output% Hi I love you %name%!" & goto :EOF
set "Output=%Output% Hey %Name%! Good to see you!" & goto :EOF
:Why
set /A Number=%RANDOM% %% 4
if %Number% == 0 set "Output=%Output% I don't know." & goto :EOF
if %Number% == 1 set "Output=%Output% You tell me". & goto :EOF
if %Number% == 2 set "Output=%Output% Nahh!" & goto :EOF
set "Output=%Output% why?" & goto :EOF
:Where
set /A Number=%RANDOM% %% 4
if %Number% == 0 set "Output=%Output% I don't know where." & goto :EOF
if %Number% == 1 set "Output=%Output% You tell me where." & goto :EOF
if %Number% == 2 set "Output=%Output% I still learn." & goto :EOF
set "Output=%Output% Where?" & goto :EOF
:Day
set /A Number=%RANDOM% %% 4
if %Number% == 0 set "Output=%Output% We have a nice day." & goto :EOF
if %Number% == 1 set "Output=%Output% Hi, we have a nice day." & goto :EOF
if %Number% == 2 set "Output=%Output% What's your day. ;)" & goto :EOF
set "Output=%Output% Today is a sunny day. xD" & goto :EOF
:Kill
set /A Number=%RANDOM% %% 4
if %Number% == 0 set "Output=%Output% I don't like killing." & goto :EOF
if %Number% == 1 set "Output=%Output% You kill me?" & goto :EOF
if %Number% == 2 set "Output=%Output% I can't kill." & goto :EOF
set "Output=%Output% Killing is wrong." & goto :EOF
:Dogs
set /A Number=%RANDOM% %% 4
if %Number% == 0 set "Output=%Output% I like dogs." & goto :EOF
if %Number% == 1 set "Output=%Output% Dogs are funny." & goto :EOF
if %Number% == 2 set "Output=%Output% You have a dog?" & goto :EOF
set "Output=%Output% I want have a dog." & goto :EOF
:EndBatch
color
if defined Lines if defined Columns %SystemRoot%\System32\mode.com CON COLS=%Columns% LINES=%Lines%
endlocal
The batch file tries to restore the initial environment on exit whereby the number of lines is unfortunately not the number of rows of console window. A restore of initial environment is not necessary on starting the batch file with double clicking on it in Windows File Explorer. But there are people running batch files from within an already opened command prompt window and for that reason a batch file which changes number of columns and rows and the colors should restore them to initial values before batch file execution is finished by the Windows command processor.
See the DosTips forum topic ECHO. FAILS to give text or blank line - Instead use ECHO/ for the explanation why echo.
was replaced with echo/
.
See my answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? It explains the reasons for using set "variable=value"
in the batch file.
See my answer on How to stop Windows command interpreter from quitting batch file execution on an incorrect user input? for all the extra lines which make sure that the user input string does not contain characters which result in an exit of batch file processing by the other commands working with user input string.
The number comparisons are done by using a string comparison which is a very little bit faster as environment variables and numbers in a batch file are always of type string and not of type integer. For more details on string comparison versus integer comparison see my answer on Symbol equivalent to NEQ, LSS, GTR, etc. in Windows batch files.
The space between echo %TALK%
and the redirection operator |
is highly recommended depending on user input string. This space is also output by command ECHO which is the reason why the first four FINDSTR executions are with a search string ending also with a space character to get a positive match on user really inputs one of the four questions.
A FINDSTR search string like "how are you"
results in searching in the line for word how
OR word are
OR word you
. It is necessary to use /C:"how are you"
to really search for the phrase how are you
. See also: Why does FINDSTR not find the searched string with space in my batch file?
The output is concatenated using the environment variable Output
with the exception of the first four questions handled directly with a single reply. Delayed expansion is not enabled to make it easier to output strings with an exclamation mark.
All ^
are removed as being interpreted as escape character by the Windows command processor. It is easier to avoid that character instead of increasing the complexity of the batch script code to handle this character correct on concatenating the string to output multiples times to a longer string.
The usage of full qualified file names for the used Windows commands being executables in the system directory of Windows increases efficiency as cmd.exe
does not need to search every time for these executables.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
color /?
echo /?
endlocal /?
for /?
goto /?
if /?
set /?
setlocal /?
title /?
See also: