forked from snarron/doctorbook
/
get_wiki_text.py
195 lines (151 loc) · 6.66 KB
/
get_wiki_text.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
#!/usr/bin/env python
import re
import yaml
import urllib
import urllib2
class WikipediaError(Exception):
pass
class Wikipedia:
url_article = 'http://%s.wikipedia.org/w/index.php?action=raw&title=%s'
url_image = 'http://%s.wikipedia.org/w/index.php?title=Special:FilePath&file=%s'
url_search = 'http://%s.wikipedia.org/w/api.php?action=query&list=search&srsearch=%s&sroffset=%d&srlimit=%d&format=yaml'
def __init__(self, lang):
self.lang = lang
def __fetch(self, url):
request = urllib2.Request(url)
request.add_header('User-Agent', 'Mozilla/5.0')
try:
result = urllib2.urlopen(request)
except urllib2.HTTPError, e:
raise WikipediaError(e.code)
except urllib2.URLError, e:
raise WikipediaError(e.reason)
return result
def article(self, url):
article = url[url.rfind('/')+1:]
url = self.url_article % (self.lang, urllib.quote_plus(article))
content = self.__fetch(url).read()
if content.upper().startswith('#REDIRECT'):
match = re.match('(?i)#REDIRECT \[\[([^\[\]]+)\]\]', content)
if not match == None:
return self.article(match.group(1))
raise WikipediaError('Can\'t found redirect article.')
return content
def image(self, image, thumb=None):
url = self.url_image % (self.lang, image)
result = self.__fetch(url)
content = result.read()
if thumb:
url = result.geturl() + '/' + thumb + 'px-' + image
url = url.replace('/commons/', '/commons/thumb/')
url = url.replace('/' + self.lang + '/', '/' + self.lang + '/thumb/')
return self.__fetch(url).read()
return content
def search(self, query, page=1, limit=10):
offset = (page - 1) * limit
url = self.url_search % (self.lang, urllib.quote_plus(query), offset, limit)
content = self.__fetch(url).read()
parsed = yaml.load(content)
search = parsed['query']['search']
results = []
if search:
for article in search:
title = article['title'].strip()
snippet = article['snippet']
snippet = re.sub(r'(?m)<.*?>', '', snippet)
snippet = re.sub(r'\s+', ' ', snippet)
snippet = snippet.replace(' . ', '. ')
snippet = snippet.replace(' , ', ', ')
snippet = snippet.strip()
wordcount = article['wordcount']
results.append({
'title' : title,
'snippet' : snippet,
'wordcount' : wordcount
})
# yaml.dump(results, default_style='', default_flow_style=False,
# allow_unicode=True)
return results
class Wiki2Plain:
# url_article = 'http://%s.wikipedia.org/w/index.php?action=raw&title=%s'
url_image = 'http://%s.wikipedia.org/w/index.php?title=Special:FilePath&file=%s'
# url_search = 'http://%s.wikipedia.org/w/api.php?action=query&list=search&srsearch=%s&sroffset=%d&srlimit=%d&format=yaml'
def __init__(self, url):
self.wiki = Wikipedia('en')
self.wiki_article = self.wiki.article(url)
self.text = self.wiki_article
self.text = self.unhtml(self.text)
self.text = self.unwiki(self.text)
self.text = self.punctuate(self.text)
self.text = self.get_summary(self.text)
def __str__(self):
return self.text
def unwiki(self, wiki):
"""
Remove wiki markup from the text.
"""
wiki = re.sub(r'(?i)\{\{IPA(\-[^\|\{\}]+)*?\|([^\|\{\}]+)(\|[^\{\}]+)*?\}\}', lambda m: m.group(2), wiki)
wiki = re.sub(r'(?i)\{\{Lang(\-[^\|\{\}]+)*?\|([^\|\{\}]+)(\|[^\{\}]+)*?\}\}', lambda m: m.group(2), wiki)
wiki = re.sub(r'\{\{[^\{\}]+\}\}', '', wiki)
wiki = re.sub(r'(?m)\{\{[^\{\}]+\}\}', '', wiki)
wiki = re.sub(r'(?m)\{\|[^\{\}]*?\|\}', '', wiki)
wiki = re.sub(r'(?i)\[\[Category:[^\[\]]*?\]\]', '', wiki)
wiki = re.sub(r'(?i)\[\[Image:[^\[\]]*?\]\]', '', wiki)
wiki = re.sub(r'(?i)\[\[File:[^\[\]]*?\]\]', '', wiki)
wiki = re.sub(r'\[\[[^\[\]]*?\|([^\[\]]*?)\]\]', lambda m: m.group(1), wiki)
wiki = re.sub(r'\[\[([^\[\]]+?)\]\]', lambda m: m.group(1), wiki)
wiki = re.sub(r'\[\[([^\[\]]+?)\]\]', '', wiki)
wiki = re.sub(r'(?i)File:[^\[\]]*?', '', wiki)
wiki = re.sub(r'\[[^\[\]]*? ([^\[\]]*?)\]', lambda m: m.group(1), wiki)
wiki = re.sub(r"''+", '', wiki)
wiki = re.sub(r'(?m)^\*$', '', wiki)
return wiki
def unhtml(self, html):
"""
Remove HTML from the text.
"""
html = re.sub(r'(?i) ', ' ', html)
html = re.sub(r'(?i)<br[ \\]*?>', '\n', html)
html = re.sub(r'(?m)<!--.*?--\s*>', '', html)
html = re.sub(r'(?i)<ref[^>]*>[^>]*<\/ ?ref>', '', html)
html = re.sub(r'(?m)<.*?>', '', html)
html = re.sub(r'(?i)&', '&', html)
return html
def punctuate(self, text):
"""
Convert every text part into well-formed one-space
separate paragraph.
"""
text = re.sub(r'\r\n|\n|\r', '\n', text)
text = re.sub(r'\n\n+', '\n\n', text)
parts = text.split('\n\n')
partsParsed = []
for part in parts:
part = part.strip()
if len(part) == 0:
continue
partsParsed.append(part)
return '\n\n'.join(partsParsed)
def get_summary(self,text):
text = text[:text.find('==')]
return text
def image(self):
url_image = 'http://simple.wikipedia.org/w/index.php?title=Special:FilePath&file='
"""
Retrieve the first image in the document.
"""
# match = re.search(r'(?i)\|?\s*(image|img|image_flag)\s*=\s*(<!--.*-->)?\s*([^\\/:*?<>"|%]+\.[^\\/:*?<>"|%]{3,4})', self.wiki)
match = re.search(r'= (\b[\w ]+\b)+.(gif|jpg|jpeg|png|bmp)', self.wiki_article)
if match:
image_url = url_image + '%s.%s' % match.groups()
image_url = re.sub(' ', '_', image_url)
return image_url
return None
if __name__ == '__main__':
wiki2plain = Wiki2Plain('http://en.wikipedia.org/wiki/Labor_union')
content = wiki2plain.text
image = wiki2plain.image()
print '---'
print content
print '---'
print image