This repository has been archived by the owner on Apr 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.py
126 lines (91 loc) · 2.98 KB
/
agent.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
'''
Agent
Created on Jan 28, 2011
@author: yangwookkang
'''
import time
import utils
import sys
from nlu.NLparser import NLparser
from nlg.NLgenerator import NLgenerator
from dm.dialogmanager import DialogManager
from datetime import date
from dm.imdb_wrapper import IMDBWrapper
from dm.localdb_wrapper import LocalDBWrapper
from nlu.entity import EntitySet
class Agent:
def __init__(self, verbose = False):
# load modules
# NLU, DB connection test
self.nlu = NLparser(verbose)
# verbose true for DM and NLG
verbose = True
self.dm = DialogManager(verbose)
self.nlg = NLgenerator(verbose)
self.sessionid = date.strftime(date.today(),"%y%m%d") + "_" + time.strftime("%H%M%S")
self.logger = utils.ConsoleAndFileLogger(self.sessionid)
def run(self):
self.logger.log("Hello. I am YARB (Yet Another Recommendation Bot).")
self.logger.log("Please tell me your name.")
usermsg = raw_input("> ")
self.logger.logtofile("> " + usermsg)
if (self.dm.processUserName(usermsg) == 1):
self.logger.log("Welcome back, " + usermsg + ".")
else:
self.logger.log("Nice to meet you, " + usermsg + ".")
self.logger.log("If you'd like a recommendation, please tell\nme about what you like or dislike.")
self.dm.loadOptions()
while not self.dm.sessionclosed():
usermsg = raw_input("> ")
self.logger.logtofile("> " + usermsg)
if usermsg == "":
continue
nluoutput = self.nlu.process(usermsg) # NLU
for output in nluoutput:
dmoutput = self.dm.process(output) # DM
#dmoutput = self.dm.process(nluoutput)
response = self.nlg.process(dmoutput) # NLG
self.logger.log(response)
self.dm.saveUserPreferences()
self.logger.log("Session closed [id = {0:s}].".format(self.sessionid))
def test(self, inputfilename):
print 'reading: ' + inputfilename
infile = open(inputfilename, 'r')
num = 1
breakpoint = 29
print 'processing... classifier: trivia'
for line in infile:
# NLU process
input = line.strip()
#print input
nluoutput = self.nlu.process(input) # NLU
#if num != breakpoint and nluoutput.get_classifier() != "userPreference":
# num = num + 1
# continue
#if breakpoint == num:
#print str(num) + ". " + line.replace('\\','') + " --> " + nluoutput.get_classifier() + " , [", nluoutput.tostr_entities(), "]"
#if nluoutput.get_classifier() == "trivia":
print str(num) + ". " + input
dmoutput = self.dm.process(nluoutput)
msg = self.nlg.process(dmoutput)
print "> " + msg
print
num = num + 1
def test_db():
IMDBWrapper()
pass
# main function
if __name__ == '__main__':
#test_db()
#Agent().test("./corpus1.txt")
Agent().run()
"""
# imdb test
localdb = LocalDBWrapper()
localdb.load_preference("ywkang")
#localdb.add_preference("genre", "Comedy", 4)
print localdb.get_preference()
db = IMDBWrapper()
entities = EntitySet("dummy")
db.get_recommendation(entities, localdb.get_preference())
"""