-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
309 lines (252 loc) · 8.54 KB
/
Copy pathapp.py
File metadata and controls
309 lines (252 loc) · 8.54 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import sys,os
sys.path.append('/home/xpjp/analyze_api')
from Psgr import *
from Analyze import *
import simplejson as json
from bottle import route, request, run, template, HTTPResponse
@route('/hello')
def index(name):
return "hello world"
@route('/all/select',method='GET')
def select():
psgr = Psgr()
cur = psgr.getCur()
# データを閲覧する
print("---------------")
print("[sentences]")
cur.execute("select * from sentences")
for row in cur:
print(row)
print("---------------")
print("[words]")
cur.execute("select * from words")
for row in cur:
print(row)
print("---------------")
print("[channels]")
cur.execute("select * from channels")
for row in cur:
print(row)
print("---------------")
print("[markov_chain]")
cur.execute("select * from markov_chain")
for row in cur:
print(row)
print("---------------")
del psgr
# responseを作成
return make_response('selected')
@route('/all/delete',method='GET')
def delete():
psgr = Psgr()
cur = psgr.getCur()
# データを削除する
cur.execute("DELETE FROM SENTENCES")
cur.execute("DELETE FROM WORDS")
cur.execute("DELETE FROM CHANNELS")
cur.execute("DELETE FROM markov_chain")
psgr.dbCommit()
del psgr
# responseを作成
return make_response('deleted')
@route('/insert', method='POST')
def insert():
# psgr = Psgr()
# cur = psgr.getCur()
# 送られてきたJSONを読み込む
postdata = request.body.read()
print (postdata)
json_dict = json.loads(postdata)
print(type(json_dict), json_dict['sentence'])
sentence = json_dict['sentence']
channel_id = str(json_dict['channel_id'])
channel = json_dict['channel']
# messageを登録
sentence_id = insert_message(sentence,channel_id)
# messageを解析(機械学習用に溜めとくため)
analyze_message(sentence,sentence_id,channel_id)
# チャンネル情報を登録
insert_channels(channel_id, channel)
# markov連鎖用データ登録
insert_data_markov(sentence_id,sentence, channel_id)
return make_response("登録されました")
# markov連鎖で文章作成
@route('/talk', method='GET')
def talk():
return make_response(make_sentence(390124129430536194))
# messageの解析
def analyze_message(sentence, sentence_id,channel_id):
psgr = Psgr()
cur = psgr.getCur()
# messageの解析
try:
psgr.begin()
analyze = Analyze()
parse_data = analyze.parse_sentence(sentence)
for data in parse_data:
detail_array = ['*'] * 9
detail_array[:len(data[1].split(','))] = data[1].split(',')
# 登録用データ↓↓
# word = data[0] # Surfaceデータが取れないので。
word = detail_array[6]
part_of_speech = detail_array[0]
if part_of_speech == "BOS/EOS":
continue
part_of_speech_detail1 = detail_array[1]
part_of_speech_detail2 = detail_array[2]
part_of_speech_detail3 = detail_array[3]
conjugate1 = detail_array[4]
conjugate2 = detail_array[5]
original = detail_array[6]
pronunciation1 = detail_array[7]
pronunciation2 = detail_array[8]
# 単語の登録
values = (word,part_of_speech,part_of_speech_detail1,part_of_speech_detail2,part_of_speech_detail3,conjugate1,conjugate2,original,pronunciation1,pronunciation2)
sqlcom = "INSERT INTO words (word,part_of_speech,part_of_speech_detail1,part_of_speech_detail2,part_of_speech_detail3,conjugate1,conjugate2,original,pronunciation1,pronunciation2) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) RETURNING word_id;"
psgr.execute(sqlcom,values)
word_id = psgr.lastrowid()
# 単語と文章を繋ぐ
values = (sentence_id,word_id,channel_id)
sqlcom = "INSERT INTO sentence_word (sentence_id, word_id, channel_id) VALUES (%s,%s,%s) RETURNING sentence_word_id;"
psgr.execute(sqlcom,values)
except Exception as err:
print(err)
psgr.rollback()
# コミット
psgr.dbCommit()
del psgr
def insert_message(sentence, channel_id):
psgr = Psgr()
cur = psgr.getCur()
# messageを登録
sentence_id = -1
try:
psgr.begin()
values = (sentence, channel_id)
sqlcom = "INSERT INTO sentences (sentence, channel_id) VALUES (%s,%s) RETURNING sentence_id;"
psgr.execute(sqlcom,values)
sentence_id = psgr.lastrowid()
except Exception as err:
print(err)
psgr.rollback()
# コミット
psgr.dbCommit()
del psgr
return sentence_id
def insert_channels(channel_id, channel):
psgr = Psgr()
cur = psgr.getCur()
# チャンネル情報を登録
try:
psgr.begin()
cur.execute("select * from channels")
data = cur.fetchall()
exist_id = False
for row in data:
if int(row[2]) == int(channel_id):
exist_id = True
break
if not exist_id:
print("insert channel")
values = (channel_id, channel)
sqlcom = "INSERT INTO channels (channel_id, channel) VALUES (%s,%s)"
psgr.execute(sqlcom,values)
except Exception as err:
print(err)
psgr.rollback()
# コミット
psgr.dbCommit()
del psgr
# 文章を分かち書きしてDBに登録する
def insert_data_markov(sentence_id,sentence, channel_id):
# データベースInstanceの作成
psgr = Psgr()
try:
#トランザクション処理開始
psgr.begin()
# messageの形態素解析
analyze = Analyze()
# 分かち書きした文章を取得する
# 吾輩 は 猫 で ある
parse_data = analyze.parse_wakati_sentence(sentence)
w1 = ''
w2 = ''
for data in parse_data:
if data[0] == '':
continue
# 登録用データ↓↓
word = data[0]
if w1 and w2:
values = (sentence_id, w1, w2, word, channel_id)
sqlcom = "INSERT INTO markov_chain (sentence_id, word1, word2, word3, channel_id) VALUES (%s,%s,%s,%s,%s)"
psgr.execute(sqlcom,values)
w1, w2 = w2, word
psgr.commit()
del analyze
except Exception as e:
print (e)
psgr.rollback()
del psgr
# 文章作成
def make_sentence(channel_id):
# データベースInstanceの作成
sentence = ""
psgr = Psgr()
try:
#トランザクション処理開始
psgr.begin()
cur = psgr.getCur()
# 1単語目所得
cur.execute("SELECT * FROM markov_chain WHERE word1 = 'これ' AND channel_id = "+ str(channel_id) +" ORDER BY random() LIMIT 1")
list1 = cur.fetchall()
w1 = list1[0][2]
w2 = list1[0][3]
sentence += w1 + w2
word_count = 0
loop_count = 3 # 最初の2単語+終了単語
while True:
if w1 == w2:
bRet = False
print ("失敗:w1 = w2" + sentence)
break
loop_count += 1
obj_len = -1
obj = None
sqlcom = "SELECT * FROM markov_chain WHERE word1 = '" + w1 + "' AND word2 = '" + w2 + "' ORDER BY random() LIMIT 1"
psgr.execute(sqlcom,[])
obj = psgr.fetchall()
obj_len = len(obj)
if not (obj_len > 0):
bRet = False
print ("失敗:not object" + sentence)
break
w3 = obj[0][4]
# if loop_count > 100:
# bRet = False
# print ("失敗:loop over" + sentence)
# break
# elif word_count > 100:
# bRet = False
# print ("失敗:word over" + sentence)
# break
w1, w2 = w2, w3
sentence += w3
word_count += 1
psgr.commit()
except Exception as e:
print (e)
psgr.rollback()
finally:
del psgr
return sentence
# 返却message作成
def make_response(message):
body = json.dumps({'message': message})
response = HTTPResponse(status=200, body=body)
response.set_header('Content-Type', 'application/json;charset=utf-8')
return response
if __name__ == '__main__':
# run(host='localhost', port=8080)
run(host='198.13.43.77', port=8080)
else:
run(host='198.13.43.77', port=8080)