Loader script is added
Browse files
uzabsa.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import xml.etree.ElementTree as ET
|
3 |
+
from datasets import DatasetBuilder, DatasetInfo, BuilderConfig, Split, SplitGenerator
|
4 |
+
|
5 |
+
class UZABSAConfig(BuilderConfig):
|
6 |
+
def __init__(self, **kwargs):
|
7 |
+
super(UZABSAConfig, self).__init__(version="1.0.0", **kwargs)
|
8 |
+
|
9 |
+
class UzABSA(DatasetBuilder):
|
10 |
+
BUILDER_CONFIG_CLASS = UZABSAConfig
|
11 |
+
BUILDER_CONFIGS = [
|
12 |
+
UZABSAConfig(name="uzabsa", description="Uzbek ABSA dataset"),
|
13 |
+
]
|
14 |
+
|
15 |
+
def _info(self):
|
16 |
+
# Define the dataset features
|
17 |
+
return DatasetInfo(
|
18 |
+
features={
|
19 |
+
"sentence_id": datasets.Value("string"),
|
20 |
+
"text": datasets.Value("string"),
|
21 |
+
"aspect_terms": datasets.Sequence(
|
22 |
+
{
|
23 |
+
"term": datasets.Value("string"),
|
24 |
+
"polarity": datasets.Value("string"),
|
25 |
+
"from": datasets.Value("int32"),
|
26 |
+
"to": datasets.Value("int32"),
|
27 |
+
}
|
28 |
+
),
|
29 |
+
"aspect_categories": datasets.Sequence(
|
30 |
+
{
|
31 |
+
"category": datasets.Value("string"),
|
32 |
+
"polarity": datasets.Value("string"),
|
33 |
+
}
|
34 |
+
),
|
35 |
+
}
|
36 |
+
)
|
37 |
+
|
38 |
+
def _split_generators(self, dl_manager):
|
39 |
+
# Use os.path.join to find the XML file path
|
40 |
+
filepath = os.path.join(os.path.dirname(os.path.abspath(__file__)), "/dataset/uzabsa_all.xml")
|
41 |
+
return [
|
42 |
+
SplitGenerator(
|
43 |
+
name=Split.TRAIN,
|
44 |
+
gen_kwargs={
|
45 |
+
"filepath": filepath,
|
46 |
+
},
|
47 |
+
),
|
48 |
+
]
|
49 |
+
|
50 |
+
def _generate_examples(self, filepath):
|
51 |
+
tree = ET.parse(filepath)
|
52 |
+
root = tree.getroot()
|
53 |
+
|
54 |
+
for sentence in root.findall("sentence"):
|
55 |
+
sentence_id = sentence.attrib["ID"]
|
56 |
+
text = sentence.find("text").text
|
57 |
+
|
58 |
+
aspect_terms = []
|
59 |
+
for aspect_term in sentence.findall(".//aspectTerms/aspectTerm"):
|
60 |
+
aspect_terms.append(
|
61 |
+
{
|
62 |
+
"term": aspect_term.attrib["term"],
|
63 |
+
"polarity": aspect_term.attrib["polarity"],
|
64 |
+
"from": int(aspect_term.attrib["from"]),
|
65 |
+
"to": int(aspect_term.attrib["to"]),
|
66 |
+
}
|
67 |
+
)
|
68 |
+
|
69 |
+
aspect_categories = []
|
70 |
+
for aspect_category in sentence.findall(".//aspectCategories/aspectCategory"):
|
71 |
+
aspect_categories.append(
|
72 |
+
{
|
73 |
+
"category": aspect_category.attrib["category"],
|
74 |
+
"polarity": aspect_category.attrib["polarity"],
|
75 |
+
}
|
76 |
+
)
|
77 |
+
|
78 |
+
yield sentence_id, {
|
79 |
+
"sentence_id": sentence_id,
|
80 |
+
"text": text,
|
81 |
+
"aspect_terms": aspect_terms,
|
82 |
+
"aspect_categories": aspect_categories,
|
83 |
+
}
|