Submissions/Wikidata Toolkit: A Java library for working with Wikidata

From Wikimania 2014 • London, United Kingdom
Jump to: navigation, search
Yes check.svg

This is an accepted submission for Wikimania 2014.

Submission no. 5053
Title of the submission

Wikidata Toolkit: A Java library for working with Wikidata

Type of submission (discussion, hot seat, panel, presentation, tutorial, workshop)

Tutorial

Author of the submission

Markus Krötzsch (contact author), Michael Günther (co-presenter), Julian Mendez (co-presenter)

E-mail address

markus.kroetzsch@tu-dresden.de

Username

Markus Krötzsch (talk)

Country of origin

Germany

Affiliation, if any (organisation, company etc.)

TU Dresden

Personal homepage or blog

http://korrekt.org/

Abstract (at least 300 words to describe your proposal)

Wikidata Toolkit is a Java library that greatly simplifies using data from Wikidata or other Wikibase installations in your programs. It provides data structures to mirror all Wikibase data in Java, and convenient facilities to load, manipulate, analyse, and query such data. The primary goal of the project is to enable new and innovative applications around Wikidata, and thus to serve a wider community of developers, researchers, and practitioners that are eager to take advantage of that new data resource.

However, the project is very recent and thus not widely known yet. In fact, development is supported by an Individual Engagement Grant of the WMF that runs from February till August 2014, such that the initial funding phase only just finished at the time of Wikimania. It is therefore an ideal time to present the features, provide help to current users, and discuss next steps with the community.

This tutorial therefore provides a practical introduction to the Wikidata Toolkit for the working Java developer. The goal of this initial introduction is to explain the overall architecture and programming facilities that the library provides, and to enable participants to develop their own data-driven applications.

The planned structure of the tutorial is as follows:

  • Feature overview: what Wikidata Toolkit can do for you
  • Main components: which parts do you actually need
  • The Wikidata data model for the working developer
  • My first application: a data-driven equivalent of "Hello World"
  • Towards serious applications: further examples explained
  • Performance considerations: how big a machine you might need
  • Wikidata Toolkit workshop: bring your own questions

The overall time being quite short, extensive hands-on sessions are not included here, but we will have a few developers around who can help with practical problems, also in the breaks after the tutorial.

Although Java is the programming language used by Wikidata Toolkit, the tutorial should also be of interest to developers working in other languages. On the one hand, the toolkit can still be a valuable resource for pre-processing data to be used in another software. On the other hand, it provides reference implementations of several key mechanisms and data structures that are useful to work with Wikidata.


Track
  • Technology, Interface & Infrastructure
Length of session (if other than 30 minutes, specify how long)
30 minutes
Will you attend Wikimania if your submission is not accepted?

Yes

