<jupyter_start><jupyter_text>ABSA data processingThis notebook pre-processes the xml format data files into pkl file that we will be using for the experiments. Before following the steps in this notebook, you have to 1) download the data and 2) rename it into a specific format.1. Go to http://alt.qcri.org/semeval2016/task5/index.php?id=data-and-tools and download training and test data of Arabic (ar), Chinese (zh), English (en), Russian (ru), Spanish (es). You can select subtask 1 for all languages, and you will have to sign up to get access to the data. For Chinese, we used Mobile Phone reviews, but you can use the digital camera one too. 2. Rename all downloaded data to the format of `{two_character_lang_code}_{train/test}.xml`. For example, the Chinese training file should be renamed to `zh_train.xml`, and the Russian test file should be `ru_test.xml`.3. make new directory called `data/` and move all files under the directory.4. Execute this notebook and you are good to go! -- it will create `absa.pkl` under the same `data/` directory.<jupyter_code>import pickle
import xml.etree.ElementTree as ET
def aggregate_labels(opinions:list) -> "str":
    labels, categories = zip(*opinions)
    if len(set(labels)) == 1:
        return "pos" if labels[0] == "positive" else "neg"
    else:
        num_pos, num_neg = labels.count("positive"), labels.count("negative")
        if num_pos > num_neg:
            return "pos"
        elif num_neg < num_pos:
            return "neg"
        else:
            return None
data = {}
for lang in ["en","es","ru","ar","zh"]:
    data[lang] = {}
    for mode in ["train","test"]:
        tree = ET.parse(f'data/{mode}_{lang}.xml')
        root = tree.getroot()
        xs, ys = [], []
        for child in root:
            for sentences in child:
                for sent in sentences:
                    if sent.find("Opinions"):
                        opinions = [(opinion.attrib["polarity"], opinion.attrib["category"]) for opinion in sent.find("Opinions")]
                        if len(opinions) > 0:
                            xs.append(sent.find("text").text)
                            ys.append(aggregate_labels(opinions))
        len_old = len(ys)
        xs, ys = zip(*[(x,y) for x,y in zip(xs, ys) if y!=None])
        print(f"{lang}-{mode}: {len_old-len(ys)}/{len_old}")
        assert len(xs) == len(ys)
        data[lang][mode] = list(zip(xs,ys))
with open("data/absa.pkl","wb") as file:
    pickle.dump(data, file)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12341269841269842, 'fraction_numerical': 0.005952380952380952, 'mean_word_length': 2.7739520958083834, 'pattern_counts': {'":': 1, '<': 6, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '185002', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 772, 'n_tokens_neox': 743, 'n_words': 265, 'path': 'exercises/06-multilingual/preprocess_data.ipynb', 'repo_name': 'chan0park/lowresource-nlp-bootcamp-2020', 'size': '2520'}