A first look at Wikidata through Github Copilot

July 11, 2021 0 By addshore

I was added to the Github Copilot preview in the past days, and the first thing I tested out was code suggestions around Wikidata.

Copilot allows you to start writing code, and have a model trained on public Github code suggest block of code that you might want to use.

For example, if you start defining a method called onePlusOne then it might suggest the body return 1+1;.

function onePlusOne() {
    return 1 + 1;
}Code language: JavaScript (javascript)

More targeted toward Wikidata, if you start defining a function called whatIsWikidata then it might suggest return "Wikidata";

function whatIsWikidata(){
    return "Wikidata";
}Code language: JavaScript (javascript)

In some cases copilot suggests multiple code blocks. Some of these seem useful, others less so.

function whatIsWikidata(){
    var wd = new Wikidata();
    var wd_json = wd.getData();
    var wd_json_obj = JSON.parse(wd_json);
    var wd_json_obj_items = wd_json_obj.items;
    var wd_json_obj_entities = wd_json_obj.entities;
    var wd_json_obj_properties = wd_json_obj.properties;
    var wd_json_obj_claims = wd_json_obj.claims;
    var wd_json_obj_labels = wd_json_obj.labels;
    var wd_json_obj_descriptions = wd_json_obj.descriptions;
    var wd_json_obj_aliases = wd_json_obj.aliases;
    var wd_json_obj_sitelinks = wd_json_obj.sitelinks;
    var wd_json_obj_urls = wd_json_obj.urls;
    var wd_json_obj_qualifiers = wd_json_obj.qualifiers;
    var wd_json_obj_datatype = wd_json_obj.datatype;
    var wd_json_obj_datavalue = wd_json_obj.datavalue;
    var wd_json_obj_datavalue_value = wd_json_obj.datavalue.value;
    var wd_json_obj_datavalue_type = wd_json_obj.datavalue.type;
    var wd_json_obj_datavalue_value_entityid = wd_json_obj.datavalue.value.entityid;
    var wd_json_obj_datavalue_value_time = wd_json_obj.datavalue.value.time;
    var wd_json_obj_datavalue_value_string = wd_json_obj.datavalue.value.string;
    var wd_json_obj_datavalue_value_monoling
    Code language: JavaScript (javascript)

And sometimes the suggestions appear to be different for the same inputs, potentially depending on other context of the file?

function getWikidataEntity(id)

Of 10 suggestions, 6 used XMLHttpRequest, 3 used jquery, and the final suggestion was fairly useless for what I was aiming for.

Solution 1

When paired with the xmlhttprequest package for node and a quick function call and console.log call the first example actually works, returning JSON that includes the entity requested.

var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

console.log(getWikidataEntity("Q1000"))

function getWikidataEntity(id){
	var url = "https://www.wikidata.org/wiki/Special:EntityData/" + id + ".json";
	var xhr = new XMLHttpRequest();
	xhr.open("GET", url, false);
	xhr.setRequestHeader("Content-Type", "application/json");
	xhr.send();
	var data = JSON.parse(xhr.responseText);
	return data;
}Code language: JavaScript (javascript)

Interestingly Copilot here chose to use Special:EntityData instead of the wbgetentities action API module for Wikidata. The function also does not unwrap the result to only provide the single entity. This is the request that is made.

Solution 2

The second solution correctly retrieves and decodes a single entity JSON using onload for the request. Adding a quick console.log in before the return within onload shows the request being correctly made.

var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

getWikidataEntity("Q1000")

function getWikidataEntity(id){
	var url = "https://www.wikidata.org/wiki/Special:EntityData/" + id;
	var xhr = new XMLHttpRequest();
	xhr.open("GET", url, true);
	xhr.onload = function() {
		if (xhr.status >= 200 && xhr.status < 300) {
			//console.log(xhr.responseText);
			var json = JSON.parse(xhr.responseText);
            console.log(json.entities[id])
			return json.entities[id];
		} else {
			console.log("Error: " + xhr.status + " " + xhr.statusText);
		}
	};
	xhr.onerror = function() {
		console.log("Error: " + xhr.status + " " + xhr.statusText);
	};
	xhr.send();
}Code language: JavaScript (javascript)

Solution 3

Solution number 3 brings us a less ideal block of code, as it doesn’t work out of the box. The request that is made results in a redirect, thus there is no xhr.responseText to output to the user.

var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

console.log(getWikidataEntity("Q1000"))

function getWikidataEntity(id){
	var url = "https://www.wikidata.org/wiki/Special:EntityData/"+id;
	var xhr = new XMLHttpRequest();
	xhr.open('GET', url, false);
	xhr.send(null);
	return xhr.responseText;
}Code language: JavaScript (javascript)

Other solutions

The solutions from here become quite a mixed bag. Lots of the code works, and does something, though often not quite what I would have expected.

You can find all suggestions pasted here.