Using Wikipedia API for knowledge graph in SUSPER

Knowledge Graph is way to give a brief description about search query by connecting it to a real world entity. This helps users to get information about exactly what they want. Previously Susper had a Knowledge Graph which was implemented using DBpedia API. But since DBpedia do not provide content over HTTPS connections therefore the content was blocked on susper.com and there was a need to implement the Knowledge Graph using a new API that provide contents over HTTPS. In this blog, I will describe how getting a knowledge graph was made possible using Wikipedia API.

What is Wikipedia API ?

The MediaWiki action API is a web service that provides convenient access to wiki features, data, and metadata over HTTP, via a URL usually at api.php. Clients request particular “actions” by specifying an action parameter, mainly action=query to get information.

The endpoint :

https://en.wikipedia.org/w/api.php

The format :

format=json This tells the API that we want data to be returned in JSON format.

The action :

action=query

The MediaWiki web service API implements dozens of actions and extensions implement many more; the dynamically generated API help documents all available actions on a wiki. In this case, we’re using the “query” action to get some information.

The complete API which is used in SUSPER to extract information of a query is :

https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=japan

Where titles=Search_Query, here Japan

How it is implemented in SUSPER?

For implementing it a service has been created which fetches information by setting various URL parameters. This result can be fetched by creating an instance of service and passing search query to getsearchresults(searchquery) function.

export class KnowledgeapiService {
 server = 'https://en.wikipedia.org';
 searchURL = this.server + '/w/api.php?';
 homepage = 'http://susper.com';
 logo = '../images/susper.svg';
 constructor(private http: Http,
             private jsonp: Jsonp,
             private store: Store<fromRoot.State>) {
 }
 getsearchresults(searchquery) {
   let params = new URLSearchParams();
   params.set('origin', '*');
   params.set('format', 'json');
   params.set('action', 'query');
   params.set('prop', 'extracts');
   params.set('exintro', '');
   params.set('explaintext', '');
   params.set('titles', searchquery);
   let headers = new Headers({ 'Accept': 'application/json' });
   let options = new RequestOptions({ headers: headers, search: params });
   return this.http
     .get(this.searchURL, options).map(res =>
         res.json().query.pages
     ).catch(this.handleError);
}

 

Since the result obtained is an observable therefore we have to subscribe for it and then extract information to local variables in infobox.component.ts file.

export class InfoboxComponent implements OnInit {
 public title: string;
 public description: string;
 query$: any;
 resultsearch = '/search';
 constructor(private knowledgeservice: KnowledgeapiService,
             private route: Router,
             private activatedroute: ActivatedRoute,
             private store: Store<fromRoot.State>,
             private ref: ChangeDetectorRef) {
   this.query$ = store.select(fromRoot.getquery);
   this.query$.subscribe( query => {
     if (query) {
       this.knowledgeservice.getsearchresults(query).subscribe(res => {
         const pageId = Object.keys(res)[0];
         if (res[pageId].extract) {
           this.title = res[pageId].title;
           this.description = res[pageId].extract;
         } else {
           this.title = '';
           this.description = '';
         }
       });
     }
   });
 }

The variable title and description are used to display results on results page.

<div *ngIf=“this.description” class=“card”>
  <div>
    <h2><b>{{this.title}}</b></h2>
    <p>{{this.description | slice:0:600}}<a href=‘https://en.wikipedia.org/wiki/{{this.title}}’>..more at Wikipedia</a></p>
  </div>
</div>

Resources

1.MediaWiki API : https://www.mediawiki.org/wiki/API:Main_page

2.Stackoverflow : https://stackoverflow.com/questions/8555320/is-there-a-clean-wikipedia-api-just-for-retrieve-content-summary

3.Angular Docs : https://angular.io/tutorial/toh-pt4

OpenTech and Open Knowledge in the Mekong Delta and throughout Asia

The FOSSASIA organization has been very active and I would love to give you a few updates on our activities. One thing why we love to work in this community is because you get the chance to meet some of the most awesome and friendly tech people of the world. Jonas Smeedegaard from the Debian community is one of our guests who has been visiting us three times already and conducted workshops at our events and even stayed several weeks in Can Tho to train students using and developing Debian.

Jonas Smeedegaard at Workshop: Bringing Asian and International developers together

Meetups and Code Sprints

Of course we could not do all of this alone and we would like to thank some of the most outstanding folks we are working together with including HanoiLUG and Saigonlug in Vietnam, Singapore Hackerspace and the Beijinglug.

Coding Projects

We have recently involved and organized activities with the following projects and are glad to feature the following projects on this blog:

  • Wikipedia
  • OpenWrt
  • Crypto-Stick
  • LXDE
  • MoonOS
  • Android
  • GNOME
  • Debian
  • Fedora
  • Gimp
  • Inkscape
  • Linux conversion libraries
  • OpenStreetMap
  • and many more

FOSSASIA Event in Ho Chi Minh City

Internships

In 2012 we welcomed interns in Can Tho. Fifteen students participated in the program as full time interns in the office of MBM International in the biggest city in the Mekong delta. The program took place for 2 months and a core part of it was to learn how to use collaborative tools liker issue trackers and contribute to OpenStreetMap. Our students also love the Wikipedia sprints and we got a lot of positive vibes as everyone was happy to learn how to set up the Mediawiki software, that empowers the encyclopedia, share their ideas and spread Open Knowledge and Open Data about the Mekong Delta.

Event activities

We also continued to organize the FOSSASIA OpenTechSummit in 2010 and 2011 in Raffles College and Van Lang University in Ho Chi Minh City Vietnam. Plus we had a Mini-Debconf in Saigon and organized the event series OpenDesign.Asia Weeks bringing together designers and Open Source developers. We have links to the LXDE project and GNOME community and we have sent developers to the GNOME.Asia Summit 2012 in Hong Kong and supported the TYPO3 conference 2012 in Phnom Penh.

Also globally FOSSASIA starts to receive more attention. We have received the opportunity to introduce Asian projects at the Libre Graphics Meeting in Montreal (2011) and Brussels (2011), at the Linuxtag in Berlin (2012) and many other community events.

Please Join us and Get in Touch

The FOSSASIA organization supports activities and Open Tech development and Open Knowledge projects of the community active in Asia. If you are interested to cooperate for a project, need support for a development sprint or an event, please contact us on the FOSSASIA mailing list: http://groups.google.com/group/fossasia