-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpreprocessing.py
More file actions
executable file
·81 lines (69 loc) · 2.38 KB
/
preprocessing.py
File metadata and controls
executable file
·81 lines (69 loc) · 2.38 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
import csv
from datetime import datetime
FLOOR_DATE = datetime(2014, 1, 1)
CEILING_DATE = datetime(2014, 12, 31)
# trim the csv file and store only
# source and target subreddit column
# between floor and celing date range
def load_csv(path: str, outpath: str):
dataframe = []
with open(path, newline='') as csvfile:
lines = csv.reader(csvfile, delimiter='\t')
counter = 0
for row in lines:
if counter == 0:
counter += 1
continue
# elif counter >= 10:
# break
item = []
source = row[0].lower()
target = row[1].lower()
timestamp = datetime.strptime(row[3], '%Y-%m-%d %H:%M:%S')
if FLOOR_DATE < timestamp < CEILING_DATE:
item.append(source)
item.append(target)
item.append(timestamp.strftime("%Y-%m-%d"))
dataframe.append(item)
counter += 1
outfile = outpath + '.csv'
columns = ['from', 'to', 'date']
with open(outfile, 'w') as f:
wr = csv.writer(f)
wr.writerow(columns)
wr.writerows(dataframe)
print("Output finished")
def load_save_text_propery(path: str, outpath: str):
dataframe = []
with open(path, newline='') as csvfile:
lines = csv.reader(csvfile, delimiter='\t')
counter = 0
for row in lines:
if counter == 0:
counter += 5
continue
# elif counter >= 10:
# break
item = []
source = row[0].lower()
post_props = row[5]
timestamp = datetime.strptime(row[3], '%Y-%m-%d %H:%M:%S')
if FLOOR_DATE < timestamp < CEILING_DATE:
item.append(source)
props_splitted = post_props.split(',')
item.extend(props_splitted)
dataframe.append(item)
counter += 1
outfile = outpath + '.csv'
columns = ['subreddit']
columns.extend([str(i+1) for i in range(86)])
print(columns)
with open(outfile, 'w') as f:
wr = csv.writer(f)
wr.writerow(columns)
wr.writerows(dataframe)
print("Output finished")
if __name__ == '__main__':
src = "dataset/soc-redditHyperlinks-title.tsv"
output = "dataset/preprocessed-post-vector"
load_save_text_propery(src, output)