File size: 2,985 Bytes
cbccf14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os
import xml.etree.ElementTree as ET
from datasets import DatasetBuilder, DatasetInfo, BuilderConfig, Split, SplitGenerator

class UZABSAConfig(BuilderConfig):
    def __init__(self, **kwargs):
        super(UZABSAConfig, self).__init__(version="1.0.0", **kwargs)

class UzABSA(DatasetBuilder):
    BUILDER_CONFIG_CLASS = UZABSAConfig
    BUILDER_CONFIGS = [
        UZABSAConfig(name="uzabsa", description="Uzbek ABSA dataset"),
    ]

    def _info(self):
        # Define the dataset features
        return DatasetInfo(
            features={
                "sentence_id": datasets.Value("string"),
                "text": datasets.Value("string"),
                "aspect_terms": datasets.Sequence(
                    {
                        "term": datasets.Value("string"),
                        "polarity": datasets.Value("string"),
                        "from": datasets.Value("int32"),
                        "to": datasets.Value("int32"),
                    }
                ),
                "aspect_categories": datasets.Sequence(
                    {
                        "category": datasets.Value("string"),
                        "polarity": datasets.Value("string"),
                    }
                ),
            }
        )

    def _split_generators(self, dl_manager):
        # Use os.path.join to find the XML file path
        filepath = os.path.join(os.path.dirname(os.path.abspath(__file__)), "/dataset/uzabsa_all.xml")
        return [
            SplitGenerator(
                name=Split.TRAIN,
                gen_kwargs={
                    "filepath": filepath,
                },
            ),
        ]

    def _generate_examples(self, filepath):
        tree = ET.parse(filepath)
        root = tree.getroot()

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

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

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

            yield sentence_id, {
                "sentence_id": sentence_id,
                "text": text,
                "aspect_terms": aspect_terms,
                "aspect_categories": aspect_categories,
            }