-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassyFire.py
More file actions
200 lines (178 loc) · 6.67 KB
/
ClassyFire.py
File metadata and controls
200 lines (178 loc) · 6.67 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
import plotly.plotly as py
import plotly.graph_objs as go
import plotly
from pymongo import MongoClient
import pandas as pd
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import re
import datetime as dt
#Connexion Server
client = MongoClient('127.0.0.1',27017)
db = client.DataProject
path = "bags_of_words/"
######### Creating Bag of words ########################
politicians = ["Arthaud","Fillon","Hamon","Hollande","Sarkozy","Le Pen","Macron","Melenchon","Poutou","Valls","Cheminade","Aignan","Asselineau","Lassalle"]
bag_of_politicians = dict()
for politician in politicians:
bag_path = path + politician + ".txt"
with open(bag_path,'r', encoding='ISO-8859-1') as f:
bag_politician = f.read().split(',')
bag_of_politicians[politician] = bag_politician
########################################################
def toDate(date):
return dt.datetime.strptime(date, "%Y,%m,%d")
def toString(date):
return dt.datetime.strftime(date, "%Y,%m,%d")
########################################################
def classify(text):
#Initializing
pts_politicians =pd.Series(0,index=politicians)
for politician in politicians:
pts_politician = 0
for word in bag_of_politicians[politician]:
pts_politician += len(re.findall(word, text.lower()))
#pts_politician += sum(1 for _ in re.finditer(r'\b%s\b' % re.escape(word), text.lower()))
pts_politicians[politician] = pts_politician
#Normalizating to a probability vector
psum = pts_politicians.sum()
proba = pts_politicians
if psum !=0:
proba /=psum
return proba
#######################################
def classy_fire(article,alpha = 0.6):
ptitle = classify(article['Title'])
pcontent = classify(article['Content'])
return alpha*ptitle + (1-alpha)*pcontent
#########################################
def number_of_articles_date(source,date,threshold=0.2):
#Init
number_of_articles = pd.Series(0,index = politicians)
#Extracting Articles
articles = list(db.Articles.find({'Source':source,'Date':date},{'Title':1,'Content':1,'_id':0}))
for ID in range(len(articles)):
if (classy_fire(articles[ID]).max()) > threshold:
number_of_articles.ix[classy_fire(articles[ID]).argmax()] += 1
return number_of_articles
########################################################
def number_of_articles(source,dateBegin,dateEnd,Candidates=["Fillon","Macron","Le Pen","Melenchon","Hamon"]):
dateBegin = toDate(dateBegin)
dateEnd = toDate(dateEnd)
delta = dateEnd-dateBegin
df = pd.DataFrame()
for i in range(delta.days + 1):
date = toString(dateBegin + dt.timedelta(days=i))
df[date]=number_of_articles_date(source,date)
return df.transpose()[Candidates]
########################################################
def article_candidate(dateBegin,dateEnd,Candidate,Sources=["LeMonde","LeFigaro","France24"]):
dateBegin = toDate(dateBegin)
dateEnd = toDate(dateEnd)
delta = dateEnd-dateBegin
dflist = list()
for i in range(delta.days + 1):
date = toString(dateBegin + dt.timedelta(days=i))
article_day = dict.fromkeys(Sources)
article_day['date'] = date
for source in Sources:
article_day[source]= number_of_articles_date(source,date)[Candidate]
dflist.append(article_day)
df = pd.DataFrame(dflist).set_index('date')
return df
def graph_media(source, dateBegin='2017,03,28', dateEnd='2017,04,23'):
df = number_of_articles(source,dateBegin,dateEnd)
data_1 = go.Scatter(
x= [toDate(x) for x in df.index.values],
y= df['Fillon'],
mode = 'lines',
name = 'Fillon'
)
data_2 = go.Scatter(
x= [toDate(x) for x in df.index.values],
y= df['Hamon'],
mode = 'lines',
name = 'Hamon'
)
data_3 = go.Scatter(
x= [toDate(x) for x in df.index.values],
y= df['Le Pen'],
mode = 'lines',
name = 'Le Pen'
)
data_4 = go.Scatter(
x= [toDate(x) for x in df.index.values],
y= df['Macron'],
mode = 'lines',
name = 'Macron'
)
data_5 = go.Scatter(
x= [toDate(x) for x in df.index.values],
y= df['Melenchon'],
mode = 'lines',
name = 'Melenchon'
)
if source == 'LeFigaro':
name_newspaper = 'le Figaro'
elif source == 'LeMonde':
name_newspaper='le Monde'
elif source == 'France24':
name_newspaper = 'France 24'
layout = go.Layout(
title = 'Number of articles for {}'.format(name_newspaper),
xaxis = dict(title='Date'),
yaxis = dict(title = 'Number of Articles')
)
data = [data_1, data_2, data_3, data_4, data_5]
fig = go.Figure(data=data, layout=layout)
return plotly.offline.plot(fig,filename='templates/number_articles_media_{}.html'.format(source),auto_open=False)
def graph_candidate(Candidate, dateBegin='2017,03,28', dateEnd='2017,04,23'):
df = article_candidate(dateBegin,dateEnd,Candidate)
data_1 = go.Scatter(
x= [toDate(x) for x in df.index.values],
y= df['LeMonde'],
mode = 'lines',
name = 'Le Monde'
)
data_2 = go.Scatter(
x= [toDate(x) for x in df.index.values],
y= df['LeFigaro'],
mode = 'lines',
name = 'Le Figaro'
)
data_3 = go.Scatter(
x= [toDate(x) for x in df.index.values],
y= df['France24'],
mode = 'lines',
name = 'France 24'
)
layout = go.Layout(
title = 'Number of Articles for {}'.format(Candidate),
xaxis = dict(title='Date'),
yaxis = dict(title = 'Number of Articles')
)
data = [data_1, data_2, data_3]
fig = go.Figure(data=data, layout=layout)
return plotly.offline.plot(fig,filename='templates/number_articles_candidate_{}.html'.format(Candidate),auto_open=False)
def graph_total():
S = pd.Series()
df= number_of_articles('LeFigaro',dateBegin='2017,03,28', dateEnd='2017,04,23')
S = df.sum(axis=1)
for source in ['LeMonde', 'France24']:
df = number_of_articles(source,dateBegin='2017,03,28', dateEnd='2017,04,23')
S+=df.sum(axis=1)
data_1 = go.Scatter(
x= [toDate(x) for x in df.index.values],
y= S.values,
mode = 'lines',
name = 'Total number'
)
layout = go.Layout(
title = 'Total number of articles written per day',
xaxis = dict(title='Date'),
yaxis = dict(title = 'Number of Articles')
)
data = [data_1]
fig = go.Figure(data=data, layout=layout)
return plotly.offline.plot(fig,filename='templates/number_articles_total.html',auto_open=False)