File size: 13,932 Bytes
2e237ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Improved Master Key for NHVAS Audit extraction:
- TABLE_SCHEMAS: Enhanced definitions with better matching criteria for Summary vs Basic tables
- HEADING_PATTERNS: Improved regex patterns for main/sub headings
- PARAGRAPH_PATTERNS: Enhanced patterns for key narrative sections
"""

# 1. Enhanced table schemas with better matching logic
TABLE_SCHEMAS = {
    "Tick as appropriate": {
        "headings": [
            {"level": 1, "text": "NHVAS Audit Summary Report"},
        ],
        "orientation": "left",
        "labels": [
            "Mass",
            "Entry Audit", 
            "Maintenance",
            "Initial Compliance Audit",
            "Basic Fatigue",
            "Compliance Audit",
            "Advanced Fatigue",
            "Spot Check",
            "Triggered Audit"
        ],
        "priority": 90  # High priority for direct match
    },
    "Audit Information": {
        "orientation": "left",
        "labels": [
            "Date of Audit",
            "Location of audit", 
            "Auditor name",
            "Audit Matrix Identifier (Name or Number)",
            "Auditor Exemplar Global Reg No.",
            "expiry Date:",
            "NHVR Auditor Registration Number",
            "expiry Date:"
        ],
        "priority": 80
    },
    "Operator Information": {
        "headings": [
            {"level": 1, "text": "Operator Information"}
        ],
        "orientation": "left",
        "labels": [
            "Operator name (Legal entity)",
            "NHVAS Accreditation No. (If applicable)",
            "Registered trading name/s", 
            "Australian Company Number",
            "NHVAS Manual (Policies and Procedures) developed by"
        ],
        "priority": 85
    },
    "Operator contact details": {
        "orientation": "left",
        "labels": [
            "Operator business address",
            "Operator Postal address",
            "Email address", 
            "Operator Telephone Number"
        ],
        "priority": 75,
        "context_keywords": ["contact", "address", "email", "telephone"]
    },
    "Attendance List (Names and Position Titles)": {
        "headings": [
            {"level": 1, "text": "NHVAS Audit Summary Report"}
        ],
        "orientation": "row1",
        "labels": ["Attendance List (Names and Position Titles)"],
        "priority": 90
    },
    "Nature of the Operators Business (Summary)": {
        "orientation": "row1",
        "labels": ["Nature of the Operators Business (Summary):"],
        "split_labels": ["Accreditation Number:", "Expiry Date:"],
        "priority": 85
    },
    "Accreditation Vehicle Summary": {
        "orientation": "left",
        "labels": ["Number of powered vehicles", "Number of trailing vehicles"],
        "priority": 80
    },
    "Accreditation Driver Summary": {
        "orientation": "left", 
        "labels": ["Number of drivers in BFM", "Number of drivers in AFM"],
        "priority": 80
    },
    "Compliance Codes": {
        "orientation": "left",
        "labels": ["V", "NC", "TNC", "SFI", "NAP", "NA"],
        "priority": 70,
        "context_exclusions": ["MASS MANAGEMENT", "MAINTENANCE MANAGEMENT", "FATIGUE MANAGEMENT"]
    },
    "Corrective Action Request Identification": {
        "orientation": "row1",
        "labels": ["Title", "Abbreviation", "Description"],
        "priority": 80
    },
    
    # 🎯 BASIC MANAGEMENT SCHEMAS (Compliance Tables - Lower Priority)
    "Maintenance Management": {
        "headings": [
            {"level": 1, "text": "NHVAS AUDIT SUMMARY REPORT"}
        ],
        "orientation": "left",
        "labels": [
            "Std 1. Daily Check",
            "Std 2. Fault Recording and Reporting", 
            "Std 3. Fault Repair",
            "Std 4. Maintenance Schedules and Methods",
            "Std 5. Records and Documentation",
            "Std 6. Responsibilities",
            "Std 7. Internal Review",
            "Std 8. Training and Education"
        ],
        "priority": 60,
        "context_keywords": ["maintenance"],
        "context_exclusions": ["summary", "details", "audit findings"]  # Exclude Summary tables
    },
    "Mass Management": {
        "headings": [
            {"level": 1, "text": "NHVAS AUDIT SUMMARY REPORT"}
        ],
        "orientation": "left",
        "labels": [
            "Std 1. Responsibilities",
            "Std 2. Vehicle Control",
            "Std 3. Vehicle Use", 
            "Std 4. Records and Documentation",
            "Std 5. Verification",
            "Std 6. Internal Review",
            "Std 7. Training and Education",
            "Std 8. Maintenance of Suspension"
        ],
        "priority": 60,
        "context_keywords": ["mass"],
        "context_exclusions": ["summary", "details", "audit findings"]  # Exclude Summary tables
    },
    "Fatigue Management": {
        "headings": [
            {"level": 1, "text": "NHVAS AUDIT SUMMARY REPORT"}
        ],
        "orientation": "left",
        "labels": [
            "Std 1. Scheduling and Rostering",
            "Std 2. Health and wellbeing for performed duty",
            "Std 3. Training and Education",
            "Std 4. Responsibilities and management practices", 
            "Std 5. Internal Review",
            "Std 6. Records and Documentation",
            "Std 7. Workplace conditions"
        ],
        "priority": 60,
        "context_keywords": ["fatigue"],
        "context_exclusions": ["summary", "details", "audit findings"]  # Exclude Summary tables
    },
    
    # 🎯 SUMMARY MANAGEMENT SCHEMAS (Detailed Tables with DETAILS column - Higher Priority)
    "Maintenance Management Summary": {
        "headings": [
            {"level": 1, "text": "Audit Observations and Comments"},
            {"level": 2, "text": "Maintenance Management Summary of Audit findings"}
        ],
        "orientation": "left",
        "columns": ["MAINTENANCE MANAGEMENT", "DETAILS"],
        "labels": [
            "Std 1. Daily Check", 
            "Std 2. Fault Recording and Reporting",
            "Std 3. Fault Repair", 
            "Std 4. Maintenance Schedules and Methods",
            "Std 5. Records and Documentation", 
            "Std 6. Responsibilities",
            "Std 7. Internal Review", 
            "Std 8. Training and Education"
        ],
        "priority": 85,  # Higher priority than basic Maintenance Management
        "context_keywords": ["maintenance", "summary", "details", "audit findings"]
    },
    "Mass Management Summary": {
        "headings": [
            {"level": 1, "text": "Mass Management Summary of Audit findings"}
        ],
        "orientation": "left",
        "columns": ["MASS MANAGEMENT", "DETAILS"],
        "labels": [
            "Std 1. Responsibilities",
            "Std 2. Vehicle Control", 
            "Std 3. Vehicle Use",
            "Std 4. Records and Documentation",
            "Std 5. Verification",
            "Std 6. Internal Review",
            "Std 7. Training and Education",
            "Std 8. Maintenance of Suspension"
        ],
        "priority": 85,  # Higher priority than basic Mass Management
        "context_keywords": ["mass", "summary", "details", "audit findings"]
    },
    "Fatigue Management Summary": {
        "headings": [
            {"level": 1, "text": "Fatigue Management Summary of Audit findings"}
        ],
        "orientation": "left",
        "columns": ["FATIGUE MANAGEMENT", "DETAILS"],
        "labels": [
            "Std 1. Scheduling and Rostering",
            "Std 2. Health and wellbeing for performed duty",
            "Std 3. Training and Education",
            "Std 4. Responsibilities and management practices",
            "Std 5. Internal Review", 
            "Std 6. Records and Documentation",
            "Std 7. Workplace conditions"
        ],
        "priority": 85,  # Higher priority than basic Fatigue Management
        "context_keywords": ["fatigue", "summary", "details", "audit findings"]
    },
    
    # Vehicle Registration Tables
    "Vehicle Registration Numbers Mass": {
    "headings": [
        {"level": 1, "text": "Vehicle Registration Numbers of Records Examined"},
        {"level": 2, "text": "MASS MANAGEMENT"}
    ],
    "orientation": "row1", 
    "labels": [
        "No.", "Registration Number", "Sub contractor",
        "Sub-contracted Vehicles Statement of Compliance",
        "Weight Verification Records",
        "RFS Suspension Certification #",
        "Suspension System Maintenance", "Trip Records",
        "Fault Recording/ Reporting on Suspension System"
    ],
    "priority": 90,  # Higher priority
    "context_keywords": ["mass", "vehicle registration", "rfs suspension", "weight verification"],
    "context_exclusions": ["maintenance", "roadworthiness", "daily checks"]  # Exclude maintenance-specific terms
},
"Vehicle Registration Numbers Maintenance": {
    "headings": [
        {"level": 1, "text": "Vehicle Registration Numbers of Records Examined"},
        {"level": 2, "text": "Maintenance Management"}
    ],
    "orientation": "row1",
    "labels": [
        "No.", "Registration Number", "Roadworthiness Certificates",
        "Maintenance Records", "Daily Checks",
        "Fault Recording/ Reporting", "Fault Repair"
    ],
    "priority": 85,  # Lower priority
    "context_keywords": ["maintenance", "vehicle registration", "roadworthiness", "daily checks"],
    "context_exclusions": ["mass", "rfs suspension", "weight verification"]  # Exclude mass-specific terms
},
    "Driver / Scheduler Records Examined": {
        "headings": [
            {"level": 1, "text": "Driver / Scheduler Records Examined"},
            {"level": 2, "text": "FATIGUE MANAGEMENT"},
        ],
        "orientation": "row1",
        "labels": [
            "No.",
            "Driver / Scheduler Name", 
            "Driver TLIF Course # Completed",
            "Scheduler TLIF Course # Completed",
            "Medical Certificates (Current Yes/No) Date of expiry",
            "Roster / Schedule / Safe Driving Plan (Date Range)",
            "Fit for Duty Statement Completed (Yes/No)",
            "Work Diary Pages (Page Numbers) Electronic Work Diary Records (Date Range)"
        ],
        "priority": 80,
        "context_keywords": ["driver", "scheduler", "fatigue"]
    },
    
    # Other Tables
    "Operator's Name (legal entity)": {
        "headings": [
            {"level": 1, "text": "CORRECTIVE ACTION REQUEST (CAR)"}
        ],
        "orientation": "left",
        "labels": ["Operator's Name (legal entity)"],
        "priority": 85
    },
    "Non-conformance and CAR details": {
        "orientation": "left",
        "labels": [
            "Non-conformance agreed close out date",
            "Module and Standard",
            "Corrective Action Request (CAR) Number",
            "Observed Non-conformance:",
            "Corrective Action taken or to be taken by operator:",
            "Operator or Representative Signature",
            "Position",
            "Date",
            "Comments:",
            "Auditor signature", 
            "Date"
        ],
        "priority": 75,
        "context_keywords": ["non-conformance", "corrective action"]
    },
    "NHVAS Approved Auditor Declaration": {
        "headings": [
            {"level": 1, "text": "NHVAS APPROVED AUDITOR DECLARATION"}
        ],
        "orientation": "row1",
        "labels": ["Print Name", "NHVR or Exemplar Global Auditor Registration Number"],
        "priority": 90,
        "context_keywords": ["auditor declaration", "NHVR"],
        "context_exclusions": ["manager", "operator declaration"]
    },
    "Audit Declaration dates": {
        "headings": [
            {"level": 1, "text": "Audit Declaration dates"}
        ],
        "orientation": "left",
        "labels": [
            "Audit was conducted on",
            "Unconditional CARs closed out on:",
            "Conditional CARs to be closed out by:"
        ],
        "priority": 80
    },
    "Print accreditation name": {
        "headings": [
            {"level": 1, "text": "(print accreditation name)"}
        ],
        "orientation": "left", 
        "labels": ["(print accreditation name)"],
        "priority": 85
    },
    "Operator Declaration": {
        "headings": [
            {"level": 1, "text": "Operator Declaration"}
        ],
        "orientation": "row1",
        "labels": ["Print Name", "Position Title"],
        "priority": 90,
        "context_keywords": ["operator declaration", "manager"],
        "context_exclusions": ["auditor", "nhvas approved"]
    }
}

# 2. Enhanced heading detection patterns
HEADING_PATTERNS = {
    "main": [
        r"NHVAS\s+Audit\s+Summary\s+Report",
        r"NATIONAL\s+HEAVY\s+VEHICLE\s+ACCREDITATION\s+AUDIT\s+SUMMARY\s+REPORT",
        r"NHVAS\s+AUDIT\s+SUMMARY\s+REPORT"
    ],
    "sub": [
        r"AUDIT\s+OBSERVATIONS\s+AND\s+COMMENTS",
        r"MAINTENANCE\s+MANAGEMENT", 
        r"MASS\s+MANAGEMENT",
        r"FATIGUE\s+MANAGEMENT",
        r"Fatigue\s+Management\s+Summary\s+of\s+Audit\s+findings",
        r"MAINTENANCE\s+MANAGEMENT\s+SUMMARY\s+OF\s+AUDIT\s+FINDINGS",
        r"MASS\s+MANAGEMENT\s+SUMMARY\s+OF\s+AUDIT\s+FINDINGS",
        r"Vehicle\s+Registration\s+Numbers\s+of\s+Records\s+Examined",
        r"CORRECTIVE\s+ACTION\s+REQUEST\s+\(CAR\)",
        r"NHVAS\s+APPROVED\s+AUDITOR\s+DECLARATION",
        r"Operator\s+Declaration",
        r"Operator\s+Information"
    ]
}

# 3. Enhanced paragraph patterns for key narrative sections
PARAGRAPH_PATTERNS = {
    "findings_summary": r"Provide a summary of findings based on the evidence gathered during the audit\.",
    "declaration_text": r"I hereby acknowledge and agree with the findings.*",
    "introductory_note": r"This audit assesses the.*",
    "date_line": r"^\s*\d{1,2}(?:st|nd|rd|th)?\s+[A-Za-z]+\s+\d{4}\s*$|^Date$"
}