Sanatbek_Matlatipov
jsonL file is added. XML TO jsonl Converter is added
c111fdf
raw
history blame
1.41 kB
import json
import xml.etree.ElementTree as ET
# 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: Save as JSONL
with open("../aspect-based-sentiment-analysis-uzbek.jsonl", "w") as outfile:
for entry in xml_data:
json_str = json.dumps(entry)
outfile.write(json_str + "\n")