File size: 1,377 Bytes
7d27e01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 xml.etree.ElementTree as ET
import pandas as pd


# 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: Convert to DataFrame
df = pd.DataFrame(xml_data)

# Step 3: Save as Parquet
df.to_parquet("../aspect-based-sentiment-analysis-uzbek.parquet", index=False)