File size: 18,152 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package bg.bas.dcl.LLMs.IfGPTDataset;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

/**
 * DocumentMetadata
 *
 * Canonical in-memory representation of the ifGPT dataset metadata schema.
  
 */
@SuppressWarnings("unchecked")
public class DocumentMetadata {

    // -----------------------------------------------------------------------
    // ── MANDATORY (15) ──────────────────────────────────────────────────────
    // -----------------------------------------------------------------------

    /** Unique document identifier with the language prefix "bg". */
    private String       identifier        = "";

    /** Licence name (open, restricted, …). */
    private String       licence           = "";

    /** Publication date yyyy-mm-dd. */
    private String       publicationDate   = "";

    /** Title of the document. */
    private String       documentTitle     = "";

    /** Publishing organisation / media outlet / institutional originator. */
    private String       source            = "";

    /** Modality: "textual" | "multimodal". */
    private String       medium            = "textual";

    /** Original web address. */
    private String       url               = "";

    /** Up to six subject-area labels from a controlled vocabulary. */
    private List<String> domain            = new ArrayList<>();

    /** Up to six free-text keywords. */
    private List<String> keywords          = new ArrayList<>();

    /** Total word count (non-punctuation tokens). */
    private int          numberWords       = 0;

    /** Total sentence count. */
    private int          numberSentences   = 0;

    /** Total paragraph count. */
    private int          numberParagraphs  = 0;

    /** Total token count (words + punctuation). */
    private int          numberTokens      = 0;

    /**
     * Per-sentence PII coverage vector.
     * Entry i = proportion of tokens in sentence i flagged as PII ∈ [0,1].
     * Length == numberSentences after pipeline completion.
     */
    private List<Double> piiVector         = new ArrayList<>();

    /**
     * Per-sentence bias coverage vector.
     * Entry i = proportion of tokens in sentence i flagged as biased ∈ [0,1].
     * Length == numberSentences after pipeline completion.
     */
    private List<Double> biasVector        = new ArrayList<>();

    // -----------------------------------------------------------------------
    // ── OPTIONAL (8) ────────────────────────────────────────────────────────
    // -----------------------------------------------------------------------

    /** Name(s) of the author(s). */
    private List<String> author            = new ArrayList<>();

    /** Stylistic register: legal | journalistic | administrative | … */
    private String       style             = "";

    /** Document genre: book | document | article | … */
    private String       type              = "";

    /** Narrower thematic classification, hierarchically linked to Domain. */
    private List<String> subdomain         = new ArrayList<>();

    /** true = translation, false = original Bulgarian text. */
    private Boolean      translatedDocument = null;  // null = unknown

    /** Date of acquisition yyyy-mm-dd. */
    private String       collectionDate    = "";

    /** URL of the licence text. */
    private String       licenceLink       = "";

    /** Anticipated NLP applications from a predefined list. */
    private List<String> taskCategories    = new ArrayList<>();

    // -----------------------------------------------------------------------
    // Constructor
    // -----------------------------------------------------------------------

    public DocumentMetadata() {}

    public DocumentMetadata(String identifier) {
        this.identifier = identifier;
    }

    // -----------------------------------------------------------------------
    // Fluent setters — mandatory
    // -----------------------------------------------------------------------

