forked from yannguegan/marottes-medias-back
-
Notifications
You must be signed in to change notification settings - Fork 0
/
media_analyse.py
412 lines (359 loc) · 14.7 KB
/
media_analyse.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# coding: utf-8
# !/usr/bin/python3.6
# This script prepares a corpus of scraped stories (headline and intro)
# and send it to TextRazor to perform named entities recognition
# Libraries we need
import requests
import pandas as pd
import json
import time
import datetime
import re
import os
import textrazor
import unicodedata
from bs4 import BeautifulSoup
from pprint import pprint
from dateutil.parser import parse
print('')
print('/// Librairies correctly imported')
# Global variables we will use
context = 'dev'
print('Current environment:',context)
textrazor_api_key = '[TEXT-RAZOR-API-KEY]'
path_to_export = '[LOCAL-PATH]'
if context == 'prod':
path_to_export = '[SERVER-PATH]'
relevance_threshold = 0.3
confidence_threshold = 6
media_list_URL = '[GOOGLE-SHEET-CSV-URL]'
this_week = datetime.datetime.now().isocalendar()[1]
this_week = str(datetime.datetime.now().year) + '.' + str(this_week)
this_month = str(datetime.datetime.now().year) + '.' + str(datetime.datetime.now().month)
now = datetime.datetime.now()
this_day = now.date()
timespans = ['weekly', 'monthly', '7daysCurrent', '7daysPrevious']
print('This week is:', this_week)
print('This month is:', this_month)
print('This day is:', this_day)
print('')
print('/// Global variables defined')
# Clear all outputs
def clear_all_outputs():
os.system('clear')
clear_output(wait=True)
# Load media list
try:
media_list = pd.read_csv(media_list_URL, index_col='code')
if media_list['media'][1] == 'NaN':
print('Error when reading media list ! Using previously saved list')
media_list = pd.read_csv(path_to_export + 'csv/media_list.csv', index_col=0)
else:
media_list.to_csv(path_to_export + 'csv/media_list.csv', index_label="code")
except:
print('Error when loading media list ! Using previously saved list')
media_list = pd.read_csv(path_to_export + 'csv/media_list.csv', index_col=0)
media_list['code'] = media_list.index.values
media_list = media_list.reindex(media_list.index.rename('id'))
media_list = media_list[media_list['scrap'] == 'oui']
# Truncate list when in dev
if context == 'dev':
media_list = media_list[-1:]
# Prepare dictionary with media info
media = []
for row in media_list.iterrows():
medium = {
'code': row[1]['code'],
'name': row[1]['media'],
'domain': row[1]['domaine']
}
media.append(medium)
nb_media=len(media)
print('')
print('/// Media dictionary ready,', len(media), 'medias found')
# Load analysis status for each media
for i,medium in enumerate(media):
if context == 'prod':
clear_all_outputs()
print('Loading analysis status for medium',(i+1),'/',nb_media,":",medium['name'])
try:
medium_status_file = open(path_to_export + medium['code'] + '/status_analyse.json').read()
medium_status = json.loads(medium_status_file)
medium['status'] = medium_status
for key in ['nbStories7daysAnalysis', 'latestStoryDate']:
if key not in medium['status']:
medium['status'][key] = 'nc'
except:
print('No file found or file error for medium',(i+1),'/',nb_media,":",medium['name'])
medium['status'] = {
'nbStories7daysAnalysis': 'nc',
'latestStoryDate': 'nc'
}
print('')
print('')
print('/// Analysis status ready for all media')
# Load existing stories for each media
for i,medium in enumerate(media):
if context == 'prod':
clear_all_outputs()
print('Loading existing stories for medium',(i+1),'/',nb_media,":",medium['name'])
try:
medium_content_file = open(path_to_export + medium['code'] + '/stories/all.json').read()
medium_content = json.loads(medium_content_file)
medium['stories'] = medium_content
print(len(medium['stories']), 'stories found')
except:
print('No file found or file error for medium',(i+1),'/',nb_media,":",medium['name'])
medium['stories'] = []
print('')
print('')
print('/// Stories loaded for all media')
# Look for average description length
description_length = 0
for medium in media:
medium_description_length = 0
for story in medium['stories']:
medium_description_length += len(story['description'])
try:
medium_description_length = round(medium_description_length/len(medium['stories']))
# print(medium['name'],':',medium_description_length)
description_length += medium_description_length
except:
print('! No story found for:', medium['name'])
description_average = round(description_length / len(media))
print('')
print('// Average description length for all media :', description_average)
def prepareCorpus(medium, ts):
medium['analysis'][ts]['corpus'] = ''
medium['analysis'][ts]['nbStories'] = 0
for story in medium['stories']:
keep_story = False
# 7days analysis corpus
if ts == '7daysCurrent':
if story['age'] < 7:
keep_story = True
if ts == '7daysPrevious':
if story['age'] >= 7 and story['age'] < 13:
keep_story = True
# Weekly and monthly corpus
if ts == 'weekly':
if story['week'] == this_week:
keep_story = True
if ts == 'monthly':
if story['month'] == this_month:
keep_story = True
# Add story to related corpus
if keep_story == True:
medium['analysis'][ts]['corpus'] += story['title'] + ' ' + story['description'][:description_average]
medium['analysis'][ts]['nbStories'] += 1
# Truncate corpus
medium['analysis'][ts]['corpus'] = medium['analysis'][ts]['corpus'].replace('\n',' ')
corpus_length = len(medium['analysis'][ts]['corpus'])
medium['analysis'][ts]['corpusLength'] = corpus_length
medium['analysis'][ts]['corpusTooLong'] = False
if corpus_length > 180000:
medium['analysis'][ts]['corpusTooLong'] = True
medium['analysis'][ts]['corpus'] = medium['analysis'][ts]['corpus'][:180000]
return medium
def last_day_of_month(any_day):
next_month = any_day.replace(day=28) + datetime.timedelta(days=4)
return next_month - datetime.timedelta(days=next_month.day)
last_month_day = last_day_of_month(now).date()
# Prepare analysis
print('Last month day:', last_month_day)
print('Today:', this_day)
print('Weekday:', datetime.datetime.today().weekday())
print('')
for m, medium in enumerate(media):
if context == 'prod':
clear_all_outputs()
print('Prepare analysis for medium #', m+1,'/',nb_media,':', medium['name'])
# Prepare dict
medium['analysis'] = {}
for ts in timespans:
medium['analysis'][ts] = {}
# Sort stories
medium['stories'].sort(key=lambda x: x['date'], reverse=True)
# Prepare true date and age
stories_with_age = []
for story in medium['stories']:
try:
story['trueDate'] = parse(story['date'], ignoretz=True)
story['age'] = (now - story['trueDate']).days
stories_with_age.append(story)
except:
pass
medium['stories'] = stories_with_age
latest_story_date = medium['stories'][0]['date']
print('Latest story date:', latest_story_date)
# Prepare corpus for 7days current analysis (has to be done before anything else
# to get nb of stories in corpus and decide to run analysis or not)
medium = prepareCorpus(medium, '7daysCurrent')
# Check if analysis has to be made
for ts in timespans:
medium['analysis'][ts]['run'] = False
# Perform 7 days analysis if new stories found
if str(latest_story_date) != medium['status']['latestStoryDate'] or context == 'dev':
medium['analysis']['7daysCurrent']['run'] = True
medium['analysis']['7daysPrevious']['run'] = True
medium = prepareCorpus(medium, '7daysPrevious')
print('7days analysis will be performed (current and previous)')
medium['status']['latestStoryDate'] = latest_story_date
# Perform monthly analysis if today is last day of the month
if this_day == last_month_day or context == 'dev':
medium['analysis']['monthly']['run'] = True
medium = prepareCorpus(medium, 'monthly')
print('Monthly analysis will be performed')
# Perform monthly analysis if today is saturday
if datetime.datetime.today().weekday() == 5 or context == 'dev':
medium['analysis']['weekly']['run'] = True
medium = prepareCorpus(medium, 'weekly')
print('Weekly analysis will be performed')
print('')
print('')
print('/// All analysis have been prepared')
# Perform analysis
textrazor.api_key = textrazor_api_key
client = textrazor.TextRazor(extractors=["entities"])
def do_TextRazor_Analysis(corpus):
print('')
print('Corpus start with:')
print(corpus[0:500])
response = {}
if context == 'prod' or context == 'dev':
try:
response = client.analyze(corpus)
except:
print('!!! TextRazor analysis has failed')
#if context == 'dev':
# print('I am doing the analyse ! Just kidding…')
return response
def normalize(term):
normalized = ''.join((c for c in unicodedata.normalize('NFD', term) if unicodedata.category(c) != 'Mn'))
normalized = re.sub(r'( |\(|\)|\'|"|\.|;|\:|\?|!)', '', normalized).lower()
return normalized
def get_entities_list(response):
entities_list = []
entities = list(response.entities())
entities.sort(key=lambda x: x.relevance_score, reverse=True)
seen = []
for y,entity in enumerate(entities):
if entity.id not in seen:
seen.append(entity.id)
medium_entity = {
'code': normalize(entity.id),
'name': entity.id,
'wikipedia': entity.wikipedia_link,
'relevance': entity.relevance_score,
'confidence': entity.confidence_score,
'terms': []
}
medium_entity['terms'].append(entity.matched_text)
if medium_entity['relevance'] > 0.2:
if len(entities_list) <= 20:
print(y,'/',medium_entity['name'],'(',medium_entity['relevance'],'/ 1)')
entities_list.append(medium_entity)
else:
term = entity.matched_text
for current_entity in entities_list:
if current_entity['name'] == entity.id:
if term not in current_entity['terms']:
current_entity['terms'].append(term)
return(entities_list)
def save_analyse(analysis, code, ts):
del analysis['response']
del analysis['run']
del analysis['corpus']
# pprint(analysis, depth=2)
export_paths = []
if ts == 'monthly':
export_paths.append({
'directory': 'analysesByMonth',
'filename' : this_month + '.json'
})
if ts == 'weekly':
export_paths.append({
'directory': 'analysesByWeek',
'filename' : this_week + '.json'
})
if ts == '7daysCurrent':
export_paths.append({
'directory': 'analysesBy7days',
'filename' : 'current.json'
})
export_paths.append({
'directory': 'analysesByDay',
'filename' : str(this_day) + '.json'
})
if ts == '7daysPrevious':
export_paths.append({
'directory': 'analysesBy7days',
'filename' : 'previous.json'
})
for export_path in export_paths:
with open(path_to_export + code + '/' + export_path['directory'] + '/' + export_path['filename'], 'w+') as fp:
json.dump(analysis, fp)
print('Analyse files for media', code, 'saved')
def save_status(medium):
medium['status']['nbStories7daysAnalysis'] = medium['analysis']['7daysCurrent']['nbStories']
for el in ['lastAnalyse', 'nbEntities' , 'corpusLength' , 'corpusTooLong' , 'nbStories' , 'entities']:
try:
medium['status'][el] = medium['analysis']['7daysCurrent'][el]
except:
medium['status'][el] = 'nc'
with open(path_to_export + medium['code'] + '/status_analyse.json', 'w+') as fp:
json.dump(medium['status'], fp)
print('Status saved for medium:', medium['name'])
for medium in media:
if context == 'prod':
clear_all_outputs()
for ts in timespans:
if medium['analysis'][ts]['run'] == True:
medium['analysis'][ts]['response'] = {}
print(medium['name'],': analysing',ts,'corpus')
if medium['analysis'][ts]['nbStories'] > 0:
medium['analysis'][ts]['response'] = do_TextRazor_Analysis(medium['analysis'][ts]['corpus'])
else:
print('! No stories in corpus')
if medium['analysis'][ts]['response']:
print('')
print('Entities:')
medium['analysis'][ts]['entities'] = get_entities_list(medium['analysis'][ts]['response'])
medium['analysis'][ts]['lastAnalyse'] = str(this_day)
medium['analysis'][ts]['nbEntities'] = len(medium['analysis'][ts]['entities'])
print('')
save_analyse(medium['analysis'][ts], medium['code'], ts)
if ts == '7daysCurrent':
save_status(medium)
print('')
print('')
print('/// All analysis have been performed')
# Write CSV file with status info for 7days analysis
index = media_list.index
columns = ['name','lastAnalyse', 'nbEntities' , 'corpusLength' , 'corpusTooLong' , 'nbStories' , 'entities']
table = pd.DataFrame(index=index, columns=columns)
for medium in media:
analysis = medium['status']
for col in ['name']:
table.loc[medium['code'], col] = medium[col]
for col in ['lastAnalyse', 'corpusLength' , 'corpusTooLong' , 'nbStories', 'nbEntities']:
try:
table.loc[medium['code'], col] = analysis[col]
except:
table.loc[medium['code'], col] = 'nc'
for col in ['entities']:
entities_string = ''
entities_count = 0
try:
for entity in analysis['entities']:
if entity['confidence'] > confidence_threshold and entity['relevance'] > relevance_threshold:
if entities_count > 0:
entities_string += ' | '
entities_count += 1
entities_string += entity['name'] + ' (' + str(entity['relevance']) + ', ' + str(entity['confidence']) + ')'
table.loc[medium['code'], col] = entities_string
except:
table.loc[medium['code'], col] = 'nc'
table.to_csv(path_to_export + 'csv/status.csv' ,index_label='code')
print('')
print('/// CSV status file saved')