-
Notifications
You must be signed in to change notification settings - Fork 8
/
enwp_wikidata_import_shortdesc.py
214 lines (189 loc) · 7.04 KB
/
enwp_wikidata_import_shortdesc.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
# !/usr/bin/python
# -*- coding: utf-8 -*-
# Synchronise enwp short description and wikidata en descriptions
# Mike Peel 03-Aug-2020 v1 - start
# Mike Peel 12-Sep-2020 v2 - reconfigure just for empty Wikidata descriptions.
import pywikibot
from pywikibot import pagegenerators
from pywikibot.data import api
import random
def get_pageinfo(site, itemtitle):
params = { 'action' :'query',
'format' : 'json',
'prop' : 'pageprops',
'titles': itemtitle}
request = api.Request(site=site, parameters=params)
return request.submit()
maxnum = 50000
nummodified = 0
debug = False
trip = True
# replace_existing = False
maxwords = 10
minwords = 2
targetcat = 'Category:Short description with empty Wikidata description'
# Set up the wikimedia site links
wikidata_site = pywikibot.Site("wikidata", "wikidata")
repo = wikidata_site.data_repository() # this is a DataSite object
commons = pywikibot.Site('commons', 'commons')
wikipedia = pywikibot.Site('en', 'wikipedia')
# Fetch the exclusion list for lower case for the first letter
page = pywikibot.Page(repo, 'User:Pi bot/lowercase_exceptions')
items = page.text
lowercase_exceptions = items.split()
# Fetch the exclusion list for bad enwp short descriptions
page = pywikibot.Page(repo, 'User:Pi bot/shortdesc_exclusions')
items = page.text
shortdesc_exclusions = items.split('\n')
print(shortdesc_exclusions)
# List of words to add to the double-check report
page = pywikibot.Page(repo, 'User:Pi bot/doublecheck_words')
items = page.text
doublecheck_words = items.split('\n')
print(doublecheck_words)
# List of starting words to add to the double-check report
page = pywikibot.Page(repo, 'User:Pi bot/doublecheck_start_words')
items = page.text
doublecheck_start_words = items.split('\n')
print(doublecheck_start_words)
cat = pywikibot.Category(wikipedia, targetcat)
pages = pagegenerators.CategorizedPageGenerator(cat, recurse=False);
# todo = []
# count = 0
# maxcount = 50000
# for page in pages:
# todo.append(page.title())
# count += 1
# if count >= maxcount:
# break
# random.shuffle(todo)
# for item in sorted(todo,reverse=True):
# for item in todo:
for page in pages:
# if count > 0:
# page = pywikibot.Page(wikipedia,item)
enwiki_description = ''
wikidata_description = ''
if not trip:
if "Varanasi" in page.title():
trip = True
else:
print(page.title())
continue
try:
wd_item = pywikibot.ItemPage.fromPage(page)
item_dict = wd_item.get()
qid = wd_item.title()
except:
print('Huh - no page found')
continue
print("\n" + qid)
print('https://en.wikipedia.org/wiki/'+page.title().replace(' ','_'))
# Get the short description from enwp
try:
test = get_pageinfo(wikipedia,page)
for item in test['query']['pages']:
enwiki_description = test['query']['pages'][item]['pageprops']['wikibase-shortdesc']
except:
enwiki_description = ''
if len(enwiki_description) == 0:
print('No enwiki description found')
continue
if len(enwiki_description) < 5:
print('Very short enwiki description found! ' + str(enwiki_description))
continue
print(enwiki_description)
# Check that the short description isn't in the exclusion list
if enwiki_description.strip() in shortdesc_exclusions:
print('enwiki description is in the exclusion list, skipping')
continue
# Change the first letter to lower case unless it's an exception
if enwiki_description.split()[0] not in lowercase_exceptions:
# ... and if the second letter isn't also upper case.
if enwiki_description[1].lower() == enwiki_description[1] and 'football season' not in enwiki_description and 'basketball season' not in enwiki_description:
enwiki_description = enwiki_description[0].lower() + enwiki_description[1:]
# Catch some common things we want to remove
enwiki_description = enwiki_description.strip()
enwiki_description = enwiki_description.replace('"`UNIQ--ref-00000000-QINU`"','')
enwiki_description = enwiki_description.replace('"`UNIQ--ref-00000001-QINU`"','')
enwiki_description = enwiki_description.replace('"`UNIQ--ref-00000002-QINU`"','')
enwiki_description = enwiki_description.replace('"`UNIQ--ref-00000003-QINU`"','')
enwiki_description = enwiki_description.replace("?''?"," ")
enwiki_description = enwiki_description.replace(" "," ")
enwiki_description = enwiki_description.replace(" "," ")
if enwiki_description[-1] == '.':
enwiki_description = enwiki_description[0:-1]
if enwiki_description[0:2] == 'a ':
enwiki_description = enwiki_description[2:]
if enwiki_description[0:3] == 'an ':
enwiki_description = enwiki_description[3:]
if enwiki_description[0:4] == 'the ':
enwiki_description = enwiki_description[4:]
if enwiki_description[0:9] == 'upcoming ':
enwiki_description = enwiki_description[9:]
enwiki_description = enwiki_description.replace("'''","")
enwiki_description = enwiki_description.replace("''","")
enwiki_description = enwiki_description.replace(" "," ")
enwiki_description = enwiki_description.replace(" "," ")
# Check the length of the short description
if len(enwiki_description.split()) > maxwords:
print('enwiki description is too long, skipping')
continue
if len(enwiki_description.split()) < minwords:
print('enwiki description is too short, skipping')
continue
if enwiki_description.lower() == page.title().lower():
print('enwiki description matches the page title')
continue
# Get the description from Wikidata
try:
wikidata_description = item_dict['descriptions']['en']
except:
null = 0
# Save it to Wikidata
if wikidata_description == '':
# We have no en description on Wikidata, so we can add the enwp one
print(enwiki_description)
# See if we want to add this to the list to double-check
doublecheck = False
wordfound = ''
for word in doublecheck_words:
if word in enwiki_description:
doublecheck = True
wordfound = word
for word in doublecheck_start_words:
if (enwiki_description.strip())[:len(word)+1] == word+" ":
doublecheck = True
wordfound = word
if debug:
test = input('No description, import it?')
else:
test = 'y'
mydescriptions = {u'en': enwiki_description}
if test == 'y':
try:
wd_item.editDescriptions(mydescriptions, summary=u'Importing description from enwiki: ' + enwiki_description)
nummodified += 1
except:
print('Save went wrong')
continue
if doublecheck:
page = pywikibot.Page(repo, 'User:Pi bot/doublecheck')
page.text = page.text + "\n* {{Q|" + str(qid) + "}} - " + enwiki_description + " ('"+wordfound+"')"
page.save("Adding " + str(qid))
# elif wikidata_description.lower() != enwiki_description.lower() and replace_existing:
# # The Wikidata description doesn't match enwp, update it using enwp
# print('enwp: ' + enwiki_description)
# print('wikidata: ' + wikidata_description)
# if debug:
# test = input('Change description?')
# else:
# test = 'y'
# mydescriptions = {u'en': enwiki_description}
# if test == 'y':
# wd_item.editDescriptions(mydescriptions, summary=u'Matching short description from the English Wikipedia')
# nummodified += 1
if nummodified >= maxnum:
break
print('Done! Edited ' + str(nummodified) + ' entries')
# EOF