Working with Freebase from .NET

Recently I started working on a project that requires information about films, music, books. To do this, you need to populate the database from another resource. I decided to use the free Freebase database.

Freebase is a large knowledge base containing structured data collected from many different sources. It currently has approximately 23 million topics. Each topic is associated with one or more types (people, places, films).

There are several options for retrieving data from Freebase:
  • Download dump database (you can do it here )
  • Use API

I immediately discarded the permanent recovery from the dump (I want something more automated). He began to understand further. To work with the API, you must use one of the 6 services:
  • Search Service - search for entities by keyword;
  • Mql Read Service - extracting detailed data about entities;
  • Topic Service - extracting all information about the entity;
  • RDF Service - extract all information about an entity in RDF format;
  • Text Service - retrieving a short description for an entity;
  • Image Service - receiving a picture for an entity.

The first task that I had to do was autocomplete using Freebase and then writing it to my database. Two services are suitable for this purpose: Search Service and Mql Read Service. I chose the second one because with it you can not only find but also get additional information. Immediately google on the subject of ready libs (I do not like to write bicycles). All that was found was freebase-dotnet (using the old API) and google-api-dotnet-client . Both do not implement the asynchronous features of C #.

Therefore, I was forced to write my own lib, which can be found here . It is still in beta, but in the course of the current project, I will finalize it and make it stable, since it will be used in production.

Having registered in the nuget search for Freebase4net, we install the finished package.

To use the API, we need to get the key from Google (metaweb was bought by Google). The number of possible queries is limited to 100k queries per day.

To install ApiKey we can do so
FreebaseServices.SetApiKey("YOUR API KEY");
either like that

Next, we create the necessary service:
MqlReadService readService = FreebaseServices.CreateMqlReadService();

To search by entity name, you need to send the following MQL query that uses regex: This is done very simply using dynamic:
"name~=":"^The Sco*$"

dynamic films = new ExpandoObject();
films.type = "/film/film"; = FreebaseHelpers.Operators.CreateLikeOperator("^The Sco*$");

Now we actually make a request and get the data. Synchronously
MqlReadServiceResponse result = _readService.Read(films);
or asynchronously
MqlReadServiceResponse result = await _readService.ReadAsync(films);

Under the hood, the new HttpClient and its asynchronous capabilities are used.

Next we get the name of the entity
var name = result.Results[0].name;

Results - an array of dynamic. Since the answer depends on the request, that is, dynamic.

PS There is a ready-made autocomplete , which has a fairly attractive look and functionality. But I had to make my logic for caching data into my database.

With autocomplete sorted out, we move on. Next, on the movie page, I need to display a description and a picture. You can use Topic Service for this, but its response structure is quite complicated - you need to figure out where to get the necessary information. To simplify, TextService and ImageService were created. Therefore, I decided to use them.
string id = "/en/the_animal";
var textService = FreebaseServices.CreateTextService();
var response = await textService.ReadAsync(id);
string description = response.Result;

Getting a link to a picture:
string id = "/en/the_animal";
var imageService = FreebaseServices.CreateImageService();
string image = imageService.GetImageUrl(id, maxwidth: "150");

If you need to make several requests, the functionality is implemented asynchronously.
 dynamic thescorpions = new ExpandoObject(); = "The Scorpions";
thescorpions.type = "/music/artist";
dynamic thepolice = new ExpandoObject(); = "The Police";
thepolice.type = "/music/artist";
List multiFreebaseResponse = readService.ReadMultipleAsyncWithWait(thescorpions, thepolice);

If you have any ideas how to do it better, I will be glad to hear them.

Also popular now: