Neo
Neo

Reputation: 407

How to limit Wikipedia API Autocomplete suggestions to a certain category?

I have this awesome working code that pulls Autocomplete suggestions from Wikipedia. This is really useful, except that I'd like it to only recommend Bands. So for instance, typing in "Pink" would only return /bands/ that start with the word "Pink", rather than everything in the world dealing with 'Pink'.

Does anyone know how to accomplish this? http://codepen.io/anon/pen/VePapK

I found there's a way to filter results by category by using the query action here (rather than opensearch), but the strange thing is that there doesn't seem to be a broad "Music" or "Bands" category. So that's opening even more unanswered questions.... Any help appreciated.

EDIT: Alternatively, if anyone knows an easier way to feature an updated list of all significant bands on your site's dropdown, please let me know.

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<body>
            <form method="get" id="search">
                <input type="text" class="searchbox" value="Type here.. " onblur="if(this.value == '') { this.value = 'Type here..'; }" onfocus="if(this.value == 'Type here..') { this.value = ''; }" name="s">
                <button type="submit">Submit</button>
            </form> 





<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/themes/smoothness/jquery-ui.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js"></script>
<script type="text/javascript">
$(".searchbox").autocomplete({
    source: function(request, response) {
        console.log(request.term);
        $.ajax({
            url: "http://en.wikipedia.org/w/api.php",
            dataType: "jsonp",
            data: {
                'action': "opensearch",
                'format': "json",
                'search': request.term
            },
            success: function(data) {
                response(data[1]);
            }
        });
    }
});
</script>
</body>
</html>

Upvotes: 3

Views: 748

Answers (1)

slaporte
slaporte

Reputation: 703

There are indeed a lot of questions to open about Wikipedia's category system. Thankfully, there is a better way.

Here is an alternative approach that will get you what you need from Wikipedia search: you can use search parameters with Wikipedia's CirrusSearch API to get articles within a category (via incategory:<category title>), with a link to another Wikipedia page (via linksto:<page name>), or by a page that transcludes a common Wikipedia template (via hastemplate:<template name>). If you know the right Wikipedia category or page, you can use the first two options easily. Although the last one might sound odd, Wikipedia's well organized template system may be your best bet. Many band articles will have a common infobox on the right that will help you narrow your search to bands.

You can use CirrusSearch and hastemplate:Infobox_musical_artist together like this:

https://en.wikipedia.org/w/api.php?action=query&list=search&format=json&srsearch=hastemplate%3AInfobox_musical_artist+daft

Which returns a list of pages in this format:

{
"batchcomplete": "",
"continue": {
    "sroffset": 10,
    "continue": "-||"
},
"query": {
    "searchinfo": {
        "totalhits": 470,
        "suggestion": "hastemplate:Infobox_musical_artist dart",
        "suggestionsnippet": "hastemplate:Infobox_musical_artist <em>dart</em>"
    },
    "search": [
        {
            "ns": 0,
            "title": "Daft Punk",
            "snippet": "<span class=\"searchmatch\">Daft</span> Punk are an electronic music duo consisting of French musicians Guy-Manuel de Homem-Christo and Thomas Bangalter. The duo achieved significant popularity",
            "size": 76278,
            "wordcount": 8170,
            "timestamp": "2016-02-08T01:33:54Z"
        },
        ...

Here is a CodePen demo: http://codepen.io/slaporte/pen/obmaWY

It still may not capture 100% of the bands on Wikipedia. If there are specific subgenres you are looking for you, you can try using the hastemplate parameter along with links or categories to narrow or expand your search. You can learn more about the other Wikipedia CirrusSearch parameters here.

By the way, this is an interesting approach to autocomplete!

Upvotes: 4

Related Questions