-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.py
177 lines (146 loc) · 4.6 KB
/
code.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
import xml.etree.ElementTree as ET
from collections import defaultdict
import operator
import sys
stop_words = input().split(';')
index_terms = input().split(';')
#document_xml = input()
document_xml = str(sys.stdin.read())
document_xml = document_xml.lower()
tree = ET.fromstring(document_xml)
special_tags = ['title', 'abstract', 'body']
punctuation = [',' , '.' , '?' , '!']
def normalize(data):
normalised=''
for char in data:
if char not in punctuation:
normalised = normalised+char
data = normalised.split(' ')
return data
def count(data):
length=0
for j in data:
if j not in stop_words:
if(len(j)>=4):
length+=1
return length
l1=l2=l3=0
d=defaultdict(int)
for i in special_tags:
e=tree.find(i)
if(i=='title'):
if not e:
#no nested tags
l = tree.findall('.//title')
for tag in l:
#print(tag)
data1=tag.text
d1=[]
d1=normalize(data1)
l1=count(d1)
for j in d1:
for k in index_terms:
if k==j:
d[k]+=5
else:
for tags in tree.find('.//title').iter():
data1=tags.text
data1=data1.split(' ')
normalised=''
for strings in data1:
for char in strings:
if char not in punctuation:
normalised = normalised+char
normalised+=' '
data1 = normalised.split(' ')
for j in data1:
if j not in stop_words:
if(len(j)>=4):
l3+=1
for k in index_terms:
if k==j:
d[k]+=5
elif(i=='abstract'):
if not e:
#no nested tags
l = tree.findall('.//abstract')
for tag in l:
#print(tag)
data2=tag.text
d2=[]
d2=normalize(data2)
l2=count(d2)
for j in d2:
for k in index_terms:
if k==j:
d[k]+=3
else:
for tags in tree.find('.//abstract').iter():
data2=tags.text
data2=data2.split(' ')
normalised=''
for strings in data2:
for char in strings:
if char not in punctuation:
normalised = normalised+char
normalised+=' '
data2 = normalised.split(' ')
for j in data2:
if j not in stop_words:
if(len(j)>=4):
l3+=1
for k in index_terms:
if k==j:
d[k]+=3
elif(i=='body'):
if not e:
#no nested tags
l = tree.findall('.//abstract')
for tag in l:
#print(tag)
data3=tag.text
d3=[]
d3=normalize(data3)
l3=count(d3)
for j in d3:
for k in index_terms:
if k==j:
d[k]+=1
else:
for tags in tree.find('.//body').iter():
data3=tags.text
data3=data3.split(' ')
normalised=''
for strings in data3:
for char in strings:
if char not in punctuation:
normalised = normalised+char
normalised+=' '
data3 = normalised.split(' ')
for j in data3:
if j not in stop_words:
if(len(j)>=4):
l3+=1
for k in index_terms:
if k==j:
d[k]+=1
#print(d)
#print(l1,l2,l3)
L=l1+l2+l3
kd={}
for k in d.keys():
den=float(d[k]/L)*100
kd[k]=den;
#kd = sorted(kd.items(), key=lambda x: x[1], reverse=True)
kd = dict( sorted(kd.items(), key=operator.itemgetter(1),reverse=True))
flag=0
for q,r in kd.items():
flag+=1
if(flag<=3):
q=q+':'
print(q,r)
f=r
if(flag==4):
if(r==f):
q=q+':'
print(q,r)