Slides or further information (optional)
  • Slides for the talk
  • Information about the Wikidata Toolkit is found on the project homepage
  • Some relevant applications fields are outlined in the paper Wikidata: A Free Collaborative Knowledge Base (with Denny Vrandecic).
  • A wider introduction to Wikidata usage for the non-technical audience is given in Submissions/How to use Wikidata: Things to make and do with 30 million statements (Open Data track). In contrast to this presentation, the present tutorial is specifically about the recent Wikidata Toolkit IEG project. The target audience here are developers and the presentation will be delivered by several developers of the Wikidata Toolkit project.
  • Code used in the demo:
    • The class WikimaniaExample used for the (slow version of the) iteration over all Wikidata items; this code will work on Wikidata Toolkit 0.2.0:
  1. package org.wikidata.wdtk.examples;
    
  2.  
    
  3. import org.wikidata.wdtk.dumpfiles.DumpProcessingController;
    
  4. import org.wikidata.wdtk.dumpfiles.MwRevision;
    
  5. import org.wikidata.wdtk.dumpfiles.StatisticsMwRevisionProcessor;
    
  6.  
    
  7. public class WikimaniaExample {
    
  8.  
    
  9. 	public static void main(String[] args) {
    
  10. 		ExampleHelpers.configureLogging();
    
  11.  
    
  12. 		// Controller object for processing dumps:
    
  13. 		DumpProcessingController dumpProcessingController = new DumpProcessingController(
    
  14. 				"wikidatawiki");
    
  15. 		dumpProcessingController.setOfflineMode(true);
    
  16.  
    
  17. 		// Example processor for item documents:
    
  18. 		WikimaniaDocumentProcessor documentProcessor = new WikimaniaDocumentProcessor();
    
  19. 		dumpProcessingController.registerEntityDocumentProcessor(
    
  20. 				documentProcessor, MwRevision.MODEL_WIKIBASE_ITEM, true);
    
  21.  
    
  22. 		// Another processor for statistics & time keeping:
    
  23. 		dumpProcessingController.registerMwRevisionProcessor(
    
  24. 				new StatisticsMwRevisionProcessor("statistics", 10000), null,
    
  25. 				true);
    
  26.  
    
  27. 		dumpProcessingController.processMostRecentMainDump();
    
  28.  
    
  29. 		documentProcessor.storeResults();
    
  30. 	}
    
  31.  
    
  32. }
    
    • The class WikimaniaDocumentProcessor used to compute average life expectancy and to print it to a CSV file:
  1. package org.wikidata.wdtk.examples;
    
  2.  
    
  3. import java.io.FileOutputStream;
    
  4. import java.io.IOException;
    
  5. import java.io.PrintStream;
    
  6.  
    
  7. import org.wikidata.wdtk.datamodel.interfaces.EntityDocumentProcessor;
    
  8. import org.wikidata.wdtk.datamodel.interfaces.ItemDocument;
    
  9. import org.wikidata.wdtk.datamodel.interfaces.PropertyDocument;
    
  10. import org.wikidata.wdtk.datamodel.interfaces.Statement;
    
  11. import org.wikidata.wdtk.datamodel.interfaces.StatementGroup;
    
  12. import org.wikidata.wdtk.datamodel.interfaces.TimeValue;
    
  13. import org.wikidata.wdtk.datamodel.interfaces.Value;
    
  14. import org.wikidata.wdtk.datamodel.interfaces.ValueSnak;
    
  15.  
    
  16. public class WikimaniaDocumentProcessor extends Object implements
    
  17. 		EntityDocumentProcessor {
    
  18. 	long countItems = 0;
    
  19. 	long populationCount = 0;
    
  20.  
    
  21. 	final long[] lifeSpans = new long[2100];
    
  22. 	final long[] peopleCount = new long[2100];
    
  23.  
    
  24. 	@Override
    
  25. 	public void processItemDocument(ItemDocument itemDocument) {
    
  26. 		this.countItems++;
    
  27.  
    
  28. 		int birthYear = Integer.MIN_VALUE;
    
  29. 		int deathYear = Integer.MIN_VALUE;
    
  30.  
    
  31. 		for (StatementGroup sg : itemDocument.getStatementGroups()) {
    
  32. 			// P569 is "birth date"
    
  33. 			if ("P569".equals(sg.getProperty().getId())) {
    
  34. 				for (Statement s : sg.getStatements()) {
    
  35. 					if (s.getClaim().getMainSnak() instanceof ValueSnak) {
    
  36. 						Value v = ((ValueSnak) s.getClaim().getMainSnak())
    
  37. 								.getValue();
    
  38. 						if (v instanceof TimeValue) {
    
  39. 							birthYear = (int) ((TimeValue) v).getYear();
    
  40. 							break;
    
  41. 						}
    
  42. 					}
    
  43. 				}
    
  44. 			}
    
  45. 			// P570 is "death date"
    
  46. 			if ("P570".equals(sg.getProperty().getId())) {
    
  47. 				for (Statement s : sg.getStatements()) {
    
  48. 					if (s.getClaim().getMainSnak() instanceof ValueSnak) {
    
  49. 						Value v = ((ValueSnak) s.getClaim().getMainSnak())
    
  50. 								.getValue();
    
  51. 						if (v instanceof TimeValue) {
    
  52. 							deathYear = (int) ((TimeValue) v).getYear();
    
  53. 							break;
    
  54. 						}
    
  55. 					}
    
  56. 				}
    
  57. 			}
    
  58. 		}
    
  59.  
    
  60. 		if (birthYear != Integer.MIN_VALUE && deathYear != Integer.MIN_VALUE
    
  61. 				&& birthYear >= 1200) {
    
  62. 			if (deathYear > birthYear && deathYear - birthYear < 130) {
    
  63. 				lifeSpans[birthYear] += (deathYear - birthYear);
    
  64. 				peopleCount[birthYear]++;
    
  65. 			}
    
  66. 		}
    
  67. 	}
    
  68.  
    
  69. 	@Override
    
  70. 	public void processPropertyDocument(PropertyDocument propertyDocument) {
    
  71. 		// TODO Auto-generated method stub
    
  72. 	}
    
  73.  
    
  74. 	@Override
    
  75. 	public void finishProcessingEntityDocuments() {
    
  76. 		// TODO Auto-generated method stub
    
  77. 	}
    
  78.  
    
  79. 	public void storeResults() {
    
  80. 		try (PrintStream out = new PrintStream(new FileOutputStream(
    
  81. 				"results.csv"))) {
    
  82. 			for (int i = 0; i < lifeSpans.length; i++) {
    
  83. 				if (peopleCount[i] != 0) {
    
  84. 					out.println(i + "," + (double) lifeSpans[i]
    
  85. 							/ peopleCount[i] + "," + peopleCount[i]);
    
  86. 				}
    
  87. 			}
    
  88. 		} catch (IOException e) {
    
  89. 			System.out.println("Oops");
    
  90. 		}
    
  91.  
    
  92. 	}
    
  93.  
    
  94. }
    
Special requests
  • Must leave on Sunday, so presentation should be on Friday or Saturday if at all possible
  • This talk should be given later than Lydia's Wikidata keynote and not in parallel to any Wikidata talk in the Open Data track.


Interested attendees[edit | edit source]

If you are interested in attending this session, please sign with your username below. This will help reviewers to decide which sessions are of high interest. Sign with a hash and four tildes. (# ~~~~).

  1. --Sannita (talk) 22:14, 31 March 2014 (UTC)
  2. Bene* (talk) 14:05, 1 April 2014 (UTC)
  3. Tpt (talk) 14:27, 4 April 2014 (UTC)
  4. Promelior (talk) 14:07, 31 July 2014 (UTC)
  5. I will be your session host Edwardx (talk) 18:02, 31 July 2014 (UTC)
  6. Maximilianklein (talk) 15:45, 7 August 2014 (UTC)
  7. Add your username here.