forked from stanford-oval/storm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikipage_extractor.py
223 lines (187 loc) · 8.17 KB
/
wikipage_extractor.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
import argparse
import json
import os
import pathlib
import re
import pandas as pd
import requests
import wikipediaapi
from bs4 import BeautifulSoup
from flair.data import Sentence
from flair.nn import Classifier
from tqdm import tqdm
def get_references(sentence, reference_dict):
"""
Given a sentence, extract all reference index and find links from dictionary,
then remove reference brackets from original sentences
@param sentence, sentence to process
@param reference_dict, dictionary of references
@return cleaned sentence, reference_list pair
"""
refs = re.findall(r'\[\d+\]', sentence)
sentence = re.sub(r'\[\d+\]', '', sentence).strip().replace("\n", "")
return sentence, [reference_dict[ref.replace("[", "").replace("]", "")] for ref in refs]
def extract_data(url, reference_dict):
"""
Extract section data from wiki url.
@param url: wiki url
@reference_dict, reference dict from extract_references()
@return a dictionary, key is section / subsection name, value is a list of {"sentence": ..., "refs": []}
"""
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
data = {}
for header in soup.find_all(['h1', 'h2', 'h3', "h4", "h5", "h6"]):
section_title = header.text.replace('[edit]', '').strip().replace('\xa0', ' ')
section_data = []
for sibling in header.find_next_siblings():
if sibling.name in ['h1', 'h2', 'h3', "h4", "h5", "h6"]:
break
if sibling.name == 'p':
for sentence in sibling.text.replace("[", " [").split('. '):
if sentence:
sentence, refs = get_references(sentence, reference_dict)
if sentence:
section_data.append({"sentence": sentence, "refs": refs})
data[section_title] = section_data
return data
def extract_references(url):
"""
Extract references from reference section with following structure:
{
"1": "https://...",
"2": "https://...",
}
@param url, url of the wikipedia page
@return dictionary of references, key is the citation index string, value is correpsonding url link
"""
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
references = {}
for references_section in soup.find_all('ol', {'class': 'references'}):
for ref in references_section.find_all('li', {'id': lambda x: x and x.startswith('cite_note-')}):
index = str(ref['id'].rsplit('-', 1)[-1])
link_tag = ref.find('a', {'href': True, 'rel': 'nofollow'})
isbn_tag = ref.find('a', href=re.compile(r'Special:BookSources'))
if link_tag:
link = link_tag['href']
references[index] = link
elif isbn_tag:
link = f'ISBN: {isbn_tag.text}'
references[index] = link
else:
references[index] = "[ERROR retrieving ref link]"
return references
def getSections(page, structured_data):
"""
Recursively extract each section title and plain text.
@param page, page variable from wikipediaapi (e.g. wiki_api.page("page name"))
@return a list of nested json for each section and corresponding subsections
{
"section_title": ...,
"section_text": ...,
"subsections": [
{...},
{...}
]
}
"""
return [{"section_title": i.title,
"section_content": structured_data[i.title],
"subsections": getSections(i, structured_data)
} for i in page.sections]
def get_wikipedia_json_output(username, url):
"""
Get wikepdia output as format json
@param username, username for wikipedia api agent.
@param url, url of wikipedia page
"""
wiki_api = wikipediaapi.Wikipedia(username, 'en')
wikipedia_page_name = url.replace("https://en.wikipedia.org/wiki/", "")
wikiapi_page = wiki_api.page(wikipedia_page_name)
# extract references
reference_dict = extract_references(url)
structured_data = extract_data(url, reference_dict)
# save extracted result to file
result = {"title": wikipedia_page_name,
"url": url,
"summary": wikiapi_page.summary,
"content": getSections(wikiapi_page, structured_data),
"references": reference_dict}
return result, wikipedia_page_name, reference_dict
def section_dict_to_text(data, inv_reference_dict, level=1):
title = data["section_title"]
content = data["section_content"]
subsections = data["subsections"]
if len(content) == 0 and len(subsections) == 0:
return ""
result = f"\n\n{'#' * level} {title}"
if content:
result += "\n\n"
for cur_sentence in content:
result += cur_sentence["sentence"]
if cur_sentence["refs"]:
result += " "
result += " ".join(f"[{inv_reference_dict[ref]}]" for ref in cur_sentence["refs"] if
ref != "[ERROR retrieving ref link]")
result += ". "
for subsection in subsections:
result += section_dict_to_text(subsection, inv_reference_dict, level=level + 1)
return result
def output_as_text(result, reference_dict):
inv_reference_dict = {v: k for k, v in reference_dict.items()}
output = result["title"] + "\n\n"
output += result["summary"]
for section in result["content"]:
output += section_dict_to_text(section, inv_reference_dict)
output += "\n\n# References\n\n"
for idx, link in reference_dict.items():
output += f"[{idx}] {link}\n"
return output
def extract_entities_flair(text):
sentences = re.split(r'(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?)\s', text)
tagger = Classifier.load('ner')
entities = []
for sentence in sentences:
if len(sentence) == 0:
continue
sentence = Sentence(sentence)
tagger.predict(sentence)
entities.extend([entity.text for entity in sentence.get_spans('ner')])
entities = list(set([e.lower() for e in entities]))
return entities
def process_url(url, output_dir, username='Knowledge Curation Project'):
result, wikipedia_page_name, reference_dict = get_wikipedia_json_output(username=username, url=url)
txt = output_as_text(result, reference_dict)
clean_txt = re.sub(r'#+ ', '', re.sub(r'\[\d+\]', '', txt[:txt.find("\n\n# References\n\n")]))
# Extract entities for future analysis.
result['flair_entities'] = extract_entities_flair(clean_txt)
wikipedia_page_name = wikipedia_page_name.replace("/", "_")
with open(os.path.join(output_dir, 'json', wikipedia_page_name + ".json"), "w") as f:
json.dump(result, f, indent=2)
with open(os.path.join(output_dir, 'txt', wikipedia_page_name + ".txt"), "w") as f:
f.write(txt)
def main(args):
pathlib.Path(f'{args.outputDirectory}/json').mkdir(parents=True, exist_ok=True)
pathlib.Path(f'{args.outputDirectory}/txt').mkdir(parents=True, exist_ok=True)
if args.batch:
df = pd.read_csv(args.batch_path)
for _, row in tqdm(df.iterrows()):
try:
process_url(row['url'], args.outputDirectory)
except Exception as e:
print(e)
print(f'Error occurs when processing {row["url"]}')
else:
process_url(args.url, args.outputDirectory)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Parse a Wikipedia page into sections.')
parser.add_argument('--batch', action='store_true', help='Process data in a batch.')
parser.add_argument('--batch_path', type=str, help='Path of the batch topic file.')
parser.add_argument('-u', '--url',
default='https://en.wikipedia.org/wiki/Python_(programming_language)',
help='The URL of the Wikipedia page to parse (default: https://en.wikipedia.org/wiki/Python_(programming_language))')
parser.add_argument('-o', '--outputDirectory',
default='./',
help='The path where the parsed content will be saved (default: current directory)')
main(parser.parse_args())