-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfirstparagraph.py
314 lines (287 loc) · 10.2 KB
/
firstparagraph.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Public domain; MZMcBride; 2012
debug = False
test_case = False
"""
; PASSING TEST CASES (RETURN SNIPPET)
* Suite
* Metasyntactic variable
* Mubarak
* Barack Obama
* iPod
* Roe v. Wade
* [empty string]
* Atalaya Castle
* Atalaya Castle (Spain)
* William Morrison (dentist)
* Kohei Yoshiyuki
* Mass–energy equivalence
* Novye Aldi massacre
* Paul Simon (politician)
* Hilary Clinton
* FAP 403 RHD
* List of television stations in Bangkok
* Quincy-Voisins
* Dannemarie, Haut-Rhin
* \
; PASSING TEST CASES (RETURN ERROR)
* Suiteeee
; PASSING TEST CASES (RETURN URL)
* Fry's
FAILING TEST CASES
* Schwa
* PIR
* ROC
* blaze of glory
* Key bridge
* S&M (song)
* AT&T
* Dick Dawkins
* wikt:santorum
; TO-DO
* Truncate output more cleanly?
* Remove references?
"""
if debug or test_case:
import sys
import re
import urllib
import urllib2
import json
import htmlentitydefs
from bs4 import BeautifulSoup
base_url = 'https://en.wikipedia.org'
api_url = base_url+'/w/api.php'
def get_random_article_title():
values = {'action' : 'query',
'list' : 'random',
'rnlimit' : '1',
'rnnamespace' : '0',
'format' : 'json'}
query_url = api_url+'?'+urllib.urlencode(values)
url_contents = urllib.urlopen(query_url).read()
parsed_content = json.loads(url_contents)
random_article_title = parsed_content['query']['random'][0]['title'].encode('utf-8')
return random_article_title
def get_page_section(article):
values = {'action' : 'query',
'prop' : 'revisions',
'rvlimit' : '1',
'rvprop' : 'content',
'format' : 'json',
'rvsection' : '0',
'titles' : article}
query_url = api_url+'?'+urllib.urlencode(values)+'&redirects'
if debug:
print query_url
url_contents = urllib.urlopen(query_url).read()
parsed_content = json.loads(url_contents)
page_id = str(parsed_content['query']['pages'].keys()[0])
if int(page_id) < 0:
return False
page_section = parsed_content['query']['pages'][page_id]['revisions'][0]['*']
return page_section
def get_parsed_page_section(page_section):
values = {'action' : 'parse',
'prop' : 'text',
'format' : 'json',
'text' : page_section.encode('utf-8')}
data = urllib.urlencode(values)
req = urllib2.Request(api_url, data)
response = urllib2.urlopen(req)
url_contents = response.read()
if debug:
print url_contents
parsed_content = json.loads(url_contents)
parsed_page_section = parsed_content['parse']['text']['*']
return parsed_page_section
# From http://effbot.org/zone/re-sub.htm
def unescape(text):
def fixup(m):
text = m.group(0)
if text[:2] == "&#":
# character reference
try:
if text[:3] == "&#x":
return unichr(int(text[3:-1], 16))
else:
return unichr(int(text[2:-1]))
except ValueError:
pass
else:
# named entity
try:
text = unichr(htmlentitydefs.name2codepoint[text[1:-1]])
except KeyError:
pass
return text # leave as is
return re.sub("&#?\w+;", fixup, text)
def is_ambiguous(article):
clean_article = re.sub(' ', '_', article)
line = '%s is ambiguous: <%s/wiki/%s>.' % (article, base_url, urllib.quote(clean_article))
return line
def search_initial_bold(article, page_section):
clean_page_section = format_line(page_section)
global line
first_part = get_first_part(article)
try:
coded_line_lower = clean_page_section.decode('utf-8').lower()
except UnicodeEncodeError:
coded_line_lower = clean_page_section.encode('utf-8').lower()
if re.search(r"<p><b>%s" % re.escape(first_part), page_section, re.I):
line = format_line(page_section)
if line.endswith(':'):
line = is_ambiguous(article)
return line
elif re.search(r'<p>.*?<b>.*?%s' % re.escape(first_part), page_section[:50], re.I):
line = format_line(page_section)
if line.endswith(':'):
line = is_ambiguous(article)
return line
elif first_part.lower().split(' ') <= coded_line_lower.split(' '):
line = format_line(page_section)
if line.endswith(':'):
line = is_ambiguous(article)
return line
return False
def search_bold_sentence(article, page_section):
global line
for line in page_section.strip('\n').split('\n'):
if line.startswith('<p>'):
if re.search(r'<b>'+re.escape(article)+r'</b>', line, re.I):
line = format_line(line)
if line.endswith(':'):
line = is_ambiguous(article)
return line
return False
def search_first_paragraph(article, page_section):
global line
first_part = get_first_part(article)
for line in page_section.strip('\n').split('\n'):
if line.startswith('<p>'):
try:
coded_line_lower = line.decode('utf-8').lower()
except UnicodeEncodeError:
coded_line_lower = line.encode('utf-8').lower()
if article.lower() in coded_line_lower:
line = format_line(line)
if line.endswith(':'):
line = is_ambiguous(article)
return line
elif first_part.split(' ') <= coded_line_lower.split(' '):
line = format_line(line)
if line.endswith(':'):
line = is_ambiguous(article)
return line
return False
def remove_sup(line):
sup_re = re.compile(r'\s*<sup.+?</sup>\s*')
for match in sup_re.finditer(line):
line = re.sub(sup_re, '', line)
return line
def format_line(line):
#line = remove_sup(line)
# Remove all HTML elements
line = ''.join(BeautifulSoup(line, 'html.parser').findAll(text=True))
# Clean up any random entities and other bullshit
line = unescape(line)
return line
def get_first_part(article):
first_part = article
first_part = article.split(' ', 1)[0]
if article.find(', ') != -1:
first_part = article.split(', ', 1)[0]
return first_part
def guess_line(article):
global line
page_section = get_page_section(article)
if not page_section:
# Article does not exist, just say so
line = '"%s" does not exist.' % (article)
return line.encode('utf-8')
else:
parsed_page_section = get_parsed_page_section(page_section)
if debug:
print parsed_page_section
# Build a BeautifulSoup object
soup = BeautifulSoup(parsed_page_section, 'html.parser')
# Kill all tables!
for match in soup.findAll('table'):
if debug:
print len(soup.findAll('table'))
match = str(match).decode('utf-8')
c_match = re.sub(r'\s{2,}', ' ', match)
c_match = unescape(c_match)
parsed_page_section = re.sub(r'\s{2,}', ' ', parsed_page_section)
parsed_page_section = unescape(parsed_page_section)
if c_match in parsed_page_section and debug:
print 'this is true'
parsed_page_section = parsed_page_section.replace(c_match, '')
# Kill any spurious breaks
for match in soup.findAll('br'):
match = str(match).decode('utf-8')
c_match = re.sub(r'\s{2,}', ' ', match)
parsed_page_section = re.sub(r'\s{2,}', ' ', parsed_page_section)
if c_match in parsed_page_section and debug:
print 'this is true too'
parsed_page_section = parsed_page_section.replace(c_match, '')
# Back to a string; strip newlines
clean_parsed_page_section = parsed_page_section.replace('\n', '')
# Now iterate through the 'p' elements and try to grab the appropriate one
soup2 = BeautifulSoup(clean_parsed_page_section, 'html.parser')
if debug:
print soup2
for p in soup2.findAll('p'):
stripped_p = ''.join(p.findAll(text=True)).encode('utf-8')
first_part = get_first_part(article)
if debug:
print len(first_part)+100
print len(stripped_p)
print first_part
print stripped_p
print first_part.lower().split(' ')
print stripped_p.lower().split(' ')
if first_part.lower().split(' ') < stripped_p.lower().split(' '):
print 'penis12'
if ((first_part.lower().split(' ') < stripped_p.lower().split(' ')
or len(stripped_p) > (len(first_part)+120))
and len(stripped_p) > len(first_part)):
first_parsed_paragraph = str(p).decode('utf-8')
break
else:
first_parsed_paragraph = str(p).decode('utf-8')
if debug:
print 'penis2'
print first_parsed_paragraph
target_paragraph = first_parsed_paragraph
if debug:
print target_paragraph
clean_target_paragraph = ''.join(BeautifulSoup(first_parsed_paragraph, 'html.parser').findAll(text=True))
if search_initial_bold(article, target_paragraph):
if debug:
print 'success?'
elif search_bold_sentence(article, target_paragraph):
if debug:
print 'success!'
elif search_first_paragraph(article, target_paragraph):
if debug:
print 'success.'
else:
clean_article = re.sub(' ', '_', article)
line = '%s/wiki/%s' % (base_url, urllib.quote(clean_article))
if debug:
print 'failure :-('
if len(line) > 400:
line = line[:400].rsplit(' ', 1)[0] + ' ...'
return line.encode('utf-8')
if debug or test_case:
try:
article = sys.argv[1]
except IndexError:
article = get_random_article_title()
if not article.strip():
article = get_random_article_title()
print article
line = guess_line(article)
print line