-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathpopulate_family_names.py
More file actions
182 lines (166 loc) · 6.41 KB
/
Copy pathpopulate_family_names.py
File metadata and controls
182 lines (166 loc) · 6.41 KB
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Try to auto-populate family names
# Mike Peel 09-Jun-2018 v1 - start
from __future__ import unicode_literals
import pywikibot
import numpy as np
import time
import string
from pywikibot import pagegenerators
import urllib
import csv
maxnum = 1000000
nummodified = 0
stepsize = 10000
maximum = 10000000
numsteps = int(maximum / stepsize)
wikidata_site = pywikibot.Site("wikidata", "wikidata")
repo = wikidata_site.data_repository() # this is a DataSite object
commons = pywikibot.Site('commons', 'commons')
debug = 1
# Read in the family names database
with open('populate_family_names_cache.csv', mode='r') as infile:
reader = csv.reader(infile)
with open('coors_new.csv', mode='w') as outfile:
writer = csv.writer(outfile)
names = {rows[1]:rows[0] for rows in reader}
# print names
for i in range(0,numsteps):
print 'Starting at ' + str(i*stepsize)
# query = 'SELECT ?item WHERE {\n'\
# ' ?item wdt:P31 wd:Q5 .\n'\
# ' ?item wdt:P735 ?givenname .\n'\
# ' MINUS {?item wdt:P734 ?familyname.}\n'\
# '} LIMIT ' + str(stepsize) + ' OFFSET ' + str(i*stepsize)
query = 'SELECT ?item\n'\
'WITH {\n'\
' SELECT ?item WHERE {\n'\
' ?item wdt:P31 wd:Q5 .\n'\
' } LIMIT ' + str(stepsize) + ' OFFSET ' + str(i*stepsize) + '\n'\
'} AS %humans \n'\
'WHERE {\n'\
' INCLUDE %humans .\n'\
' FILTER EXISTS {?item wdt:P735 ?givenname.}.\n'\
' MINUS {?item wdt:P734 ?familyname.}\n'\
'}'
print query
# exit()
generator = pagegenerators.WikidataSPARQLPageGenerator(query, site=wikidata_site)
for page in generator:
try:
item_dict = page.get()
qid = page.title()
except:
print 'Huh - no page found'
continue
print "\n" + qid
try:
name = page.labels['en']
except:
print 'Name not found!'
continue
try:
givenname_item = item_dict['claims']['P735']
count = 0
for clm in givenname_item:
if count == 0:
val = clm.getTarget()
print val
name_dict = val.get()
givenname = name_dict['labels']['en']
count += 1
except:
print 'Given name not found!'
continue
try:
familyname_item = item_dict['claims']['P734']
print "Hmm, that shouldn't have worked!"
continue
except:
null = 0
print name
print givenname
if givenname in name:
familyname_attempt = name.replace(givenname, "").strip()
if familyname_attempt != "" and familyname_attempt == familyname_attempt.replace(" ", ""):
print familyname_attempt
try:
new_qid = names[familyname_attempt]
except:
print 'Family name not found!'
continue
if len(familyname_attempt) < 4:
print 'Name too short - not attempting this one!'
continue
if "Saint" in familyname_attempt:
print "Avoiding Saint"
continue
# Double-check the native names
nativename = ''
nativelang = ''
try:
print 'Hello!'
nativename_item = item_dict['claims']['P1559']
count = 0
for clm in nativename_item:
if count == 0:
# print clm
val = clm.getTarget()
print val
nativename = val.text
nativelang = val.language
count += 1
except:
print 'Native name not found!'
if nativename != '' and nativelang != 'en':
print 'Hello again!'
familynativename = ''
try:
familyname_item = pywikibot.ItemPage(repo, new_qid)
familyname_item_dict = familyname_q.get()
familyname_native = familyname_item_dict['claims']['P1705']
count = 0
for clm in familyname_native:
if count == 0:
# print clm
val = clm.getTarget()
print val
familynativename = val.text
familynativelang = val.language
count += 1
except:
print 'Family name has no native name'
if familynativename != '' and nativename != '':
if familynativename not in name:
print 'Family native name not in name'
text = raw_input("Pay attention. ")
if familynativename not in nativename:
print 'Family native name not in native name!'
text = raw_input("Save? ")
continue
else:
print '... but it is in the native name.'
# OK, if we're here then we're probably good to go.
print new_qid
stringclaim = pywikibot.Claim(repo, 'P734')
stringclaim.setTarget(pywikibot.ItemPage(repo, new_qid))
print stringclaim
# text = raw_input("Save? ")
# if text == 'y':
try:
page.addClaim(stringclaim, summary=u'Adding family name based on the label minus P735')
except:
print "That didn't work!"
nummodified += 1
print nummodified
else:
continue
else:
print 'Given name is not in family name! Continuing...'
continue
if nummodified >= maxnum:
print 'Reached the maximum of ' + str(maxnum) + ' entries modified, quitting!'
exit()
print 'Done! Edited ' + str(nummodified) + ' entries'
# EOF