Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions LexData/claim.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ def pure_value(self) -> Union[str, int, float, Tuple[float, float]]:
return value["id"]
if vtype == "string":
return value
if vtype == "external-id":
return value
if vtype == "monolingualtext":
return value["text"]
if vtype == "quantity":
Expand Down
41 changes: 37 additions & 4 deletions LexData/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Dict, List, Union

from .claim import Claim
from .utils import getPropertyType
from .wikidatasession import WikidataSession


Expand Down Expand Up @@ -87,19 +88,51 @@ def __setEntityClaim__(self, idProp: str, idStr: str):
:param idProp: id of the property (example: "P31")
:param idItem: id of the entity (example: "Q1")
"""
entityId = int(idStr[1:])
claim_value = json.dumps({"entity-type": "item", "numeric-id": entityId})
self.__setClaim__(idProp, claim_value)
# Check if this is an external-id property
datatype = None
try:
datatype = getPropertyType(idProp)
except Exception:
# If we can't get the property type, assume it's an entity
pass
if datatype == "external-id":
# For external-id properties, create a Claim object and use __setClaims__
claim = Claim(propertyId=idProp, value=idStr)
self.__setClaims__([claim])
return
# Handle entity-type properties as before
if idStr.startswith(('Q', 'P', 'L')):
entityId = int(idStr[1:])
claim_value = json.dumps({"entity-type": "item", "numeric-id": entityId})
self.__setClaim__(idProp, claim_value)
else:
raise ValueError(f"Invalid entity ID format: {idStr}. Expected Q, P, or L prefix.")

def __setClaim__(self, idProp: str, claim_value):
from .utils import getPropertyType
import LexData.lexeme
is_lexeme = isinstance(self, LexData.lexeme.Lexeme)
if isinstance(claim_value, Claim):
datatype = getPropertyType(idProp)
if datatype == "external-id":
claim_value = claim_value.pure_value
else:
snak_data = claim_value["mainsnak"]
claim_value = json.dumps(snak_data["datavalue"])
datatype = getPropertyType(idProp)
if datatype == "external-id":
claim_value_json = json.dumps(claim_value)
else:
claim_value_json = claim_value
# Use wbcreateclaim for both lexemes and other entities
PARAMS = {
"action": "wbcreateclaim",
"format": "json",
"entity": self.id,
"snaktype": "value",
"bot": "1",
"property": idProp,
"value": claim_value,
"value": claim_value_json,
"token": "__AUTO__",
}

Expand Down
7 changes: 7 additions & 0 deletions LexData/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ def buildDataValue(datatype: str, value):
raise TypeError(
f"Can not convert type {type(value)} to datatype {datatype}"
)
elif datatype == "external-id":
if type(value) == str:
return {"value": value, "type": "external-id"}
else:
raise TypeError(
f"Can not convert type {type(value)} to datatype {datatype}"
)
elif datatype in [
"string",
"tabular-data",
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ power of the access to the internals.
LexData is still in beta phase and there fore some features are missing and
functions might be renamed in future.

## Features

- Create and manage Wikidata Lexemes
- Add forms and senses to lexemes
- Add claims to lexemes, forms, and senses (including external-id properties)
- Search and find existing lexemes
- Support for various Wikidata data types (entities, strings, external-ids, etc.)

The code of AitalvivemBot was used as a starting point, but probably theres not
a single line of code that wasn't rewritten.

Expand Down
7 changes: 6 additions & 1 deletion example.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
if len(L2.forms) == 0:
L2.createForm("firsts", ["Q146786"])

# …or senses, with or without additional claims
# …or senses, with or without additional claims
if len(L2.senses) == 0:
L2.createSense(
{
Expand All @@ -38,3 +38,8 @@
},
claims={"P5137": ["Q19269277"]},
)

# …and add external-id claim to lexeme
if len(L2.claims.get("P12682", [])) == 0:
external_id_claim = LexData.Claim("P12682", "example_50bcf7bc0a0ae2bab9011b09139f6f8a")
L2.addClaims([external_id_claim])