Topic on User talk:ValterVB

Jump to navigation Jump to search
Ganeshk (talkcontribs)

Hi Valter, thanks for your note on the portal. Can you please point me to a simple example to fetch an entity using page name and finding its QID? thanks, ~~~~

ValterVB (talkcontribs)

Premise: I have created this framework to elaborate many items.

Below you can see 3 examples:

  1. Find single item using only Wikidata API
  2. Find multiple item using only Wikidata API
  3. Find multiple item using only Wikipedia API

If something isn't clear ask me.

/// <summary> Find single item from wiki and page using only Wikidata </summary>
private void button39_Click(object sender, EventArgs e)
{
	Site WD = new Site("https://www.wikidata.org", user, password); //Login
	string strJson=WD.LoadWD("enwiki", "Douglas Adams"); //Search page "Douglas Adams" in english wikipedia
	Entities EntityList = new Entities();
	EntityList = JsonConvert.DeserializeObject<Entities>(strJson, new DatavalueConverter()); //create a collection of entity: only one in this case

	Entity entity = EntityList.entities.Values.First(); //extract the first and only item
	string id = entity.id; //extract Q number
	string label=entity.labels["en"].value; //extract english label
	string description = entity.descriptions["en"].value; //extract english description

	txtMessage.Text = 
		"Qnumber: " + id + Environment.NewLine + 
		"Label: " + label + Environment.NewLine + 
		"Description: " + description;
}

/// <summary> Find a list of item from wiki and page using only Wikidata </summary>
private void button39_Click(object sender, EventArgs e)
{
	Site WD = new Site("https://www.wikidata.org", user, password); //Login
	string strJson=WD.LoadWD("enwiki", "Douglas Adams|Italy|Jimmy Wales"); //Search page "Douglas Adams" in english wikipedia
	Entities EntityList = new Entities();
	EntityList = JsonConvert.DeserializeObject<Entities>(strJson, new DatavalueConverter()); //create a collection of entity: three in this case

	if (EntityList.entities != null)
	{
		foreach (Entity entity in EntityList.entities.Values) //loop on each entity
		{
			string id = entity.id; //extract Q number
			string label = entity.labels["en"].value; //extract english label
			string description = entity.descriptions["en"].value; //extract english description
			txtMessage.AppendText(
				"Qnumber: " + id + " - " +
				"Label: " + label + " - " +
				"Description: " + description + Environment.NewLine);
		}
	}
}

/// <summary> Find a list of item from wiki and page using only Wikipedia </summary>
private void button39_Click(object sender, EventArgs e)
{
	Site WP = new Site("https://en.wikipedia.org", user, password); //Login
	string strJson = WP.LoadWP("Douglas Adams|Italy|Jimmy Wales");
	Pages pages = new Pages();
	pages = JsonConvert.DeserializeObject<Pages>(strJson);
	foreach (Page page in pages.query.pages.Values) //loop on each page
	{
		txtMessage.AppendText(page.title + " = " + page.item + Environment.NewLine);
	}
}
Ganeshk (talkcontribs)

That worked! Thank you very much for the example. Is it possible to package the entire library into a single DLL? Right now I am having to include all the classes (site, pages, utilities etc). I am also having to include the change namespace on the classes.

Ganeshk (talkcontribs)

One more request, can you provide me with sample code on how to create a new item like Q60204555 with all its statements and interwiki links?

Ganeshk (talkcontribs)

You can skip this one. I have decided to batch add items using Quickstatements.

ValterVB (talkcontribs)

OK, but if you want, you can see this page The example is for edit one item, if you wan create a new item you must copy only from // Edit entity to the end and change last row in WD.EditEntity("", Sitelinks, Aliases, Descriptions, Aliases, Claims, "BOT:Test"); BOT:Test is the object of the edit.

Ganeshk (talkcontribs)

Hi Valter, the getentities requires that a enwiki link is present. How do I retrieve wikidata items that do not have an interwiki link for enwiki using just the text and language?

Ganeshk (talkcontribs)

I solved the above by creating a search class that can work with wbsearchentities.


    public class Searchinfo

    {

        public string search { get; set; }

    }

    public class Matches

    {

        public string type { get; set; }

        public string language { get; set; }

        public string text { get; set; }

    }

    public class Search

    {

        public string repository { get; set; }

        public string id { get; set; }

        public string concepturi { get; set; }

        public string title { get; set; }

        public int pageid { get; set; }

        public string url { get; set; }

        public string label { get; set; }

        public string description { get; set; }

        public Matches match { get; set; }

    }

    public class Searches

    {

        public Searchinfo searchinfo { get; set; }

        public List<Search> search { get; set; }

        public int success { get; set; }

    }


    public string SearchWD(string lang, string pages)

    {

            string post = "";

            post = string.Format("action=wbsearchentities&format=json&language={0}&search={1}", WebUtility.UrlEncode(lang), WebUtility.UrlEncode(pages));

            return PostRequest(_url + _api, post);

    }

ValterVB (talkcontribs)

Yes, this is a possible solution but wasn't implemented in my BOT because I've never needed it. I had implemented the simple search:

private void button39_Click(object sender, EventArgs e)
{
	Site WD = new Site("https://www.wikidata.org", user, password); //Login
	var res = ListGenerator.ListFromSearch(WD, "Torino", "0"); //Return a tuple  res.count=n° of items res.list=item separated by pipe
	if (res.count != 0)
	{
		txtMessage.Text = "Numebr of items: " + res.count.ToString() + Environment.NewLine;
		txtMessage.AppendText(res.list); //list of items to be passed to  LoadWD
	}
}