-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclustering_1.py
57 lines (49 loc) · 2.56 KB
/
clustering_1.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
from nltk.tokenize import word_tokenize
import random
from rouge import Rouge
import pandas as pd
from nltk.tokenize.treebank import TreebankWordDetokenizer
rouge = Rouge()
def clustering(corpus_for_clustering):
cluster = []
clusters = []
token_len = 0
if(len(corpus_for_clustering) <= 2):
clusters = [review for review in corpus_for_clustering]
while len(corpus_for_clustering) > 2:
pivot_data = random.choice(corpus_for_clustering)
token_len = len(word_tokenize(pivot_data))
cluster = [pivot_data]
df_cluster = pd.DataFrame(columns = ['text', 'rouge-1 score'])
for j in range(len(corpus_for_clustering)): # getting rouge-1 f1 score for all data wrt to pivot_data
if (pivot_data != corpus_for_clustering[j]):
scores = rouge.get_scores(pivot_data, corpus_for_clustering[j])
df_cluster = df_cluster.append({'text': corpus_for_clustering[j], 'rouge-1 score': scores[0].get('rouge-1').get('f')}, ignore_index=True)
df_cluster.sort_values("rouge-1 score", axis = 0, ascending = False, inplace = True, na_position ='last')
token_len = token_len + len(word_tokenize(df_cluster['text'][0])) + len(word_tokenize(df_cluster['text'][1]))
already_in_cluster = [pivot_data, df_cluster['text'][0], df_cluster['text'][1]]
cluster.append(df_cluster['text'][0])
cluster.append(df_cluster['text'][1])
for k in range(2, len(df_cluster)):
if(len(word_tokenize(df_cluster['text'][k])) + token_len < 512):
token_len = token_len + len(word_tokenize(df_cluster['text'][k]))
cluster.append(df_cluster['text'][k])
df_cluster.drop(k, inplace=True)
already_in_cluster.append(df_cluster['text'][k])
else:
break
corpus_for_clustering = [ review for review in corpus_for_clustering if review not in already_in_cluster ]
del df_cluster
cluster = []
for i in range(len(clusters)):
tokenized_text = word_tokenize(str(clusters[i]).strip("[]").replace("'", ""))
cluster_len = len(tokenized_text)
if (cluster_len > 512):
new_token_cluster = tokenized_text[0:128] + tokenized_text[-382:]
# print(len(new_token_cluster))
# print(tokenized_text)
# print(new_token_cluster)
# more = more + 1
cluster_text = TreebankWordDetokenizer().detokenize(tokenized_text)
clusters = clusters[:i]+[cluster_text]+clusters[i+1:]
return clusters