-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_data.py
53 lines (42 loc) · 1.38 KB
/
generate_data.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
from piazza_api import Piazza
import csv
import re
import string
import cmd
p = Piazza()
p.user_login()
def cleanhtml(raw_html):
cleantext = re.sub(html_re, '', raw_html)
return cleantext
def cleanpunc(text):
return text.translate(punc_table)
def cleanpiazza(text):
cleantext = re.sub(piazza_re, '', text)
return cleantext
def clean(text):
text = cleanpiazza(text)
text = cleanhtml(text)
text = cleanpunc(text)
return text.lower()
## Initialize cleaners
piazza_re = re.compile('&.*?;')
punc_table = str.maketrans({key: None for key in string.punctuation})
html_re = re.compile('<.*?>')
eecs280 = p.network("jlco33n8mip65u")
filterfunction = lambda x : len(x['folders']) is not 0 and x['type'] == 'question'
try:
LIMIT = int(input("How many posts do you want to process? (-1 to process all posts)\n"))
except Exception as e:
LIMIT = -1
if (LIMIT > 0):
posts = filter(filterfunction, eecs280.iter_all_posts(limit=LIMIT))
else:
posts = filter(filterfunction, eecs280.iter_all_posts())
with open('f18_projects_exam_long.csv', mode='w') as train_file:
writer = csv.writer(train_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerow(['tag', 'content'])
for post in posts:
content = post['history'][-1]['content']
cleaned = clean(content)
tag = post['folders'][-1]
writer.writerow([tag, cleaned])