/
Kmeans001.py
171 lines (115 loc) · 3.85 KB
/
Kmeans001.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
'''
Created on 30Oct.,2016
@author: Adam Abedini
'''
list_of_lists = []
listofwordsTmp = []
listofwordsAll = []
docslist = []
import urllib
import nltk
import re
import numpy
import sys
import json
from pprint import pprint
from macpath import split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.cluster import KMeans
from sklearn.metrics import adjusted_rand_score
list_of_lists = []
listofwordsTmp = []
listofwordsAll = []
docslist = []
def remove_html_tags(text):
"""Remove html tags from a string"""
import re
clean = re.compile('<.*?>')
return re.sub(clean, '', text)
class tfidf:
def __init__(self):
self.weighted = False
self.documents = []
self.corpus_dict = {}
def addDocument(self, doc_name, list_of_words):
# building a dictionary
doc_dict = {}
for w in list_of_words:
doc_dict[w] = doc_dict.get(w, 0.) + 1.0
self.corpus_dict[w] = self.corpus_dict.get(w, 0.0) + 1.0
# normalizing the dictionary
length = float(len(list_of_words))
for k in doc_dict:
doc_dict[k] = doc_dict[k] / length
# add the normalized document to the corpus
self.documents.append([doc_name, doc_dict])
def addingdocument (tfidf__ ,doc_name, list_of_words):
tfidf__.addDocument(doc_name, list_of_words)
return tfidf__
def getsimilarity (tfidf__ , list_of_words):
return tfidf__.similarities(list_of_words)
def wordListToFreqDict(wordlist):
wordfreq = [wordlist.count(p) for p in wordlist]
return dict(zip(wordlist,wordfreq))
def sortFreqDict(freqdict):
aux = [(freqdict[key], key) for key in freqdict]
aux.sort()
aux.reverse()
return aux
class Document(object):
_id = ""
summaryid = ""
listofwords = []
def __init__(self, _id, summaryid, listofwords):
self._id = _id
self.summaryid = summaryid
self.listofwords = listofwords
def make_imDocument(_id_new, summaryid_new, listofwords_new):
imDocument = Document(_id_new, summaryid_new, listofwords_new)
return imDocument
return 2
path=r'C:\inetpub\json.txt'
with open(path,'r') as data_file:
data = json.load(data_file)
data = [doc for doc in data['hits']['hits']]
for doc in data:
#print("%s) %s" % (doc['_id'], doc['_source']['summaryid']))
listofwordsTmp = split(doc['_source']['searchabletext'])
listofwordsAll += listofwordsTmp
d01 = make_imDocument(doc['_id'] ,doc['_source']['summaryid'], remove_html_tags(doc['_source']['searchabletext']))
list_of_lists.append(d01)
docslist.append(remove_html_tags(doc['_source']['searchabletext']))
vectorizer = TfidfVectorizer(stop_words='english')
X = vectorizer.fit_transform(docslist)
true_k = 3
j = 0
model = KMeans(n_clusters=true_k, init='k-means++', max_iter=10000, n_init=1)
model.fit(X)
#Matrix = [[0 for x in range(true_k+1)] for y in range(11)]
Matrix = [[0 for x in range(11)] for y in range(true_k+1)]
order_centroids = model.cluster_centers_.argsort()[:, ::-1]
terms = vectorizer.get_feature_names()
for i in range(true_k):
print ("Cluster %d:" % i),
j = 0
for ind in order_centroids[i, :10]:
Matrix[i][j] = terms[ind]
j += 1
print (' %s' % terms[ind]),
print
docscore = []
scoretemp = 0
i = 0
for i in range(30):
k = 0
for k in range(true_k):
j = 0
scoretemp = 0
for j in range(10):
#print( docslist[i].count(Matrix[k][j]))
scoretemp += docslist[i].count(Matrix[k][j])
j += 1
print("document id: %d cluster: %d scored: %d" % (i, k, scoretemp))
scoretemp= 0
k +=1
i+=1