forked from adeshpande3/Chatbot-Flask-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
37 lines (35 loc) · 1.19 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import tensorflow as tf
import numpy as np
def getTestInput(inputMessage, wList, maxLen):
encoderMessage = np.full((maxLen), wList.index('<pad>'), dtype='int32')
inputSplit = inputMessage.lower().split()
for index,word in enumerate(inputSplit):
try:
encoderMessage[index] = wList.index(word)
except ValueError:
continue
encoderMessage[index + 1] = wList.index('<EOS>')
encoderMessage = encoderMessage[::-1]
encoderMessageList=[]
for num in encoderMessage:
encoderMessageList.append([num])
return encoderMessageList
def idsToSentence(ids, wList):
EOStokenIndex = wList.index('<EOS>')
padTokenIndex = wList.index('<pad>')
myStr = ""
listOfResponses=[]
for num in ids:
if (num[0] == EOStokenIndex or num[0] == padTokenIndex):
listOfResponses.append(myStr)
myStr = ""
else:
myStr = myStr + wList[num[0]] + " "
if myStr:
listOfResponses.append(myStr)
listOfResponses = [i for i in listOfResponses if i]
listOfResponses = list(set(listOfResponses))
#chosenString = ''.join(listOfResponses)
chosenString = listOfResponses[0]
#chosenString = max(listOfResponses, key=len)
return chosenString