    public DocumentMetadata setIdentifier(String v)       { identifier       = v; return this; }
    public DocumentMetadata setLicence(String v)          { licence          = v; return this; }
    public DocumentMetadata setPublicationDate(String v)  { publicationDate  = v; return this; }
    public DocumentMetadata setDocumentTitle(String v)    { documentTitle    = v; return this; }
    public DocumentMetadata setSource(String v)           { source           = v; return this; }
    public DocumentMetadata setMedium(String v)           { medium           = v; return this; }
    public DocumentMetadata setUrl(String v)              { url              = v; return this; }
    public DocumentMetadata setDomain(List<String> v)     { domain           = v != null ? v : new ArrayList<>(); return this; }
    public DocumentMetadata addDomain(String v)           { domain.add(v); return this; }
    public DocumentMetadata setKeywords(List<String> v)   { keywords         = v != null ? v : new ArrayList<>(); return this; }
    public DocumentMetadata addKeyword(String v)          { keywords.add(v); return this; }
    public DocumentMetadata setNumberWords(int v)         { numberWords      = v; return this; }
    public DocumentMetadata setNumberSentences(int v)     { numberSentences  = v; return this; }
    public DocumentMetadata setNumberParagraphs(int v)    { numberParagraphs = v; return this; }
    public DocumentMetadata setNumberTokens(int v)        { numberTokens     = v; return this; }
    public DocumentMetadata setPiiVector(List<Double> v)  { piiVector        = v != null ? v : new ArrayList<>(); return this; }
    public DocumentMetadata setBiasVector(List<Double> v) { biasVector       = v != null ? v : new ArrayList<>(); return this; }

    // Fluent setters — optional
    public DocumentMetadata setAuthor(List<String> v)          { author            = v != null ? v : new ArrayList<>(); return this; }
    public DocumentMetadata addAuthor(String v)                { author.add(v); return this; }
    public DocumentMetadata setStyle(String v)                 { style             = v; return this; }
    public DocumentMetadata setType(String v)                  { type              = v; return this; }
    public DocumentMetadata setSubdomain(List<String> v)       { subdomain         = v != null ? v : new ArrayList<>(); return this; }
    public DocumentMetadata addSubdomain(String v)             { subdomain.add(v); return this; }
    public DocumentMetadata setTranslatedDocument(Boolean v)   { translatedDocument= v; return this; }
    public DocumentMetadata setCollectionDate(String v)        { collectionDate    = v; return this; }
    public DocumentMetadata setLicenceLink(String v)           { licenceLink       = v; return this; }
    public DocumentMetadata setTaskCategories(List<String> v)  { taskCategories    = v != null ? v : new ArrayList<>(); return this; }
    public DocumentMetadata addTaskCategory(String v)          { taskCategories.add(v); return this; }

    // -----------------------------------------------------------------------
    // Getters
    // -----------------------------------------------------------------------

    public String       getIdentifier()        { return identifier; }
    public String       getLicence()           { return licence; }
    public String       getPublicationDate()   { return publicationDate; }
    public String       getDocumentTitle()     { return documentTitle; }
    public String       getSource()            { return source; }
    public String       getMedium()            { return medium; }
    public String       getUrl()               { return url; }
    public List<String> getDomain()            { return Collections.unmodifiableList(domain); }
    public List<String> getKeywords()          { return Collections.unmodifiableList(keywords); }
    public int          getNumberWords()       { return numberWords; }
    public int          getNumberSentences()   { return numberSentences; }
    public int          getNumberParagraphs()  { return numberParagraphs; }
    public int          getNumberTokens()      { return numberTokens; }
    public List<Double> getPiiVector()         { return Collections.unmodifiableList(piiVector); }
    public List<Double> getBiasVector()        { return Collections.unmodifiableList(biasVector); }

    public List<String> getAuthor()            { return Collections.unmodifiableList(author); }
    public String       getStyle()             { return style; }
    public String       getType()              { return type; }
    public List<String> getSubdomain()         { return Collections.unmodifiableList(subdomain); }
    public Boolean      getTranslatedDocument(){ return translatedDocument; }
    public String       getCollectionDate()    { return collectionDate; }
    public String       getLicenceLink()       { return licenceLink; }
    public List<String> getTaskCategories()    { return Collections.unmodifiableList(taskCategories); }

    // -----------------------------------------------------------------------
    // Validation
    // -----------------------------------------------------------------------

