Skip to content

Instantly share code, notes, and snippets.

@tmtmtmtm
Last active August 6, 2020 08:31
Show Gist options
  • Save tmtmtmtm/03432f79e2ef0b15c1e574a987142e0e to your computer and use it in GitHub Desktop.
Save tmtmtmtm/03432f79e2ef0b15c1e574a987142e0e to your computer and use it in GitHub Desktop.
Working with family names in Wikidata using wikibase-cli

Working with surnames in Wikidata

These use wikibase-cli and jq.

Find an existing family name item:

find-surname.js:

module.exports = name => `SELECT ?item WHERE { ?item wdt:P31 wd:Q101352; rdfs:label '${name}'@en }`

with shell alias:

surname_find () {
    wd sparql ~/Code/wikidata-scripting/names/find-surname.js $1 | tee >(pbcopy)
}

Set a surname claim on an item:

surname_set () {
    wd ac $1 P734 $2
}

Both in one:

surname_find_and_set () {
    surname_find $2 | xargs wd ac $1 P734
}

Create a new surname item:

add-surname.js:

module.exports = (name) => {
  return {
    type: 'item',
    labels: { en: name },
    descriptions: { en: 'Family name' },
    claims: {
      P31: { value: 'Q101352' },
      P1705: { value: { text: name, language: 'mul' } },
      P282: { value: 'Q8229' }
    }
  }
}

with shell alias:

surname_create () {
    wd ce ~/Code/wikidata-scripting/names/add-surname.js $1 | jq -r .entity.id | tee >(pbcopy)
}

Set someone's surname to a new item

When a surname definitely doesn't already exist, the above can be combined as:

surname_create_and_set () {
    wd ac $1 P734 $(surname_create $2)
}

In principle these could be combined further into a find-or-create approach, but I prefer trying the "known" route first and then manually follow up with a create-and-set where needed, at least until I'm more confident I know various failure modes etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment