如標題所示。如何更改以下批處理代碼,以便機器人回答在一行中,而不是在多行中。當我寫一個問題時,機器人會用幾行回答我,當我輸入對 findstr 的一些參考時,我希望機器人在一行/一句話中給出答案。
@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
uj5u.com熱心網友回復:
這是您重寫的批處理檔案:
@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
批處理檔案試圖在退出時恢復初始環境,不幸的是,行數不是控制臺視窗的行數。在 Windows 檔案資源管理器中雙擊啟動批處理檔案,不需要恢復初始環境。但是有些人從已經打開的命令提示符視窗中運行批處理檔案,因此在 Windows 命令處理器完成批處理檔案執行之前,更改列數和行數以及顏色的批處理檔案應該將它們恢復到初始值。
請參閱 DosTips 論壇主題ECHO。無法給出文本或空行 - 而是使用 ECHO/來解釋為什么echo.被替換為echo/.
請參閱我在命令列上使用“set var = text”后為什么沒有帶有“echo %var%”的字串輸出的答案?它解釋了set "variable=value"在批處理檔案中使用的原因。
請參閱我關于如何停止 Windows 命令解釋器在不正確的用戶輸入上退出批處理檔案執行的回答?對于所有額外的行,確保用戶輸入字串不包含導致其他命令處理用戶輸入字串退出批處理檔案的字符。
數字比較是通過使用字串比較來完成的,因為環境變數和批處理檔案中的數字始終是字串型別而不是整數型別,因此速度要快一些。有關字串比較與整數比較的更多詳細資訊,請參閱我在 Windows 批處理檔案中等效于 NEQ、LSS、GTR 等符號的回答。
根據用戶輸入字串,強烈建議使用echo %TALK%和 重定向運算子之間的空格|。這個空格也由命令ECHO輸出,這就是為什么前四個FINDSTR執行的搜索字串也以空格字符結尾,以獲得對用戶真正輸入的四個問題之一的肯定匹配的原因。
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:
- Microsoft documentation about Using command redirection operators
- How does the Windows Command Interpreter (CMD.EXE) parse scripts?
- Single line with multiple commands using Windows batch file
- Where does GOTO :EOF return to?
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/312639.html
標籤:批处理文件
