質問編集履歴

5

ソースコード挿入

2018/09/26 13:16

投稿

1976kaca
1976kaca

スコア15

test CHANGED
File without changes
test CHANGED
@@ -38,6 +38,158 @@
38
38
 
39
39
 
40
40
 
41
+ app.pyのソースコードは以下です。
42
+
43
+
44
+
45
+ from flask import Flask, render_template, request
46
+
47
+ from wtforms import Form, TextAreaField, validators
48
+
49
+ import pickle
50
+
51
+ import sqlite3
52
+
53
+ import os
54
+
55
+ import numpy as np
56
+
57
+
58
+
59
+ from vectorizer import vect
60
+
61
+
62
+
63
+ app = Flask(__name__)
64
+
65
+
66
+
67
+ cur_dir = os.path.dirname(__file__)
68
+
69
+ clf = pickle.load(open(os.path.join(cur_dir, 'pkl_objects', 'classifier.pkl'),
70
+
71
+ 'rb'))
72
+
73
+ db = os.path.join(cur_dir, 'reviews.sqlite')
74
+
75
+
76
+
77
+ def classify(document):
78
+
79
+ label = {0: 'negative', 1: 'positive'}
80
+
81
+ X = vect.transform([document])
82
+
83
+ y = clf.predict(X)[0]
84
+
85
+ proba = clf.predict_proba(X).max()
86
+
87
+ return label[y], proba
88
+
89
+
90
+
91
+ def train(document, y):
92
+
93
+ X = vect.transform([document])
94
+
95
+ clf.partial_fit(X, [y])
96
+
97
+
98
+
99
+ def sqlite_entry(path, document, y):
100
+
101
+ conn = sqlite3.connect(path)
102
+
103
+ c = conn.cursor()
104
+
105
+ c.execute("INSERT INTO review_db (review, sentiment, date)"\
106
+
107
+ " VALUES (?, ?, DATETIME('now'))", (document, y))
108
+
109
+ conn.commit()
110
+
111
+ conn.close()
112
+
113
+
114
+
115
+ class ReviewForm(Form):
116
+
117
+ moviereview = TextAreaField('',
118
+
119
+ [validators.DataRequired(),
120
+
121
+ validators.length(min=15)])
122
+
123
+ @app.route('/')
124
+
125
+ def index():
126
+
127
+ form = ReviewForm(request.form)
128
+
129
+ return render_template('reviewform.html', form=form)
130
+
131
+
132
+
133
+ @app.route('/results', methods=['POST'])
134
+
135
+ def results():
136
+
137
+ form = ReviewForm(request.form)
138
+
139
+ if request.method == 'POST' and form.validate():
140
+
141
+ review = request.form['moviereview']
142
+
143
+ y, proba = classify(review)
144
+
145
+ return render_template('results.html',
146
+
147
+ content=review,
148
+
149
+ prediction=y,
150
+
151
+ probability=round(proba*100, 2))
152
+
153
+ return render_template('reviewform.html', form=form)
154
+
155
+
156
+
157
+ @app.route('/thanks', methods=['POST'])
158
+
159
+ def feedback():
160
+
161
+ feedback = request.form['feedback_button']
162
+
163
+ review = request.form['review']
164
+
165
+ prediction = request.form['prediction']
166
+
167
+
168
+
169
+ inv_label = {'negative': 0, 'positive': 1}
170
+
171
+ y = inv_label[prediction]
172
+
173
+ if feedback == 'Incorrect':
174
+
175
+ y = int(not(y))
176
+
177
+ train(review, y)
178
+
179
+ sqlite_entry(db, review, y)
180
+
181
+ return render_template('thanks.html')
182
+
183
+
184
+
185
+ if __name__ == '__main__':
186
+
187
+ app.run()
188
+
189
+
190
+
191
+
192
+
41
193
  ### 試したこと
42
194
 
43
195
 
@@ -75,151 +227,3 @@
75
227
  とフォルダーの配置などは同じにしてあるのですが、何が間違っているのでしょうか?