    /**
     * Returns a list of missing mandatory fields.
     * An empty list means the record is complete.
     */
    public List<String> missingMandatoryFields() {
        List<String> missing = new ArrayList<>();
        if (identifier.isBlank())       missing.add("Identifier");
        if (licence.isBlank())          missing.add("Licence");
        if (medium.isBlank())           missing.add("Medium");
        if (numberWords == 0)           missing.add("NumberWords");
        if (numberSentences == 0)       missing.add("NumberSentences");
        if (numberParagraphs == 0)      missing.add("NumberParagraphs");
        if (numberTokens == 0)          missing.add("NumberTokens");
        // piiVector and biasVector may legitimately be empty for clean docs
        return missing;
    }

    // -----------------------------------------------------------------------
    // JSON serialisation  (json-simple)
    // -----------------------------------------------------------------------

    /** Serialises this record to a json-simple JSONObject. */
    public JSONObject toJson() {
        JSONObject o = new JSONObject();

        // Mandatory
        o.put("Identifier",                       identifier);
        o.put("Licence",                          licence);
        o.put("PublicationDate",                  publicationDate);
        o.put("DocumentTitle",                    documentTitle);
        o.put("Source",                           source);
        o.put("Medium",                           medium);
        o.put("Url",                              url);
        o.put("Domain",                           toJsonArray(domain));
        o.put("Keywords",                         toJsonArray(keywords));
        o.put("NumberWords",                      numberWords);
        o.put("NumberSentences",                  numberSentences);
        o.put("NumberParagraphs",                 numberParagraphs);
        o.put("NumberTokens",                     numberTokens);
        o.put("PersonallyIdentifiableInformation",toJsonDoubleArray(piiVector));
        o.put("BiasedInformation",                toJsonDoubleArray(biasVector));

        // Optional
        o.put("Author",            toJsonArray(author));
        o.put("Style",             style);
        o.put("Type",              type);
        o.put("Subdomain",         toJsonArray(subdomain));
        o.put("TranslatedDocument",
              translatedDocument == null ? "" : translatedDocument.toString());
        o.put("CollectionDate",    collectionDate);
        o.put("LicenceLink",       licenceLink);
        o.put("TaskCategories",    toJsonArray(taskCategories));

        return o;
    }

    /**
     * Populates a DocumentMetadata from a json-simple JSONObject previously
     * produced by {@link #toJson()}.
     */
    public static DocumentMetadata fromJson(JSONObject o) {
        DocumentMetadata m = new DocumentMetadata();

        m.identifier        = str(o, "Identifier");
        m.licence           = str(o, "Licence");
        m.publicationDate   = str(o, "PublicationDate");
        m.documentTitle     = str(o, "DocumentTitle");
        m.source            = str(o, "Source");
        m.medium            = str(o, "Medium");
        m.url               = str(o, "Url");
        m.domain            = strList(o, "Domain");
        m.keywords          = strList(o, "Keywords");
        m.numberWords       = intVal(o, "NumberWords");
        m.numberSentences   = intVal(o, "NumberSentences");
        m.numberParagraphs  = intVal(o, "NumberParagraphs");
        m.numberTokens      = intVal(o, "NumberTokens");
        m.piiVector         = doubleList(o, "PersonallyIdentifiableInformation");
        m.biasVector        = doubleList(o, "BiasedInformation");

        m.author            = strList(o, "Author");
        m.style             = str(o, "Style");
        m.type              = str(o, "Type");
        m.subdomain         = strList(o, "Subdomain");
        String td           = str(o, "TranslatedDocument");
        m.translatedDocument= td.isBlank() ? null : Boolean.parseBoolean(td);
        m.collectionDate    = str(o, "CollectionDate");
        m.licenceLink       = str(o, "LicenceLink");
        m.taskCategories    = strList(o, "TaskCategories");

        return m;
    }

    // -----------------------------------------------------------------------
    // Interop with legacy JSONObject format (used by source processors)
    // -----------------------------------------------------------------------

