import os import xml.etree.ElementTree as ET import datasets from datasets import GeneratorBasedBuilder, DatasetInfo, Split, SplitGenerator, Features, Value, Sequence class UzABSA(GeneratorBasedBuilder): VERSION = datasets.Version("1.0.0") BUILDER_CONFIGS = [ datasets.BuilderConfig(name="uzabsa", version=VERSION, description="UZABSA dataset for sentiment analysis in Uzbek"), ] def _info(self): return DatasetInfo( features=Features({ "sentence_id": Value("string"), "text": Value("string"), "aspect_terms": Sequence({ "term": Value("string"), "polarity": Value("string"), "from": Value("int32"), "to": Value("int32"), }), "aspect_categories": Sequence({ "category": Value("string"), "polarity": Value("string"), }), }) ) def _split_generators(self, dl_manager): # Use the dl_manager to download and cache the data downloaded_file = dl_manager.download_and_extract("file:///Sanatbek/aspect-based-sentiment-analysis-uzbek" "/data/absa_uz_all.xml") return [ SplitGenerator(name=Split.TRAIN, gen_kwargs={"filepath": downloaded_file}), ] def _generate_examples(self, filepath): tree = ET.parse(filepath) root = tree.getroot() 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"), }) yield sentence_id, { "sentence_id": sentence_id, "text": text, "aspect_terms": aspect_terms, "aspect_categories": aspect_categories, }