76
228
 
77
229
  教えていただければ幸いです。
78
-
79
- app.pyのコードは以下です。
80
-
81
-
82
-
83
- from flask import Flask, render_template, request
84
-
85
- from wtforms import Form, TextAreaField, validators
86
-
87
- import pickle
88
-
89
- import sqlite3
90
-
91
- import os
92
-
93
- import numpy as np
94
-
95
-
96
-
97
- from vectorizer import vect
98
-
99
-
100
-
101
- app = Flask(__name__)
102
-
103
-
104
-
105
- cur_dir = os.path.dirname(__file__)
106
-
107
- clf = pickle.load(open(os.path.join(cur_dir, 'pkl_objects', 'classifier.pkl'),
108
-
109
- 'rb'))
110
-
111
- db = os.path.join(cur_dir, 'reviews.sqlite')
112
-
113
-
114
-
115
- def classify(document):
116
-
117
- label = {0: 'negative', 1: 'positive'}
118
-
119
- X = vect.transform([document])
120
-
121
- y = clf.predict(X)[0]
122
-
123
- proba = clf.predict_proba(X).max()
124
-
125
- return label[y], proba
126
-
127
-
128
-
129
- def train(document, y):
130
-
131
- X = vect.transform([document])
132
-
133
- clf.partial_fit(X, [y])
134
-
135
-
136
-
137
- def sqlite_entry(path, document, y):
138
-
139
- conn = sqlite3.connect(path)
140
-
141
- c = conn.cursor()
142
-
143
- c.execute("INSERT INTO review_db (review, sentiment, date)"\
144
-
145
- " VALUES (?, ?, DATETIME('now'))", (document, y))
146
-
147
- conn.commit()
148
-
149
- conn.close()
150
-
151
-
152
-
153
- class ReviewForm(Form):
154
-
155
- moviereview = TextAreaField('',
156
-
157
- [validators.DataRequired(),
158
-
159
- validators.length(min=15)])
160
-
161
- @app.route('/')
162
-
163
- def index():
164
-
165
- form = ReviewForm(request.form)
166
-
167
- return render_template('reviewform.html', form=form)
168
-
169
-
170
-
171
- @app.route('/results', methods=['POST'])
172
-
173
- def results():
174
-
175
- form = ReviewForm(request.form)
176
-
177
- if request.method == 'POST' and form.validate():
178
-
179
- review = request.form['moviereview']
180
-
181
- y, proba = classify(review)
182
-
183
- return render_template('results.html',
184
-
185
- content=review,
186
-
187
- prediction=y,
188
-
189
- probability=round(proba*100, 2))
190
-
191
- return render_template('reviewform.html', form=form)
192
-
193
-
194
-
195
- @app.route('/thanks', methods=['POST'])
196
-
197
- def feedback():
198
-
199
- feedback = request.form['feedback_button']
200
-
201
- review = request.form['review']
202
-
203
- prediction = request.form['prediction']
204
-
205
-
206
-
207
- inv_label = {'negative': 0, 'positive': 1}
208
-
209
- y = inv_label[prediction]
210
-
211
- if feedback == 'Incorrect':
212
-
213
- y = int(not(y))
214
-
215
- train(review, y)
216
-
217
- sqlite_entry(db, review, y)
218
-
219
- return render_template('thanks.html')
220
-
221
-
222
-
223
- if __name__ == '__main__':
224
-
225
- app.run()

4

コード修正

2018/09/26 13:16

投稿

1976kaca
1976kaca

スコア15

test CHANGED
File without changes
test CHANGED
@@ -166,7 +166,7 @@
166
166
 
167
167
  return render_template('reviewform.html', form=form)
168
168
 
169
-
169
+
170
170
 
171
171
  @app.route('/results', methods=['POST'])
172
172
 

3

コード修正

2018/09/26 13:14

投稿

1976kaca
1976kaca

スコア15

test CHANGED
File without changes
test CHANGED
File without changes

2

文字化け修正