    /**
     * Merges fields from a legacy source-processor JSONObject (the format
     * produced by MarcellProcessor, BulNCProcessor, etc.) into this record.
     * Fields already set on {@code this} are NOT overwritten.
     */
    public void mergeLegacy(JSONObject legacy) {
        if (identifier.isBlank())      setIdentifier(str(legacy, "Identifier"));
        if (licence.isBlank())         setLicence(str(legacy, "Licence"));
        if (licenceLink.isBlank())     setLicenceLink(str(legacy, "LicenceLink"));
        if (publicationDate.isBlank()) setPublicationDate(str(legacy, "PublicationDate"));
        if (documentTitle.isBlank())   setDocumentTitle(str(legacy, "DocumentTitle"));
        if (source.isBlank())          setSource(str(legacy, "Source"));
        if (url.isBlank())             setUrl(str(legacy, "Url"));
        if (style.isBlank())           setStyle(str(legacy, "Style"));
        if (type.isBlank())            setType(str(legacy, "Type"));
        if (collectionDate.isBlank())  setCollectionDate(str(legacy, "CollectionDate"));

        if (author.isEmpty()) {
            String a = str(legacy, "Author");
            if (!a.isBlank()) author.add(a);
        }
        if (domain.isEmpty()) {
            String d = str(legacy, "Domain");
            if (!d.isBlank()) domain.add(d);
        }
        if (subdomain.isEmpty()) {
            String s = str(legacy, "Subdomain");
            if (!s.isBlank()) subdomain.add(s);
        }
        if (numberWords      == 0) numberWords      = intVal(legacy, "NumberWords");
        if (numberSentences  == 0) numberSentences  = intVal(legacy, "NumberSentences");
        if (numberParagraphs == 0) numberParagraphs = intVal(legacy, "NumberParagraphs");
        if (numberTokens     == 0) numberTokens     = intVal(legacy, "NumberTokens");

        String translated = str(legacy, "TranslatedDocument");
        if (translatedDocument == null && !translated.isBlank())
            translatedDocument = Boolean.parseBoolean(translated);
    }

    // -----------------------------------------------------------------------
    // Private helpers
    // -----------------------------------------------------------------------

    private static String str(JSONObject o, String key) {
        Object v = o.get(key);
        return v == null ? "" : v.toString().trim();
    }

    private static int intVal(JSONObject o, String key) {
        Object v = o.get(key);
        if (v == null) return 0;
        try { return Integer.parseInt(v.toString().trim()); }
        catch (NumberFormatException e) { return 0; }
    }

    private static List<String> strList(JSONObject o, String key) {
        Object v = o.get(key);
        List<String> list = new ArrayList<>();
        if (v instanceof JSONArray) {
            for (Object item : (JSONArray) v)
                if (item != null) list.add(item.toString());
        } else if (v != null && !v.toString().isBlank()) {
            list.add(v.toString().trim());
        }
        return list;
    }

    private static List<Double> doubleList(JSONObject o, String key) {
        Object v = o.get(key);
        List<Double> list = new ArrayList<>();
        if (v instanceof JSONArray) {
            for (Object item : (JSONArray) v) {
                try { list.add(Double.parseDouble(item.toString())); }
                catch (NumberFormatException ignored) {}
            }
        }
        return list;
    }

    private JSONArray toJsonArray(List<String> list) {
        JSONArray a = new JSONArray();
        if (list != null) a.addAll(list);
        return a;
    }

    private JSONArray toJsonDoubleArray(List<Double> list) {
        JSONArray a = new JSONArray();
        if (list != null) a.addAll(list);
        return a;
    }

    @Override
    public String toString() {
        return String.format(
            "DocumentMetadata{id='%s', sentences=%d, words=%d, piiEntries=%d, biasEntries=%d}",
            identifier, numberSentences, numberWords, piiVector.size(), biasVector.size());
    }
}