Kim T
Kim T

Reputation: 6444

Auto-suggest entities using the Wikipedia API

I want to provide an auto-suggest feature to my users, where they can choose from a list of known "things" from a semantic entity database.

I'm looking at using the Wikipedia Media API instead of setting up my own: https://www.mediawiki.org/wiki/API:Main_page

There is an API tool for testing requests: https://www.mediawiki.org/wiki/Special:ApiSandbox

For example if a user likes cats: https://www.wikidata.org/wiki/Q146

The requests would be: https://en.wikipedia.org/w/api.php?action=query&format=jsonfm&prop=pageterms&list=&meta=&titles=C https://en.wikipedia.org/w/api.php?action=query&format=jsonfm&prop=pageterms&list=&meta=&titles=Ca https://en.wikipedia.org/w/api.php?action=query&format=jsonfm&prop=pageterms&list=&meta=&titles=Cat

The user would select Cat from the dropdown, and I would save the ID.

Is this a good approach? How could I improve it?

Upvotes: 1

Views: 652

Answers (1)

Kim T
Kim T

Reputation: 6444

I managed to load autosuggestions using the Wikipedia REST api:

https://en.wikipedia.org/w/api.php?action=query&format=json&gsrlimit=15&generator=search&origin=*&gsrsearch=

The Angular code:

this.resultsCtrl = new FormControl();
this.resultsCtrl.valueChanges
  .debounceTime(400)
  .subscribe(name => {
    if (name) {
      this.filteredResults = this.filterResults(name);
    }
  });

filterResults(name: string) {
  return Observable.create(obs => {
    const displaySuggestions = function (response) {
      if (!response) {
        obs.error(status);
      } else {
        obs.next(Object.keys(response.query.pages).map(key => response.query.pages[key]));
        obs.complete();
      }
    };
    this.http.get('https://en.wikipedia.org/w/api.php?action=query&format=json&gsrlimit=15&generator=search&origin=*&gsrsearch='
      + encodeURI(name))
      .subscribe(displaySuggestions);
  });
}

Upvotes: 1

Related Questions