forked from ckdu/ai-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhklDatasetChannel.py
More file actions
50 lines (36 loc) · 1.33 KB
/
hklDatasetChannel.py
File metadata and controls
50 lines (36 loc) · 1.33 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
import json
import os
import hickle as hkl
import numpy as np
for jsonFileName in os.listdir('input'):
rank = jsonFileName.split(".")[0]
#yearToSave = "2021"
hklFileName = rank + ".hkl"
os.chdir('input')
f = open(jsonFileName, mode="r", encoding="utf-8")
data = json.load(f)
f.close()
os.chdir('../')
messagesPerId = {}
rankObjects = []
blacklistedIds = ["4321", "1234"]
for i in data["messages"]:
content = i["content"].encode("ascii", "ignore").decode()
userId = i["author"]["id"]
isBot = i["author"]["isBot"]
if not isBot and content and not userId in blacklistedIds:
if not content[:1] == "!" and not content[:1] == "?" and not content[:1] == ",":
#if i["timestamp"][:4] == yearToSave:
if userId in messagesPerId:
messagesPerId[userId] += " " + content
else:
messagesPerId[userId] = content
# print(len(messagesPerId))
for id, messages in messagesPerId.items():
rankObjects.append({"rank": rank, "messages": messages})
os.chdir('output')
npMessages = np.array(rankObjects)
hkl.dump(npMessages, hklFileName, mode='w', compression='gzip')
#loaded = hkl.load(hklFileName).tolist()
#print(loaded[0])
os.chdir('../')