-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
387 lines (308 loc) · 14.9 KB
/
main.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# -*- coding: utf-8 -*-
__author__ = "Arunprasath Shankar"
__copyright__ = "Copyright 2012, Arunprasath Shankar"
__license__ = "GPL"
__version__ = "1.0.1"
__email__ = "[email protected]"
from corpus import *
from spec_analyser import *
from csv_to_html import *
from clips_facts_generation import ClipsFactsGeneration
from range_estimator import RangeCalculator
#from fuzzy_plot import *
#from scatter_plot import *
from dom import DegreeOfMembership
from neuro_fuzzy import NeuroFuzzySystem
#from neuron import *
#from surface_plot import *
from sect_clustering import SectionWiseClustering
from concept_skeleton import ConceptSkeleton
from proximity_finder import ProximityFinder
from fuzzy_concepts import FuzzyConcept
import csv
import sys
import os
import pygraphviz as pgv
if __name__ == '__main__':
spec_count = 0
spec_word_count_list, corpus_words = [], []
spec_bi_gram_count_list, corpus_bi_grams = [], []
spec_tri_gram_count_list, corpus_tri_grams = [], []
spec_four_gram_count_list, corpus_four_grams = [], []
spec_five_gram_count_list, corpus_five_grams = [], []
for root, directory, files in os.walk('./corpus'):
for f in files:
if f.endswith('.xml'):
path = os.path.join(root, f)
spec_words = []
bi_grams = []
tri_grams = []
four_grams = []
five_grams = []
cor = Corpus(path, spec_words, bi_grams, tri_grams, four_grams, five_grams)
cor.generateLocationVector(cor.parseXML(), [0])
spec_count += 1
spec_word_count = len(spec_words)
spec_word_count_list.append(spec_word_count)
corpus_words.append(spec_words)
spec_bi_gram_count = len(bi_grams)
spec_bi_gram_count_list.append(spec_bi_gram_count)
corpus_bi_grams.append(bi_grams)
spec_tri_gram_count = len(tri_grams)
spec_tri_gram_count_list.append(spec_tri_gram_count)
corpus_tri_grams.append(tri_grams)
spec_four_gram_count = len(four_grams)
spec_four_gram_count_list.append(spec_four_gram_count)
corpus_four_grams.append(four_grams)
spec_five_gram_count = len(five_grams)
spec_five_gram_count_list.append(spec_five_gram_count)
corpus_five_grams.append(five_grams)
corpus = []
for spec in corpus_words:
for word in spec:
corpus.append(word)
corpus_bigrams = []
for spec in corpus_bi_grams:
for word in spec:
corpus_bigrams.append(word)
corpus_trigrams = []
for spec in corpus_tri_grams:
for word in spec:
corpus_trigrams.append(word)
corpus_fourgrams = []
for spec in corpus_four_grams:
for word in spec:
corpus_fourgrams.append(word)
corpus_fivegrams = []
for spec in corpus_five_grams:
for word in spec:
corpus_fivegrams.append(word)
output = open('output_table.csv', 'wb')
output_write = csv.writer(output)
output_write.writerow(['S.No', 'Word', 'Location Vector', 'Signature', 'Signature Weight', 'Tf', 'Idf', 'Tf - Idf Score', 'English Dictionary Match', 'Number Match', 'Abbreviation Match', 'Symbol Match', 'Repetitive Word'])
output_bigrams = open('output_table_bigrams.csv', 'wb')
output_bigrams_write = csv.writer(output_bigrams)
output_bigrams_write.writerow(['S.No', 'Bigram', 'Location Vector', 'Signature', 'Signature Weight', 'Tf', 'Idf', 'Tf - Idf Score', 'Is First Letter Capitalized', 'POS'])
output_trigrams = open('output_table_trigrams.csv', 'wb')
output_trigrams_write = csv.writer(output_trigrams)
output_trigrams_write.writerow(['S.No', 'Trigram', 'Location Vector', 'Signature', 'Signature Weight', 'Tf', 'Idf', 'Tf - Idf Score', 'Is First Letter Capitalized', 'POS'])
output_fourgrams = open('output_table_fourgrams.csv', 'wb')
output_fourgrams_write = csv.writer(output_fourgrams)
output_fourgrams_write.writerow(['S.No', 'Fourgram', 'Location Vector', 'Signature', 'Signature Weight', 'Tf', 'Idf', 'Tf - Idf Score', 'Is First Letter Capitalized', 'POS'])
output_fivegrams = open('output_table_fivegrams.csv', 'wb')
output_fivegrams_write = csv.writer(output_fivegrams)
output_fivegrams_write.writerow(['S.No', 'Fivegram', 'Location Vector', 'Signature', 'Signature Weight', 'Tf', 'Idf', 'Tf - Idf Score', 'Is First Letter Capitalized', 'POS'])
candidates = open('candidates.csv','wb')
candidates_write = csv.writer(candidates)
candidates_write.writerow(['S.No', 'Word', 'Location Vector', 'Signature', 'Tf', 'Idf', 'Tf - Idf Score'])
spec_ID = 0
path = sys.argv[1]
spec_words = []
bi_grams = []
tri_grams = []
four_grams = []
five_grams = []
loc_vec = LocationVector(path, spec_words, bi_grams, tri_grams, four_grams, five_grams)
loc_vec.generateLocationVector(loc_vec.parseXML(), [0])
spec_ID += 1
statement_facts_data = []
tagger = WordTagger(spec_ID, path, output_write, output_bigrams_write, output_trigrams_write, output_fourgrams_write, output_fivegrams_write, candidates_write, spec_words, bi_grams, tri_grams, four_grams, five_grams, statement_facts_data, corpus, corpus_bigrams, corpus_trigrams, corpus_fourgrams, corpus_fivegrams, spec_count, spec_word_count_list, spec_bi_gram_count_list, spec_tri_gram_count_list, spec_four_gram_count_list, spec_five_gram_count_list)
tagger.generateLocationVector(tagger.parseXML(), [0])
#tagger.printPercentageMatch()
#tagger.accuracy()
output.close()
output_bigrams.close()
output_trigrams.close()
output_fourgrams.close()
output_fivegrams.close()
candidates.close()
# ------------- printing out CLIPS facts ------------
word_facts_list = tagger.word_facts_data
statement_facts_list = tagger.statement_facts_data
facts_gen = ClipsFactsGeneration()
#facts_gen.generateWordFacts(word_facts_list)
#facts_gen.generateSentenceFacts(statement_facts_list)
# ------------- Word Bags ------------
# tfidf info list of N-grams
tf_idf_list = tagger.tf_idf_list # all spec words (unique)
tf_idf_bigram_list = tagger.tf_idf_bigram_list
tf_idf_trigram_list = tagger.tf_idf_trigram_list
tf_idf_fourgram_list = tagger.tf_idf_fourgram_list
tf_idf_fivegram_list = tagger.tf_idf_fivegram_list
# word bags Unigrams
tf_idf_common_eng_words = tagger.common_eng_words # common english excluding stopwords and words whose IDF = 1
tf_idf_nouns_unigrams = tagger.nouns_unigrams # uni-gram nouns excluding stopwords and words whose IDF = 1
tf_idf_loc_sig_link = tagger.loc_sig_link_unigrams # uni-grams whose location signature is "Link"
loc_sig_H1_unigrams = tagger.loc_sig_H1_unigrams
loc_sig_H2_unigrams = tagger.loc_sig_H2_unigrams
loc_sig_H3_unigrams = tagger.loc_sig_H3_unigrams
loc_sig_H4_unigrams = tagger.loc_sig_H4_unigrams
loc_sig_H5_unigrams = tagger.loc_sig_H5_unigrams
loc_sig_H6_unigrams = tagger.loc_sig_H6_unigrams
loc_sig_TD_unigrams = tagger.loc_sig_TD_unigrams
loc_sig_TH_unigrams = tagger.loc_sig_TH_unigrams
loc_sig_LI_Title_unigrams = tagger.loc_sig_LI_Title_unigrams
# ------------ word bags Bigrams -----------
tf_idf_bigram_NNP_NNP = tagger.bigram_NNP_NNP # bi-grams with NNP + NNP POS
tf_idf_bigram_NNP_NN = tagger.bigram_NNP_NN # bi-grams with NNP + NN POS
tf_idf_bigram_NN_NN = tagger.bigram_NN_NN # bi-grams with NN + NN POS
# ------------ word bags Trigrams -----------
tf_idf_trigram_NNP_NNP_NNP = tagger.trigram_NNP_NNP_NNP # tri-grams with NNP + NNP + NNP POS
tf_idf_trigram_NNP_NNP_NN = tagger.trigram_NNP_NNP_NN # tri-grams with NNP + NNP + NN POS
tf_idf_trigram_NNP_NN_NN = tagger.trigram_NNP_NN_NN # tri-grams with NNP + NN + NN POS
tf_idf_trigram_NN_NN_NN = tagger.trigram_NN_NN_NN # tri-grams with NN + NN + NN POS
# ------------ word bags Fourgrams -----------
tf_idf_fourgram_NNP_NNP_NNP_NNP = tagger.fourgram_NNP_NNP_NNP_NNP # fourgrams with NNP + NNP + NNP + NNP POS
# ------------ word bags Fivegrams -----------
tf_idf_fivegram_NNP_NNP_NNP_NNP_NNP = tagger.fivegram_NNP_NNP_NNP_NNP_NNP # fivegrams with NNP + NNP + NNP + NNP + NNP POS
def neuro_fuzzy(x):
range_span = RangeCalculator()
range_span.calculateFilterIRange(x)
#tf_idf_values = range_span.tf_idf_values
span = range_span.span
span_pivots = range_span.pivots
# ------------- Drawing Fuzzy & Scatter Plots --------------
#draw_fuzzy = FuzzyPlotFilterI()
#draw_fuzzy.drawFuzzyPlotFilterI(tf_idf_values, span)
#draw_scatter = ScatterPlot()
#draw_scatter.drawScatterPlot(tf_idf_values)
# ------------- calculating DOM of fuzzy sets --------------
dom = DegreeOfMembership()
dom.findFuzzySet(x, span, span_pivots)
y = dom.dom_data_list
return y
# ----------------------------------------------------------
u1 = neuro_fuzzy(tf_idf_common_eng_words)
u2 = neuro_fuzzy(tf_idf_nouns_unigrams)
u3 = neuro_fuzzy(tf_idf_loc_sig_link)
u4 = neuro_fuzzy(loc_sig_H1_unigrams)
u5 = neuro_fuzzy(loc_sig_H2_unigrams)
u6 = neuro_fuzzy(loc_sig_H3_unigrams)
u7 = neuro_fuzzy(loc_sig_H4_unigrams)
u8 = neuro_fuzzy(loc_sig_H5_unigrams)
u9 = neuro_fuzzy(loc_sig_H6_unigrams)
u10 = neuro_fuzzy(loc_sig_LI_Title_unigrams)
u11 = neuro_fuzzy(loc_sig_TD_unigrams)
u12 = neuro_fuzzy(loc_sig_TH_unigrams)
# feature set for bigrams
b1 = neuro_fuzzy(tf_idf_bigram_NNP_NNP)
b2 = neuro_fuzzy(tf_idf_bigram_NNP_NN)
b3 = neuro_fuzzy(tf_idf_bigram_NN_NN)
# feature set for trigrams
t1 = neuro_fuzzy(tf_idf_trigram_NNP_NNP_NNP)
t2 = neuro_fuzzy(tf_idf_trigram_NNP_NNP_NN)
t3 = neuro_fuzzy(tf_idf_trigram_NNP_NN_NN)
t4 = neuro_fuzzy(tf_idf_trigram_NN_NN_NN)
# feature set for fourgrams
f1 = neuro_fuzzy(tf_idf_fourgram_NNP_NNP_NNP_NNP)
# feature set for fivegrams
p1 = neuro_fuzzy(tf_idf_fivegram_NNP_NNP_NNP_NNP_NNP) # p --> penta = five
nf = NeuroFuzzySystem()
nf.neuroFuzzyModelling(tf_idf_list, u1, u2, u3, u4, u5, u6, u7, u8, u9, u10, u11, u12, tf_idf_bigram_list, b1, b2, b3, tf_idf_trigram_list, t1, t2, t3, tf_idf_fourgram_list, f1, tf_idf_fivegram_list, p1)
nf.normCOGUnigrams()
nf.normCOGBigrams()
nf.normCOGTrigrams()
nf.normCOGFourgrams()
nf.normCOGFivegrams()
PI_bundle_unigrams = NeuroFuzzySystem.PI_bundle_unigrams
PI_bundle_bigrams = NeuroFuzzySystem.PI_bundle_bigrams
PI_bundle_trigrams = NeuroFuzzySystem.PI_bundle_trigrams
PI_bundle_fourgrams = NeuroFuzzySystem.PI_bundle_fourgrams
PI_bundle_fivegrams = NeuroFuzzySystem.PI_bundle_fivegrams
section_bundle = tagger.sections
c = open('pi_sheet.csv', 'wb')
csv_1 = csv.writer(c)
sec = SectionWiseClustering(csv_1, PI_bundle_unigrams, PI_bundle_bigrams, PI_bundle_trigrams, PI_bundle_fourgrams, PI_bundle_fivegrams, section_bundle)
sec.findSectionHeaders()
c.close()
# ******** Proximity Finder ********
file_1 = open('pi_sheet.csv', 'rU')
csv_file_1 = csv.reader(file_1)
file_2 = open('modified_pi_sheet.csv', 'wb')
csv_file_2 = csv.writer(file_2)
pf = ProximityFinder(csv_file_1, csv_file_2)
pf.readPISheet()
pf.subSectionClustering()
pf.buildDistanceMatrix()
file_1.close()
file_2.close()
# ******** Proximity Finder ********
# ******** Concept Mining ********
file_3 = open('modified_pi_sheet.csv', 'rU')
csv_file_3 = csv.reader(file_3)
file_3_instance = open('modified_pi_sheet.csv', 'rU')
csv_file_3_instance = csv.reader(file_3_instance)
fc = FuzzyConcept(csv_file_3, csv_file_3_instance)
fc.normalizeProximityScores()
file_3.close()
file_4 = open('final_pi_sheet.csv', 'wb')
csv_file_4 = csv.writer(file_4)
fc.writeFinalPISheet(csv_file_4)
file_3_instance.close()
file_4.close()
file_5 = open('final_pi_sheet.csv', 'rU')
csv_file_5 = csv.reader(file_5)
g = pgv.AGraph(directed=False, strict=True)
fc.drawConceptGraphs(csv_file_5, g)
file_5.close()
pi_dict = fc.PI_dict
ps_dict = fc.PS_dict
skeletons = fc.skeletons
inference_paths = fc.inference_paths
# ******** Concept Mining ********
# ******** Concept Skeleton ********
out_1 = open('fuzzy_concepts.csv', 'wb')
csv_out_1 = csv.writer(out_1)
out_2 = open('PI.csv', 'wb')
csv_out_2 = csv.writer(out_2)
out_3 = open('PS.csv', 'wb')
csv_out_3 = csv.writer(out_3)
ske = ConceptSkeleton(pi_dict, ps_dict, csv_out_1, csv_out_2, csv_out_3, skeletons, inference_paths)
ske.extractConcepts()
ske.writeOutputsToCsvFiles()
out_1.close()
out_2.close()
out_3.close()
# ******** Concept Skeleton ********
#cog_list = nf.cog_list
#surface = SurfacePlotCOG()
#surface.drawSurfacePlot(cog_list)
# ------------------- letting NN do it's job --------------------
#nn = NeuralNetwork(tf_idf_list, f1, f2, f3)
#nn.trainNN()
# ---------------------------------------------------------------
output_csv = csv.reader(open('output_table.csv', 'rb'))
output_html = open('output_table.html', 'w')
html = CsvToHtml()
html.htmlOutputTable(output_csv, output_html)
output_bigrams_csv = csv.reader(open('output_table_bigrams.csv', 'rb'))
output_bigrams_html = open('output_table_bigrams.html', 'w')
html.htmlOutputTable(output_bigrams_csv, output_bigrams_html)
output_trigrams_csv = csv.reader(open('output_table_trigrams.csv', 'rb'))
output_trigrams_html = open('output_table_trigrams.html', 'w')
html.htmlOutputTable(output_trigrams_csv, output_trigrams_html)
output_fourgrams_csv = csv.reader(open('output_table_fourgrams.csv', 'rb'))
output_fourgrams_html = open('output_table_fourgrams.html', 'w')
html.htmlOutputTable(output_fourgrams_csv, output_fourgrams_html)
output_fivegrams_csv = csv.reader(open('output_table_fivegrams.csv', 'rb'))
output_fivegrams_html = open('output_table_fivegrams.html', 'w')
html.htmlOutputTable(output_fivegrams_csv, output_fivegrams_html)
candidates_csv = csv.reader(open('candidates.csv', 'rb'))
candidates_html = open('candidates.html', 'w')
html.htmlOutputTable(candidates_csv, candidates_html)
def _test():
import doctest
doctest.testmod()
# calculating lines of code of Project
cur_path = os.getcwd()
ignore_set = {"foo.py"}
loc_list = []
_test()
for pydir, _, pyfiles in os.walk(cur_path):
for pyfile in pyfiles:
if pyfile.endswith(".py") and pyfile not in ignore_set:
total_path = os.path.join(pydir, pyfile)
loc_list.append((len(open(total_path, "r").read().splitlines()), total_path.split(cur_path)[1]))
for line_number_count, filename in loc_list:
print "%05d lines in %s" % (line_number_count, filename)
print "\nTotal: %s lines (%s)" %(sum([x[0] for x in loc_list]), cur_path)