-
Notifications
You must be signed in to change notification settings - Fork 0
/
coocs.py
132 lines (94 loc) · 3.46 KB
/
coocs.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/python
"""
Word-Word Co-Occurrence Matrix Builder
======================================
If you have a corpus in text file (one line -- one sentence),
run this from terminal (see `main` function)
... or for corpora in arbitrary format, use `count_coocs` function.
"""
import pickle
import sys
import numpy as np
from collections import defaultdict
from scipy.sparse import coo_matrix, csr_matrix
# Local imports
from misc import LineCorpus
REPORT_DELAY = 10**5 # in struct. items (e.g. sentences)
ARR_SIZE = 10 ** 8
def count_coocs(corpus, output_name, min_count=1, window=4):
"""
`corpus` should be a stream of sentences,
where sentence is a non-empty list of words
"""
assert min_count >= 1
assert window >= 1
print("Building vocab...")
vocab = defaultdict(lambda: 0)
for sentence in corpus:
for word in sentence:
vocab[word] += 1
vocab = {w: c for w, c in vocab.items() if c >= min_count}
print("Vocabulary built.")
sorted_vocab = sorted(vocab.items(), key=lambda (w, c): c, reverse=True)
word2i = {word: i for i, (word, _) in enumerate(sorted_vocab)}
rows = np.zeros((ARR_SIZE, ), dtype=np.int32)
cols = np.zeros((ARR_SIZE, ), dtype=np.int32)
vals = np.zeros((ARR_SIZE, ), dtype=np.int32)
ind = 0
max_ind = ARR_SIZE
shape = (len(vocab), len(vocab))
m = coo_matrix(shape, dtype=np.float64)
weights = np.array([(window - k) / window for k in range(window)])
for i, sentence in enumerate(corpus):
sentence = np.array([word2i[w] for w in sentence if w in word2i])
for j in range(len(sentence)):
target_id = sentence[j]
for k in range(window):
q = j + 1 + k
if q >= len(sentence):
break
context_id = sentence[q]
if ind >= max_ind:
m = coo_matrix(
(np.concatenate([m.data, vals]),
(np.concatenate([m.row, rows]),
np.concatenate([m.col, cols]))),
shape=shape, dtype=np.float64,
)
m.sum_duplicates()
ind = 0
rows[ind] = target_id
cols[ind] = context_id
vals[ind] = weights[k]
ind += 1
if i % REPORT_DELAY == 0:
print("Sentence #%i" % i)
m = coo_matrix(
(np.concatenate([m.data, vals[:ind]]),
(np.concatenate([m.row, rows[:ind]]),
np.concatenate([m.col, cols[:ind]]))),
shape=shape, dtype=np.float64
)
m.sum_duplicates()
print("Counting completed.")
# Make the context window and the matrix symmetric
m_csr = csr_matrix(m)
m = coo_matrix(m_csr + m_csr.transpose())
np.save(output_name + "-rows.npy", m.row)
np.save(output_name + "-cols.npy", m.col)
np.save(output_name + "-vals.npy", m.data)
with open(output_name + "-target2i.pickle", "w") as f:
pickle.dump(word2i, f)
def main():
if len(sys.argv) != 5:
sys.stderr.write("Usage: python coocs.py "
"CORPUS_FILE OUTPUT_NAME MIN_COUNT WINDOW_SIZE\n")
sys.exit(1)
corpus_file = sys.argv[1]
output_name = sys.argv[2]
min_count = int(sys.argv[3])
window_size = int(sys.argv[4])
corpus = LineCorpus(corpus_file)
count_coocs(corpus, output_name, min_count, window_size)
if __name__ == "__main__":
main()