-
Notifications
You must be signed in to change notification settings - Fork 1
/
15IT106_M2_Sourcefile_CHI.py
208 lines (171 loc) · 5.46 KB
/
15IT106_M2_Sourcefile_CHI.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import sys
import ast
from collections import Counter
from os import listdir
import simplejson
import math
import numpy as np
###To find out frequency of documents that contain a particular term in the vocabulary###
def document_frequency(pos_documents,neg_documents,vocabulary_list):
pos_documents_freq=[]
neg_documents_freq=[]
for word in vocabulary_list:
pos_count=0
neg_count=0
for document in pos_documents:
if word in document:
pos_count+=1
pos_documents_freq.append(pos_count)
for document in neg_documents:
if word in document:
neg_count+=1
neg_documents_freq.append(neg_count)
return pos_documents_freq,neg_documents_freq
###CHI for Positive Corpus###
def CHI_for_positive_corpus(pos_documents,neg_documents,pos_documents_freq,neg_documents_freq):
pos_D=len(pos_documents)
neg_D=len(neg_documents)
D=pos_D+neg_D
CHI_pos=[]
for i in range(len(pos_documents_freq)):
ncti=pos_documents_freq[i]
ncbtib=neg_D - neg_documents_freq[i]
ncbti=neg_documents_freq[i]
nctib=pos_D - pos_documents_freq[i]
numerator = D * math.pow((ncti * ncbtib - ncbti * nctib ),2)
denominator=(ncti+nctib)*(ncbti+ncbtib)*(ncti+ncbti)*(nctib+ncbtib)
if denominator == 0:
CHI_per_term=0
else:
CHI_per_term = float(numerator)/denominator
CHI_pos.append(CHI_per_term)
return CHI_pos
###CHI for Negative Corpus###
def CHI_for_negative_corpus(pos_documents,neg_documents,pos_documents_freq,neg_documents_freq):
pos_D=len(pos_documents)
neg_D=len(neg_documents)
D=pos_D+neg_D
CHI_neg=[]
for i in range(len(neg_documents_freq)):
ncti=neg_documents_freq[i]
ncbtib=pos_D - pos_documents_freq[i]
ncbti=pos_documents_freq[i]
nctib=neg_D - neg_documents_freq[i]
numerator = D * math.pow((ncti * ncbtib - ncbti * nctib ),2)
denominator=(ncti+nctib)*(ncbti+ncbtib)*(ncti+ncbti)*(nctib+ncbtib)
if denominator == 0:
CHI_per_term = 0
else:
CHI_per_term = float(numerator)/denominator
CHI_neg.append(CHI_per_term)
return CHI_neg
###Calculating CHI###
def CHI(CHI_pos,CHI_neg):
CHI_result=[]
for i in range(len(CHI_pos)):
CHI_result.append(max(CHI_pos[i],CHI_neg[i]))
return CHI_result
###Accessing the Vocabulary###
file_path=sys.argv[1]+'/vocabulary.txt'
f1 = open(file_path, 'r')
vocabulary_list=f1.read()
vocabulary_list = ast.literal_eval(vocabulary_list)
f1.close()
###Accessing the Documents###
pos_directory_path="/Users/aimanabdullahanees/Desktop/Sentiment_Analysis/"+sys.argv[1]+"/Cleaning_Stopword_Removal/pos"
#pos_directory_path=['Absolute_Path_To_This_Directory']+sys.argv[1]+"/Cleaning_Stopword_Removal/pos"
neg_directory_path="/Users/aimanabdullahanees/Desktop/Sentiment_Analysis/"+sys.argv[1]+"/Cleaning_Stopword_Removal/neg"
#neg_directory_path=['Absolute_Path_To_This_Directory']+sys.argv[1]+"/Cleaning_Stopword_Removal/neg"
pos_documents=[]
neg_documents=[]
documents=[]
for file in listdir(pos_directory_path):
file_path=pos_directory_path+"/"+file
f=open(file_path,'r')
document=f.read()
document = ast.literal_eval(document)
pos_documents.append(document)
documents.append(document)
for file in listdir(neg_directory_path):
file_path=neg_directory_path+"/"+file
f=open(file_path,'r')
document=f.read()
document = ast.literal_eval(document)
neg_documents.append(document)
documents.append(document)
###Performing CHI###
pos_documents_freq,neg_documents_freq=document_frequency(pos_documents,neg_documents,vocabulary_list)
CHI_pos=CHI_for_positive_corpus(pos_documents,neg_documents,pos_documents_freq,neg_documents_freq)
CHI_neg=CHI_for_negative_corpus(pos_documents,neg_documents,pos_documents_freq,neg_documents_freq)
CHI_result=CHI(CHI_pos,CHI_neg)
###Output###
print("Vocabulary to be considered: ")
print(vocabulary_list)
print()
print("Document Frequency wrt Positive Corpus: ")
print(pos_documents_freq)
print()
print("Document Frequency wrt Negative Corpus: ")
print(neg_documents_freq)
print()
print("CHI for Positive Corpus: ")
print(CHI_pos)
print()
print()
print("CHI for Negative Corpus: ")
print(CHI_neg)
print()
print("CHI: ")
print(CHI_result)
file_path=sys.argv[1]+'/chi.txt'
f = open(file_path, 'w')
simplejson.dump(CHI_result, f)
f.close()
"""
Tirvial Example:
----------------
doc=[['to', 'do', 'is', 'to', 'be', 'to', 'be', 'is', 'to', 'do'],
['to', 'be', 'or', 'not', 'to', 'be', 'i', 'am', 'what', 'i', 'am'],
['i', 'think', 'therefore', 'i', 'am', 'do', 'be', 'do', 'be', 'do'],
['do', 'do', 'do', 'da', 'da', 'da', 'let', 'it', 'be', 'let', 'it', 'be']]
i_n=['to',
'do',
'is',
'be',
'or',
'not',
'i',
'am',
'what',
'think',
'therefore',
'da',
'let',
'it']
pos_doc=[['to', 'do', 'is', 'to', 'be', 'to', 'be', 'is', 'to', 'do'],
['to', 'be', 'or', 'not', 'to', 'be', 'i', 'am', 'what', 'i', 'am']]
neg_doc=[['i', 'think', 'therefore', 'i', 'am', 'do', 'be', 'do', 'be', 'do'],
['do', 'do', 'do', 'da', 'da', 'da', 'let', 'it', 'be', 'let', 'it', 'be']]
pos_documents_freq,neg_documents_freq=document_frequency(pos_doc,neg_doc,i_n)
CHI_pos=CHI_for_positive_corpus(pos_doc,neg_doc,pos_documents_freq,neg_documents_freq)
CHI_neg=CHI_for_negative_corpus(pos_doc,neg_doc,pos_documents_freq,neg_documents_freq)
CHI_result=CHI(CHI_pos,CHI_neg)
print("Vocabulary to be considered: ")
print(i_n)
print()
print("Document Frequency wrt Positive Corpus: ")
print(pos_documents_freq)
print()
print("Document Frequency wrt Negative Corpus: ")
print(neg_documents_freq)
print()
print("CHI for Positive Corpus: ")
print(CHI_pos)
print()
print()
print("CHI for Negative Corpus: ")
print(CHI_neg)
print()
print("CHI: ")
print(CHI_result)
"""