-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchat_convert.py
executable file
·89 lines (73 loc) · 2.77 KB
/
chat_convert.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
#!/usr/bin/env python2
import json
import re
import sys
from glove import Corpus, Glove
import unicodecsv as csv
with open('settings.json') as settingsfile:
settings = json.load(settingsfile)
mods = settings['ignored_moderators']
# Bundle groups of symbols and make sure words are otherwise alone
print("Parsing JSON log")
logfile = open(sys.argv[1], 'r')
if len(sys.argv) > 2:
adminfile = open(sys.argv[2], 'r')
jsons = []
logs = []
adminlogs = []
for line in logfile.readlines():
try:
json_parsed = json.loads(line)
logs.append(json_parsed['text'])
jsons.append(json_parsed)
except KeyError:
pass
if len(sys.argv) > 2:
for line in adminfile.readlines():
try:
json_parsed = json.loads(line)
if 'fwd_from' in json_parsed and len(json_parsed['text']) > 7:
adminlogs.append(json_parsed['text'])
except KeyError:
pass
intersects = set.intersection(set(logs), set(adminlogs))
matching_indexes = []
for index, item in enumerate(logs):
if item in intersects:
matching_indexes.append(index)
# Flag all matching forwarded messages
for idx in matching_indexes:
jsons[idx]['flagged'] = '1'
# Create a fast lookup map
id_lookup = dict()
for index, message in enumerate(jsons):
id_lookup[message['id']] = index
for message in jsons:
# Flag slurs
if any( key in message['text'].lower() for key in settings['slurs']):
message['flagged'] = '1'
# Flag mod replies with mod names
try:
if 'reply_id' in message and 'username' in message['from'] and any( name in message['from']['username'].lower() for name in mods):
jsons[id_lookup[message['reply_id']]]['flagged'] = message['from']['username']
print("Labeling mod reply")
except KeyError:
pass
for message in jsons:
# Mods are blameless
if 'username' in message['from'] and any( key in message['from']['username'].lower() for key in mods):
message['flagged'] = ''
with open('chat-converted.csv', 'wb') as csvfile:
spamwriter = csv.writer(csvfile)
index = 0
for message in jsons:
name = (message['from']['first_name'] if 'first_name' in message['from'] else "") + " " + (message['from']['last_name'] if 'last_name' in message['from'] else "")
spamwriter.writerow(
([message['flagged']] if 'flagged' in message else ['']) +
[name] +
([message['from']['username']] if 'username' in message['from'] else ['']) +
([message['text']] if 'text' in message else ['']) +
[index]
)
index += 1
print("Outputted pre-flagged chat file chat-converted.csv. Continue manually flagging from there.")