-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvectorcluster.py
205 lines (169 loc) · 5.58 KB
/
vectorcluster.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
from gensim.models import KeyedVectors
import numpy as np
from sklearn.cluster import KMeans
import Levenshtein
import spacy
from collections import defaultdict
import re
from sklearn.cluster import spectral_clustering
import math
en_nlp = spacy.load('en')
def part_of_speech_features(words_array):
tags = []
for word in words_array:
word = word.strip()
if bool(re.search(r'-', word)):
word = word.split('-')
word = word[0]
curr_tag = en_nlp(word)
curr_tag = curr_tag[0]
tags.append(curr_tag)
#print("Tags: " + str(len(tags)))
seen_tag_indices = {}
tag_counter = 0
# print('tags starting')
for i in range(len(tags)):
curr_tag = tags[i].tag_
# print(curr_tag)
if curr_tag not in seen_tag_indices:
seen_tag_indices[curr_tag] = tag_counter
tag_counter += 1
# print(seen_tag_indices)
tag_matrix = np.zeros((len(tags), len(seen_tag_indices)))
for i in range(len(tags)):
curr_tag = tags[i].tag_
tag_matrix[i, seen_tag_indices[curr_tag]] = 1
return tag_matrix
def part_of_speech_test():
test_arr = ['this', 'is', 'a', 'words', 'array', 'made', 'of',
'only', 'the', 'best', 'phrases', 'arrays' 'have' 'to ''offer',
'in', 'the', 'office']
test_output = part_of_speech_features(test_arr)
print(test_output)
# testing part of speech features
# part_of_speech_test()
def create_PPMI_matrix(term_context_matrix):
def divide_array(matrix_array, marginal_array):
return matrix_array - marginal_array
term_context_matrix = term_context_matrix + pow(10, -3)
#print(str(term_context_matrix))
context_sum = np.sum(term_context_matrix, axis=0)
#print(context_sum)
word_sum = np.sum(term_context_matrix, axis=1)
#print(word_sum)
total_sum1 = np.sum(context_sum)
context_sum = np.log2(context_sum/total_sum1)
word_sum = np.log2(word_sum / total_sum1)
term_context_matrix = np.log2(term_context_matrix / total_sum1)
term_context_matrix = np.apply_along_axis(divide_array, 1, term_context_matrix, context_sum)
term_context_matrix = np.apply_along_axis(divide_array, 0, term_context_matrix, word_sum)
term_context_matrix = np.clip(term_context_matrix, 0, None)
return term_context_matrix
def compute_cosine_similarity(vector1, vector2):
'''Computes the cosine similarity of the two input vectors.
Inputs:
vector1: A nx1 numpy array
vector2: A nx1 numpy array
eturns:
A scalar similarity value.
'''
products = vector1 * vector2
vector1 = np.square(vector1)
vector2 = np.square(vector2)
mag1 = sqrt(np.sum(vector1))
mag2 = sqrt(np.sum(vector2))
mags = mag1 * mag2
return np.sum(products)/mags
def create_norm_matrix(term_context_matrix):
def divide_array(matrix_array, marginal_array):
return matrix_array / marginal_array
term_context_matrix = term_context_matrix + 1
#print(str(term_context_matrix))
#print(context_sum)
word_sum = np.sum(term_context_matrix, axis=1)
#print(word_sum)
term_context_matrix = np.apply_along_axis(divide_array, 0, term_context_matrix, word_sum)
term_context_matrix = np.clip(term_context_matrix, 0, None)
return term_context_matrix
def findClosest(word, cands):
minDist = 400
ret = ""
for val in cands:
if val == word: continue
if Levenshtein.distance(word, val) < minDist:
minDist = Levenshtein.distance(word, val)
ret = val
return ret
def create_sim_matrix(matrix):
def get_mag(row_vector):
row_vector = np.square(row_vector)
row_vector = math.sqrt(np.sum(row_vector))
return row_vector
def divide_by_mag(mat_vector, mag_vector):
return mat_vector / mag_vector
trans_matrix = matrix.transpose()
sim_matrix = np.matmul(matrix, trans_matrix)
magnitudes = np.apply_along_axis(get_mag, 1, matrix)
sim_matrix = np.apply_along_axis(divide_by_mag, 1, sim_matrix, magnitudes)
sim_matrix = np.apply_along_axis(divide_by_mag, 0, sim_matrix, magnitudes)
return sim_matrix
def weight_pos(pos_features, matrix):
row_mean = np.mean(matrix)
return row_mean * pos_features
# This maps from word -> list of candidates
word2cands = {}
# This maps from word -> number of clusters
word2num = {}
# Read the words file.
with open("data/dev_input.txt") as f:
for line in f:
word, numclus, cands = line.split(" :: ")
cands = cands.split()
word2num[word] = int(numclus)
word2cands[word] = cands
# Load cooccurrence vectors (question 2)
vec = KeyedVectors.load_word2vec_format("data/coocvec-500mostfreq-window-3.vec.filter")
# Load dense vectors (uncomment for question 3)
# vec = KeyedVectors.load_word2vec_format("data/GoogleNews-vectors-negative300.filter")
filename = "dev_output_features.txt"
f = open(filename, "w")
j = 0
for word in word2cands:
cands = word2cands[word]
numclusters = word2num[word]
dic = {}
for k in range (1, numclusters + 1):
dic[k] = []
pos_features = part_of_speech_features(cands)
#print(len(cands))
#for cand in cands:
# print(cand)
matrix = np.zeros((len(cands), 500))
i = 0
for val in cands:
try:
matrix[i, :] = (vec[val])
except:
# print (matrix[i, :])
continue
# matrix[i, :] = (vec[findClosest(val, cands)])
i += 1
matrix = np.append(matrix, weight_pos(pos_features, matrix), axis=1)
# print(str(matrix.max()))
matrix = create_sim_matrix(matrix)
matrix = 1 - matrix
kmeans = KMeans(n_clusters = numclusters).fit(matrix)
# kmeans = spectral_clustering(matrix, n_clusters=numclusters)
for l in range (0, len(kmeans.labels_)):
dic[kmeans.labels_[l] + 1].append(cands[l])
for key, value in dic.items():
f.write(word + " :: " + str(key) + " :: " + ' '.join(dic[key]) + "\n")
# print (dic)
# print(word)
# print(len(cands))
# print(len(kmeans.labels_))
j += 1
f.close()
# TODO: get word vectors from vec
# Cluster them with k-means
# Write the clusters to file.