-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexplist.py
86 lines (73 loc) · 2.71 KB
/
explist.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
import http.client
import re
import collections
import operator
def getTranscript(num, debug = 0):
conn = http.client.HTTPSConnection("www.explainxkcd.com")
searched = r"<span.*?>\s*Transcript\s*</span>.*?</h\d>(.*?)<h\d>"
#probably not the best way to do it but it works for:
#1, 25, 180, 500, 1000, 1500, 1840
conn.request("GET", "/wiki/index.php/"+str(num))
site = conn.getresponse().read().decode("utf-8")
if debug:
print(site)
m = re.search(searched, site, re.I | re.DOTALL)
if m == None or m.lastindex == None:
print("\rno transcript found for "+str(num))
if debug:
print("no transcript found for "+str(num))
return "";#no transcript found
if debug:
print(m.group(0)+"\n")
desc = m.group(1)
if debug:
print(desc+"\n")
desc = re.sub("<.*?>", "",desc) #remove html markups
if debug:
print(desc+"\n")
desc = re.sub(r"\[.*?\]", "",desc) # remove [situation description]
if debug:
print(desc+"\n")
desc = re.sub(r"\\n", " ",desc) # remove "\n"s
if debug:
print(desc+"\n")
desc = re.sub(" .*", "",desc) #remove things like "add a comment" (at the end)
if debug:
print(desc+"\n")
return desc
def splitWords(transcript):
words = list()
wordRegex = re.compile(r"\b(\w+)\b")
for word in re.findall(wordRegex,transcript):
#print(word)
words.append(word)
return words
class WordCounter:
def __init__(self):
self.counter = collections.OrderedDict()
def add(self, word):
if word in self.counter:
self.counter[word] += 1
else:
self.counter[word] = 1
wordCounter = WordCounter()
first = 1
last = 2090
total = last-first+1
out_file = 'list.txt'
print(wordCounter.counter)
for i in range(first, last+1):
print('\r%i out of %i done' % (i-first, total), end = '\r')
transcript = getTranscript(i)
words = splitWords(transcript)
for word in words:
wordCounter.add(word.lower())
print('\n%i out of %i done' % (last-first+1, total))
print('saving results to %s' % out_file)
stats = sorted(wordCounter.counter.items(),
key=operator.itemgetter(1),
reverse = True)
with open(out_file, 'w', encoding='utf-8') as f:
for wordStat in stats:
#print(wordStat)
print(wordStat, file=f)