-
Notifications
You must be signed in to change notification settings - Fork 0
/
Alice_AI.py
213 lines (167 loc) · 6.28 KB
/
Alice_AI.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#! /usr/bin/python
#-*- coding:utf-8 -*-
"""
モジュール化
"""
import random
import sys
import re
EOS = [u'。',u'!',u'?',u'!',u'?',u'.']
class StrFunctions(object):
def __init__(self):
pass
def tounicode(self, _lines):
lines = []
for i in range(len(_lines)):
lines.append(_lines[i].decode('utf-8'))
return "".join(lines)
def ngramlist(self, _list):
wrdList = []
for i in range(len(_list)-1):
wrdList.append([_list[i],_list[i+1]])
return wrdList
def judgesentence(self, msg_list, str_mlist, pre_str_mlist):
score = 0
for i in str_mlist:
if i in msg_list:
score += 3
for i in pre_str_mlist:
if i in msg_list:
score += 1
return score
def morphtoken(self,_str):
morphlist = []
nowMode = 'None'
preMode = 'None'
index = -1
for i in range(len(_str)):
if re.match(u"[ー]", _str[i]) != None:
nowMode = preMode
elif re.match(u"[\u4e00-\u9fa5]", _str[i]) != None:
nowMode = 'ZEN_KANJI'
elif re.match(u"[ぁ-ゞ]", _str[i]) != None:
nowMode = 'ZEN_HIRAGANA'
elif re.match(u"[ァ-ヾ]", _str[i]) != None:
nowMode = 'ZEN_KATAKANA'
elif re.match(u"[ヲ-ン]", _str[i]) != None:
nowMode = 'HAN_KATAKANA'
elif re.match(u"[。、!?―〜()・…]", _str[i]) != None:
nowMode = 'ZEN_KIGOU'
elif re.match(u"[\.,!\?\-\(\)]", _str[i]) != None:
nowMode = 'HAN_KIGOU'
elif re.match(u"[゙゚]", _str[i]) != None:
nowMode = 'OTHER'
if preMode == 'HAN_KATAKANA':
nowMode = preMode
elif re.match(u"[a-zA-Z]", _str[i]) != None:
nowMode = 'ALPHABET'
elif re.match(u"[0-9]", _str[i]) != None:
nowMode = 'HAN_NUMBER'
elif re.match(u"[0-9]", _str[i]) != None:
nowMode = 'ZEN_NUMBER'
else:
nowMode = 'OTHER'
if nowMode == 'OTHER':
preMode = nowMode
continue
if nowMode != preMode:
morphlist.append(_str[i])
index += 1
else:
if nowMode == 'OTHER': continue
morphlist[index]+=_str[i]
preMode = nowMode
return morphlist
class AliceEngine(object):
def __init__(self, _filename, _name=u"Alice"):
self.name = _name
self.strf = StrFunctions()
self.lines, self.filename = self.readtoken(_filename)
self.tokenlist = self.strf.ngramlist(self.strf.morphtoken(self.lines))
self.message = u""
self.userstr = u""
self.pre_userstr = u""
print self.name+u"さんがログインしました。"
def readtoken(self, _filename):
filename = ''
if len(sys.argv)==1: filename = _filename
else: filename = sys.argv[1]
lines = open(filename,'r').readlines()
lines = self.strf.tounicode(lines)
return lines, filename
def addtoken(self, _msg):
_tokens = self.strf.morphtoken(_msg)
if _tokens == []: return []
if _tokens[-1] not in EOS:
_tokens.append(u'。')
f = open(self.filename,'a')
for i in _tokens:
f.write(i.encode("utf-8")+"\n")
f.close()
self.tokenlist.append([self.tokenlist[-1][-1],_tokens[0]])
self.tokenlist += self.strf.ngramlist(_tokens)
return _tokens
def makerouret(self, iniChar):
rouret = []
iniList = [x[0] for x in self.tokenlist]
if not iniChar in iniList: return []
index = iniList.index(iniChar)
while iniChar in iniList[index]:
rouret.append(index)
index += 1
if index >= len(iniList)-1:
break
return rouret
def makesentence(self, iniIndex):
msg = ''
cnt = 0
while cnt == 0 or (not self.tokenlist[iniIndex][0][-1] in EOS and cnt <= 20):
msg += self.tokenlist[iniIndex][0]
cnt += 1
iniList = self.makerouret(self.tokenlist[iniIndex][1])
if not iniList: iniIndex = random.randint(0,len(self.tokenlist)-1)
else: iniIndex = random.choice(iniList)
return msg
def makeinilist(self):
inilist = []
for i in self.userstr:
inilist += self.makerouret(i)
if self.pre_userstr != []:
for i in self.pre_userstr:
inilist += self.makerouret(i)
inilist.append(random.randint(0,len(self.tokenlist)-1))
return inilist
def makemessage(self, inilist):
message = []
for i in inilist:
_msg = self.makesentence(i)
message.append([_msg, self.strf.judgesentence(_msg, self.userstr, self.pre_userstr)])
message = sorted(message, key=lambda x:x[1], reverse=True)
return message
def choosemessage(self):
messagelist = self.makemessage(self.makeinilist())
_message = self.message
cnt = 0
rouret = []
for i in range(len(messagelist)):
rouret += [i]*(messagelist[i][1]+1)
while _message == self.message and cnt <= 100:
_message = messagelist[random.choice(rouret)][0]
cnt += 1
# print _message, self.message
return _message
def mainloop(self, input):
self.userstr = self.addtoken(input)
for i in EOS:
while i in self.userstr: self.userstr.remove(i)
while i in self.pre_userstr: self.pre_userstr.remove(i)
self.message = self.choosemessage()
print self.name+u":"+self.message
self.pre_userstr = self.userstr
return self.message
if __name__ == "__main__":
alice = AliceEngine("save/alice.txt")
while True:
input = raw_input("You:").decode('utf-8')
if input == '': continue
alice.mainloop(input)