August 5, 2013

How to get all aliases/nicknames/redirects of a wikipedia entity?

You have a wikipedia entity like Boris Berezovsky (businessman):

What you want is all the nicknames or aliases of this specific entity. Here I will describe how to get this information from DBPedia, Freebase and if you want to stay hard core from wikipedia itself.

To use DBpedia we can use Jena  I use scala SBT (maven equivalent for java) to import the dependency

In the build.sbt file add the following line:
       libraryDependencies += "org.apache.jena" % "jena-core" % "2.7.3"

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.vocabulary.OWL;

public class z {
public static void main(String[] args) {
 String rdfFile = "";
 Model model = ModelFactory.createDefaultModel();;
 System.out.println("Following onbjects have same owl:sameas property: ");
 StmtIterator statements = model.listStatements((Resource)null, OWL.sameAs, (RDFNode)null);
  Statement statement = statements.nextStatement();
  Resource subject = statement.getSubject();
  System.out.println("Same as");
  Resource object = (Resource) statement.getObject();
  } else if(object.isLiteral()){
  } else if(object.isResource()){
  } else{
   System.out.println("none: " + object);

This will require the RDF link of the entity you are looking for (here: You can get this from the DBPedia page. The output would be:
Following onbjects have same owl:sameas property:
Same as
Same as
 If you go to the freebase page you will notice an entry named Also known as /common/topic/alias which would enumerate all aliases of this person. 


Ok if you want aliases or nicknames from wikipedia itself, you can use wikipedia API as follows:

HAppy Aliasing :)

No comments:

Post a Comment