Example #1
0
def item_claim(nc=1, extra=0):
    s = ''.join(random.choice(letters) for i in range(30))
    if extra == 0:
        q, r = [], []
    if extra == 1:
        q, r = [snak('string', s, 'P1')], []
    if extra == 2:
        q, r = [snak('string', s, 'P1')], [snak('string', s, 'P1')]
    edict = entity(labels={'en': {
        'language': 'en',
        'value': s
    }},
                   aliases={'en': [{
                       'language': 'en',
                       'value': s
                   }]},
                   descriptions={'en': {
                       'language': 'en',
                       'value': s
                   }},
                   claims=claim('P1',
                                snak('string', s, 'P1'),
                                qualifiers=q,
                                references=r),
                   etype="item",
                   datatype='string')
    if nc >= 2:
        for n in range(2, nc + 1):
            edict['claims'].update(
                claim('P' + str(n),
                      snak('string', s, 'P' + str(n)),
                      qualifiers=q,
                      references=r))
    return edict
Example #2
0
def property_wd_all():
    """ Send a SPARQL-request to Wikidata, get all properties with labels,
    aliases, descriptions and datatypes, and return a list of JSON 
    representations of the properties"""
    props = get_wikidata_properties(language='en')
    props = props.replace(np.nan,
                          '').replace("http://www.wikidata.org/entity/",
                                      "",
                                      regex=True)
    props = props.groupby([
        'propertyWikidata', 'propertyType', 'propertyLabel',
        'propertyDescription'
    ]).agg(
        propertyAlias=('propertyAlias', lambda x: list(x)),
        fURL=('fURL', lambda x: list(x)),
        cURI=('cURI', lambda x: list(x)),
    ).reset_index()
    props = props[props.propertyWikidata != ("P1630" or "P1921")]
    props = props.sort_values(
        by=['propertyWikidata'],
        key=lambda col: col.str.replace('P', '', regex=True).astype(float))
    plist = []
    for ind, row in props.iterrows():
        url, dtype, title, desc, alia, furl, curi = row
        p = entity(labels=label(value=title),
                   aliases=alias(value=alia),
                   descriptions=description(value=desc),
                   claims=claim(prop='P1',
                                mainsnak=snak(datatype='external-id',
                                              value=url,
                                              prop='P1',
                                              snaktype='value')),
                   etype='property',
                   datatype=datatypes[dtype])
        for f in furl:
            if f != '':
                p['claims'].update(
                    claim(prop='P2',
                          mainsnak=snak(datatype='string',
                                        value=f,
                                        prop='P2',
                                        snaktype='value')))
        for c in curi:
            if c != '':
                p['claims'].update(
                    claim(prop='P3',
                          mainsnak=snak(datatype='string',
                                        value=c,
                                        prop='P3',
                                        snaktype='value')))
        plist.append(p)
    return plist
Example #3
0
def property_hrn(prop=''):
    """Create a simplified JSON represetation (only the label, aliases, 
    description and one claim) of the first local property 'Native company number'"""
    p = entity(
        labels=label(value='Native company number'),
        aliases=alias(value=[
            "Court, code and number",
            'Register, type of register and company number',
            'Registergericht, Registerart and Handelsregister number '
        ]),
        descriptions=description(
            value=
            "The registration authority (Amtsgericht), the registration code and the registration number"
        ),
        claims={},
        etype='property',
        datatype='external-id')
    return p
Example #4
0
def property_wd(prop=''):
    """For the given PID of a property in Wikidata returns its 
    simplified JSON represetation (only the label, aliases, description 
    and one claim)"""
    if prop:
        r = requests.get('https://www.wikidata.org/entity/' + prop +
                         '.json').json()
        po = r.get('entities').get(prop)
        p = entity(labels=restrict(po.get('labels')),
                   aliases=restrict(po.get('aliases')),
                   descriptions=restrict(po.get('descriptions')),
                   claims=claim(prop='P1',
                                mainsnak=snak(datatype='external-id',
                                              value=prop,
                                              prop='P1',
                                              snaktype='value')),
                   etype='property',
                   datatype='string')
    else:
        p = None
    return p
Example #5
0
def property_wid(prop=''):
    """Create a simplified JSON represetation (only the label, aliases, 
    description and one claim) of the first local property 'Wikidata ID'"""
    p = entity(labels=label(value='Wikidata ID'),
               aliases=alias(value=["WID", 'WikidataID']),
               descriptions=description(value="ID of an entity in Wikidata"),
               claims=claim(prop='P2',
                            mainsnak=snak(
                                datatype='string',
                                value='http://www.wikidata.org/entity/$1',
                                prop='P2',
                                snaktype='value')),
               etype='property',
               datatype='external-id')
    p['claims'].update(
        claim(prop='P3',
              mainsnak=snak(datatype='string',
                            value='http://www.wikidata.org/entity/$1',
                            prop='P3',
                            snaktype='value')))
    return p
Example #6
0
     native_company_number, registered_office, registrar,
     register_art, register_nummer, former_registrar,
     register_flag_, register_flag_Note,
     registerNummerSuffix, register_flag_Status_information
 ] = line
 e = entity(
     labels=label(value=name),
     aliases={},
     descriptions={},
     claims=claim(
         prop=opencorporatesid,
         mainsnak=snak(datatype='external-id',
                       value='de/' + company_number,
                       prop=opencorporatesid,
                       snaktype='value'),
         qualifiers=[
             snak("time", [
                 '+' + retrieved_at, 0, 11,
                 'http://www.wikidata.org/entity/Q1985727'
             ], retrieved, 'value')
         ],
         references=[
             snak('external-id', 'de/' + company_number,
                  opencorporatesid, 'value')
         ]),
     etype='item')
 if registered_address:
     e['claims'].update(
         claim(
             prop=streetaddress,
             mainsnak=snak(datatype='monolingualtext',