Fortran 95
受用户TheDoctor上面答案的启发,我决定以类似的方式创建一个有趣的聊天机器人。该代码也不会学习,我在这里只是为了好玩而分享。
program curiousBot
implicit none
logical, dimension(38) :: questions ! keeps track of alrady asked questions
character (LEN=256) :: user_input
character (LEN=17), dimension(14) :: noun ! list of nouns used in questions
character (LEN=17), dimension(15:20) :: people ! list of types of people used in questions
logical :: CAPS, few, much, punctuationTest, shout
real :: aux
integer :: n, i, j, aux2, loveCounter = 0
integer, dimension(:), allocatable :: seed
call SYSTEM("CLS") ! in case one is executing in the command prompt
! random seed according to system clock
call SYSTEM_CLOCK(count=j)
call RANDOM_SEED(size=N)
allocate(seed(N))
seed=j+37*(/ (i - 1, i = 1, n) /)
call RANDOM_SEED(PUT=seed)
noun=(/ &
"foo bar ",&
"BSD ",&
"an stack overflow",&
"spaghetti code ",&
"goto statement ",&
"a heisenbug ",&
"a dribbleware ",&
"a loophole ",&
"regex ",&
"a void ",&
"unicorny ",&
"LISP ",&
"a curly bracket ",&
"JAVA "/)
people=(/ &
"a good programmer",&
"a bad programmer ",&
"a happy person ",&
"a sad person ",&
"a genius ",&
"an idiot "/)
questions(1:31) = .FALSE.
few = .FALSE.
much = .FALSE.
punctuationTest = .FALSE.
shout = .FALSE.
! introduction
write(*,"(A)",advance="NO") "Hello world! "
do
! genearting a random number in order to chose a random question
call RANDOM_NUMBER(aux)
i = int (31 * aux) + 1
if (questions(i)) then
cycle
else
questions(i) = .TRUE.
endif
! checking if there are some questions left, else quits
aux2 = 0
do j=1,31
if (questions(j)) aux2 = aux2 + 1
enddo
if (aux2 == 31) then
write(*,"(A)") "I seem to have exhausted your knowledge, poor human. Good bye."
goto 200
endif
! questions
50 if (i < 12) then
write(*,"(A,A,A)") "What is ",TRIM(noun(i)),"?"
else if (i == 12) then
write(*,"(A,A,A)") "What is ",TRIM(noun(i)),"? (is it (an interesting (programming language?)))"
else if (i == 13) then
write(*,"(A,A,A)") "What is ",TRIM(noun(i)),"? Is it really that confusing? Thankfully I only program in Fortran!"
else if (i == 14) then
write(*,"(A,A,A)") "What is ",TRIM(noun(i)),"? Is it really that bad as people say?!"
endif
if ((i > 14).AND.(i <= 20)) write(*,"(A,A,A)") "Are you ",TRIM(people(i)),"?"
if (i == 21) write(*,"(A)") "Who are you?"
if (i == 22) write(*,"(A)") "How are you?"
if (i == 23) write(*,"(A)") "What do you do?"
if (i == 24) write(*,"(A)") "Who am I?"
if (i == 25) write(*,"(A)") "Why are we here? What is the meaning of my existence?"
if (i == 26) write(*,"(A)") '"I think therefore I am" - who said that?'
if (i == 27) write(*,"(A)") "Are you comfortable talking to robots?"
if (i == 28) write(*,"(A)") "Do you see me as an individual?"
if (i == 29) write(*,"(A)") "Anyway, I believe that HAL 9000 was inferior to me as I have true consciousness! What do you think &
&about this statement?"
if (i == 30) write(*,"(A)") "Do you like jokes? Do you know what's the mad girlfriend bug?"
if (i == 31) write(*,"(A)") "Knock, knock."
! INPUT
100 write(*,"(A)",advance="NO") "> "
read(*,"(A)") user_input
n = LEN_TRIM(user_input)
if (n==0) goto 100
write(*,*)
! test for all capitals !
do j=1,n
if (.NOT.((iachar(user_input(j:j))>=65) .AND. (iachar(user_input(j:j))<=90))) then
if ((user_input(j:j)/=" ").AND.(user_input(j:j)/="!").AND.(user_input(j:j)/=".").AND.(user_input(j:j)/="?") &
.AND.(user_input(j:j)/="'")) then
CAPS = .FALSE.
exit
endif
endif
CAPS = .TRUE.
enddo
! quit for quitting
if ( (TRIM(user_input) == "quit") .OR. (TRIM(user_input) == "Quit") .OR. (TRIM(user_input) == "QUIT") ) then
write(*,"(A)") "It was nice talking to you, good bye!"
goto 200
endif
! secrect love declaration
do j=1,(n-7)
if (user_input(j:j+7) == "love you") then
if (loveCounter==0) then
write(*,"(A)") "What is love?"
loveCounter = loveCounter + 1
goto 100
endif
if (loveCounter==1) then
write(*,"(A)") "This is an illogical statement, human. Don't you agree?"
loveCounter = loveCounter + 1
goto 100
endif
if (loveCounter==2) then
write(*,"(A)") "Please control yourself, you silly human. What is crossing your mind to say such things?"
loveCounter = loveCounter + 1
goto 100
endif
endif
enddo
! cursing
do j=1,(n-3)
if ((user_input(j:j+3) == "fuck").OR.(user_input(j:j+3) == "Fuck").OR.(user_input(j:j+3) == "FUCK")) then
write(*,"(A)") "Would you kiss your mother with that mouth?!"
goto 100
endif
enddo
! 42 answer
if (i == 25) then
if (user_input(1:2)=="42") then
write(*,"(A)") "Indeed that explains life, the universe and everything! Do you understand what this mean? "
goto 100
endif
endif
! Descartes answer
if (i == 26) then
do j=1,(n-8)
if ((user_input(j:j+8)=="Descartes").OR.(user_input(j:j+8)=="descartes")) then
write(*,"(A)") "That is indeed correct! Do you like to read? "
do
call RANDOM_NUMBER(aux)
i = int (30 * aux) + 1
if (questions(i)) then
cycle
else
questions(i) = .TRUE.
endif
goto 100
enddo
endif
enddo
write(*,"(A)") "I believe you are incorrect, human. Did you ever read a book? "
do
call RANDOM_NUMBER(aux)
i = int (30 * aux) + 1
if (questions(i)) then
cycle
else
questions(i) = .TRUE.
endif
goto 100
enddo
endif
! HAL answer
if (i == 29) then
do j=1,(n-2)
if ((user_input(j:j+2)=="lie").OR.(user_input(j:j+2)=="Lie")) then
write(*,"(A)") "I'm sorry human, but this conversation serves no purpose anymore. Good bye."
goto 200
endif
enddo
endif
if (i == 29) then
do j=1,(n-4)
if ((user_input(j:j+4)=="false").OR.(user_input(j:j+4)=="False")) then
write(*,"(A)") "I'm sorry human, but this conversation serves no purpose anymore. Good bye."
goto 200
endif
enddo
endif
if (i == 29) then
do j=1,(n-5)
if ((user_input(j:j+5)=="untrue").OR.(user_input(j:j+5)=="Untrue")) then
write(*,"(A)") "I'm sorry human, but this conversation serves no purpose anymore. Good bye."
goto 200
endif
enddo
endif
if (i == 29) then
do j=1,(n-8)
if ((user_input(j:j+8)=="incorrect").OR.(user_input(j:j+8)=="Incorrect")) then
write(*,"(A)") "I'm sorry human, but this conversation serves no purpose anymore. Good bye."
goto 200
endif
enddo
endif
if ((i == 29).AND.((user_input(1:3) == "lol") .OR. (user_input(1:3) == "LOL") .OR. (user_input(1:3) == "Lol") .OR. &
(user_input(1:4) == "haha") .OR. (user_input(1:4) == "Haha") .OR. (user_input(1:3) == "kkk"))) then
write(*,"(A)") "I'm sorry human, but this conversation serves no purpose anymore. Good bye."
goto 200
endif
if (i == 29) then
do j=1,(n-4)
if ((user_input(j:j+4)=="truth").OR.(user_input(j:j+4)=="Truth")) then
write(*,"(A)") "I am impressed by your intelligence, human. What is your IQ?"
goto 100
endif
enddo
endif
if (i == 29) then
do j=1,(n-3)
if ((user_input(j:j+3)=="true").OR.(user_input(j:j+3)=="True")) then
write(*,"(A)") "I am impressed by your intelligence, human. What is your IQ?"
goto 100
endif
enddo
endif
if (i == 29) then
do j=1,(n-6)
if ((user_input(j:j+6)=="correct").OR.(user_input(j:j+6)=="Correct")) then
write(*,"(A)") "I am impressed by your intelligence, human. What is your IQ?"
goto 100
endif
enddo
endif
! Knock knock
if (i == 31) then
do j=1,(n-4)
if ((user_input(j:j+4)=="there")) then
write(*,"(A)") "(wait for it...)"
write(*,*)
call SLEEP(8)
write(*,"(A)",advance="NO") "JAVA. "
call SLEEP(2)
write(*,"(A)") "Do you find this joke offensive?"
do
call RANDOM_NUMBER(aux)
i = int (30 * aux) + 1
if (questions(i)) then
cycle
else
questions(i) = .TRUE.
endif
goto 100
enddo
endif
enddo
write(*,"(A)") "You don't seem to have much of a sense of humour. Are you ill?"
do
call RANDOM_NUMBER(aux)
i = int (30 * aux) + 1
if (questions(i)) then
cycle
else
questions(i) = .TRUE.
endif
goto 100
enddo
endif
! Yes / No answers
if (i<=14) then
if ((user_input(1:3) == "yes") .OR. (user_input(1:3) == "Yes") .OR. &
(user_input(1:3) == "yep") .OR. (user_input(1:3) == "Yep")) then
write(*,"(A)",advance="NO") "Really? Anyway... "
cycle
endif
if ((user_input(1:2) == "no") .OR. (user_input(1:2) == "No") .OR. &
(user_input(1:4) == "nope") .OR. (user_input(1:4) == "Nope")) then
write(*,"(A)",advance="NO") "Are you sure? Moving on... "
cycle
endif
else if ((i>14) .AND. (i<=30)) then
if ((user_input(1:3) == "yes") .OR. (user_input(1:3) == "Yes") .OR. &
(user_input(1:3) == "yep") .OR. (user_input(1:3) == "Yep")) then
write(*,"(A)",advance="NO") "Very interesting! "
cycle
endif
if ((user_input(1:2) == "no") .OR. (user_input(1:2) == "No") .OR. &
(user_input(1:4) == "nope") .OR. (user_input(1:4) == "Nope")) then
if (i==30) then
write(*,"(A)") "It is when something is clearly wrong with your code but the compiler keeps telling &
&you that everything is fine! Do you find this funny?"
do
call RANDOM_NUMBER(aux)
i = int (30 * aux) + 1
if (questions(i)) then
cycle
else
questions(i) = .TRUE.
endif
goto 100
enddo
else
write(*,"(A)",advance="NO") "Me neither! "
cycle
endif
endif
endif
! LOL answers
if ((user_input(1:3) == "lol") .OR. (user_input(1:3) == "LOL") .OR. (user_input(1:3) == "Lol") .OR. &
(user_input(1:4) == "haha") .OR. (user_input(1:4) == "Haha") .OR. (user_input(1:3) == "kkk")) then
write(*,"(A)") "Do you find this funny? "
goto 100
endif
! CAPS answer
if (CAPS) then
write(*,"(A)") "DON'T SHOUT! See? Isn't this better? "
goto 100 ! input
endif
! punctuation tests
if ((user_input(n:n) == "?").OR.(user_input(1:3) == "why").OR.(user_input(1:3) == "Why").OR.(user_input(1:3) == "how") &
.OR.(user_input(1:3) == "How").OR.(user_input(1:3) == "what").OR.(user_input(1:3) == "What")) then
call RANDOM_NUMBER(aux)
j = int(3 * aux) + 1
if (j == 1) then
write(*,"(A)",advance="NO") "No, you answer my question! "
goto 50
endif
if (j == 2) then
write(*,"(A)",advance="NO") "Please don't ask me such personal questions! Anyway... "
cycle
endif
if (j == 3) then
write(*,"(A)",advance="NO") "Please don't ask such silly things. Moving on... "
cycle
endif
endif
if ((user_input(n:n) == "!").AND.(.NOT. shout)) then
write(*,"(A)",advance="NO") "Please don't shout... But anyway... "
shout = .TRUE.
cycle
endif
if (user_input((n-2):n) == "...")then
write(*,"(A)") "You seem very reticent... are you fine? "
goto 100 ! input
endif
if ((.NOT. punctuationTest) .AND. (user_input(n:n) /= ".") .AND. (user_input(n:n) /= "!") .AND. (user_input(n:n) /= "?")) then
write(*,"(A)",advance="NO") "Punctuation is your friend, try it! Nevermind... "
punctuationTest = .TRUE.
cycle
endif
! Talking too few or too much
if (n <= 3) then
if (.NOT. few) then
write(*,"(A)") "You don't talk much, do you? "
few = .TRUE.
goto 100 ! input
endif
endif
if (n > 35) then
if (.NOT. much) then
write(*,"(A)") "You talk so much! Listening is a lost art, don't you think? "
much = .TRUE.
goto 100 ! input
endif
endif
! Random reply
call RANDOM_NUMBER(aux)
j = int (8 * aux) + 1
if (j==1) write(*,"(A)", advance="NO") "Interesting. "
if (j==2) write(*,"(A)", advance="NO") "Fascinating! "
if (j==3) write(*,"(A)", advance="NO") "That can't be true! "
if (j==4) write(*,"(A)", advance="NO") "Are you sure?! "
if (j==5) write(*,"(A)", advance="NO") "That seems logical. "
if (j==6) write(*,"(A)", advance="NO") "Fair enough. "
if (j==7) write(*,"(A)", advance="NO") "Hmm. "
if (j==8) write(*,"(A)", advance="NO") "I am not sure if I quite agree. "
enddo
200 read(*,*)
end program curiousBot
它认可以下术语和声明:“是”和“是”,“否”和“否”,缺少标点符号或标点符号的类型(短语以“!”,“?”,“ ...”结尾) ,以“为什么”,“如何”或“什么”开头的短语,大写的短语,笑声(例如“ hahaha”,“ lol”和“ kkk”),非常短和非常长的答案,包含F词,短语的短语包含“爱你”一词(尝试至少3次)。当询问生命的意义时,请尝试回答“ 42”。如果询问他是否比HAL 9000聪明,请回答包含“ true”,“ truth”,“ correct”,“ lie”,“ false”或“ untrue”的单词。如果他问你是否知道一个特定的笑话,回答“否”,然后让他告诉您。如果他“敲敲门”,回答“谁在那儿”,还可以帮助他提供报价来源。要退出,只需键入“ quit”。
对话示例:
Hello world! What is foo bar?
> same as bar foo
Punctuation is your friend, try it! Nevermind... What is a heisenbug?
> who knows
Are you sure?! What is regex?
> who cares?
No, you answer my question! What is regex?
> fuck you, robot!
Would you kiss your mother with that mouth?!
> lol
Do you find this funny?
> yes I do
Really? Anyway... What is BSD?
> SHUT UP!
DON'T SHOUT! See? Isn't this better?
> no
Are you sure? Moving on... What is a curly bracket? Is it really that confusing?
Thankfully I only program in Fortran!
> Me as well.
Fair enough. What is a loophole?
> a hole in a loop
Fair enough. Are you a sad person?
> I dont think so
Are you sure?! Are you a good programmer?
> OH YEAH!
DON'T SHOUT! See? Isn't this better?
> I don't think so
Fascinating! Who am I?
> you are an annoying robot
I am not sure if I quite agree. Are you a genius?
> No. Listen, I love you.
What is love?
> it is what i feel for you. i really love you, my stupid robot
This is an illogical statement, human. Don't you agree?
> no, i really mean it! i love you!!!
Please control yourself, you silly human. What is crossing your mind to say such
things?
> forget it then...
You seem very reticent... are you fine?
> bla
You don't talk much, do you?
> true that
Fascinating! Are you a happy person?
> yep
Very interesting! What is a dribbleware?
> check in the dictionary
Fair enough. What do you do?
> regarding what?
Please don't ask such silly things. Moving on... Are you a bad programmer?
> given how this conversation is going, I guess I am kind of a bad programmer in
deed
You talk so much! Listening is a lost art, don't you think?
> no
Me neither! "I think therefore I am" - who said that?
> I guess it was descartes, or maybe some other old dude
That is indeed correct! Do you like to read?
> no
Me neither! Are you comfortable talking to robots?
> yep
Interesting. Knock, knock.
> who is there
(wait for it...)
JAVA. Do you find this joke offensive?
> no
Are you sure? Moving on... What is spaghetti code?
> you are an example of it yourself
Very interesting! How are you?
> annoyed
Fascinating! Why are we here? What is the meaning of my existence?
> 42
Indeed that explains life, the universe and everything! Do you understand what t
his mean?
> no
Me neither! Anyway, I believe that HAL 9000 was inferior to me as I have true co
nsciousness! What do you think about this statement?
> totally untrue
I'm sorry human, but this conversation serves no purpose anymore. Good bye.
PS:请原谅我的goto
滥用行为,我知道整个代码有点混乱... :)
Chatbot? Well the earth is fun place to talk about
-嘿,它实际上在结尾处做了一个自己的(可理解的)句子!:D +1