forked from natashamjaques/neural_chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reddit_utils.py
executable file
·92 lines (79 loc) · 3.27 KB
/
reddit_utils.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
import re
import json
import praw
import pandas as pd
from praw.models import MoreComments
import nltk
import pickle
import numpy as np
def clean_post(text):
text = re.sub(r'https?:\/\/.*[\r\n]*', '', text, flags=re.MULTILINE)
text = re.sub(r'\n\n', ' ', text, flags=re.MULTILINE)
text = re.sub(r'\[removed\]', ' ', text, flags=re.MULTILINE)
text = re.sub(r'\[deleted\]', ' ', text, flags=re.MULTILINE)
text_arr = re.split(r'edit', text, flags=re.IGNORECASE)
return text_arr[0]
def clean_thread_conversations(sub_str):
conversations = []
for mon in ['07', '08', '09', '10', '11', '12']:
with open('datasets/raw_reddit/reddit_{}_{}_18threads.json'.format(sub_str, mon)) as f:
data = json.load(f)
for thread in data:
new_convo = {}
new_convo['lines'] = []
speaker = 0
for msg in thread:
text = clean_post(msg['text'])
if len(text) > 1:
sentences = nltk.sent_tokenize(text)
for sent in sentences:
sent_dict = {}
sent_dict['character'] = speaker
sent_dict['text'] = sent
new_convo['lines'].append(sent_dict)
speaker = 1 - speaker
if len(new_convo['lines']) > 1:
conversations.append(new_convo)
return conversations
def clean_sub_conversations(sub_str, repeat=False):
sub_conversations = []
for mon in ['07', '08', '09', '10', '11', '12']:
with open('datasets/raw_reddit/reddit_{}_{}_18submissions.json'.format(sub_str, mon)) as f:
data = json.load(f)
for sub in data:
speaker = 0
main_text = clean_post(" ".join([sub['title'], sub['text']]))
main_lines = []
if len(main_text) > 1:
sentences = nltk.sent_tokenize(main_text)
for sent in sentences:
sent_dict = {}
sent_dict['character'] = speaker
sent_dict['text'] = sent
main_lines.append(sent_dict)
for comment in sub['comments']:
speaker = 1
reply_lines = []
text = clean_post(comment['text'])
if len(text) > 1:
sentences = nltk.sent_tokenize(text)
for sent in sentences:
sent_dict = {}
sent_dict['character'] = speaker
sent_dict['text'] = sent
reply_lines.append(sent_dict)
if len(reply_lines + main_lines) > 1:
new_convo = {}
new_convo['lines'] = main_lines + reply_lines
sub_conversations.append(new_convo)
if not repeat:
break
return sub_conversations
if __name__ == "__main__":
sub = 'relationship'
thread_conversations = clean_thread_conversations(sub)
print(len(thread_conversations))
sub_conversations = clean_sub_conversations(sub)
print(len(sub_conversations))
with open('datasets/reddit_{}/{}.json'.format(sub, sub), 'w') as f:
json.dump(thread_conversations + sub_conversations, f)