from bs4 import BeautifulSoup
from typing import Counter


def get_places(file, num=10):
    """Extract most frequent places from xml files by tag

    Args:
        file (string): path to .xml file
        num (int): number of places returned

    Returns:
        places_list (list): list of most common places
        frequency_list (list): frequency of places_list
    """
    soup = BeautifulSoup(open(file, "rb"), "html.parser")

    # Get text and authname of all places if available
    places = [(p["reg"], p["authname"]) for p in soup.find_all("placename") if ("authname" in p.attrs and "reg" in p.attrs)]

    # Separate authnames and count the most common
    authnames = [p[1] for p in places]
    counter_authnames = Counter(authnames).most_common(num)

    # For each authname list the reg names appearing in the tag
    # and exclude frequencies
    names_count = [
        (
            list(set([place[0] for place in places if p[0] == place[1]])),
            p[1]
        )
        for p in counter_authnames
    ]
    frequency_list = [n[1] for n in names_count]

    # Create a list of places by sorting by the longest name with capital letter
    places_list = [n[0] for n in names_count]
    places_list = [[n for n in p if n] for p in places_list]
    places_list = [sorted(p, key=lambda x: (len(x), x[0].isupper()),
                          reverse=True) for p in places_list]
    places_list = [p[0] for p in places_list if p]
    places_list = places_list[::-1]
    frequency_list = frequency_list[::-1]

    return places_list, frequency_list