-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
318 lines (300 loc) · 9.83 KB
/
helper.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
import numpy as np
import pytz
import datetime
DAILY_SECONDS = 86400
HOURLY_SECONDS = 3600
N_TIME_GAUSSIANS = 4
def gaussian(x, center, width):
return np.exp(-(x - center)**2 / (2 * width **2))
def periodic_gaussian(x, center_in_hours, width_in_hours):
center = center_in_hours * HOURLY_SECONDS
width = width_in_hours * HOURLY_SECONDS
return gaussian(x - DAILY_SECONDS, center, width) + gaussian(x, center, width) + gaussian(x + DAILY_SECONDS, center, width)
def time_process(date, hour, minute, am_or_pm, timezone):
time_dict = dict()
tz = pytz.timezone(timezone)
inputTime = tz.localize(datetime.datetime.combine(date, datetime.datetime.strptime(f'{hour}:{minute} {am_or_pm}', '%I:%M %p').time()))
time_as_Eastern = inputTime.astimezone(pytz.timezone('US/Eastern'))
dayofweek = time_as_Eastern.weekday()
time_dict['dayofweek'] = dayofweek
seconds_since_midnight = (time_as_Eastern - time_as_Eastern.replace(hour = 0, minute = 0, second = 0, microsecond = 0)).total_seconds()
for basis_index in range(N_TIME_GAUSSIANS):
hour = basis_index * 24 // N_TIME_GAUSSIANS
time_dict[f'hour_{hour}'] = periodic_gaussian(seconds_since_midnight, hour, 24 // N_TIME_GAUSSIANS)
return time_dict
def text_len(text):
if text is None:
return None
return len(text.strip().split()) or None
def percentile_rank(score, population):
return (population < score).sum() / len(population)
def number_to_ordinal(number: int) -> str:
if number % 100 in [11, 12, 13]:
return f'{number}th'
suffix = {1: 'st', 2: 'nd', 3: 'rd'}.get(number % 10, 'th')
return f'{number}{suffix}'
# books/review, your-money, and special-series are not encoded because they were not in the training data
_sections = ['world',
'world/europe',
'us',
'us/politics',
'nyregion',
'business',
'world/asia',
'sports/olympics',
'opinion',
'movies',
'sports',
'arts/music',
'arts/television',
'technology',
'health',
'magazine',
'science',
'climate',
'article',
'style',
'wirecutter',
'espanol',
'recipes',
'world/middleeast',
'business/economy',
'podcasts/the-daily',
'business/media',
'world/americas',
'arts',
'sports/football',
'travel',
'world/africa',
'sports/basketball',
'world/australia',
'realestate',
'books',
'arts/design',
'sports/tennis',
'well/live',
'dining',
'briefing',
'upshot',
'sports/soccer',
'well/mind',
'theater',
'headway',
'sports/baseball',
'world/canada',
'podcasts',
'well',
'books/review',
'obituaries',
'business/energy-environment',
'well/move',
'well/family',
'sports/golf',
'your-money',
'well/eat',
'arts/dance',
'special-series',
'fashion',
'business/dealbook'
]
_feature_columns = ['tweet_has_video',
'tweet_has_photo',
'article_has_video',
'article_has_audio',
'comments',
'tweetlength',
'titlelength',
'summarylength',
'articlelength',
'section',
'dayofweek',
'hour_0',
'hour_6',
'hour_12',
'hour_18',
'topic_000',
'topic_001',
'topic_002',
'topic_003',
'topic_004',
'topic_005',
'topic_006',
'topic_007',
'topic_008',
'topic_009',
'topic_010',
'topic_011',
'topic_012',
'topic_013',
'topic_014',
'topic_015',
'topic_016',
'topic_017',
'topic_018',
'topic_019',
'topic_020',
'topic_021',
'topic_022',
'topic_023',
'topic_024',
'topic_025',
'topic_026',
'topic_027',
'topic_028',
'topic_029',
'topic_030',
'topic_031',
'topic_032',
'topic_033',
'topic_034',
'topic_035',
'topic_036',
'topic_037',
'topic_038',
'topic_039',
'topic_040',
'topic_041',
'topic_042',
'topic_043',
'topic_044',
'topic_045',
'topic_046',
'topic_047',
'topic_048',
'topic_049',
'topic_050',
'topic_051',
'topic_052',
'topic_053',
'topic_054',
'topic_055',
'topic_056',
'topic_057',
'topic_058',
'topic_059',
'topic_060',
'topic_061',
'topic_062',
'topic_063',
'topic_064',
'topic_065',
'topic_066',
'topic_067',
'topic_068',
'topic_069',
'topic_070',
'topic_071',
'topic_072',
'topic_073',
'topic_074',
'topic_075',
'topic_076',
'topic_077',
'topic_078',
'topic_079',
'topic_080',
'topic_081',
'topic_082',
'topic_083',
'topic_084',
'topic_085',
'topic_086',
'topic_087',
'topic_088',
'topic_089',
'topic_090',
'topic_091',
'topic_092',
'topic_093',
'topic_094',
'topic_095',
'topic_096',
'topic_097',
'topic_098',
'topic_099',
'topic_100',
'topic_101',
'topic_102',
'topic_103',
'topic_104',
'topic_105',
'topic_106',
'topic_107',
'topic_108',
'topic_109',
'topic_110',
'topic_111',
'topic_112',
'topic_113',
'topic_114',
'topic_115',
'topic_116',
'topic_117',
'topic_118',
'topic_119',
'topic_120',
'topic_121',
'topic_122',
'topic_123',
'topic_124',
'topic_125',
'topic_126',
'topic_127',
'topic_128',
'topic_129'
]
_topics = [feature for feature in _feature_columns if 'topic' in feature]
_select_categoricals = {
'Article includes video?' : 'article_has_video',
'Article includes audio?' : 'article_has_audio',
'Enable reader comments?' : 'comments',
'Tweet includes video?' : 'tweet_has_video',
'Tweet includes photograph?' : 'tweet_has_photo',
'Day of week' : 'dayofweek',
'Time of day' : 'hour'
}
# Maybe namedtuple or dataclass would be more appropriate here
_conditional_proportions_options = {
'tweet_has_video' : dict(
xlabel = 'Does the tweet include a video?',
order = [0, 1],
xticks = ['No', 'Yes'],
aspect = 1.25
),
'tweet_has_photo' : dict(
xlabel = 'Does the tweet include a photo?',
order = [0, 1],
xticks = ['No', 'Yes'],
aspect = 1.25
),
'article_has_video' : dict(
xlabel = 'Does the article include a video?',
order = [0, 1],
xticks = ['No', 'Yes'],
aspect = 1.25
),
'article_has_audio' : dict(
xlabel = 'Does the article include audio?',
order = [0, 1],
xticks = ['No', 'Yes'],
aspect = 1.25
),
'comments' : dict(
xlabel = 'Are readers allowed to comment on the article webpage?',
order = [0, 1],
xticks = ['No', 'Yes'],
aspect = 1.25
),
'dayofweek' : dict(
xlabel = 'Day of the week posted on Twitter',
order = range(7),
xticks = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
aspect = 3
),
'hour' : dict(
xlabel = 'Time of Twitter post',
order = range(24),
xticks = ['12 AM'] + [f'{h} AM' for h in range(1, 12)] + ['12 PM'] + [f'{h} PM' for h in range(1, 12)],
aspect = 4
)
}