2018/09/26 13:12

投稿

1976kaca
1976kaca

スコア15

test CHANGED
File without changes
test CHANGED
File without changes

1

コードの追記

2018/09/26 13:08

投稿

1976kaca
1976kaca

スコア15

test CHANGED
File without changes
test CHANGED
@@ -75,3 +75,151 @@
75
75
  とフォルダーの配置などは同じにしてあるのですが、何が間違っているのでしょうか?
76
76
 
77
77
  教えていただければ幸いです。
78
+
79
+ app.pyのコードは以下です。
80
+
81
+
82
+
83
+ from flask import Flask, render_template, request
84
+
85
+ from wtforms import Form, TextAreaField, validators
86
+
87
+ import pickle
88
+
89
+ import sqlite3
90
+
91
+ import os
92
+
93
+ import numpy as np
94
+
95
+
96
+
97
+ from vectorizer import vect
98
+
99
+
100
+
101
+ app = Flask(__name__)
102
+
103
+
104
+
105
+ cur_dir = os.path.dirname(__file__)
106
+
107
+ clf = pickle.load(open(os.path.join(cur_dir, 'pkl_objects', 'classifier.pkl'),
108
+
109
+ 'rb'))
110
+
111
+ db = os.path.join(cur_dir, 'reviews.sqlite')
112
+
113
+
114
+
115
+ def classify(document):
116
+
117
+ label = {0: 'negative', 1: 'positive'}
118
+
119
+ X = vect.transform([document])
120
+
121
+ y = clf.predict(X)[0]
122
+
123
+ proba = clf.predict_proba(X).max()
124
+
125
+ return label[y], proba
126
+
127
+
128
+
129
+ def train(document, y):
130
+
131
+ X = vect.transform([document])
132
+
133
+ clf.partial_fit(X, [y])
134
+
135
+
136
+
137
+ def sqlite_entry(path, document, y):
138
+
139
+ conn = sqlite3.connect(path)
140
+
141
+ c = conn.cursor()
142
+
143
+ c.execute("INSERT INTO review_db (review, sentiment, date)"\
144
+
145
+ " VALUES (?, ?, DATETIME('now'))", (document, y))
146
+
147
+ conn.commit()
148
+
149
+ conn.close()
150
+
151
+
152
+
153
+ class ReviewForm(Form):
154
+
155
+ moviereview = TextAreaField('',
156
+
157
+ [validators.DataRequired(),
158
+
159
+ validators.length(min=15)])
160
+
161
+ @app.route('/')
162
+
163
+ def index():
164
+
165
+ form = ReviewForm(request.form)
166
+
167
+ return render_template('reviewform.html', form=form)
168
+
169
+
170
+
171
+ @app.route('/results', methods=['POST'])
172
+
173
+ def results():
174
+
175
+ form = ReviewForm(request.form)
176
+
177
+ if request.method == 'POST' and form.validate():
178
+
179
+ review = request.form['moviereview']
180
+
181
+ y, proba = classify(review)
182
+
183
+ return render_template('results.html',
184
+
185
+ content=review,
186
+
187
+ prediction=y,
188
+
189
+ probability=round(proba*100, 2))
190
+
191
+ return render_template('reviewform.html', form=form)
192
+
193
+
194
+
195
+ @app.route('/thanks', methods=['POST'])
196
+
197
+ def feedback():
198
+
199
+ feedback = request.form['feedback_button']
200
+
201
+ review = request.form['review']
202
+
203
+ prediction = request.form['prediction']
204
+
205
+
206
+
207
+ inv_label = {'negative': 0, 'positive': 1}
208
+
209
+ y = inv_label[prediction]
210
+
211
+ if feedback == 'Incorrect':
212
+
213
+ y = int(not(y))
214
+
215
+ train(review, y)
216
+
217
+ sqlite_entry(db, review, y)
218
+
219
+ return render_template('thanks.html')
220
+
221
+
222
+
223
+ if __name__ == '__main__':
224
+
225
+ app.run()