-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_cosines.py
165 lines (123 loc) · 5.16 KB
/
get_cosines.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
# -*- coding: utf-8 -*-
import os
import sys
import string
import numpy as np
import pickle
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.cross_validation import train_test_split
from sklearn.feature_extraction.text import TfidfTransformer
import nltk
import pandas as pd
from scipy.spatial.distance import cosine
from nltk.stem.porter import PorterStemmer
import math
stemmer = PorterStemmer()
FREQ_THRESH = 200000
NO_SENTS = 4784808
vocab = pickle.load( open("vocab_dict.pkl", "rb" ) )
common_english_words = ["the", "be", "to", "of", "and", "a", "in", "that", "have", "i", "it",
"for", "not", "on","with", "he", "as", "you", "do","at","this","but",
"his", "by","from","they","we","say","her","she","or","an","will","my",
"one", "all","would","there","their","what","so","if","about","who",
"which", "me", "when", "go","make","can","like","no","him","your","could",
"them", "other","than","then","only","its","also","after","how","our",
"well","way","want","because","any","these","most","into","up","problem"]
def stem_tokens(tokens, stemmer):
stemmed = []
for item in tokens:
stemmed_item = item
try:
stemmed_item = stemmer.stem(item)
except Exception:
pass
stemmed.append(stemmed_item)
return stemmed
def tokenize(text):
tokens = nltk.word_tokenize(text)
stems = stem_tokens(tokens, stemmer)
return stems
def countTerm(term):
if vocab.has_key(term):
vocab[term] += 1
else:
vocab[term] = 1
def computeTFsaccross(docs):
index = 0
for q in docs:
tokens = tokenize(q)
for token in tokens:
countTerm(token)
index += 1
if index % 100000 == 0:
print(">>>>>>>> Generated data for {} sents... ".format(index))
print(">>>>>>>> Vocab length is {} terms... ".format(len(vocab)))
print(">>>>>>>> Memory of the vocab this far {} ... ".format(float(sys.getsizeof(vocab)) / 1024 / 1024))
with open("vocab_dict.pkl", "wb") as f:
pickle.dump(vocab, f, pickle.HIGHEST_PROTOCOL)
def getFrqMatrix(curr_pair):
try:
# 3.2. get their TF vectors
curr_pair_tf_mtx = CountVectorizer(tokenizer=tokenize, stop_words=common_english_words).fit_transform(
curr_pair).todense()
# 3.3 get their joint feature names
terms_union = CountVectorizer(tokenizer=tokenize, stop_words=common_english_words).fit(
curr_pair).get_feature_names()
except:
try:
# 3.2. get their TF vectors
curr_pair_tf_mtx = CountVectorizer(tokenizer=tokenize).fit_transform(curr_pair).todense()
# 3.3 get their joint feature names
terms_union = CountVectorizer(tokenizer=tokenize).fit(curr_pair).get_feature_names()
except:
curr_pair_tf_mtx = []
terms_union = []
pass
return curr_pair_tf_mtx, terms_union
if __name__ == '__main__':
#show("STARTING")
train1 = pd.read_csv("data/train.csv")
#test1 = pd.read_csv("data/test.csv")
index = 0
tf_idf_cosine = []
for i in range(train1.shape[0]):
# 3. for each pair
# 3.1. make them a list
q1 = map(lambda x: str(x).lower().translate(None, string.punctuation), [train1.loc[i, "question1"]])
q2 = map(lambda x: str(x).lower().translate(None, string.punctuation), [train1.loc[i, "question2"]])
curr_pair = q1
curr_pair.extend(q2)
curr_pair_tf_mtx, terms_union = getFrqMatrix(curr_pair)
# 3.4 get the IDF vecs from the - all sents idfs list
idfs = [vocab.get(key) for key in terms_union]
for i in range(len(idfs)):
if idfs[i] == None :
idfs[i] = 0
# print terms_union
## 3.4.1 filter which terms have freq lower than thr
filtered_terms_indx = [idfs.index(x) for x in idfs if x < FREQ_THRESH]
curr_pair_tf_mtx = curr_pair_tf_mtx[:, filtered_terms_indx]
terms_union = [terms_union[x] for x in filtered_terms_indx]
idfs = [idfs[x] for x in filtered_terms_indx]
# print terms_union
if len(idfs) > 0:
## transform IDF
try:
idfs = [math.log(float(NO_SENTS) / (key +1)) for key in idfs]
# 3.5 multiply idfs with tf
curr_pair_tf_mtx = np.array(curr_pair_tf_mtx) * np.array(idfs).tolist()
tf_idf_cosine.append(cosine(curr_pair_tf_mtx[0], curr_pair_tf_mtx[1]))
except:
#print(idfs)
tf_idf_cosine.append([1])
pass
else:
tf_idf_cosine.append([1])
index += 1
if index % 10000 == 0:
print(">>>>>>>> Generated data for {} sents... ".format(index))
print(">>>>>>>> Generated data for {} sents... ".format(len(tf_idf_cosine)))
print(">>>>>>>> Generated data for {} sents... ".format(len(tf_idf_cosine)))
with open("Train_cosines.pkl", "wb") as f:
pickle.dump(tf_idf_cosine, f, pickle.HIGHEST_PROTOCOL)
#np.save("Train_cosines.npy", np.array(tf_idf_cosine))