Shami96 commited on
Commit
a4dde4c
·
verified ·
1 Parent(s): 4e326f4

Update master_key.py

Browse files
Files changed (1) hide show
  1. master_key.py +283 -124
master_key.py CHANGED
@@ -1,114 +1,197 @@
 
1
  """
2
- Improved Master Key for NHVAS Audit extraction:
3
- - TABLE_SCHEMAS: Enhanced definitions with better matching criteria for Summary vs Basic tables
4
- - HEADING_PATTERNS: Improved regex patterns for main/sub headings
5
- - PARAGRAPH_PATTERNS: Enhanced patterns for key narrative sections
 
 
 
 
 
 
 
6
  """
7
 
8
- # 1. Enhanced table schemas with better matching logic
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  TABLE_SCHEMAS = {
 
10
  "Tick as appropriate": {
11
  "headings": [
12
- {"level": 1, "text": "NHVAS Audit Summary Report"},
13
  ],
14
  "orientation": "left",
15
- "labels": [
16
- "Mass",
17
- "Entry Audit",
18
- "Maintenance",
19
- "Initial Compliance Audit",
20
- "Basic Fatigue",
21
- "Compliance Audit",
22
- "Advanced Fatigue",
23
- "Spot Check",
24
- "Triggered Audit"
25
- ],
26
- "priority": 90 # High priority for direct match
27
  },
 
28
  "Audit Information": {
29
  "orientation": "left",
30
  "labels": [
31
  "Date of Audit",
32
- "Location of audit",
33
  "Auditor name",
34
  "Audit Matrix Identifier (Name or Number)",
35
  "Auditor Exemplar Global Reg No.",
36
- "expiry Date:",
37
- "NHVR Auditor Registration Number",
38
- "expiry Date:"
39
  ],
40
- "priority": 80
 
 
 
 
 
 
 
 
 
 
41
  },
 
42
  "Operator Information": {
43
  "headings": [
44
- {"level": 1, "text": "Operator Information"}
45
  ],
46
  "orientation": "left",
47
  "labels": [
48
  "Operator name (Legal entity)",
 
49
  "NHVAS Accreditation No. (If applicable)",
50
- "Registered trading name/s",
51
  "Australian Company Number",
52
  "NHVAS Manual (Policies and Procedures) developed by"
53
  ],
54
- "priority": 85
 
 
 
 
 
 
 
 
55
  },
 
56
  "Operator contact details": {
57
  "orientation": "left",
58
  "labels": [
59
  "Operator business address",
60
  "Operator Postal address",
61
- "Email address",
62
  "Operator Telephone Number"
63
  ],
 
64
  "priority": 75,
65
- "context_keywords": ["contact", "address", "email", "telephone"]
66
  },
 
67
  "Attendance List (Names and Position Titles)": {
68
  "headings": [
69
- {"level": 1, "text": "NHVAS Audit Summary Report"}
70
  ],
71
  "orientation": "row1",
72
  "labels": ["Attendance List (Names and Position Titles)"],
73
- "priority": 90
 
 
 
 
 
 
 
 
 
 
 
74
  },
 
75
  "Nature of the Operators Business (Summary)": {
76
  "orientation": "row1",
77
- "labels": ["Nature of the Operators Business (Summary):"],
 
78
  "split_labels": ["Accreditation Number:", "Expiry Date:"],
79
- "priority": 85
 
80
  },
 
81
  "Accreditation Vehicle Summary": {
82
  "orientation": "left",
83
  "labels": ["Number of powered vehicles", "Number of trailing vehicles"],
84
- "priority": 80
 
 
85
  },
 
86
  "Accreditation Driver Summary": {
87
- "orientation": "left",
88
  "labels": ["Number of drivers in BFM", "Number of drivers in AFM"],
89
- "priority": 80
 
90
  },
 
91
  "Compliance Codes": {
92
  "orientation": "left",
93
  "labels": ["V", "NC", "TNC", "SFI", "NAP", "NA"],
94
  "priority": 70,
95
  "context_exclusions": ["MASS MANAGEMENT", "MAINTENANCE MANAGEMENT", "FATIGUE MANAGEMENT"]
96
  },
 
97
  "Corrective Action Request Identification": {
98
  "orientation": "row1",
99
  "labels": ["Title", "Abbreviation", "Description"],
100
- "priority": 80
 
 
101
  },
102
-
103
- # 🎯 BASIC MANAGEMENT SCHEMAS (Compliance Tables - Lower Priority)
104
  "Maintenance Management": {
105
  "headings": [
106
- {"level": 1, "text": "NHVAS AUDIT SUMMARY REPORT"}
107
  ],
108
  "orientation": "left",
109
  "labels": [
110
  "Std 1. Daily Check",
111
- "Std 2. Fault Recording and Reporting",
112
  "Std 3. Fault Repair",
113
  "Std 4. Maintenance Schedules and Methods",
114
  "Std 5. Records and Documentation",
@@ -116,19 +199,24 @@ TABLE_SCHEMAS = {
116
  "Std 7. Internal Review",
117
  "Std 8. Training and Education"
118
  ],
 
 
 
 
119
  "priority": 60,
120
  "context_keywords": ["maintenance"],
121
- "context_exclusions": ["summary", "details", "audit findings"] # Exclude Summary tables
122
  },
 
123
  "Mass Management": {
124
  "headings": [
125
- {"level": 1, "text": "NHVAS AUDIT SUMMARY REPORT"}
126
  ],
127
  "orientation": "left",
128
  "labels": [
129
  "Std 1. Responsibilities",
130
  "Std 2. Vehicle Control",
131
- "Std 3. Vehicle Use",
132
  "Std 4. Records and Documentation",
133
  "Std 5. Verification",
134
  "Std 6. Internal Review",
@@ -137,57 +225,60 @@ TABLE_SCHEMAS = {
137
  ],
138
  "priority": 60,
139
  "context_keywords": ["mass"],
140
- "context_exclusions": ["summary", "details", "audit findings"] # Exclude Summary tables
141
  },
 
142
  "Fatigue Management": {
143
  "headings": [
144
- {"level": 1, "text": "NHVAS AUDIT SUMMARY REPORT"}
145
  ],
146
  "orientation": "left",
147
  "labels": [
148
  "Std 1. Scheduling and Rostering",
149
  "Std 2. Health and wellbeing for performed duty",
150
  "Std 3. Training and Education",
151
- "Std 4. Responsibilities and management practices",
152
  "Std 5. Internal Review",
153
  "Std 6. Records and Documentation",
154
  "Std 7. Workplace conditions"
155
  ],
156
  "priority": 60,
157
  "context_keywords": ["fatigue"],
158
- "context_exclusions": ["summary", "details", "audit findings"] # Exclude Summary tables
159
  },
160
-
161
- # 🎯 SUMMARY MANAGEMENT SCHEMAS (Detailed Tables with DETAILS column - Higher Priority)
162
  "Maintenance Management Summary": {
163
  "headings": [
164
- {"level": 1, "text": "Audit Observations and Comments"},
165
- {"level": 2, "text": "Maintenance Management Summary of Audit findings"}
166
  ],
167
  "orientation": "left",
168
  "columns": ["MAINTENANCE MANAGEMENT", "DETAILS"],
169
  "labels": [
170
- "Std 1. Daily Check",
171
  "Std 2. Fault Recording and Reporting",
172
- "Std 3. Fault Repair",
173
  "Std 4. Maintenance Schedules and Methods",
174
- "Std 5. Records and Documentation",
175
  "Std 6. Responsibilities",
176
- "Std 7. Internal Review",
177
  "Std 8. Training and Education"
178
  ],
179
- "priority": 85, # Higher priority than basic Maintenance Management
 
180
  "context_keywords": ["maintenance", "summary", "details", "audit findings"]
181
  },
 
182
  "Mass Management Summary": {
183
  "headings": [
184
- {"level": 1, "text": "Mass Management Summary of Audit findings"}
185
  ],
186
  "orientation": "left",
187
  "columns": ["MASS MANAGEMENT", "DETAILS"],
188
  "labels": [
189
  "Std 1. Responsibilities",
190
- "Std 2. Vehicle Control",
191
  "Std 3. Vehicle Use",
192
  "Std 4. Records and Documentation",
193
  "Std 5. Verification",
@@ -195,12 +286,13 @@ TABLE_SCHEMAS = {
195
  "Std 7. Training and Education",
196
  "Std 8. Maintenance of Suspension"
197
  ],
198
- "priority": 85, # Higher priority than basic Mass Management
199
  "context_keywords": ["mass", "summary", "details", "audit findings"]
200
  },
 
201
  "Fatigue Management Summary": {
202
  "headings": [
203
- {"level": 1, "text": "Fatigue Management Summary of Audit findings"}
204
  ],
205
  "orientation": "left",
206
  "columns": ["FATIGUE MANAGEMENT", "DETAILS"],
@@ -209,57 +301,61 @@ TABLE_SCHEMAS = {
209
  "Std 2. Health and wellbeing for performed duty",
210
  "Std 3. Training and Education",
211
  "Std 4. Responsibilities and management practices",
212
- "Std 5. Internal Review",
213
  "Std 6. Records and Documentation",
214
  "Std 7. Workplace conditions"
215
  ],
216
- "priority": 85, # Higher priority than basic Fatigue Management
217
  "context_keywords": ["fatigue", "summary", "details", "audit findings"]
218
  },
219
-
220
- # Vehicle Registration Tables
221
  "Vehicle Registration Numbers Mass": {
222
- "headings": [
223
- {"level": 1, "text": "Vehicle Registration Numbers of Records Examined"},
224
- {"level": 2, "text": "MASS MANAGEMENT"}
225
- ],
226
- "orientation": "row1",
227
- "labels": [
228
- "No.", "Registration Number", "Sub contractor",
229
- "Sub-contracted Vehicles Statement of Compliance",
230
- "Weight Verification Records",
231
- "RFS Suspension Certification #",
232
- "Suspension System Maintenance", "Trip Records",
233
- "Fault Recording/ Reporting on Suspension System"
234
- ],
235
- "priority": 90, # Higher priority
236
- "context_keywords": ["mass", "vehicle registration", "rfs suspension", "weight verification"],
237
- "context_exclusions": ["maintenance", "roadworthiness", "daily checks"] # Exclude maintenance-specific terms
238
- },
239
- "Vehicle Registration Numbers Maintenance": {
240
- "headings": [
241
- {"level": 1, "text": "Vehicle Registration Numbers of Records Examined"},
242
- {"level": 2, "text": "Maintenance Management"}
243
- ],
244
- "orientation": "row1",
245
- "labels": [
246
- "No.", "Registration Number", "Roadworthiness Certificates",
247
- "Maintenance Records", "Daily Checks",
248
- "Fault Recording/ Reporting", "Fault Repair"
249
- ],
250
- "priority": 85, # Lower priority
251
- "context_keywords": ["maintenance", "vehicle registration", "roadworthiness", "daily checks"],
252
- "context_exclusions": ["mass", "rfs suspension", "weight verification"] # Exclude mass-specific terms
253
- },
 
 
 
 
254
  "Driver / Scheduler Records Examined": {
255
  "headings": [
256
- {"level": 1, "text": "Driver / Scheduler Records Examined"},
257
- {"level": 2, "text": "FATIGUE MANAGEMENT"},
258
  ],
259
  "orientation": "row1",
260
  "labels": [
261
  "No.",
262
- "Driver / Scheduler Name",
263
  "Driver TLIF Course # Completed",
264
  "Scheduler TLIF Course # Completed",
265
  "Medical Certificates (Current Yes/No) Date of expiry",
@@ -267,19 +363,22 @@ TABLE_SCHEMAS = {
267
  "Fit for Duty Statement Completed (Yes/No)",
268
  "Work Diary Pages (Page Numbers) Electronic Work Diary Records (Date Range)"
269
  ],
 
270
  "priority": 80,
271
  "context_keywords": ["driver", "scheduler", "fatigue"]
272
  },
273
-
274
- # Other Tables
275
  "Operator's Name (legal entity)": {
276
  "headings": [
277
- {"level": 1, "text": "CORRECTIVE ACTION REQUEST (CAR)"}
278
  ],
279
  "orientation": "left",
280
- "labels": ["Operator's Name (legal entity)"],
 
281
  "priority": 85
282
  },
 
283
  "Non-conformance and CAR details": {
284
  "orientation": "left",
285
  "labels": [
@@ -292,24 +391,29 @@ TABLE_SCHEMAS = {
292
  "Position",
293
  "Date",
294
  "Comments:",
295
- "Auditor signature",
296
- "Date"
297
  ],
 
298
  "priority": 75,
299
- "context_keywords": ["non-conformance", "corrective action"]
300
  },
 
301
  "NHVAS Approved Auditor Declaration": {
302
  "headings": [
303
- {"level": 1, "text": "NHVAS APPROVED AUDITOR DECLARATION"}
304
  ],
305
  "orientation": "row1",
306
- "labels": ["Print Name", "NHVR or Exemplar Global Auditor Registration Number"],
 
307
  "priority": 90,
308
- "context_exclusions": ["manager", "operator declaration"]
 
 
309
  },
 
310
  "Audit Declaration dates": {
311
  "headings": [
312
- {"level": 1, "text": "Audit Declaration dates"}
313
  ],
314
  "orientation": "left",
315
  "labels": [
@@ -317,29 +421,51 @@ TABLE_SCHEMAS = {
317
  "Unconditional CARs closed out on:",
318
  "Conditional CARs to be closed out by:"
319
  ],
 
320
  "priority": 80
321
  },
 
322
  "Print accreditation name": {
323
  "headings": [
324
- {"level": 1, "text": "(print accreditation name)"}
325
  ],
326
- "orientation": "left",
327
- "labels": ["(print accreditation name)"],
328
- "priority": 85
 
 
 
 
 
329
  },
 
330
  "Operator Declaration": {
331
  "headings": [
332
- {"level": 1, "text": "Operator Declaration"}
333
  ],
334
  "orientation": "row1",
335
  "labels": ["Print Name", "Position Title"],
 
 
 
336
  "priority": 90,
337
- "context_keywords": ["operator declaration", "manager"],
338
- "context_exclusions": ["auditor", "nhvas approved"]
 
 
 
 
 
 
 
 
 
339
  }
340
  }
341
 
342
- # 2. Enhanced heading detection patterns
 
 
343
  HEADING_PATTERNS = {
344
  "main": [
345
  r"NHVAS\s+Audit\s+Summary\s+Report",
@@ -348,7 +474,7 @@ HEADING_PATTERNS = {
348
  ],
349
  "sub": [
350
  r"AUDIT\s+OBSERVATIONS\s+AND\s+COMMENTS",
351
- r"MAINTENANCE\s+MANAGEMENT",
352
  r"MASS\s+MANAGEMENT",
353
  r"FATIGUE\s+MANAGEMENT",
354
  r"Fatigue\s+Management\s+Summary\s+of\s+Audit\s+findings",
@@ -358,14 +484,47 @@ HEADING_PATTERNS = {
358
  r"CORRECTIVE\s+ACTION\s+REQUEST\s+\(CAR\)",
359
  r"NHVAS\s+APPROVED\s+AUDITOR\s+DECLARATION",
360
  r"Operator\s+Declaration",
361
- r"Operator\s+Information"
 
 
362
  ]
363
  }
364
 
365
- # 3. Enhanced paragraph patterns for key narrative sections
 
 
366
  PARAGRAPH_PATTERNS = {
367
- "findings_summary": r"Provide a summary of findings based on the evidence gathered during the audit\.",
368
  "declaration_text": r"I hereby acknowledge and agree with the findings.*",
369
  "introductory_note": r"This audit assesses the.*",
370
- "date_line": r"^\s*\d{1,2}(?:st|nd|rd|th)?\s+[A-Za-z]+\s+\d{4}\s*$|^Date$"
371
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # master_key.py
2
  """
3
+ Comprehensive Master Key for NHVAS Audit extraction (updated & hardened)
4
+
5
+ Usage:
6
+ - Import TABLE_SCHEMAS, HEADING_PATTERNS, PARAGRAPH_PATTERNS, GLOBAL_SETTINGS
7
+ - The pipeline should:
8
+ 1) normalize candidate header/key using GLOBAL_SETTINGS['normalize']
9
+ 2) attempt exact matches using schema['preferred_json_keys'] (if provided)
10
+ 3) try direct label matches (schema['labels'] + schema['label_synonyms'])
11
+ 4) fallback to fuzzy matching using GLOBAL_SETTINGS thresholds
12
+ 5) respect schema['skip_if_date_like'] for sensitive fields (e.g. Print Name)
13
+ 6) for multi-value cells, use schema['split_on'] to split JSON values into lines
14
  """
15
 
16
+ import re
17
+
18
+ # ---------------------------
19
+ # Global matching / normalization settings
20
+ # ---------------------------
21
+ GLOBAL_SETTINGS = {
22
+ "normalize": {
23
+ # Normalization pipeline (applied in this order)
24
+ "lower": True,
25
+ "strip_punctuation": True, # remove punctuation like ()'.,–
26
+ "collapse_whitespace": True,
27
+ "replace_smart_dashes": True # normalize – — to -
28
+ },
29
+ "fuzzy_thresholds": {
30
+ # Minimum score thresholds (0-100) for different priority tiers
31
+ "high_priority": 70,
32
+ "medium_priority": 60,
33
+ "low_priority": 45
34
+ },
35
+ "fuzzy_algorithm": "token_set_ratio", # recommendation
36
+ "split_on": [" – ", " - ", ";", "\n", " / ", " & "], # common attendance / multi-name delimiters
37
+ "date_like_pattern": r"^\s*(\d{1,2}(st|nd|rd|th)?\s+[A-Za-z]+|\d{1,2}\/\d{1,2}\/\d{2,4}|\d{1,2}\.\d{1,2}\.\d{2,4}|\d{1,2}\s+[A-Za-z]{3,})",
38
+ # used for skip_if_date_like checks
39
+ "ocr_repair_rules": [
40
+ # small fixes for common OCR noise in headers
41
+ (r"\s*\(\s*Yes\s*/\s*No\s*\)", " (Yes/No)"),
42
+ (r"R[e3]gistrat[i1]on", "Registration"),
43
+ (r"Prin?t", "Print"),
44
+ (r"Accredi[ta]tion", "Accreditation"),
45
+ (r"[^\w\s\-\&\(\)\/:]", " "), # remove stray symbols but keep parentheses/slash
46
+ ]
47
+ }
48
+
49
+ # ---------------------------
50
+ # Table schemas
51
+ # ---------------------------
52
  TABLE_SCHEMAS = {
53
+ # Basic top-of-form tick boxes
54
  "Tick as appropriate": {
55
  "headings": [
56
+ {"level": 1, "text": r"NHVAS\s+Audit\s+Summary\s+Report"},
57
  ],
58
  "orientation": "left",
59
+ "labels": ["Tick as appropriate"],
60
+ "label_synonyms": ["Tick as appropriate:", "Tick where appropriate", "Tick as required"],
61
+ "priority": 90,
62
+ "preferred_json_keys": ["Tick as appropriate", "Tick as appropriate.Tick as appropriate"],
63
+ "context_keywords": ["tick", "appropriate"]
 
 
 
 
 
 
 
64
  },
65
+
66
  "Audit Information": {
67
  "orientation": "left",
68
  "labels": [
69
  "Date of Audit",
70
+ "Location of audit",
71
  "Auditor name",
72
  "Audit Matrix Identifier (Name or Number)",
73
  "Auditor Exemplar Global Reg No.",
74
+ "Expiry Date",
75
+ "NHVR Auditor Registration Number"
 
76
  ],
77
+ "label_synonyms": [
78
+ "Audit Date", "Date of audit", "Location", "Auditor", "Exemplar Global Reg No.",
79
+ "Auditor Reg No", "Auditor Registration Number"
80
+ ],
81
+ "priority": 80,
82
+ "preferred_json_keys": [
83
+ "Audit Information.Date of Audit",
84
+ "Audit Information.Auditor name",
85
+ "Audit Information.Location of audit"
86
+ ],
87
+ "context_keywords": ["audit", "auditor", "matrix", "registration"]
88
  },
89
+
90
  "Operator Information": {
91
  "headings": [
92
+ {"level": 1, "text": r"Operator\s+Information"}
93
  ],
94
  "orientation": "left",
95
  "labels": [
96
  "Operator name (Legal entity)",
97
+ "Operator’s Name (legal entity)",
98
  "NHVAS Accreditation No. (If applicable)",
99
+ "Registered trading name/s",
100
  "Australian Company Number",
101
  "NHVAS Manual (Policies and Procedures) developed by"
102
  ],
103
+ "label_synonyms": [
104
+ "Operator name", "Operator name (Legal entity)", "Operator’s name", "Operator's Name (legal entity)"
105
+ ],
106
+ "priority": 85,
107
+ "preferred_json_keys": [
108
+ "Operator Information.Operator name (Legal entity)",
109
+ "Operator name (Legal entity)"
110
+ ],
111
+ "context_keywords": ["operator", "legal entity", "accreditation", "company number"]
112
  },
113
+
114
  "Operator contact details": {
115
  "orientation": "left",
116
  "labels": [
117
  "Operator business address",
118
  "Operator Postal address",
119
+ "Email address",
120
  "Operator Telephone Number"
121
  ],
122
+ "label_synonyms": ["Business address", "Postal address", "Email", "Telephone", "Phone"],
123
  "priority": 75,
124
+ "context_keywords": ["contact", "address", "email", "telephone", "phone"]
125
  },
126
+
127
  "Attendance List (Names and Position Titles)": {
128
  "headings": [
129
+ {"level": 1, "text": r"Attendance\s+List\s*\(?Names\s+and\s+Position\s+Titles\)?", "re": True}
130
  ],
131
  "orientation": "row1",
132
  "labels": ["Attendance List (Names and Position Titles)"],
133
+ "label_synonyms": [
134
+ "Attendance List", "Attendees", "Attendance", "Names and Position Titles",
135
+ "Names & Position Titles", "Attendance list (names & positions)"
136
+ ],
137
+ "priority": 90,
138
+ "preferred_json_keys": [
139
+ "Attendance List (Names and Position Titles).Attendance List (Names and Position Titles)",
140
+ "Attendance List (Names and Position Titles)"
141
+ ],
142
+ "split_on": [" – ", " - ", ";", "\n", " / "], # ensure one person per line
143
+ "context_keywords": ["attendance", "attendees", "names", "position"],
144
+ "skip_if_date_like": False
145
  },
146
+
147
  "Nature of the Operators Business (Summary)": {
148
  "orientation": "row1",
149
+ "labels": ["Nature of the Operators Business (Summary):", "Nature of the Operator's Business (Summary):"],
150
+ "label_synonyms": ["Nature of the Operators Business", "Nature of Business", "Nature of the Operator Business"],
151
  "split_labels": ["Accreditation Number:", "Expiry Date:"],
152
+ "priority": 85,
153
+ "context_keywords": ["nature", "business", "operators business", "summary"]
154
  },
155
+
156
  "Accreditation Vehicle Summary": {
157
  "orientation": "left",
158
  "labels": ["Number of powered vehicles", "Number of trailing vehicles"],
159
+ "label_synonyms": ["powered vehicles", "trailing vehicles", "Number of vehicles"],
160
+ "priority": 80,
161
+ "context_keywords": ["vehicle", "powered vehicles", "trailing"]
162
  },
163
+
164
  "Accreditation Driver Summary": {
165
+ "orientation": "left",
166
  "labels": ["Number of drivers in BFM", "Number of drivers in AFM"],
167
+ "priority": 80,
168
+ "context_keywords": ["driver", "bfm", "afm"]
169
  },
170
+
171
  "Compliance Codes": {
172
  "orientation": "left",
173
  "labels": ["V", "NC", "TNC", "SFI", "NAP", "NA"],
174
  "priority": 70,
175
  "context_exclusions": ["MASS MANAGEMENT", "MAINTENANCE MANAGEMENT", "FATIGUE MANAGEMENT"]
176
  },
177
+
178
  "Corrective Action Request Identification": {
179
  "orientation": "row1",
180
  "labels": ["Title", "Abbreviation", "Description"],
181
+ "label_synonyms": ["Corrective Action Request", "CAR Identification", "CAR"],
182
+ "priority": 80,
183
+ "context_keywords": ["corrective", "action", "CAR"]
184
  },
185
+
186
+ # Basic Management schemas (lower priority than summary versions)
187
  "Maintenance Management": {
188
  "headings": [
189
+ {"level": 1, "text": r"NHVAS\s+AUDIT\s+SUMMARY\s+REPORT", "re": True}
190
  ],
191
  "orientation": "left",
192
  "labels": [
193
  "Std 1. Daily Check",
194
+ "Std 2. Fault Recording and Reporting",
195
  "Std 3. Fault Repair",
196
  "Std 4. Maintenance Schedules and Methods",
197
  "Std 5. Records and Documentation",
 
199
  "Std 7. Internal Review",
200
  "Std 8. Training and Education"
201
  ],
202
+ "label_synonyms": [
203
+ "Daily Check", "Fault Recording/ Reporting", "Fault Repair",
204
+ "Maintenance Schedules", "Records and Documentation", "Internal Review"
205
+ ],
206
  "priority": 60,
207
  "context_keywords": ["maintenance"],
208
+ "context_exclusions": ["summary", "details", "audit findings"]
209
  },
210
+
211
  "Mass Management": {
212
  "headings": [
213
+ {"level": 1, "text": r"NHVAS\s+AUDIT\s+SUMMARY\s+REPORT", "re": True}
214
  ],
215
  "orientation": "left",
216
  "labels": [
217
  "Std 1. Responsibilities",
218
  "Std 2. Vehicle Control",
219
+ "Std 3. Vehicle Use",
220
  "Std 4. Records and Documentation",
221
  "Std 5. Verification",
222
  "Std 6. Internal Review",
 
225
  ],
226
  "priority": 60,
227
  "context_keywords": ["mass"],
228
+ "context_exclusions": ["summary", "details", "audit findings"]
229
  },
230
+
231
  "Fatigue Management": {
232
  "headings": [
233
+ {"level": 1, "text": r"NHVAS\s+AUDIT\s+SUMMARY\s+REPORT", "re": True}
234
  ],
235
  "orientation": "left",
236
  "labels": [
237
  "Std 1. Scheduling and Rostering",
238
  "Std 2. Health and wellbeing for performed duty",
239
  "Std 3. Training and Education",
240
+ "Std 4. Responsibilities and management practices",
241
  "Std 5. Internal Review",
242
  "Std 6. Records and Documentation",
243
  "Std 7. Workplace conditions"
244
  ],
245
  "priority": 60,
246
  "context_keywords": ["fatigue"],
247
+ "context_exclusions": ["summary", "details", "audit findings"]
248
  },
249
+
250
+ # Summary management schemas (higher priority)
251
  "Maintenance Management Summary": {
252
  "headings": [
253
+ {"level": 1, "text": r"Audit\s+Observations\s+and\s+Comments", "re": True},
254
+ {"level": 2, "text": r"Maintenance\s+Management\s+Summary\s+of\s+Audit\s+findings", "re": True}
255
  ],
256
  "orientation": "left",
257
  "columns": ["MAINTENANCE MANAGEMENT", "DETAILS"],
258
  "labels": [
259
+ "Std 1. Daily Check",
260
  "Std 2. Fault Recording and Reporting",
261
+ "Std 3. Fault Repair",
262
  "Std 4. Maintenance Schedules and Methods",
263
+ "Std 5. Records and Documentation",
264
  "Std 6. Responsibilities",
265
+ "Std 7. Internal Review",
266
  "Std 8. Training and Education"
267
  ],
268
+ "label_synonyms": ["Details", "Management", "Maintenance Management Summary"],
269
+ "priority": 85,
270
  "context_keywords": ["maintenance", "summary", "details", "audit findings"]
271
  },
272
+
273
  "Mass Management Summary": {
274
  "headings": [
275
+ {"level": 1, "text": r"Mass\s+Management\s+Summary\s+of\s+Audit\s+findings", "re": True}
276
  ],
277
  "orientation": "left",
278
  "columns": ["MASS MANAGEMENT", "DETAILS"],
279
  "labels": [
280
  "Std 1. Responsibilities",
281
+ "Std 2. Vehicle Control",
282
  "Std 3. Vehicle Use",
283
  "Std 4. Records and Documentation",
284
  "Std 5. Verification",
 
286
  "Std 7. Training and Education",
287
  "Std 8. Maintenance of Suspension"
288
  ],
289
+ "priority": 85,
290
  "context_keywords": ["mass", "summary", "details", "audit findings"]
291
  },
292
+
293
  "Fatigue Management Summary": {
294
  "headings": [
295
+ {"level": 1, "text": r"Fatigue\s+Management\s+Summary\s+of\s+Audit\s+findings", "re": True}
296
  ],
297
  "orientation": "left",
298
  "columns": ["FATIGUE MANAGEMENT", "DETAILS"],
 
301
  "Std 2. Health and wellbeing for performed duty",
302
  "Std 3. Training and Education",
303
  "Std 4. Responsibilities and management practices",
304
+ "Std 5. Internal Review",
305
  "Std 6. Records and Documentation",
306
  "Std 7. Workplace conditions"
307
  ],
308
+ "priority": 85,
309
  "context_keywords": ["fatigue", "summary", "details", "audit findings"]
310
  },
311
+
312
+ # Vehicle registration tables (mass / maintenance variants)
313
  "Vehicle Registration Numbers Mass": {
314
+ "headings": [
315
+ {"level": 1, "text": r"Vehicle\s+Registration\s+Numbers\s+of\s+Records\s+Examined", "re": True},
316
+ {"level": 2, "text": r"MASS\s+MANAGEMENT", "re": True}
317
+ ],
318
+ "orientation": "row1",
319
+ "labels": [
320
+ "No.", "Registration Number", "Sub contractor", "Sub-contractor (Yes/No)",
321
+ "Sub-contracted Vehicles Statement of Compliance",
322
+ "Weight Verification Records", "RFS Suspension Certification #",
323
+ "Suspension System Maintenance", "Trip Records",
324
+ "Fault Recording/ Reporting on Suspension System"
325
+ ],
326
+ "label_synonyms": [
327
+ "Reg No", "RegistrationNo", "RegistrationNumber",
328
+ "Sub-contractor", "Sub contracted", "Sub contractor"
329
+ ],
330
+ "priority": 90,
331
+ "context_keywords": ["mass", "vehicle registration", "rfs suspension", "weight verification"],
332
+ "context_exclusions": ["maintenance", "daily checks"]
333
+ },
334
+
335
+ "Vehicle Registration Numbers Maintenance": {
336
+ "headings": [
337
+ {"level": 1, "text": r"Vehicle\s+Registration\s+Numbers\s+of\s+Records\s+Examined", "re": True},
338
+ {"level": 2, "text": r"Maintenance\s+Management", "re": True}
339
+ ],
340
+ "orientation": "row1",
341
+ "labels": [
342
+ "No.", "Registration Number", "Roadworthiness Certificates",
343
+ "Maintenance Records", "Daily Checks", "Fault Recording/ Reporting", "Fault Repair"
344
+ ],
345
+ "label_synonyms": ["Roadworthiness", "Daily Checks", "Maintenance Records"],
346
+ "priority": 85,
347
+ "context_keywords": ["maintenance", "vehicle registration", "roadworthiness", "daily checks"]
348
+ },
349
+
350
  "Driver / Scheduler Records Examined": {
351
  "headings": [
352
+ {"level": 1, "text": r"Driver\s*/\s*Scheduler\s+Records\s+Examined", "re": True},
353
+ {"level": 2, "text": r"FATIGUE\s+MANAGEMENT", "re": True}
354
  ],
355
  "orientation": "row1",
356
  "labels": [
357
  "No.",
358
+ "Driver / Scheduler Name",
359
  "Driver TLIF Course # Completed",
360
  "Scheduler TLIF Course # Completed",
361
  "Medical Certificates (Current Yes/No) Date of expiry",
 
363
  "Fit for Duty Statement Completed (Yes/No)",
364
  "Work Diary Pages (Page Numbers) Electronic Work Diary Records (Date Range)"
365
  ],
366
+ "label_synonyms": ["Driver Name", "Scheduler Name", "Work Diary", "Roster / Schedule"],
367
  "priority": 80,
368
  "context_keywords": ["driver", "scheduler", "fatigue"]
369
  },
370
+
371
+ # Other tables
372
  "Operator's Name (legal entity)": {
373
  "headings": [
374
+ {"level": 1, "text": r"CORRECTIVE\s+ACTION\s+REQUEST\s+\(CAR\)", "re": True}
375
  ],
376
  "orientation": "left",
377
+ "labels": ["Operator's Name (legal entity)", "Operator’s Name (legal entity)"],
378
+ "label_synonyms": ["Operator's Name", "Operator Name (legal)"],
379
  "priority": 85
380
  },
381
+
382
  "Non-conformance and CAR details": {
383
  "orientation": "left",
384
  "labels": [
 
391
  "Position",
392
  "Date",
393
  "Comments:",
394
+ "Auditor signature"
 
395
  ],
396
+ "label_synonyms": ["Non-conformance", "CAR Number", "Observed Non-conformance"],
397
  "priority": 75,
398
+ "context_keywords": ["non-conformance", "corrective action", "CAR"]
399
  },
400
+
401
  "NHVAS Approved Auditor Declaration": {
402
  "headings": [
403
+ {"level": 1, "text": r"NHVAS\s+APPROVED\s+AUDITOR\s+DECLARATION", "re": True}
404
  ],
405
  "orientation": "row1",
406
+ "labels": ["Print Name", "NHVR or Exemplar Global Auditor Registration Number", "Auditor name"],
407
+ "label_synonyms": ["Print Name (Auditor)", "Auditor Print Name", "NHVR Auditor Reg No"],
408
  "priority": 90,
409
+ "context_exclusions": ["manager", "operator declaration"],
410
+ # Defend against date-like values accidentally being used as a name
411
+ "skip_if_date_like": ["Print Name", "Auditor name"]
412
  },
413
+
414
  "Audit Declaration dates": {
415
  "headings": [
416
+ {"level": 1, "text": r"Audit Declaration dates", "re": True}
417
  ],
418
  "orientation": "left",
419
  "labels": [
 
421
  "Unconditional CARs closed out on:",
422
  "Conditional CARs to be closed out by:"
423
  ],
424
+ "label_synonyms": ["Audit was conducted", "Audit date", "CARs closed out"],
425
  "priority": 80
426
  },
427
+
428
  "Print accreditation name": {
429
  "headings": [
430
+ {"level": 1, "text": r"\(?print accreditation name\)?", "re": True}
431
  ],
432
+ "orientation": "left",
433
+ "labels": ["(print accreditation name)", "Print accreditation name", "Print Accreditation Name"],
434
+ "label_synonyms": ["Print name accreditation", "Print accreditation"],
435
+ "priority": 85,
436
+ "preferred_json_keys": [
437
+ "Print accreditation name.(print accreditation name)",
438
+ "(print accreditation name)"
439
+ ]
440
  },
441
+
442
  "Operator Declaration": {
443
  "headings": [
444
+ {"level": 1, "text": r"Operator\s+Declaration", "re": True}
445
  ],
446
  "orientation": "row1",
447
  "labels": ["Print Name", "Position Title"],
448
+ "label_synonyms": [
449
+ "Print Name", "Printed Name", "Name (Print)", "Position", "Position Title", "Title"
450
+ ],
451
  "priority": 90,
452
+ "preferred_json_keys": [
453
+ "Operator Declaration.Print Name",
454
+ "Operator Declaration.Position Title",
455
+ "Operator Declaration.Print Name.Print Name"
456
+ ],
457
+ "fallback_keys": ["Print Name", "Position Title", "Print accreditation name.(print accreditation name)"],
458
+ "context_keywords": ["operator declaration", "manager", "operator"],
459
+ "context_exclusions": ["auditor", "nhvas approved"],
460
+ "skip_if_date_like": ["Print Name"], # do not replace name field with date-like values
461
+ "split_on": [" – ", " - ", "\n"], # in case JSON contains multiple names
462
+ # NB: pipeline should attempt exact-qualified keys first, then fallbacks
463
  }
464
  }
465
 
466
+ # ---------------------------
467
+ # Heading / paragraph patterns (expanded)
468
+ # ---------------------------
469
  HEADING_PATTERNS = {
470
  "main": [
471
  r"NHVAS\s+Audit\s+Summary\s+Report",
 
474
  ],
475
  "sub": [
476
  r"AUDIT\s+OBSERVATIONS\s+AND\s+COMMENTS",
477
+ r"MAINTENANCE\s+MANAGEMENT",
478
  r"MASS\s+MANAGEMENT",
479
  r"FATIGUE\s+MANAGEMENT",
480
  r"Fatigue\s+Management\s+Summary\s+of\s+Audit\s+findings",
 
484
  r"CORRECTIVE\s+ACTION\s+REQUEST\s+\(CAR\)",
485
  r"NHVAS\s+APPROVED\s+AUDITOR\s+DECLARATION",
486
  r"Operator\s+Declaration",
487
+ r"Operator\s+Information",
488
+ # catch small variants / OCR noise
489
+ r"OPERATOR'?S\s+NAME",
490
  ]
491
  }
492
 
493
+ # ---------------------------
494
+ # Paragraph patterns (improved)
495
+ # ---------------------------
496
  PARAGRAPH_PATTERNS = {
497
+ "findings_summary": r"Provide a summary of findings based on the evidence gathered during the audit\.?",
498
  "declaration_text": r"I hereby acknowledge and agree with the findings.*",
499
  "introductory_note": r"This audit assesses the.*",
500
+ "date_line": r"^\s*\d{1,2}(?:st|nd|rd|th)?\s+[A-Za-z]+\s+\d{4}\s*$|^Date$",
501
+ # extra patterns to help skip/identify date-like strings in name fields
502
+ "date_like": GLOBAL_SETTINGS["date_like_pattern"]
503
+ }
504
+
505
+ # ---------------------------
506
+ # Helpful hints for pipeline implementer (non-executable comments)
507
+ # ---------------------------
508
+ # 1) Matching order recommendation (pipeline):
509
+ # - Try exact qualified keys in schema['preferred_json_keys']
510
+ # - Try exact label match against schema['labels'] and schema['label_synonyms']
511
+ # - Apply normalization rules from GLOBAL_SETTINGS then try clean match
512
+ # - Fuzzy match using GLOBAL_SETTINGS['fuzzy_thresholds'] keyed by schema priority
513
+ # - If schema has 'fallback_keys', try them last
514
+ #
515
+ # 2) Date-safety: for any schema field listed under 'skip_if_date_like', verify JSON value
516
+ # or cell content is not date-like (use PARAGRAPH_PATTERNS['date_like']) before replacing.
517
+ #
518
+ # 3) Multi-value cells: If schema contains 'split_on', join JSON list items using '\n'
519
+ # or split a single JSON string on these delimiters to create separate lines.
520
+ #
521
+ # 4) OCR repairs: run GLOBAL_SETTINGS['ocr_repair_rules'] on extracted header text
522
+ # before attempting matching (fix common OCR confusion).
523
+ #
524
+ # 5) Processed flags: keep per-table attribute (e.g. table._processed_operator_declaration = True)
525
+ # to avoid running two different handlers on the same Operator Declaration table.
526
+ #
527
+ # 6) Tuning: If a schema is consistently mis-matched, add the exact observed header string
528
+ # into schema['label_synonyms'] for deterministic matching.
529
+ #
530
+ # End of master key