-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiki_classes.py
267 lines (173 loc) · 7.36 KB
/
wiki_classes.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
import urllib
from urllib import urlopen as uReq
import re
from string import ascii_lowercase as alpha
import bs4
from bs4 import BeautifulSoup as soup
from helper_functions import *
import pandas as pd
import networkx as nx
class wiki_page:
url = ''
def __init__(self, in_name):
page_name = in_name.split('.')[0]
self.name = page_name
self.supercategories = []
self.weight = 0
self.unweighted_main_topic_name = ""
self.weighted_main_topic_name = ""
self.tree_traversal_list = []
def add_supercategory(self, in_supercategory):
self.supercategories.append(in_supercategory)
def add_url(self, in_url):
self.url = in_url
def get_weight(self):
return self.weight
def get_highest_weighted_supercategory(self):
if self.supercategories:
highest_supercategory = self.supercategories[0]
highest_supercategory_weight = self.supercategories[0].get_weight()
if len(self.supercategories) > 1:
for supercategory in self.supercategories:
if supercategory.get_weight() > highest_supercategory_weight:
highest_supercategory = supercategory
highest_supercategory_weight = supercategory.get_weight()
return highest_supercategory
else:
return self
def add_unweighted_main_topic(self, in_main_topic):
self.unweighted_main_topic = in_main_topic.name
def get_unweighted_main_topic(self):
return self.unweighted_main_topic_name
def set_weighted_main_topic(self, starting_topic):
if len(self.tree_traversal_list) == 0:
self.weighted_main_topic_name == starting_topic
elif self.tree_traversal_list[-1] == starting_topic:
self.weighted_main_topic_name = self.tree_traversal_list[-2].name
else:
self.weighted_main_topic_name = self.tree_traversal_list[-1].name
def get_weighted_main_topic(self):
return self.weighted_main_topic_name
def add_tree_traversal_list(self, in_category_list):
self.tree_traversal_list = in_category_list
def add_weight(self, in_weight):
self.weight += in_weight
def make_node(self):
print 'TODO'
class category(wiki_page):
def __init__(self, in_name):
page_name = in_name.split('.')[0]
self.name = page_name
self.supercategories = []
self.subcategories = []
self.subpages = []
self.weight = 0
self.has_populated = "false"
self.closest_main_topic_name = ""
def add_subcategory(self,in_subcategory):
self.subcategories.append(in_subcategory)
self.weight += 1
def add_subpage(self, in_subpage, subpage_weight_factor):
self.subpages.append(in_subpage)
self.weight += 1 * subpage_weight_factor
return self.get_weight()
def populate_weight(self):
weight = self.weight
if self.has_populated == "true":
return
for supercategory in self.supercategories:
supercategory.add_weight(weight)
self.has_populated = "true"
def get_subcategories(current_category, page_soup, urls, BASE_URL, fixed_name, incomplete_categories, Graph):
subcategory_data = page_soup.findAll("a", {"class":"CategoryTreeLabel"})
name = fixed_name.replace('_', ":", 1)
next_page_data = page_soup.findAll("a", {"title":name})
next_page = check_for_next_page(name, next_page_data)
if next_page != 'none':
incomplete_categories.append(current_category.name)
for subcategory in subcategory_data:
class_name = fix_name(subcategory["href"])
if len(urls) < 30000:
urls.append(BASE_URL + subcategory["href"])
current_category.add_subcategory(class_name)
Graph.add_edge(current_category.name, class_name)
def get_pages(current_category, page_soup, page_list, fixed_name, BASE_URL, SUBPAGE_WEIGHT_FACTOR, Graph):
url_list = []
pagedata = page_soup.findAll("div", {"id":"mw-pages"})
#print str(pagedata)
text = str(pagedata)
page_pattern = '<a href="(/wiki/[^"]+)" title="([^"]+)"'
matches = re.findall(page_pattern, text)
name = fixed_name.replace('_', ":", 1)
name = name.replace('_', " ")
next_page_data = page_soup.findAll("a", {"title":name})
next_page = check_for_next_page(name, next_page_data)
for hit in matches:
if BASE_URL + hit[0] != 'https://en.wikipedia.org/wiki/Wikipedia:FAQ/Categorization#Why_might_a_category_list_not_be_up_to_date?':
url_list.append(BASE_URL + hit[0])
page_class_name = fix_name(BASE_URL + hit[0])
page_class = wiki_page(page_class_name)
page_class.add_supercategory(current_category)
page_class.add_url(BASE_URL + hit[0])
current_category.add_subpage(page_class_name, SUBPAGE_WEIGHT_FACTOR)
page_list.append(page_class)
Graph.add_edge(current_category.name, page_class_name)
if next_page != "none":
new = "https://en.wikipedia.org/w/index.php?title=" + name.split('/')[-1] + "&from="
num = new + "0"
urls = []
uClient = uReq(num)
page_html = uClient.read()
uClient.geturl()
uClient.close()
page_soup = soup(page_html, "html.parser")
pagedata = page_soup.findAll("div", {"id":"mw-pages"})
text = str(pagedata)
page_pattern = '<a href="(/wiki/[^"]+)" title="([^"]+)"'
matches = re.findall(page_pattern, text)
for page in pagedata:
for hit in matches:
if BASE_URL + hit[0] != 'https://en.wikipedia.org/wiki/Wikipedia:FAQ/Categorization#Why_might_a_category_list_not_be_up_to_date?':
if BASE_URL + hit[0] not in url_list:
url_list.append(BASE_URL + hit[0])
page_class_name = fix_name(BASE_URL + hit[0])
page_class = wiki_page(page_class_name)
page_class.add_supercategory(current_category)
page_class.add_url(BASE_URL + hit[0])
current_category.add_subpage(page_class_name, SUBPAGE_WEIGHT_FACTOR)
page_list.append(page_class)
Graph.add_edge(current_category.name, page_class_name)
for c in alpha:
alphabet = new + c
uClient = uReq(alphabet)
page_html = uClient.read()
uClient.geturl()
uClient.close()
page_soup = soup(page_html, "html.parser")
pagedata = page_soup.findAll("div", {"id":"mw-pages"})
text = str(pagedata)
page_pattern = '<a href="(/wiki/[^"]+)" title="([^"]+)"'
matches = re.findall(page_pattern, text)
for page in pagedata:
for hit in matches:
if BASE_URL + hit[0] != 'https://en.wikipedia.org/wiki/Wikipedia:FAQ/Categorization#Why_might_a_category_list_not_be_up_to_date?':
if BASE_URL + hit[0] not in url_list:
url_list.append(BASE_URL + hit[0])
page_class_name = fix_name(BASE_URL + hit[0])
page_class = wiki_page(page_class_name)
page_class.add_supercategory(current_category)
page_class.add_url(BASE_URL + hit[0])
current_category.add_subpage(page_class_name, SUBPAGE_WEIGHT_FACTOR)
page_list.append(page_class)
Graph.add_edge(current_category.name, page_class_name)
return page_list
def get_supercategories(current_category, page_soup, BASE_URL, Graph):
supercategory_data = page_soup.findAll("div", {"class":"mw-normal-catlinks"})
text = str(supercategory_data)
pattern = '<a href="(/wiki/[^"]+)" title="([^"]+)"'
matches = re.findall(pattern, text)
for hit in matches:
if BASE_URL + hit[0] != 'https://en.wikipedia.org/wiki/Help:Category':
page_class_name = wiki_page(fix_name(BASE_URL + hit[0]))
current_category.add_supercategory(page_class_name)
Graph.add_edge(fix_name(BASE_URL + hit[0]), current_category.name)