from bs4 import BeautifulSoup


def get_wiki_from_spotlight_by_name(spotlight, name):
    """Given the spotlight output, and a name string, e.g. 'hong kong'
    returns the wikipedia tag assigned by spotlight, if it exists, else '-'."""

    actual_found = 0
    parsed_spotlight = BeautifulSoup(spotlight.text, 'lxml')
    for wiki_tag in parsed_spotlight.find_all('a'):

        if wiki_tag.string.lower() == name.lower():
            actual_found += 1
            return wiki_tag.get('href').split('/')[-1], actual_found

    # If nothing found, try to match based on prefixes, e.g. match the name Estonia to the tag for 'Estonian'

    for wiki_tag in parsed_spotlight.find_all('a'):
        if wiki_tag.string.lower()[:len(name)] == name.lower():
            actual_found += 1
            return wiki_tag.get('href').split('/')[-1], actual_found

    return '-', actual_found