-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgsp.py
148 lines (124 loc) · 3.57 KB
/
gsp.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
import os
import codecs
import itertools
import operator
class GSP:
'''GSP algorithm implementation'''
def __init__(self, min_sup, path):
self.min_sup = min_sup
self.path = path
self.unique_words, self.word_list, self.data = self.parse_data()
self.items = {}
self.num_users = len(self.data)
def parse_data(self):
files = os.listdir(self.path)
files = [self.path + file for file in files]
unique_words = {} # contains unique words in the data files
unique_counter = 0 # ID number assigned to each unique word
seqs = []
word_list = []
for filename in files:
# read user data
lines = []
f = codecs.open(filename, encoding='utf-8')
for line in f:
lines.append(line.encode('utf-8'))
ans = {} # contains final time series dictionary
seq = {}
seq['file'] = filename
s1 = []
s2 = []
# for each line in data file
for X in lines:
Y = X.strip().split(';')
time = Y[0].split(' ')[1]
# extract time info
x = []
# extract useful info at a given time stamp
for i,data in enumerate(Y):
if i > 0:
word = data.split('#')[1]
# check if word is unique or not
# if not, assign a new ID number
if word not in unique_words:
unique_counter = unique_counter + 1
unique_words[word] = unique_counter
word_list.append(word)
x.append(unique_words[word])
# if x is not empty
if x:
ans[time] = x
s1.extend(x)
s2.append(tuple(x))
seq['data'] = ans
seq['seq_individual'] = s1
seq['seq_combined'] = s2
seqs.append(seq)
return unique_words, word_list, seqs
def is_subseq(self, x, y):
it = iter(y)
return all(c in it for c in x)
def find_support(self, item, flag):
count = 0
if flag == 1:
for i in range(self.num_users):
if self.is_subseq(item, self.data[i]['seq_individual']):
count += 1
else:
for i in range(self.num_users):
if item in self.data[i]['seq_combined']:
count += 1
return count
def get_support_items(self, level):
# Step 1: Find items that meet min. threshold requirement
print('Number of users = %d' % (self.num_users))
for word in self.unique_words:
l = [self.unique_words[word]]
sup = self.find_support(l, 1)
if sup >= self.min_sup:
self.items[tuple(l)] = sup
# If we need only 1-grams, we print those here and exit.
if level == 1:
sorted_patterns = sorted(self.items.items(), key=operator.itemgetter(1), reverse=True)
for t in sorted_patterns:
p = []
for i in t[0]:
print self.word_list[i - 1] + ' ',
print t[1]
return
# We now generate permutations of size 2.
keys = [x[0] for x in self.items.keys()]
perms = itertools.permutations(keys, 2)
self.perms = list(perms)
self.items = {}
'''for p in self.perms:
sup = self.find_support(p, 2)
if sup >= self.min_sup:
self.items[(p, )] = sup'''
for k in keys:
self.perms.append((k, k))
for p in self.perms:
sup = self.find_support(list(p), 1)
if sup >= self.min_sup:
self.items[p] = sup
level -= 2 # processing done till level 2
# processing for level > 2
while level > 0:
prev_items = dict(self.items)
self.items = {}
for item in prev_items:
for k in self.unique_words:
key = list(item)
key.append(self.unique_words[k])
key = tuple(key)
sup = self.find_support(list(key), 1)
if sup >= self.min_sup:
self.items[key] = sup
level -= 1
sorted_patterns = sorted(self.items.items(), key=operator.itemgetter(1), reverse=True)
for t in sorted_patterns:
p = []
for i in t[0]:
print self.word_list[i - 1] + ' ',
print t[1]
return