forked from AshokR/TamilNLP
/
WikiByPage.py
50 lines (40 loc) · 1.67 KB
/
WikiByPage.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from wikiapi import WikiApi
import requests, pprint
# This is suitable for extracting content that is organized by pages under a title
# This code requires the wiki-api python library created by Richard O'Dwyer of UK
# https://github.com/richardasaurus/wiki-api
wiki = WikiApi()
wiki = WikiApi({ 'locale' : 'ta'}) # to specify your locale, 'en' is default
# Get the page text of the article with the given title
def getArticleParagraphs(title):
print(title)
articleFull = wiki.get_article(title)
fullText = articleFull.content
article = ""
paragraphs = fullText.split('\n\n')
# print(paragraphs)
# We want only whole paragraphs that end in a ".", "!", "?" or '"' not fragments
for paragraph in paragraphs:
if len(paragraph) > 30:
end = paragraph[-1]
if end == '.' or end == '!' or end == '?' or end == '"':
article = article + "\n\n" + paragraph
return article
f = open('/your/folder/wikisource_content.txt', 'wt', encoding='utf-8')
def getPagesForTitle(title):
# In the wikiapi.py file change the following two lines
# api_uri = 'wikisource.org/w/api.php'
# article_uri = 'wikisource.org/wiki/'
baseUrl = 'https://ta.wikisource.org/w/api.php?action=query&list=allpages&aplimit=500&format=json'
titleUrl = '&apprefix='
url = baseUrl + titleUrl + title
print(url)
data = requests.get(url)
result = data.json()
pprint.pprint(result)
for item in result['query']['allpages']:
# print(item['title'])
f.write(getArticleParagraphs(item['title']))
getPagesForTitle('பொன்னியின் செல்வன்')