-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyHTMLParsers.py
151 lines (126 loc) · 4.75 KB
/
MyHTMLParsers.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
import urllib.request
from html.parser import HTMLParser
import NamesDealer
class MyHtmlParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.scenes = {0: {}}
self.num_words = 0
self.sc_num = 0
self.chars = []
self.rec_char = False
self.speaks = False
self.speaker = ''
self.rec_speaker = False
self.rec_url = False
self.saw_h3 = False
self.saw_main = False
def handle_starttag(self, tag, attrs):
if tag == 'h2': ## usually represent scene
self.end_scene()
self.sc_num += 1
self.scenes[self.sc_num] = {}
self.rec_url = True
if tag == 'a' and self.rec_url: ## if the sceenes in another page
for attr in attrs:
if attr[0] == 'href':
self.rec_url = False
url = attr[1]
sc = urllib.request.urlopen(url).read().decode('utf8')
htmp2 = MyHTMLParser2()
htmp2.feed(sc)
self.parsers_union(htmp2)
break
if tag == 'h3':
self.saw_h3 = True
if self.saw_main:
return
self.end_scene()
self.sc_num += 1
self.scenes[self.sc_num] = {}
if tag == 'big':
self.rec_speaker = True
if tag == 'small':
self.rec_speaker = False
def parsers_union(self, parser): ## in case that another parser parse scene of this play, take the data
for char in parser.chars:
if not char in self.chars:
self.chars.append(char)
for char in parser.scenes:
self.scenes[self.sc_num][char] = self.scenes[self.sc_num].get(char, 0) + parser.scenes[char]
def end_scene(self):
if self.num_words > 0:
for char in self.scenes[self.sc_num]:
self.scenes[self.sc_num][char] /= self.num_words ## take the proprtional part of the talking
self.num_words = 0
def handle_endtag(self, tag):
if tag == 'big' and not self.rec_char:
self.speaks = True
if tag == 'main':
self.saw_main = True
if self.saw_h3:
return
def handle_data(self, data):
if self.rec_char:
self.chars.append(NamesDealer.no_nikkud(data))
self.rec_char = False
if self.rec_speaker:
self.speaker = NamesDealer.no_nikkud(data)
if self.speaker not in self.chars:
self.chars.append(self.speaker)
try:
self.scenes[self.sc_num][self.speaker] += 1
except:
self.scenes[self.sc_num][self.speaker] = 1
self.rec_speaker = False
if self.speaks:
#if self.speaker == '':
# print(data)
data = data.split(' ')
self.scenes[self.sc_num][self.speaker] = self.scenes[self.sc_num].get(self.speaker, 0) + len(data)
self.num_words += len(data)
self.apeaks = False
# In[5]:
class MyHTMLParser2(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.scenes = {}
self.num_words = 0
self.chars = []
self.speaks = False
self.speaker = ''
self.rec_speaker = False
self.ignore = False
def handle_starttag(self, tag, attrs):
if tag == 'h3':
self.end_scene()
return
if tag == 'big':
self.rec_speaker = True
if tag == 'small':
self.ignore = True
def end_scene(self):
if self.num_words > 0:
for char in self.scenes:
self.scenes[char] /= self.num_words
self.num_words = 0
def handle_endtag(self, tag):
if tag == 'big':
self.speaks = True
if tag == 'small':
self.ignore = False
def handle_data(self, data):
if self.rec_speaker:
self.speaker = NamesDealer.no_nikkud(data)
if self.speaker not in self.chars:
self.chars.append(self.speaker)
try:
self.scenes[self.speaker] += 1
except:
self.scenes[self.speaker] = 1
self.rec_speaker = False
if self.speaks and not self.ignore:
data = data.split(' ')
self.scenes[self.speaker] = self.scenes.get(self.speaker, 0) + len(data)
self.num_words += len(data)
self.apeaks = False