Repository navigation
Expand file tree
/
Copy pathIdentifierPropertyPopularity.ps1
More file actions
224 lines (161 loc) · 5.49 KB
/
Copy pathIdentifierPropertyPopularity.ps1
File metadata and controls
224 lines (161 loc) · 5.49 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
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
224
# ============================================================
# Wikidata property usage counter
#
# Finds Wikidata properties with P3254
# Counts how many distinct entities use each property
# Generates:
# 1. CSV report
# 2. QuickStatements import file:
#
# Pxxxx P4876 count P585 +YYYY-MM-DDT00:00:00Z/11
#
# Nothing is written directly to Wikidata.
# ============================================================
$Limit = 200
$SparqlEndpoint = "https://query.wikidata.org/sparql"
$Headers = @{
"User-Agent" = "WikidataPropertyUsageCounter/1.0 (PowerShell; personal Wikidata maintenance)"
}
$OutputFolder = $PSScriptRoot
if ([string]::IsNullOrWhiteSpace($OutputFolder)) {
$OutputFolder = (Get-Location).Path
}
$CsvFile = Join-Path $OutputFolder "wikidata-property-counts.csv"
$QuickStatementsFile = Join-Path $OutputFolder "wikidata-property-counts-quickstatements.txt"
# Today's date in QuickStatements/Wikidata format
$Today = Get-Date
$WikidataDate = "+{0:yyyy-MM-dd}T00:00:00Z/11" -f $Today
function Invoke-WikidataSparql {
param (
[Parameter(Mandatory)]
[string]$Query
)
$Body = @{
query = $Query
format = "json"
}
Invoke-RestMethod `
-Uri $SparqlEndpoint `
-Method Post `
-Headers $Headers `
-Body $Body `
-ContentType "application/x-www-form-urlencoded"
}
Write-Host ""
Write-Host "Finding up to $Limit Wikidata properties with P3254..." -ForegroundColor Cyan
# ------------------------------------------------------------
# Find properties which have P3254
# ------------------------------------------------------------
$PropertyQuery = @"
SELECT ?property ?propertyLabel WHERE {
# Property is a "Wikidata property for an identifier that does not imply notability"
#?property wdt:P31 wd:Q62589320 .
# Property is a "Wikidata property related to science"
#?property wdt:P31 wd:Q61058429 .
# Only properties with datatype "External identifier"
?property wikibase:propertyType wikibase:ExternalId .
# Ignore properties where P4876 is already set
FILTER NOT EXISTS {
?property wdt:P4876 ?existingCount .
}
# Ignore properties where P8966 is already set
FILTER NOT EXISTS {
?property wdt:P8966 ?URLmatchPattern .
}
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en".
}
}
ORDER BY ?property
LIMIT $Limit
"@
try {
$PropertyResults = Invoke-WikidataSparql -Query $PropertyQuery
}
catch {
Write-Error "Unable to retrieve property list from Wikidata: $_"
exit
}
$Properties = $PropertyResults.results.bindings
if (-not $Properties) {
Write-Host "No properties found." -ForegroundColor Yellow
exit
}
$Results = @()
$QuickStatements = @()
foreach ($Property in $Properties) {
$PropertyUri = $Property.property.value
$PropertyId = $PropertyUri -replace '^.*/', ''
$PropertyLabel = $Property.propertyLabel.value
Write-Host ""
Write-Host "Processing $PropertyId - $PropertyLabel" -ForegroundColor Cyan
# --------------------------------------------------------
# Count distinct entities which have a statement using
# this property.
# --------------------------------------------------------
$CountQuery = @"
SELECT (COUNT(DISTINCT ?item) AS ?count)
WHERE {
?item wdt:$PropertyId ?value .
}
"@
try {
$CountResult = Invoke-WikidataSparql -Query $CountQuery
$Count = [int64]$CountResult.results.bindings[0].count.value
Write-Host " $Count entities use $PropertyId" -ForegroundColor Green
# ----------------------------------------------------
# Save human-readable result
# ----------------------------------------------------
$Results += [PSCustomObject]@{
Property = $PropertyId
Label = $PropertyLabel
Count = $Count
Date = $Today.ToString("yyyy-MM-dd")
}
# ----------------------------------------------------
# QuickStatements format
#
# Property P4876 Count P585 Date
# ----------------------------------------------------
#Count without unit $QuickStatements += "$PropertyId`tP4876`t$Count`tP585`t$WikidataDate"
$QuickStatements += "$PropertyId|P4876|$($Count)U18616576|P585|$WikidataDate"
}
catch {
Write-Warning "Failed to count $PropertyId : $_"
}
# Be polite to WDQS
Start-Sleep -Milliseconds 500
}
# ------------------------------------------------------------
# Create output files
# ------------------------------------------------------------
$Results |
Export-Csv `
-Path $CsvFile `
-NoTypeInformation `
-Encoding UTF8
$QuickStatements |
Set-Content `
-Path $QuickStatementsFile `
-Encoding UTF8
# ------------------------------------------------------------
# Show results
# ------------------------------------------------------------
Write-Host ""
Write-Host "=============================================" -ForegroundColor Cyan
Write-Host "Results"
Write-Host "=============================================" -ForegroundColor Cyan
$Results |
Format-Table Property, Label, Count, Date -AutoSize
Write-Host ""
Write-Host "CSV:"
Write-Host $CsvFile -ForegroundColor Yellow
Write-Host ""
Write-Host "QuickStatements:"
Write-Host $QuickStatementsFile -ForegroundColor Yellow
Write-Host ""
Write-Host "QuickStatements generated:"
Write-Host ""
$QuickStatements | ForEach-Object {
Write-Host $_
}