-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtask3.py
158 lines (105 loc) · 4.03 KB
/
task3.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
import argparse
import math
def main():
# construct the argument parser and parse the arguments (input, output path )
ap = argparse.ArgumentParser()
ap.add_argument("-i",type=str, required=True,
help="path to inputfile for the third task")
ap.add_argument("-o",type=str, required=True,
help="path to output file if it exists, if not, it will be created")
args = vars(ap.parse_args())
# Opening the input file
i=open(args["i"], "r")
# Opening the output file ( or creating it if it doesnt exist)
o=open(args["o"], "w+")
if i.mode == 'r':
# Reading the lines in the input file
contents =i.readlines()
# Getting the titles of the documents and word count for each document
header_titles = []
# Variables for use later on
dc = []
wd = []
dc.append([contents[0].split("\t")[0],len(contents[0].split("\t")[1:])])
temp = contents[0].split("\t")[1:]
for t in temp :
t = t.split("\n")[0]
header_titles.append(t)
wd.append([t,1])
for line in contents[1:] :
dc.append([line.split("\t")[0],len(line.split("\t")[1:])])
temp = line.split("\t")[1:]
for t in temp :
t = t.split("\n")[0]
exists = False
for header in header_titles :
if t == header:
exists = True
break
if not exists :
header_titles.append(t)
wd.append([t,1])
else :
for j in range(len(wd)):
if t == wd[j][0]:
wd[j][1] += 1
#Writing the titles
title_line = "\t"
for j in range(0,len(header_titles)):
title_line += header_titles[j]
if j<len(title_line)-1 :
title_line += "\t"
o.write(title_line + "\n")
#Preparing the TF-IDF weights variables
# Number of times a word appears in a document
word_cardinality = []
for line in contents :
vector = []
for head in header_titles:
exists = 0
for word in line.split("\t")[1:]:
word = word.split("\n")[0]
if head == word :
exists = 1
break
else :
exists =0
vector.append(exists)
word_cardinality.append(vector)
# Total number of documents
total_num_docs = len(header_titles)
# For each word, the number of docs it appears in
doc_card = dc
# the word count in each document
word_count = wd
# Computing final outputs
output_weights = []
for j in range(len(doc_card)):
weight_line = []
weight_line.append(doc_card[j][0])
for k in range(len(word_count)):
# IDF(t) = log_e(Total number of documents / Number of documents with term t in it).
IDF = round(math.log(total_num_docs/doc_card[j][1]),3)
# TF(t) = (Number of times term t appears in a document) / (Total number of terms in the document)
TF = round(word_cardinality[j][k]/word_count[k][1],3)
# TF-IDF weight
weight = round(IDF*TF,3)
weight_line.append(weight)
output_weights.append(weight_line)
# Saving the weights in the output file
for w in output_weights:
output_line = w[0] + "\t"
for j in range(1,len(w)):
output_line += str(w[j])
if j<len(w)-1:
output_line += "\t"
else :
output_line += "\n"
o.write(output_line)
else :
return "error reading input file"
# Closing the file readers
o.close()
i.close()
if __name__ == "__main__":
main()