-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateData.py
71 lines (48 loc) · 2.11 KB
/
generateData.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
import zipfile
import csv
import os
scriptDir = os.path.dirname(os.path.realpath(__file__))
def textContainsSpaces(text):
for item in text:
if (item == ''):
return True
return False
def generateDataset(zipPath):
# Create zip file object of package, and path object relating to zip
zip = zipfile.ZipFile(zipPath, "r")
path = zipfile.Path(zip, "messages/")
# Create initial data file to store messages
if not os.path.exists(scriptDir + '/data/'):
os.makedirs(scriptDir + '/data/')
file = open(scriptDir + "/data/data.txt", "w", encoding="utf8")
# Used to ensure that no blank lines are left during the writing process
firstRun = True
# Loop through each item in the messages directory
for channel in path.iterdir():
# If current item is a file (not a messages directory), continue to the next folder
if (channel.is_file()): continue
# Create CSV reader for each channel folder's messages
file = channel.joinpath("messages.csv")
reader = csv.reader(file.open("r", encoding="utf8"))
# Skip initial line of CSV (Contents, etc)
next(reader)
# Go through each line of current CSV and output to data file
for row in reader:
with open(scriptDir + "/data/data.txt", 'a', encoding="utf8") as dataFile:
# Clean up text
text = row[2].rstrip().lstrip().strip().splitlines()
# Clean up empty lines in multi-line messages
while (textContainsSpaces(text)):
for line in text:
if (line == ''):
text.remove(line)
# Only write the starting newline character for lines that aren't the first
# (No blank lines will be left, can cause errors in training due to encoding?)
for line in text:
if not firstRun:
dataFile.write("\n")
else:
firstRun = False
dataFile.write(line)
# Close zip file
zip.close()