py_ez_wikidata API Documentation
prefixes
Created on 2024-03-02
@author: wf
Prefixes
handle standard Prefixes
Source code in ez_wikidata/prefixes.py
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 |
|
trulytabular
Created on 2022-04-14
@author: wf
TrulyTabular
Bases: object
truly tabular SPARQL/RDF analysis
checks "how tabular" a query based on a list of properties of an itemclass is
Source code in ez_wikidata/trulytabular.py
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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
|
__init__(itemQid, propertyLabels=[], propertyIds=[], search_predicate='wdt:P31', where=None, endpointConf=None, lang='en', debug=False)
Constructor
Parameters:
Name | Type | Description | Default |
---|---|---|---|
itemQid(str) |
wikidata id of the type to analyze |
required | |
propertyLabels(list) |
a list of labels of properties to be considered |
required | |
propertyIds(list) |
a list of ids of properties to be considered |
required | |
search_predicate(str) |
the search predicate to use e.g. instanceof / subclass of |
required | |
where(str) |
extra where clause for instance selection (if any) |
required | |
endpoint(str) |
the url of the SPARQL endpoint to be used |
required |
Source code in ez_wikidata/trulytabular.py
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 |
|
__str__()
Returns:
Name | Type | Description |
---|---|---|
str |
my text representation |
Source code in ez_wikidata/trulytabular.py
74 75 76 77 78 79 |
|
addStatsColWithPercent(m, col, value, total)
add a statistics Column Args: m(dict): col(str): name of the column value: value total: total value
Source code in ez_wikidata/trulytabular.py
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
|
asText(long=True)
returns my content as a text representation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
long(bool) |
True if a long format including url is wished |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
a text representation of my content |
Source code in ez_wikidata/trulytabular.py
105 106 107 108 109 110 111 112 113 114 115 116 |
|
count()
get my count
Source code in ez_wikidata/trulytabular.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
|
genPropertyStatistics()
generate the property Statistics
Returns:
Name | Type | Description |
---|---|---|
generator |
a generator of statistic dict rows |
Source code in ez_wikidata/trulytabular.py
429 430 431 432 433 434 435 436 437 438 439 |
|
genWdPropertyStatistic(wdProperty, itemCount, withQuery=True)
generate a property Statistics Row for the given wikidata Property
Parameters:
Name | Type | Description | Default |
---|---|---|---|
wdProperty(WikidataProperty) |
the property to get the statistics for |
required | |
itemCount(int) |
the total number of items to check |
required | |
withQuery(bool) |
if true include the sparql query |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
a statistics row |
Source code in ez_wikidata/trulytabular.py
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 |
|
generateSparqlQuery(genMap, listSeparator='⇹', naive=True, lang='en')
generate a SPARQL Query
Parameters:
Name | Type | Description | Default |
---|---|---|---|
genMap(dict) |
a dictionary of generation items aggregates/ignores/labels |
required | |
listSeparator(str) |
the symbole to use as a list separator for GROUP_CONCAT |
required | |
naive(bool) |
if True - generate a naive straight forward SPARQL query if False generate a proper truly tabular aggregate query |
required | |
lang(str) |
the language to generate for |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
the generated SPARQL Query |
Source code in ez_wikidata/trulytabular.py
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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
|
getPropertyStatistics()
get the property Statistics
Source code in ez_wikidata/trulytabular.py
441 442 443 444 445 446 447 448 449 450 |
|
getQueryManager(lang='sparql', name='trulytabular', debug=False)
classmethod
get the query manager for the given language and fileName
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lang(str) |
the language of the queries to extract |
required | |
name(str) |
the name of the manager containing the query specifications |
required | |
debug(bool) |
if True set debugging on |
required |
Source code in ez_wikidata/trulytabular.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
|
mostFrequentPropertiesQuery(whereClause=None, minCount=0)
get the most frequently used properties
Parameters:
Name | Type | Description | Default |
---|---|---|---|
whereClause(str) |
an extra WhereClause to use |
required |
Source code in ez_wikidata/trulytabular.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
|
noneTabular(wdProperty)
get the none tabular result for the given Wikidata property
Parameters:
Name | Type | Description | Default |
---|---|---|---|
wdProperty(WikidataProperty) |
the Wikidata property |
required |
Source code in ez_wikidata/trulytabular.py
359 360 361 362 363 364 365 366 367 368 369 370 |
|
noneTabularQuery(wdProperty, asFrequency=True)
get the none tabular entries for the given property
Parameters:
Name | Type | Description | Default |
---|---|---|---|
wdProperty(WikidataProperty) |
the property to analyze |
required | |
asFrequency(bool) |
if true do a frequency analysis |
required |
Source code in ez_wikidata/trulytabular.py
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
|
version
Created on 2024-03-01
@author: wf
Version
dataclass
Bases: object
Version handling for easy wikidata access
Source code in ez_wikidata/version.py
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 |
|
wbquery
Created on 2022-04-30
@author: wf
WikibaseQuery
Bases: object
a Query for Wikibase
Source code in ez_wikidata/wbquery.py
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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
|
__init__(entity, wpm=None, debug=False)
Constructor
Parameters:
Name | Type | Description | Default |
---|---|---|---|
entity(str) |
the entity this query represents |
required | |
debug(bool) |
if True switch on debugging |
required |
Source code in ez_wikidata/wbquery.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
addPropertyFromDescriptionRow(row)
add a property from the given row
Parameters:
Name | Type | Description | Default |
---|---|---|---|
row(dict) |
the row to add |
required |
Source code in ez_wikidata/wbquery.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
|
asSparql(filterClause=None, orderClause=None, pk=None, lang='en')
get the sparqlQuery for this query optionally applying a filterClause
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filterClause(str) |
a filter to be applied (if any) |
required | |
orderClause(str) |
an orderClause to be applied (if any) |
required | |
pk(str) |
primaryKey (if any) |
required | |
lang(str) |
the language to be used for labels |
required |
Source code in ez_wikidata/wbquery.py
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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
|
getColumnTypeAndVarname(propName)
get a signature tuple consisting of columnName, propertType and SPARQL variable Name for the given property Name
Parameters:
Name | Type | Description | Default |
---|---|---|---|
propName(str) |
the name of the property |
required |
Raises:
Type | Description |
---|---|
Exception
|
if property name is not known |
Returns:
Type | Description |
---|---|
(str, str, str)
|
column,propType,varName tupel |
Source code in ez_wikidata/wbquery.py
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 |
|
getValuesClause(values, propVarname='short_name', propType='text', lang=None, ignoreEmpty=True, wbPrefix='http://www.wikidata.org/entity/')
create a SPARQL Values clause
Parameters:
Name | Type | Description | Default |
---|---|---|---|
values(list) |
the list of values to create values for |
required | |
propVarname(str) |
the property variable name to assign the values for |
required | |
propType |
str
|
|
'text'
|
lang |
str
|
language of labels to query |
None
|
ignoreEmpty(bool) |
ignore empty values if True |
required | |
wbPrefix(str) |
a wikibase/wikidata prefix to be removed for items values |
required |
Returns: str: the SPARQL values clause
Source code in ez_wikidata/wbquery.py
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 |
|
get_item_mapping()
Get the mapping that describes the wikidata entity item
Source code in ez_wikidata/wbquery.py
49 50 51 52 53 |
|
get_property_mappings()
Get the property mappings as PropertyMapping list
Returns:
Type | Description |
---|---|
List[PropertyMapping]
|
List[PropertyMapping]: list of PropertyMappings |
Source code in ez_wikidata/wbquery.py
39 40 41 42 43 44 45 46 47 |
|
inFilter(values, propName='short_name', lang='en')
create a SPARQL IN filter clause
Parameters:
Name | Type | Description | Default |
---|---|---|---|
values(list) |
the list of values to filter for |
required | |
propName(str) |
the property name to filter with |
required | |
lang(str) |
the language to apply |
required |
Source code in ez_wikidata/wbquery.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
|
ofEntityMap(entity, entityMap)
classmethod
create a WikibaseQuery for the given entity and entityMap
Parameters:
Name | Type | Description | Default |
---|---|---|---|
entity(str) |
the entity name |
required | |
entityMap(dict) |
the entity property descriptions |
required |
Returns: WikibaseQuery
Source code in ez_wikidata/wbquery.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
|
ofMapRows(entityMapRows, debug=False)
classmethod
create a dict of wikibaseQueries from the given entityMap list of dicts
Parameters:
Name | Type | Description | Default |
---|---|---|---|
entityMapRows(list) |
a list of dict with row descriptions |
required | |
debug(bool) |
if True switch on debugging |
required |
Source code in ez_wikidata/wbquery.py
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
|
wdproperty
Created on 02.03.2024-03-02
@author: wf
PropertyMapping
Represents a single column Wikidata property mapping.
Attributes:
Name | Type | Description |
---|---|---|
column |
Optional[str]
|
The column name in the data source; if None, the value is directly used. |
propertyName |
str
|
The human-readable name of the property. |
propertyId |
str
|
The Wikidata property ID (e.g., "P31"). |
propertyType |
str
|
The type of the property as a string; converted to an enum in post-init. |
qualifierOf |
Optional[str]
|
Specifies if the property is a qualifier of another property. |
valueLookupType |
Optional[Any]
|
The type (instance of/P31) of the property value for lookup if the value is not already a QID. |
value |
Optional[Any]
|
The default value to set for the property. |
varname |
Optional[str]
|
An optional variable name for internal use. |
property_type_enum |
WdDatatype
|
The enum representation of the property type, initialized based on propertyType. |
The post_init method ensures the propertyType is correctly interpreted and stored as both a string and an enum.
Source code in ez_wikidata/wdproperty.py
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 |
|
__post_init__()
Convert propertyType from string to WdDatatype enum if necessary
Source code in ez_wikidata/wdproperty.py
452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
|
from_record(wpm, record)
classmethod
initialize PropertyMapping from the given record Args: wpm(WikidataPropertyManager): to be used for type lookup record(Dict): property mapping information
Returns:
Type | Description |
---|---|
PropertyMapping
|
PropertyMapping |
Source code in ez_wikidata/wdproperty.py
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 |
|
getDefaultItemPropertyMapping()
classmethod
get the defaultItemPropertyMapping
Source code in ez_wikidata/wdproperty.py
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 |
|
get_item_mapping(property_mappings)
classmethod
get the property mapping that is used for the default "item" primary key if no property is defined use the default "item" mapping
Source code in ez_wikidata/wdproperty.py
599 600 601 602 603 604 605 606 607 608 609 610 611 |
|
get_legacy_mapping()
classmethod
Returns the Mapping from old prop map keys to the new once
Source code in ez_wikidata/wdproperty.py
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 |
|
get_qualifier_lookup(properties)
classmethod
Get a lookup for a property and all its qualifier
Parameters:
Name | Type | Description | Default |
---|---|---|---|
properties |
List[PropertyMapping]
|
property mappings to generate the lookup from |
required |
Returns:
Type | Description |
---|---|
Dict[str, List[PropertyMapping]]
|
dict as property qualifier lookup |
Source code in ez_wikidata/wdproperty.py
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 |
|
is_item_itself()
Check if the property_type is an item
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the property mapping links to the existing item |
Source code in ez_wikidata/wdproperty.py
563 564 565 566 567 568 569 570 571 |
|
is_qualifier()
Returns true if the property mapping describes a qualifier
Source code in ez_wikidata/wdproperty.py
540 541 542 543 544 545 |
|
to_record()
convert property mapping to its dict representation
Source code in ez_wikidata/wdproperty.py
530 531 532 533 534 535 536 537 538 |
|
PropertyMappings
dataclass
A collection of Wikidata property mappings, with metadata.
Source code in ez_wikidata/wdproperty.py
614 615 616 617 618 619 620 621 622 623 624 |
|
Variable
Variable e.g. name handling
Source code in ez_wikidata/wdproperty.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
|
validVarName(varStr)
classmethod
convert the given potential variable name string to a valid variable name
see https://stackoverflow.com/a/3305731/1497139
Parameters:
Name | Type | Description | Default |
---|---|---|---|
varStr(str) |
the string to convert |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
a valid variable name |
Source code in ez_wikidata/wdproperty.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
|
WdDatatype
Bases: Enum
Supported Wikidata datatypes, sorted by frequency and including special cases.
Source code in ez_wikidata/wdproperty.py
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 |
|
from_wb_type_name(wb_type_name)
classmethod
convert a wikibase type name to a WdDatatype
Parameters:
Name | Type | Description | Default |
---|---|---|---|
wb_type_name(str) |
the string name of the wikibase type (with or without wikibase ontology prefix) |
required |
Source code in ez_wikidata/wdproperty.py
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 |
|
get_by_wikibase(property_type)
classmethod
Get WdDatatype by the corresponding wikibase datatype Args: property_type: wikibase name of the type
Returns:
Type | Description |
---|---|
Union[WdDatatype, None]
|
WdDatatype |
Source code in ez_wikidata/wdproperty.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
|
WikidataProperty
Represents a Wikidata Property.
Source code in ez_wikidata/wdproperty.py
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 |
|
__post_init__()
creates and modify calculated fields
Source code in ez_wikidata/wdproperty.py
147 148 149 150 151 152 153 154 155 156 157 158 159 |
|
getPredicate()
get me as a Predicate
Source code in ez_wikidata/wdproperty.py
161 162 163 164 165 166 167 |
|
WikidataPropertyManager
handle Wikidata Properties
Source code in ez_wikidata/wdproperty.py
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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
|
__init__(endpoint_url='https://qlever.cs.uni-freiburg.de/api/wikidata', langs=['en', 'zh', 'hi', 'de', 'fr', 'ar', 'es', 'bn', 'ru'], with_load=True, profile=True, debug=False)
initialize the lookups
Source code in ez_wikidata/wdproperty.py
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 |
|
get_instance(endpoint_url='https://qlever.cs.uni-freiburg.de/api/wikidata')
classmethod
initialize the wikidata property manager
Parameters:
Name | Type | Description | Default |
---|---|---|---|
endpoint_url(str) |
the SPARQL endpoint to query if there is no cache available |
required | |
lang(str) |
the languages to query propery labels and descriptions for |
required |
Source code in ez_wikidata/wdproperty.py
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
|
get_mappings_for_records(prop_mapping_records)
convert given list of property mapping records to list of PropertyMappings Args: prop_mapping_records: records to convert
Returns:
Type | Description |
---|---|
List[PropertyMapping]
|
property mappings |
Source code in ez_wikidata/wdproperty.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
|
get_properties_by_ids(ids, lang='en')
Get properties by their IDs for a specific language.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ids |
List[str]
|
List of property IDs to search for. |
required |
lang(str) |
the language |
required |
Returns:
Type | Description |
---|---|
Dict[str, Optional[WikidataProperty]]
|
A dictionary of {property ID: WikidataProperty or None} for found and not found properties. |
Source code in ez_wikidata/wdproperty.py
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
|
get_properties_by_labels(labels, lang='en')
Get properties by their labels for a specific language.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
labels |
List[str]
|
List of property labels to search for. |
required |
lang |
str
|
the language to match with |
'en'
|
Returns: A dictionary of {label: WikidataProperty} for found properties.
Source code in ez_wikidata/wdproperty.py
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
|
get_property_by_id(property_id)
lookup a WikidataProperty for the given property_id
Parameters:
Name | Type | Description | Default |
---|---|---|---|
property_id(str) |
a property ID e.g. "P6375" |
required |
Source code in ez_wikidata/wdproperty.py
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
|
get_query_for_langs(langs=None)
Get the SPARQL query for the given list of langs.
Source code in ez_wikidata/wdproperty.py
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
|
init_props()
initialize my property structures
Source code in ez_wikidata/wdproperty.py
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
|
load()
load the properties
Source code in ez_wikidata/wdproperty.py
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
|
load_from_sparql()
get my list of dicts from sparql
Source code in ez_wikidata/wdproperty.py
208 209 210 211 212 213 214 |
|
load_from_sql()
load from SQL
Source code in ez_wikidata/wdproperty.py
237 238 239 240 241 242 243 244 245 246 |
|
store()
store my list of dicts
Source code in ez_wikidata/wdproperty.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
|
wdsearch
Created on 2022-07-24
@author: wf
WikidataSearch
Bases: object
Wikidata Search API wrapper
Source code in ez_wikidata/wdsearch.py
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 |
|
__init__(language='en', timeout=2.0)
Constructor
Parameters:
Name | Type | Description | Default |
---|---|---|---|
language(str) |
the language to use e.g. en/fr |
required | |
timeout(float) |
maximum time to wait for result |
required |
Source code in ez_wikidata/wdsearch.py
18 19 20 21 22 23 24 25 26 27 |
|
getProperties()
get the Wikidata Properties
Source code in ez_wikidata/wdsearch.py
81 82 83 84 85 86 87 88 89 |
|
search(searchFor, limit=9)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
searchFor(str) |
the string to search for |
required | |
limit(int) |
the maximum amount of results to search for |
required |
Source code in ez_wikidata/wdsearch.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
searchOptions(searchFor, limit=9)
Search and return a list of qid, itemLabel, description tuples.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
searchFor |
str
|
the string to search for. |
required |
limit |
int
|
the maximum amount of results to return. |
9
|
Returns:
Type | Description |
---|---|
List[Tuple[str, str, str]]
|
List[Tuple[str, str, str]]: |
List[Tuple[str, str, str]]
|
A list of tuples containing |
List[Tuple[str, str, str]]
|
qid, itemLabel, and description. |
Source code in ez_wikidata/wdsearch.py
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 |
|
wikidata
Created on 2022-04-18
@author: wf
UrlReference
Bases: Reference
Reference consisting of reference URL (P854) retrieved (P813)
Source code in ez_wikidata/wikidata.py
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 |
|
__init__(url, date=None)
constructor Args: url: reference URL date: retrieved at
Source code in ez_wikidata/wikidata.py
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 |
|
Wikidata
wikidata access
see http://learningwikibase.com/data-import/
Source code in ez_wikidata/wikidata.py
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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 |
|
wbi: WikibaseIntegrator
property
writable
WikibaseIntegrator
__init__(baseurl=None, wpm=None, debug=False)
Constructor
Parameters:
Name | Type | Description | Default |
---|---|---|---|
baseurl(str) |
the baseurl of the wikibase to use |
required | |
debug(bool) |
if True output debug information |
required | |
wpm(WikidataPropertymanager) |
|
required |
Source code in ez_wikidata/wikidata.py
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 |
|
addDict(row, mapDict, itemId=None, lang='en', write=False, ignoreErrors=False)
add the given row mapping with the given map Dict
Parameters:
Name | Type | Description | Default |
---|---|---|---|
row(dict) |
the data row to add |
required | |
mapDict(dict) |
the mapping dictionary to use |
required | |
itemId |
Union[str, None]
|
wikidata id of the item the data should be added to. If None a new item is created unless item id is provided in the record |
None
|
lang(str) |
the language for lookups |
required | |
write(bool) |
if True do actually write |
required | |
ignoreErrors(bool) |
if True ignore errors |
required |
Returns:
Name | Type | Description |
---|---|---|
WikiDataResult |
WikidataResult
|
the result of the operation |
Source code in ez_wikidata/wikidata.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
|
add_record(record, property_mappings, item_id=None, lang='en', write=False, ignore_errors=False, summary=None, reference=None)
add the given row mapping with the given map Dict
Parameters:
Name | Type | Description | Default |
---|---|---|---|
record(dict) |
the data row to add |
required | |
property_mappings(list) |
the mapping dictionary to use |
required | |
item_id |
Union[str, None]
|
wikidata id of the item the data should be added to. If None a new item is created unless item id is provided in the record |
None
|
lang(str) |
the language for lookups |
required | |
write(bool) |
if True do actually write |
required | |
ignore_errors(bool) |
if True ignore errors |
required | |
summary |
str
|
summary of the item edits |
None
|
reference |
Reference
|
reference to add to all claims |
None
|
Returns:
Type | Description |
---|---|
(qId, errors)
|
the wikidata item create (if any) and a dict of errors |
Source code in ez_wikidata/wikidata.py
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 |
|
convert_to_claim(value, pm)
Convert the given value to a corresponding wikidata statement Args: value: value of the statement pm: information about the property statement ot generate
Raises:
Type | Description |
---|---|
Exception
|
if property datatype is unknown or not supported |
Returns:
Type | Description |
---|---|
Union[BaseDataType, None]
|
BaseDataType |
Source code in ez_wikidata/wikidata.py
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 |
|
getCredentials()
get my credentials https://test.wikidata.org/wiki/Property:P370
from the wd npm command line tool
Throws
Exception: if no credentials are available for the baseurl
Returns:
Type | Description |
---|---|
(str, str)
|
(username, password) of the account assigned to the baseurl |
Source code in ez_wikidata/wikidata.py
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 |
|
getItemByName(itemName, itemType, lang='en')
get an item by Name ToDo: Needs to be reworked as always WDQS is used as endpoint even if a different one is defined Args: itemName(str): the item to look for itemType(str): the type of the item lang(str): the language of the itemName
Source code in ez_wikidata/wikidata.py
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 225 226 227 228 229 230 231 232 233 234 235 |
|
get_datatype_of_property(property_id)
classmethod
Get the datatype of the given property Args: property_id: id of the property e.g. P31 or 31
Returns:
Type | Description |
---|---|
Union[str, None]
|
datatype of the property of None if no datatype is defined |
Source code in ez_wikidata/wikidata.py
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 |
|
get_date_claim(date, prop_nr)
staticmethod
Get the data statement for the given date and property id Args: date: date value prop_nr: id of the property
Returns:
Type | Description |
---|---|
Claim
|
statement of the given property number with the given value |
Source code in ez_wikidata/wikidata.py
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 |
|
get_item_label(item_id, lang=None)
Get the label for the given item id Args: item_id: id of the item lang: label language to return. Default is "en"
Returns:
Name | Type | Description |
---|---|---|
str |
Union[str, None]
|
label of the item |
None |
Union[str, None]
|
If the label can not be determined or the item_id is None or can not be found |
Source code in ez_wikidata/wikidata.py
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
|
get_or_create_item(item_id)
Get or create the requested wikidata item Args: item_id: item to retrieve if None create a new item
Source code in ez_wikidata/wikidata.py
592 593 594 595 596 597 598 599 600 601 602 |
|
get_prop_value(record, pm, lang)
Retrieve the property value from the record and prepare the value if necessary Args: record: record containing the property data pm: property mapping lang: language to use
Returns:
Type | Description |
---|---|
Any
|
value of the property from the record |
Source code in ez_wikidata/wikidata.py
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 |
|
get_record(item_id, property_mappings, include_label=True, include_description=True, label_for_qids=False)
Get the properties form the given item Args: item_id: id of the item to get the data from property_mappings: list of property values to extract include_label: include_description: label_for_qids: If True fetch the label for a linked Qid Returns: dict with the property values
Source code in ez_wikidata/wikidata.py
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
|
get_wddatatype_of_property(property_id)
classmethod
Get the datatype of the given property Args: property_id: id of the property e.g. P31 or 31
Returns:
Type | Description |
---|---|
WdDatatype
|
WdDatatype of the property of None if no datatype is defined |
Source code in ez_wikidata/wikidata.py
769 770 771 772 773 774 775 776 777 778 779 780 |
|
get_wikidata_item(qid_or_label, item_type_qid=None)
Get WikidataItem for given label or Qid
Parameters:
Name | Type | Description | Default |
---|---|---|---|
qid_or_label |
str
|
label or Qid of a item |
required |
Returns:
Type | Description |
---|---|
Optional[WikidataItem]
|
WikidataItem |
Source code in ez_wikidata/wikidata.py
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 |
|
is_wikidata_item_id(value)
staticmethod
Returns true if the given value is a wikidata item id
Source code in ez_wikidata/wikidata.py
697 698 699 700 701 702 |
|
is_wikidata_property_id(value)
staticmethod
Returns true if the given value is a wikidata property id
Source code in ez_wikidata/wikidata.py
704 705 706 707 708 709 |
|
loginWithCredentials(user=None, pwd=None)
login using the given credentials or credentials retrieved via self.getCredentials
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user(str) |
the username |
required | |
pwd(str) |
the password |
required |
Source code in ez_wikidata/wikidata.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
|
logout()
log the user out again
Source code in ez_wikidata/wikidata.py
184 185 186 187 188 189 190 |
|
normalize_records(record, prop_map)
Normalize given record by converting Qids to WikidataItem objects (lookup label) and find out Qid if label given based on the given prop_map
Source code in ez_wikidata/wikidata.py
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 |
|
sanitize_label(label, limit=None, postfix=None)
staticmethod
sanitize given label by ensuring it is not too long Args: label: label to sanitize limit: max length of the label
Returns:
Type | Description |
---|---|
str
|
sanitized label |
Source code in ez_wikidata/wikidata.py
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 |
|
WikidataItem
dataclass
Source code in ez_wikidata/wikidata.py
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 |
|
__eq__(other)
WikidataItems are equal if the qid is equal
Source code in ez_wikidata/wikidata.py
841 842 843 844 845 846 847 848 |
|
__post_init__()
handle the construction
Source code in ez_wikidata/wikidata.py
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 |
|
asText(long=True, wrapAt=0)
returns my content as a text representation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
long(bool) |
True if a long format including url is wished |
required | |
wrapAt(int) |
wrap long lines at the given width (if >0) |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
a text representation of my content |
Source code in ez_wikidata/wikidata.py
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 |
|
getItemsByLabel(sparql, itemLabel, lang='en', debug=False)
classmethod
get a Wikidata items by the given label
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sparql(SPARQL) |
the SPARQL endpoint to use |
required | |
itemLabel(str) |
the label of the items |
required | |
lang(str) |
the language of the label |
required | |
debug(bool) |
if True show debugging information |
required |
Returns:
Type | Description |
---|---|
list
|
a list of potential items |
Source code in ez_wikidata/wikidata.py
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 |
|
getLabelAndDescription(sparql, itemId, lang='en', debug=False)
classmethod
get the label for the given item and language
Parameters:
Name | Type | Description | Default |
---|---|---|---|
itemId(str) |
the wikidata Q/P id |
required | |
lang(str) |
the language of the label |
required | |
debug(bool) |
if True output debug information |
required |
Returns:
Type | Description |
---|---|
(str, str)
|
the label and description as a tuple |
Source code in ez_wikidata/wikidata.py
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 |
|
WikidataResult
dataclass
a class for handling a wikidata result
Source code in ez_wikidata/wikidata.py
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 |
|
pretty_item_json: str
property
Returns a pretty-printed JSON string of the item.