-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordsToData.py
46 lines (39 loc) · 1.29 KB
/
WordsToData.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
import gensim
import pickle
import re
vocab_size = 50000
embedFile = './GoogleNews-vectors-negative300.bin'
print("Loading Pre-trained Model...")
model = gensim.models.KeyedVectors.load_word2vec_format(
embedFile, binary=True, limit=vocab_size)
word2index = {}
index2word = model.index2word
for i in range(vocab_size):
word2index[index2word[i]] = i
del model
subtexts = ['no_subtext', 'violent', 'depressive', 'sexual']
for i in range(len(subtexts)):
subtext = subtexts[i]
print("Running for ", subtext, " with ", vocab_size, " words.")
dataFile = './ReadingSamples/' + subtext + '.txt'
newFile = './ReadingSamples_Converted/' + subtext + str(vocab_size) +'.txt'
readfile = open(dataFile, mode='r')
writefile = open(newFile, mode='w')
indexedList = []
idx = 0
print("Reading Input File...")
for line in readfile:
idx += 1
line = line.split()
for w in line:
w = re.sub(r"[.,!?:;$\"\s]*", "", w)
if w in index2word:
indexedList.append(word2index[w])
else:
indexedList.append(0)
readfile.close()
print("Writing indexes to file...")
pickle.dump(indexedList, writefile)
writefile.close()
print("Word Count: ", len(indexedList))
print("All files written!")