-
Notifications
You must be signed in to change notification settings - Fork 0
/
addWeekDay.py
58 lines (50 loc) · 1.6 KB
/
addWeekDay.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
#!/usr/bin/env python
"""
addWeekDay.py: add day of the week as feature
usage: python addWeekDay < file
note: expected line input: label ... DATE=...
20190429 erikt(at)xs4all.nl
"""
import datetime
import re
import sys
COMMAND = sys.argv.pop(0)
WEEKDAYLABELS = [ "WKLBMON","WKLBTUE","WKLBWED","WDLBTHU","WKLBFRI",\
"WKLBSAT","WKLBSUN" ]
def readArticles():
articles = []
dates = []
for line in sys.stdin:
articles.append(line)
fields = line.split()
counter = 0
while len(articles) > len(dates):
match = re.search("^DATE=(.*)$",fields[counter])
if match:
dates.append(match.group(1))
counter += 1
if counter >= len(fields):
sys.exit(COMMAND+": incomplete line: "+line)
return(articles,dates)
def getGenreLabel(article):
fields = article.split()
label = fields.pop(0)
article = " ".join(fields)
return(label,article)
def getWeekDay(date):
try:
month,day,year = date.split("/")
date = datetime.date(int(year),int(month),int(day))
return(date.weekday())
except Exception as e:
sys.exit(COMMAND+": problem processing date: "+date+": "+str(e))
def printArticles(articles,dates):
for i in range(0,len(articles)):
genreLabel,article = getGenreLabel(articles[i])
weekDayId = getWeekDay(dates[i])
print(genreLabel,WEEKDAYLABELS[weekDayId],article)
def main(argv):
articles,dates = readArticles()
printArticles(articles,dates)
if __name__ == "__main__":
sys.exit(main(sys.argv))