from bs4 import BeautifulSoup


def get_person_speech_pair(file_name):
    """
    XML parser to get the person_ids from given XML file
    Args:
        file_name(str): file name
    Returns:
        person_id_speech_pair(dict): Dict[person_id(int) -> speech(str)]
    """
    person_id_speech_dict = dict()
    with open(file_name, encoding='utf-8') as file:
        soup = BeautifulSoup(file, 'xml')
        all_speech = soup.find_all('speaker')
        for single_speech in all_speech:
            try:  # newer format
                person_id = single_speech['personId']
            except KeyError:
                try:  # older format
                    person_id = single_speech['person']
                except KeyError:
                    continue
            single_speech_list = []
            for s in single_speech.stripped_strings:
                single_speech_list.append(s)
            processed_speech = ' '.join(single_speech_list)
            #             print(parsed_speech, '\n')
            if person_id not in person_id_speech_dict:
                person_id_speech_dict[person_id] = []
            person_id_speech_dict[person_id].append(processed_speech)

    for person_id in person_id_speech_dict:
        person_id_speech_dict[person_id] = ' '.join(person_id_speech_dict[person_id])
    return person_id_speech_dict