-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmine_POS_pats.py
161 lines (113 loc) · 3.02 KB
/
mine_POS_pats.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
import nltk
from collections import Counter
import timeit
from nltk.tag.stanford import StanfordPOSTagger
st = StanfordPOSTagger('/home/saurav/Documents/postagger/models/english-bidirectional-distsim.tagger',
'/home/saurav/Documents/postagger/stanford-postagger.jar')
Prob = {}
infile = open('probabilities.txt', 'r')
text = infile.readlines()
for sentence in text:
keyValPair = sentence.split(":")
Prob[keyValPair[0]] = float(keyValPair[1][:-1])
infile.close()
def prob(sequence):
if sequence in Prob.keys():
return Prob[sequence]
else:
return 0
def fairSCP(sequence):
numerator = prob(sequence)*prob(sequence)
sequence = sequence.split()
denominator = 0
for j in range(1,len(sequence)):
seq1 = ""
seq2 = ""
cnt = 1
for tag in sequence:
if cnt <= j:
seq1 = seq1 + tag + " "
cnt += 1
else:
seq2 = seq2 + tag + " "
seq2 = seq2[:-1]
seq1 = seq1[:-1]
denominator += prob(seq1)*prob(seq2)
denominator = denominator*1.0/(len(sequence)-1)
if denominator == 0:
return 0.0
SCP = numerator*1.0/denominator
return SCP
minSup = 0.3
minAdherence = 0.2
tagList = ['NN', 'CC', 'LS', 'PDT', 'POS', 'SYM', 'NNS', 'NNP', 'NNPS', 'FW', 'CD', 'JJ', 'JJR', 'JJS', 'IN', 'TO', 'DT',
'EX', 'PRP', 'PRP$', 'WDT', 'WP', 'WP$', 'MD', 'VB', 'VBZ', 'VBP', 'VBD', 'VBN', 'VBG', 'RB', 'RBR', 'RBS', 'RP', 'WRB', 'UH', '.']
infile = open('realBlogCorpusPOS.txt', 'r')
Doc = infile.readlines()
infile.close()
def candidateGen(Fk):
Ck = []
for item in Fk:
for tag in tagList:
itemTemp = item + " "+ tag
Ck.append(itemTemp)
return Ck
def minePOSPats(Doc):
C = [{} for i in range(5)]
F = [[] for i in range(5)]
SP = [[] for i in range(5)]
Cand = [[] for i in range(5)]
n = len(Doc)
for post in Doc:
for tag in tagList:
if tag in post:
if tag in C[0].keys():
C[0][tag] += 1
else:
C[0][tag] = 1
for a in C[0]:
if C[0][a]*1.0/n >= minSup:
F[0].append(a)
SP[0] = F[0]
temp={}
for k in range(1,5):
Cand[k] = candidateGen(F[k-1])
for post in Doc:
for candidate in Cand[k]:
if candidate in post:
if candidate in C[k].keys():
C[k][candidate] += 1
else:
C[k][candidate] = 1
for a in C[k]:
if C[k][a]*1.0/n >= minSup:
F[k].append(a)
for a in F[k]:
if fairSCP(a) >= minAdherence:
SP[k].append(a)
SPFinal = []
SPFinal = SP[0]+SP[1]+SP[2]+SP[3]+SP[4]
return SPFinal
#print(minePOSPats(Doc))
print("Mining POS sequence patterns...")
start_time = timeit.default_timer()
posFeatures = minePOSPats(Doc)
#print posFeatures
elapsed_time = timeit.default_timer() - start_time
print("Finished mining after %f seconds. " % (elapsed_time))
def POSFeatures(text):
tokens = nltk.word_tokenize(text)
text = nltk.Text(tokens)
#tags = st.tag(text)
tags = nltk.pos_tag(tokens)
textTags = ""
for word,tag in tags:
if tag in tagList:
textTags = textTags + tag + " "
featureValues = []
for feature in posFeatures:
if feature in textTags:
featureValues.append(1)
else:
featureValues.append(0)
return tuple(featureValues)