Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
transformers==2.1.1 | ||
|
||
gensim | ||
requests | ||
torch | ||
tqdm | ||
pandas | ||
sklearn | ||
numpy | ||
matplotlib | ||
pyyaml | ||
nltk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import requests | ||
|
||
|
||
def get_text_from_wikipedia(title, wiki_domain='en.wikipedia.org'): | ||
""" | ||
Retrieve Wikipedia article content as plain-text | ||
:param title: Title of Wikipedia article | ||
:param wiki_domain: API domain (e.g., en.wikipedia.org) | ||
:return: Article content as plain-text | ||
""" | ||
|
||
res = requests.get(f'https://{wiki_domain}/w/api.php', params={ | ||
"action":"query", | ||
"prop":"revisions", | ||
"rvprop":"content", | ||
"format":"json", | ||
"titles":title, | ||
"rvslots":"main" | ||
}) | ||
|
||
from gensim.corpora.wikicorpus import filter_wiki | ||
pages = [p for p_id, p in res.json()['query']['pages'].items()] | ||
|
||
if len(pages) == 0: | ||
raise ValueError(f'Cannot find Wikipedia article: {title}') | ||
elif len(pages) > 1: | ||
raise ValueError(f'Wikipedia article title is unambigious. Multiple articles returned from API: {title}') | ||
else: | ||
p = pages[0] | ||
|
||
wikitext = p['revisions'][0]['slots']['main']['*'] # | ||
text = filter_wiki(wikitext).strip() | ||
|
||
return text |