Sanatbek's picture
Loader script is added
cbccf14
raw
history blame
2.99 kB
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,
}