parseARFFFile method
Implementation
Future<ARFF> parseARFFFile({required String fileName}) async {
final fileContent = await rootBundle.loadString(fileName);
final lines = fileContent.split('\n');
String relation = '';
List<ARFFAttributes> attributesList = [];
List<List<ARFFData>> arffData = [];
bool dataSection = false;
for (var line in lines) {
line = line.trim();
if (line.isEmpty || line.startsWith('%')) continue;
if (line.startsWith('@relation')) {
relation = line.split(' ')[1];
} else if (line.startsWith('@attribute')) {
final parts = line.split(' ');
final name = parts[1];
final type = parts
.sublist(2)
.toString()
.replaceAll("[", "")
.replaceAll("]", "")
.replaceAll(' ', '')
.replaceAll(',,', ',');
if (type.startsWith('{')) {
final values = type
.substring(1, type.length - 1)
.split(',')
.map((v) => v
.trim()
.replaceAll('\'', '')
.replaceAll('}', '')
.replaceAll(']', ''))
.toList();
attributesList.add(ARFFAttributes(name, 'nominal', values));
} else {
attributesList.add(ARFFAttributes(name, type));
}
} else if (line.startsWith('@data')) {
dataSection = true;
} else if (dataSection) {
List<dynamic> values =
line.split(',').map((v) => v.trim().replaceAll("\'", "")).toList();
List<ARFFData> arffDtList = [];
for (int index = 0; index < attributesList.length; index++) {
ARFFData arffDt =
ARFFData(name: attributesList[index].name, value: values[index]);
arffDtList.add(arffDt);
}
arffData.add(arffDtList);
}
}
return ARFF(relation, attributesList, arffData);
}