-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
377 lines (334 loc) · 16.1 KB
/
app.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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
from flask import Flask, render_template, request, redirect, url_for, jsonify, send_file
import pandas as pd
import subprocess
import re
from apscheduler.schedulers.background import BackgroundScheduler
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import io
app = Flask(__name__)
current_target='上海商銀'
# Path to your CSV file
csv_file_path = f"{current_target}.csv"
# periodically refresh data
def refresh_data_scheduler():
try:
result = subprocess.run(['python3', 'data_gathering/web_scrape.py'],
input=current_target,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
check=True)
output = result.stdout
if "No news articles found. Exiting the program." in output:
print("Scheduler: No new articles found.")
return
# subprocess.run(['python3', 'data_gathering/data_cleaning.py'], check=True)
process_keyword=subprocess.Popen(['python3', 'data_gathering/keywords_generate.py'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
stdout, stderr = process_keyword.communicate(input=current_target)
print(stdout)
if process.returncode == 0:
process_sentiment = subprocess.run(['python3', 'data_gathering/sentiment_analysis.py'], input=current_target, capture_output=True, text=True, check=True)
print(process_sentiment.stdout)
process_topic = subprocess.run(['python3', 'data_gathering/topic_assign.py'], capture_output=True, check=True)
print(process_topic.stdout)
process_update = subprocess.run(['python3', 'data_gathering/update_finalCSV.py'], input=current_target, capture_output=True, text=True, check=True)
print(process_update.stdout)
global data
data = read_data(csv_file_path)
return
except subprocess.CalledProcessError as e:
print(f"Error refreshing data: {e}")
return
def schedule_jobs():
scheduler = BackgroundScheduler()
scheduler.add_job(refresh_data_scheduler, 'cron', hour=9, minute=10)
scheduler.start()
def convert_date_format(date_str):
# Use regular expression to extract year, month, and day
match = re.match(r"(\d{4})年(\d{2})月(\d{2})日", date_str)
if match:
year, month, day = match.groups()
# Return date in yyyy-mm-dd format
return f"{year}-{month}-{day}"
else:
return date_str
def read_data(csv_file_path):
try:
data = pd.read_csv(csv_file_path)
topic_map = {
1: "商業", # Business
2: "市場", # Markets
3: "可持續性", # Sustainability
4: "法律", # Legal
5: "技術" # Technology
}
data['topic_description'] = data['topic'].map(topic_map)
data['title'] = data.apply(lambda row: f'<a href="{row["link"]}" target="_blank">{row["title"]}</a>', axis=1)
data['snippet'] = data.apply(lambda row: f'{row["snippet"]} <a href="#" class="keyword-link" data-keyword="{" ".join([i.strip() for i in row['keywords'].replace('\'','').strip('][').split(",")])}">{"/ ".join([i.strip() for i in row['keywords'].replace('\'','').strip('][').split(",")])}</a>', axis=1)
data['date'] = pd.to_datetime(data['date'].apply(convert_date_format))
return data
except Exception as e:
print(f"Error reading data: {e}")
return pd.DataFrame()
@app.route('/')
def index():
display_data=data.copy()
display_data.drop(columns=['topic_description', 'topic','link', 'title_translated','content', 'keywords'],inplace=True)
# Add classes to the columns
display_data.columns = [
'<th class="label-col">label</th>',
'<th class="title-col">title</th>',
'<th class="snippet-col">snippet</th>',
'<th class="source-col">source</th>',
'<th class="date-col">date</th>',
]
data_html = display_data.to_html(classes='table table-striped', escape=False, index=False)
return render_template('index.html', table=data_html, topics=unique_topics, current_target=current_target, sentiments=unique_sentiments)
@app.route('/cancel', methods=['POST'])
def cancel():
global process
process.terminate()
process.wait()
return redirect(url_for('index'))
@app.route('/filter', methods=['POST'])
def filter_data():
topic = request.form.get('topic')
sentiment = request.form.get('sentiment')
search = request.form.get('search')
start_date = request.form.get('start_date')
end_date = request.form.get('end_date')
filtered_data = data.copy()
if topic:
filtered_data = filtered_data[filtered_data['topic_description'] == topic]
if sentiment:
filtered_data = filtered_data[filtered_data['label'] == sentiment]
if search:
search_terms = search.lower().split()
filtered_data = filtered_data[filtered_data.apply(lambda row: any(term in row['title'].lower() or term in row['snippet'].lower() for term in search_terms), axis=1)]
if start_date:
filtered_data = filtered_data[filtered_data['date'] >= start_date]
if end_date:
filtered_data = filtered_data[filtered_data['date'] <= end_date]
display_data = filtered_data.drop(columns=['topic_description', 'topic', 'link', 'title_translated', 'content', 'keywords'])
data_html = display_data.to_html(classes='table table-striped', escape=False, index=False)
return data_html
@app.route('/refresh', methods=['POST'])
def refresh_data():
try:
global data
global current_target
target = request.form.get('target', current_target)
csv_file_path = target+'.csv'
print(f"Refreshing data for target: {target}")
global process
print("Looking for news articles...")
process = subprocess.Popen(
['python3', 'data_gathering/web_scrape.py'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
stdout, stderr = process.communicate(input=target)
# subprocess.run(['python3', 'data_gathering/data_cleaning.py'], check=True)
# if process.returncode != 0:
# raise subprocess.CalledProcessError(process.returncode, process.args, output=stdout, stderr=stderr)
print(stdout)
if process.returncode != 0:
print(stderr)
return redirect(url_for('index'))
if "No news articles found. Exiting the program." in stdout:
try:
data = read_data(csv_file_path)
except Exception as e:
print(f"No articles found and no such file exists: {csv_file_path}")
current_target = target
return redirect(url_for('index'))
print("Generating keywords...")
# subprocess.run(['python3', 'data_gathering/data_cleaning.py'], check=True)
process=subprocess.Popen(['python3', 'data_gathering/keywords_generate.py'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
stdout, stderr = process.communicate(input=target)
print(stdout)
if process.returncode == 0:
print("Analyzing sentiment...")
process_sentiment = subprocess.run(['python3', 'data_gathering/sentiment_analysis.py'], input=target, capture_output=True, text=True, check=True)
print(process_sentiment.stdout)
print("Assigning topics...")
process_topic = subprocess.run(['python3', 'data_gathering/topic_assign.py'], capture_output=True, check=True)
print(process_topic.stdout)
print("Updating target CSV...")
process_update = subprocess.run(['python3', 'data_gathering/update_finalCSV.py'], input=target, capture_output=True, text=True, check=True)
print(process_update.stdout)
else:
print(stderr)
current_target = target
data = read_data(csv_file_path)
return redirect(url_for('index'))
except subprocess.CalledProcessError as e:
print(f"Error refreshing data: {e}")
return jsonify({"error": "Failed to refresh data"}), 500
@app.route('/get_sentiment_data', methods=['POST'])
def get_sentiment_data():
topic = request.form.get('topic')
sentiment = request.form.get('sentiment')
search = request.form.get('search')
start_date = request.form.get('start_date')
end_date = request.form.get('end_date')
filtered_data = data.copy()
if topic:
filtered_data = filtered_data[filtered_data['topic_description'] == topic]
if sentiment:
filtered_data = filtered_data[filtered_data['label'] == sentiment]
if search:
search_terms = search.lower().split()
filtered_data = filtered_data[filtered_data.apply(lambda row: any(term in row['title'].lower() or term in row['snippet'].lower() for term in search_terms), axis=1)]
if start_date:
filtered_data = filtered_data[filtered_data['date'] >= start_date]
if end_date:
filtered_data = filtered_data[filtered_data['date'] <= end_date]
sentiment_counts = filtered_data['label'].value_counts().reindex(['neutral', 'positive', 'negative'], fill_value=0)
sentiment_data = {
'neutral': int(sentiment_counts['neutral']),
'positive': int(sentiment_counts['positive']),
'negative': int(sentiment_counts['negative'])
}
print(sentiment)
return jsonify(sentiment_data)
@app.route('/get_trend_data', methods=['POST'])
def get_trend_data():
topic = request.form.get('topic')
sentiment = request.form.get('sentiment')
search = request.form.get('search')
start_date = request.form.get('start_date')
end_date = request.form.get('end_date')
time_frame = request.form.get('timeFrame', 'M') # Default to monthly
filtered_data = data.copy()
if topic:
filtered_data = filtered_data[filtered_data['topic_description'] == topic]
if sentiment:
filtered_data = filtered_data[filtered_data['label'] == sentiment]
if search:
search_terms = search.lower().split()
filtered_data = filtered_data[filtered_data.apply(lambda row: any(term in row['title'].lower() or term in row['snippet'].lower() for term in search_terms), axis=1)]
if start_date:
filtered_data = filtered_data[filtered_data['date'] >= start_date]
if end_date:
filtered_data = filtered_data[filtered_data['date'] <= end_date]
if 'date' not in filtered_data.columns:
return jsonify({"error": "Date column not found in the data"}), 500
if time_frame == 'D':
resample_rule = 'D'
time_format = '%Y-%m-%d'
elif time_frame == 'W':
resample_rule = 'W-MON'
time_format = '%Y-%W'
else:
resample_rule = 'ME'
time_format = '%Y-%m'
filtered_data.set_index('date', inplace=True)
monthly_counts = filtered_data.resample(resample_rule).size().rename('total').to_frame()
monthly_counts['neutral'] = filtered_data[filtered_data['label'] == 'neutral'].resample(resample_rule).size()
monthly_counts['positive'] = filtered_data[filtered_data['label'] == 'positive'].resample(resample_rule).size()
monthly_counts['negative'] = filtered_data[filtered_data['label'] == 'negative'].resample(resample_rule).size()
monthly_counts['total'] = monthly_counts['positive'] - monthly_counts['negative']
monthly_counts.fillna(0, inplace=True)
monthly_counts = monthly_counts.astype(int)
trend_data = {
'months': monthly_counts.index.strftime(time_format).tolist(),
'neutral_counts': monthly_counts['neutral'].tolist(),
'positive_counts': monthly_counts['positive'].tolist(),
'negative_counts': monthly_counts['negative'].tolist(),
'total_counts': monthly_counts['total'].tolist()
}
return jsonify(trend_data)
def generate_wordcloud(data):
word_list = []
for _, row in data.iterrows():
val = row['keywords']
words = [i.strip() for i in val.replace('\'','').strip('][').split(",")]
word_list.extend(words)
word_freq = pd.Series(word_list).value_counts().to_dict()
font_path = 'data_gathering/Font/STHeiti Light.ttc' # 字型路徑
#文字雲繪製參數設定
wc = WordCloud(
background_color='black', # 背景顏色
max_words=500, # 最大分詞數量
max_font_size=None, # 顯示字體的最大值
font_path=font_path, # 若為中文則需引入中文字型(.TTF)
random_state=None, # 隨機碼生成各分詞顏色
prefer_horizontal=0.9) # 調整分詞中水平和垂直的比例
wc.generate_from_frequencies(word_freq)
# Save the word cloud image to a BytesIO object
img = io.BytesIO()
wc.to_image().save(img, format='PNG')
img.seek(0)
return img
@app.route('/filtered_wordcloud', methods=['GET'])
def filtered_wordcloud():
topic = request.args.get('topic')
sentiment = request.args.get('sentiment')
search = request.args.get('search')
start_date = request.args.get('start_date')
end_date = request.args.get('end_date')
filtered_data = data.copy()
if topic:
filtered_data = filtered_data[filtered_data['topic_description'] == topic]
if sentiment:
filtered_data = filtered_data[filtered_data['label'] == sentiment]
if search:
search_terms = search.lower().split()
filtered_data = filtered_data[filtered_data.apply(lambda row: any(term in row['title'].lower() or term in row['snippet'].lower() for term in search_terms), axis=1)]
if start_date:
filtered_data = filtered_data[filtered_data['date'] >= start_date]
if end_date:
filtered_data = filtered_data[filtered_data['date'] <= end_date]
img = generate_wordcloud(filtered_data)
return send_file(img, mimetype='image/png')
if __name__ == '__main__':
try:
print("Looking for news articles...")
process = subprocess.Popen(
['python3', 'data_gathering/web_scrape.py'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
stdout, stderr = process.communicate(input=current_target)
print(stdout)
# subprocess.run(['python3', 'data_gathering/data_cleaning.py'], check=True)
if process.returncode == 0 and "No news articles found. Exiting the program." not in stdout:
process_keyword=subprocess.Popen(['python3', 'data_gathering/keywords_generate.py'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
stdout, stderr = process_keyword.communicate(input=current_target)
print(stdout)
if process.returncode == 0:
process_sentiment = subprocess.run(['python3', 'data_gathering/sentiment_analysis.py'], input=current_target, capture_output=True, text=True, check=True)
print(process_sentiment.stdout)
process_topic = subprocess.run(['python3', 'data_gathering/topic_assign.py'], capture_output=True, text=True, check=True)
print(process_topic.stdout)
process_update = subprocess.run(['python3', 'data_gathering/update_finalCSV.py'], input=current_target, capture_output=True, text=True, check=True)
print(process_update.stdout)
data = read_data(csv_file_path)
unique_topics = data['topic_description'].unique()
unique_sentiments = data['label'].unique()
schedule_jobs()
app.run(host='0.0.0.0', port=4000, debug=False)
except subprocess.CalledProcessError as e:
print(f"Error initializing application: {e}")
exit(1)