-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
259 lines (227 loc) · 7.52 KB
/
main.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 19 01:23:48 2018
@author: billxu
"""
import sys
import glob
import random
import pycrfsuite
import crf
import util
import datetime
import json
from urllib.parse import unquote
from flask import Flask, jsonify
from flask import render_template
from config import DevConfig
from flask import request
from flask import abort
from flask import render_template
from flask_cors import CORS
# 初始化 Flask 類別成為 instance
app = Flask(__name__)
CORS(app)
cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
app.config.from_object(DevConfig)
tasks = [
{
'id': 1,
'title': u'Buy groceries',
'description': u'Milk, Cheese, Pizza, Fruit, Tylenol',
'done': False
},
{
'id': 2,
'title': u'Learn Python',
'description': u'Need to find a good Python tutorial on the web',
'done': False
}
]
# 路由和處理函式配對
@app.route('/')
def index():
return render_template('index.html')
# 判斷自己執行非被當做引入的模組,因為 __name__ 這變數若被當做模組引入使用就不會是 __main__
if __name__ == '__main__':
app.run()
@app.route('/crftag', methods=['POST'])
def predic():
charstop = True # True means label attributes to previous char
features = 3 # 1=discrete; 2=vectors; 3=both
dictfile = 'vector/24scbow50.txt'
modelname = 'datalunyu5001.m'
vdict = util.readvec(dictfile)
inputtext = request.form.get('input_text','')
#li = [line for line in util.text_to_lines(inputtext)]
li = util.text_to_lines(inputtext)
print(li)
data = []
for line in li:
x, y = util.line_toseq(line, charstop)
print(x)
if features == 1:
d = crf.x_seq_to_features_discrete(x, charstop), y
elif features == 2:
d = crf.x_seq_to_features_vector(x, vdict, charstop), y
elif features == 3:
d = crf.x_seq_to_features_both(x, vdict, charstop), y
data.append(d)
tagger = pycrfsuite.Tagger()
tagger.open(modelname)
print ("Start testing...")
results = []
lines = []
Spp = []
Npp = []
#while data:
for index in range(len(data)):
print(len(data))
xseq, yref = data.pop(0)
yout = tagger.tag(xseq)
sp = 0
np = 0
for i in range(len(yout)):
sp = tagger.marginal('S',i)
Spp.append(sp) #S標記的機率
print(sp)
np = tagger.marginal('N',i)
Npp.append(np)#Nㄅ標記的機率
print(np)
results.append(util.eval(yref, yout, "S"))
lines.append(util.seq_to_line([x['gs0'] for x in xseq],yout,charstop,Spp,Npp))
#print(util.seq_to_line([x['gs0'] for x in xseq], (str(sp) +'/'+ str(np)),charstop))
U_score = 0
p_Scount = 0
p_Ncount = 0
for i in range(len(Spp)):
_s = 0
if Spp[i] > Npp[i]:
_s = Spp[i]
else :_s = Npp[i]
_s = (_s - 0.5) * 10
U_score = U_score + _s
p_Scount = p_Scount + Spp[i]
p_Ncount = p_Ncount + Npp[i]
tp, fp, fn, tn = zip(*results)
tp, fp, fn, tn = sum(tp), sum(fp), sum(fn), sum(tn)
p, r = tp/(tp+fp), tp/(tp+fn)
score = ''
score = score + '<br>' + "Total tokens in Test Set:" + repr(tp+fp+fn+tn)
score = score + '<br>' + "Total S in REF:" + repr(tp+fn)
score = score + '<br>' + "Total S in OUT:" + repr(tp+fp)
score = score + '<br>' + "Presicion:" + repr(p)
score = score + '<br>' + "Recall:" + repr(r)
score = score + '<br>' + "*******************F1-score:" + repr(2*p*r/(p+r))
score = score + '<br>' + "======================="
score = score + '<br>' + "character count:" + str(len(Spp))
score = score + '<br>' + "block uncertain rate:" + str((U_score / len(Spp)))
output = ''
key = 0
for line in lines:
#print (line.encode('utf8'))
output = output + '<br>' + line
#print (line)
key = key + 1
#for index_m in ypp:
# output = output + '<br>' + line
output = score + '<br>' + output
return (output)
#API用預測
def predic_api(inputtext):
charstop = True # True means label attributes to previous char
features = 3 # 1=discrete; 2=vectors; 3=both
dictfile = 'vector/24scbow50.txt'
modelname = 'datalunyu5001.m'
vdict = util.readvec(dictfile)
inputtext = inputtext
#li = [line for line in util.text_to_lines(inputtext)]
li = util.text_to_lines(inputtext)
print(li)
data = []
for line in li:
x, y = util.line_toseq(line, charstop)
print(x)
if features == 1:
d = crf.x_seq_to_features_discrete(x, charstop), y
elif features == 2:
d = crf.x_seq_to_features_vector(x, vdict, charstop), y
elif features == 3:
d = crf.x_seq_to_features_both(x, vdict, charstop), y
data.append(d)
tagger = pycrfsuite.Tagger()
tagger.open(modelname)
print ("Start testing...")
results = []
lines = []
Spp = []
Npp = []
out = []
#while data:
for index in range(len(data)):
print(len(data))
xseq, yref = data.pop(0)
yout = tagger.tag(xseq)
sp = 0
np = 0
for i in range(len(yout)):
sp = tagger.marginal('S',i)
Spp.append(sp) #S標記的機率
print(sp)
np = tagger.marginal('N',i)
Npp.append(np)#Nㄅ標記的機率
print(np)
results.append(util.eval(yref, yout, "S"))
lines.append(util.seq_to_line([x['gs0'] for x in xseq],yout,charstop,Spp,Npp))
#print(util.seq_to_line([x['gs0'] for x in xseq], (str(sp) +'/'+ str(np)),charstop))
out.append(yout)
tp, fp, fn, tn = zip(*results)
tp, fp, fn, tn = sum(tp), sum(fp), sum(fn), sum(tn)
p, r = tp/(tp+fp), tp/(tp+fn)
score = ''
score = score + '<br>' + "Total tokens in Test Set:" + repr(tp+fp+fn+tn)
score = score + '<br>' + "Total S in REF:" + repr(tp+fn)
score = score + '<br>' + "Total S in OUT:" + repr(tp+fp)
score = score + '<br>' + "Presicion:" + repr(p)
score = score + '<br>' + "Recall:" + repr(r)
score = score + '<br>' + "*******************F1-score:" + repr(2*p*r/(p+r))
output = ''
print (lines)
for line in lines:
#line = unquote(line)
print ("output:")
print (line.encode('utf8'))
#output = output + '<br>' + line
output += line
print (line)
output = score + '<br>' + output
#output = jsonify({'str': output})
return (out)
@app.route('/signUp')
def signUp():
return render_template('signUp.html')
@app.route('/signUpUser', methods=['POST'])
def signUpUser():
#user = request.form['username'];
#password = request.form['password'];
text = request.form['input_text']
res = predic_api(text)
return json.dumps({'status':'OK','data':res});
@app.route('/preseg', methods=['POST'])
def preseg():
#user = request.form['username'];
#password = request.form['password'];
text = request.form['input_text']
res = predic_api(text)
return json.dumps({'status':'OK','data':res});
@app.route('/api/str/<string:inputtext>', methods=['GET'])
def get_task(inputtext):
task = list(filter(lambda t: t['id'] == inputtext, tasks))
#if len(task) == 0:
# abort(404)
res = predic_api(inputtext)
#return jsonify({'task': task[0]})
return (res)
if __name__ == '__main__':
app.run(host='127.0.0.1', debug=True)