"""wiktionary.

Description
-----------
Interface to Danish part of the different Wiktionaries.

"""


import requests


API_URL_PATTERN = "https://{}.wiktionary.org/w/api.php"


def get_nouns(languages=('da', 'de', 'en')):
    """Return Danish nouns from Wiktionary.

    The function will query the API of the different language versions of
    Wiktionary getting members of the (main) category of Danish nouns.

    Parameters
    ----------
    languages : list of str
        List or set of string with indication of language version of
        Wiktionary from where to query the Danish nouns from.

    Returns
    -------
    nouns : set or str
        Set of strings with Danish nouns.

    Examples
    --------
    >>> nouns = get_nouns(languages=['de'])
    >>> 'abefest' in nouns
    True

    """
    categories = {
        'da': u'Kategori:Substantiver_p\xe5_dansk',
        'de': u'Kategorie:Substantiv (D\xe4nisch)',
        'en': 'Category:Danish_nouns'
    }

    params = {
        'format': 'json',
        'action': 'query',
        'list': 'categorymembers',
        'cmlimit': 500
    }

    nouns = []
    for language in languages:
        prefix = categories[language].split(':')[0]
        url = API_URL_PATTERN.format(language)
        params['cmtitle'] = categories[language]
        params['cmcontinue'] = ''
        response = requests.get(url, params=params).json()
        nouns.extend([member['title']
                      for member in response['query']['categorymembers']
                      if not member['title'].startswith(prefix)])
        while 'continue' in response:
            params['cmcontinue'] = response['continue']['cmcontinue']
            response = requests.get(url, params=params).json()
            nouns.extend(
                [member['title']
                 for member in response['query']['categorymembers']
                 if not member['title'].startswith(prefix)])

    return set(nouns)

@@ -1154,60 +1154,8 @@
 es:%0A
-        prefix = categories%5Blanguage%5D.split(':')%5B0%5D%0A
     
@@ -1197,16 +1197,16 @@
 nguage)%0A
+
         
@@ -1467,35 +1467,42 @@
              if 
-not
+':' not in
  member%5B'title'%5D
@@ -1501,35 +1501,16 @@
 'title'%5D
-.startswith(prefix)
 %5D)%0A     
@@ -1822,19 +1822,26 @@
      if 
-not
+':' not in
  member%5B
@@ -1852,27 +1852,8 @@
 le'%5D
-.startswith(prefix)
 %5D)%0A%0A

Fix problem with words not in main namespace