File size: 7,142 Bytes
18573e4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | package bg.bas.dcl.LLMs.IfGPTDataset;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import bg.bas.dcl.general.JSONProcessor;
import java.io.File;
/**
* Abstract base for all source processors.
*
* Provides shared utilities:
* - convertJsonToCSV: write a metadata JSONObject to a CSV file
* - estimateTokenCount: simple punctuation-aware token estimator
*
* Each concrete subclass implements {@link SourceProcessor#process(String, String)}
* with source-specific parsing logic.
*/
public abstract class BaseSourceProcessor implements SourceProcessor {
// -----------------------------------------------------------------------
// CSV export
// -----------------------------------------------------------------------
/**
* Reads a metadata.json file from disk and writes a CSV alongside it.
*
* @param metadataJsonPath path to the metadata JSON file
*/
public void convertJsonToCSV(String metadataJsonPath) {
try {
JSONProcessor pr = new JSONProcessor();
JSONObject json = pr.readJSON(new File(metadataJsonPath));
convertJsonToCSV(json, metadataJsonPath + "_CSV.csv");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Writes the "metadata" array inside {@code json} to a CSV at {@code outCsvPath}.
* Reports structural inconsistencies (missing/extra fields) to stderr.
*
* @param json JSONObject that contains a "metadata" JSONArray
* @param outCsvPath destination CSV file path
*/
public void convertJsonToCSV(JSONObject json, String outCsvPath) {
try {
JSONArray array = (JSONArray) json.get("metadata");
if (array == null || array.isEmpty()) {
System.err.println("[INCONSISTENCY] 'metadata' array is null or empty in: " + outCsvPath);
return;
}
// Collect all unique field names, preserving insertion order
LinkedHashSet<String> headersSet = new LinkedHashSet<>();
for (Object obj : array) {
if (obj instanceof JSONObject) {
headersSet.addAll(((JSONObject) obj).keySet());
} else {
System.err.println("[INCONSISTENCY] Non-JSONObject entry found in metadata array.");
}
}
ArrayList<String> headers = new ArrayList<>(headersSet);
try (PrintWriter writer = new PrintWriter(new FileWriter(outCsvPath))) {
// Header row
writer.println(String.join(",", headers));
// Data rows
for (int i = 0; i < array.size(); i++) {
Object obj = array.get(i);
if (!(obj instanceof JSONObject)) {
System.err.println("[INCONSISTENCY] Row " + i + " is not a JSONObject, skipping.");
continue;
}
JSONObject row = (JSONObject) obj;
// Structural checks
for (String header : headers) {
if (!row.containsKey(header)) {
System.err.println("[INCONSISTENCY] Row " + i + " missing field: '" + header + "'");
}
}
for (Object key : row.keySet()) {
if (!headersSet.contains(key.toString())) {
System.err.println("[INCONSISTENCY] Row " + i + " has unexpected field: '" + key + "'");
}
}
// Build CSV line with RFC-4180 escaping
ArrayList<String> values = new ArrayList<>();
for (String header : headers) {
Object value = row.get(header);
if (value == null) {
values.add("");
} else {
String strVal = value.toString();
if (strVal.contains(",") || strVal.contains("\"") || strVal.contains("\n")) {
strVal = "\"" + strVal.replace("\"", "\"\"") + "\"";
}
values.add(strVal);
}
}
writer.println(String.join(",", values));
}
}
System.out.println("CSV written to: " + outCsvPath);
} catch (Exception e) {
e.printStackTrace();
}
}
// -----------------------------------------------------------------------
// Shared helpers
// -----------------------------------------------------------------------
/**
* Estimates the number of tokens in a sentence by counting words plus
* standalone punctuation characters (.,;:?!()-).
*
* @param sentence whitespace-tokenised sentence string
* @return estimated token count
*/
protected int estimateTokenCount(String sentence) {
String[] words = sentence.split(" ");
int punctCount = sentence.length()
- sentence.replaceAll("[.,;:()?!\\-]", "").length();
return words.length + punctCount;
}
/**
* Creates a JSONObject pre-populated with the metadata fields that are
* common to every source (counts start at 0).
*
* @param identifier unique document identifier
* @return partially initialised JSONObject
*/
@SuppressWarnings("unchecked")
protected JSONObject newBaseDescriptor(String identifier) {
JSONObject fdescr = new JSONObject();
fdescr.put("Identifier", identifier);
fdescr.put("Licence", "");
fdescr.put("LicenceLink", "");
fdescr.put("PublicationDate", "");
fdescr.put("DocumentTitle", "");
fdescr.put("Source", "");
fdescr.put("Author", "");
fdescr.put("Style", "");
fdescr.put("Type", "");
fdescr.put("Subdomain", "");
fdescr.put("TranslatedDocument", "");
fdescr.put("CollectionDate", "");
fdescr.put("Medium", "text");
fdescr.put("Url", "");
fdescr.put("Domain", "");
fdescr.put("Keywords", "");
fdescr.put("PersonallyIdentifiableInformation", "");
fdescr.put("BiasedInformation", "");
fdescr.put("TaskCategories", "");
fdescr.put("NumberWords", 0);
fdescr.put("NumberSentences", 0);
fdescr.put("NumberParagraphs", 0);
fdescr.put("NumberTokens", 0);
return fdescr;
}
}
|