마르코프 체인 챗봇은 어떻게 작동합니까?
나는 markov 체인과 같은 것을 사용하여 챗봇을 만들려고 생각하고 있었지만 어떻게 작동하는지 완전히 모르겠습니다. 내가 이해하는 바에 따르면 주어진 단어와 그 뒤에 오는 단어로 데이터를 테이블로 만듭니다. 봇을 훈련하는 동안 어떤 종류의 확률이나 카운터를 붙일 수 있습니까? 그게 좋은 생각인가요?
문제의 두 번째 부분은 키워드입니다. 사용자 입력에서 이미 키워드를 식별 할 수 있다고 가정 할 때 해당 키워드를 사용하는 문장을 어떻게 생성합니까? 항상 키워드로 문장을 시작하고 싶지는 않습니다. 그렇다면 어떻게 markov 체인을 시드합니까?
저는 몇 년 전 Python으로 IRC 용 Markov 체인 챗봇을 만들었고 제가 어떻게했는지 알 수 있습니다. 생성 된 텍스트가 반드시 의미가있는 것은 아니지만 읽기가 정말 재미있을 수 있습니다. 단계별로 분석해 보겠습니다. 고정 된 입력, 텍스트 파일이 있다고 가정합니다 (채팅 텍스트 또는 가사의 입력을 사용하거나 상상력을 사용할 수 있음).
텍스트를 반복하고 키 값 컨테이너를 의미하는 사전을 만듭니다. 그리고 모든 단어 쌍을 키로, 다음 단어를 값으로 넣으십시오. 예 : 텍스트 "abcab k"가있는 경우 "ab"를 키로, "c"를 값으로 시작한 다음 "bc"및 "a"를 값으로 ... 값은 목록 또는 보유하고있는 컬렉션이어야합니다. 0 .. 주어진 단어 쌍에 대해 둘 이상의 값을 가질 수 있으므로 많은 '항목'. 위의 예에서 "a b"가 두 번 있고 주먹이 "c"가되고 마지막에 "k"가 나타납니다. 따라서 결국 다음과 같은 사전 / 해시가 생깁니다.{'a b': ['c','k'], 'b c': ['a'], 'c a': ['b']}
이제 펑키 한 텍스트를 만드는 데 필요한 구조가 생겼습니다. 임의의 키 또는 고정 된 위치로 시작하도록 선택할 수 있습니다! 그래서 우리가 가진 구조가 주어지면 우리는 "ab"를 저장 한 다음 값 c 또는 k에서 다음 단어를 무작위로 취하여 시작할 수 있습니다. 루프의 첫 번째 저장은 "ab k"( "k"가 임의의 값을 선택한 ) 그런 다음 오른쪽으로 한 단계 이동하여 계속 진행합니다. 여기서는 "bk"인 경우 해당 쌍에 대해 임의의 값을 저장합니다 (우리의 경우에는 아니오). 루프를 벗어나거나 다른 항목을 결정할 수 있습니다. 다시 시작하는 것처럼). 루프가 완료되면 저장된 텍스트 문자열을 인쇄합니다.
입력이 클수록 키 (단어 쌍)에 대한 더 많은 값을 갖게되고 "스마트 한 봇"을 갖게되어 더 많은 텍스트를 추가하여 봇을 "훈련"할 수 있습니다 (대화 입력?). 책을 입력으로 가지고 있다면 멋진 임의의 문장을 만들 수 있습니다. 한 쌍 뒤에 오는 한 단어 만 값으로 사용할 필요는 없습니다. 2 또는 10을 사용할 수 있습니다. 차이점은 "긴"빌딩 블록을 사용하면 텍스트가 더 정확하게 표시된다는 것입니다. 키로 쌍으로 시작하고 값으로 다음 단어로 시작하십시오.
따라서 기본적으로 두 단계를 가질 수 있음을 알 수 있습니다. 먼저 시작할 키를 무작위로 선택한 다음 해당 키를 가져 와서 해당 키의 임의 값을 인쇄하고 값이나 다른 조건이 없을 때까지 계속하는 구조를 만듭니다. 원하는 경우 키-값 구조의 채팅 입력에서 단어 쌍을 "시드"하여 시작할 수 있습니다. 체인을 시작하는 방법은 상상력에 달려 있습니다.
실제 단어를 사용한 예 :
"hi my name is Al and i live in a box that i like very much and i can live in there as long as i want"
"hi my" -> ["name"]
"my name" -> ["is"]
"name is" -> ["Al"]
"is Al" -> ["and"]
........
"and i" -> ["live", "can"]
........
"i can" -> ["live"]
......
이제 루프를 생성하십시오.
임의의 키를 고르고 "hi my"라고 말하고 임의로 값을 선택합니다. 여기에는 "이름" ( "hi my name"저장)이 하나만 있습니다 .
이제 "my name"을 다음 키로 사용하여 오른쪽으로 한 단계 이동하고 임의의 값을 선택합니다 ... "is" (SAVING "hi my name is") .
이제 이동하여 "name is"... "Al" ( "hi my name is AL"저장) .
이제 "is Al"... "and" ( "hi my name is Al and"저장) .
...
"and i"에 오면 무작위로 값을 선택하고 "can"이라고 말한 다음 "i can"이라는 단어가 만들어집니다. 정지 조건에 도달하거나 값이 없으면 생성 된 내용을 인쇄합니다. 우리의 경우 문자열 :
"안녕 내 이름은 Al이고 내가 원하는만큼 거기에서 살 수있어"
더 많은 값이 있으면 아무 키로 나 이동할 수 있습니다. 값이 많을수록 조합이 많을수록 텍스트가 더 무작위적이고 재미 있습니다.
봇은 입력 한 단어에서 임의의 단어를 선택하고 보류 된 단어의 후속 단어로 간주되는 다른 임의의 단어를 선택하여 응답을 생성합니다. 그런 다음 해당 단어의 후계자를 차례로 찾고 충분히 말했다고 생각 될 때까지 반복적으로 수행하여 프로세스를 반복합니다. 훈련 텍스트에서 구두점 앞에 있던 단어에서 멈추어 결론에 도달합니다. 그런 다음 다시 입력 모드로 돌아가 응답 할 수 있습니다.
It isn’t very realistic but I hereby challenge anyone to do better in 71 lines of code !! This is a great challenge for any budding Pythonists, and I just wish I could open the challenge to a wider audience than the small number of visitors I get to this blog. To code a bot that is always guaranteed to be grammatical must surely be closer to several hundred lines, I simplified hugely by just trying to think of the simplest rule to give the computer a mere stab at having something to say.
Its responses are rather impressionistic to say the least ! Also you have to put what you say in single quotes.
I used War and Peace for my “corpus” which took a couple of hours for the training run, use a shorter file if you are impatient…
here is the trainer
#lukebot-trainer.py
import pickle
b=open('war&peace.txt')
text=[]
for line in b:
for word in line.split():
text.append (word)
b.close()
textset=list(set(text))
follow={}
for l in range(len(textset)):
working=[]
check=textset[l]
for w in range(len(text)-1):
if check==text[w] and text[w][-1] not in '(),.?!':
working.append(str(text[w+1]))
follow[check]=working
a=open('lexicon-luke','wb')
pickle.dump(follow,a,2)
a.close()
Here is the bot:
#lukebot.py
import pickle,random
a=open('lexicon-luke','rb')
successorlist=pickle.load(a)
a.close()
def nextword(a):
if a in successorlist:
return random.choice(successorlist[a])
else:
return 'the'
speech=''
while speech!='quit':
speech=raw_input('>')
s=random.choice(speech.split())
response=''
while True:
neword=nextword(s)
response+=' '+neword
s=neword
if neword[-1] in ',?!.':
break
print response
You tend to get an uncanny feeling when it says something that seems partially to make sense.
You could do like this: Make a order 1 markov chain generator, using words and not letters. Everytime someone post something, what he posted is added to bot database. Also bot would save when he gone to chat and when a guy posted the first post (in multiples of 10 seconds), then he would save the amount of time this same guy waited to post again (in multiples of 10 seconds)... This second part would be used to see when the guy will post, so he join the chat and after some amount of time based on a table with "after how many 10 seconds the a guy posted after joining the chat", then he would continue to post with the same table thinking "how was the amount of time used to write the the post that was posted after a post that he used X seconds to think about and write"
참고URL : https://stackoverflow.com/questions/5306729/how-do-markov-chain-chatbots-work
'program tip' 카테고리의 다른 글
Python에서 파일 끝에 함수 선언 (0) | 2020.11.01 |
---|---|
junit의 java.lang.NoClassDefFoundError (0) | 2020.11.01 |
자동으로 가져 오기 구성 (0) | 2020.11.01 |
Html.BeginForm ()에 CSS 클래스 추가 (0) | 2020.11.01 |
각도기 : 버튼 클릭 후 페이지 완료를 기다리는 방법은 무엇입니까? (0) | 2020.11.01 |