File size: 1,408 Bytes
c111fdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import json
import xml.etree.ElementTree as ET


# Step 1: Parse XML
def xml_to_list(filepath):
    tree = ET.parse(filepath)
    root = tree.getroot()

    data = []
    for sentence in root.findall("sentence"):
        sentence_id = sentence.get("ID")
        text = sentence.find("text").text

        aspect_terms = []
        for aspect_term in sentence.findall("./aspectTerms/aspectTerm"):
            aspect_terms.append({
                "term": aspect_term.get("term"),
                "polarity": aspect_term.get("polarity"),
                "from": int(aspect_term.get("from")),
                "to": int(aspect_term.get("to")),
            })

        aspect_categories = []
        for aspect_category in sentence.findall("./aspectCategories/aspectCategory"):
            aspect_categories.append({
                "category": aspect_category.get("category"),
                "polarity": aspect_category.get("polarity"),
            })

        data.append({
            "sentence_id": sentence_id,
            "text": text,
            "aspect_terms": aspect_terms,
            "aspect_categories": aspect_categories,
        })
    return data


xml_data = xml_to_list("absa_uz_all.xml")

# Step 2: Save as JSONL
with open("../aspect-based-sentiment-analysis-uzbek.jsonl", "w") as outfile:
    for entry in xml_data:
        json_str = json.dumps(entry)
        outfile.write(json_str + "\n")