327 lines
13 KiB
PHP
327 lines
13 KiB
PHP
<?php
|
||
header('Content-Type: application/json');
|
||
|
||
// --- Helper Functions for Report Generation ---
|
||
|
||
/**
|
||
* Generates a concise summary of the text.
|
||
*/
|
||
function generate_summary_report($text) {
|
||
$report = "<h1>AI-Generated Summary Report</h1>";
|
||
$report .= "<p>A high-level overview of the key points in the document.</p><br/>";
|
||
|
||
$sentences = preg_split('/(?<=[.?!])\s+/', $text, -1, PREG_SPLIT_NO_EMPTY);
|
||
$num_sentences = count($sentences);
|
||
$summary_sentences = [];
|
||
$sentences_to_take = 3;
|
||
|
||
if ($num_sentences <= ($sentences_to_take * 2)) {
|
||
$summary_sentences = $sentences;
|
||
} else {
|
||
$summary_sentences = array_slice($sentences, 0, $sentences_to_take);
|
||
$summary_sentences[] = "[...]";
|
||
$summary_sentences = array_merge($summary_sentences, array_slice($sentences, -$sentences_to_take));
|
||
}
|
||
|
||
$report .= "<h2>Key Points:</h2><ul>";
|
||
foreach ($summary_sentences as $sentence) {
|
||
$report .= "<li>" . htmlspecialchars(trim($sentence)) . "</li>";
|
||
}
|
||
$report .= "</ul>";
|
||
return $report;
|
||
}
|
||
|
||
/**
|
||
* Extracts sentences that appear to reference dates or events to create a timeline.
|
||
*/
|
||
function generate_timeline_report($text) {
|
||
$report = "<h1>AI-Generated Timeline of Events</h1>";
|
||
$report .= "<p>A chronological list of events and important dates mentioned in the document.</p><br/>";
|
||
|
||
// Regex to find sentences with dates (e.g., 25/12/2023, Dec 25, 2023) or time-related keywords.
|
||
$date_pattern = '/\b(\d{1,2}[-\/]\d{1,2}[-\/]\d{2,4}|\w+\s\d{1,2},\s\d{4})\b/i';
|
||
$keyword_pattern = '/\b(on|at|before|after|during|later|yesterday|today|tomorrow|meanwhile)\b/i';
|
||
|
||
$sentences = preg_split('/(?<=[.?!])\s+/', $text, -1, PREG_SPLIT_NO_EMPTY);
|
||
$timeline_events = [];
|
||
|
||
foreach ($sentences as $sentence) {
|
||
if (preg_match($date_pattern, $sentence) || preg_match($keyword_pattern, $sentence)) {
|
||
$timeline_events[] = $sentence;
|
||
}
|
||
}
|
||
|
||
$report .= "<h2>Potential Events:</h2>";
|
||
if (empty($timeline_events)) {
|
||
$report .= "<p>No specific date or time-based events were detected by the AI.</p>";
|
||
} else {
|
||
$report .= "<ul>";
|
||
foreach ($timeline_events as $event) {
|
||
$report .= "<li>" . htmlspecialchars(trim($event)) . "</li>";
|
||
}
|
||
$report .= "</ul>";
|
||
}
|
||
return $report;
|
||
}
|
||
|
||
/**
|
||
* Extracts names of individuals based on common titles.
|
||
*/
|
||
function generate_persons_report($text) {
|
||
$report = "<h1>AI-Generated List of Key Individuals</h1>";
|
||
$report .= "<p>A list of individuals who may be relevant to the document.</p><br/>";
|
||
|
||
// Regex to find names with titles (Mr., Mrs., Dr., etc.) or potential full names.
|
||
$pattern = '/\b(Mr\.|Mrs\.|Ms\.|Dr\.|Prof\.)\s+[A-Z][a-z]+(\s+[A-Z][a-z]+)?/i';
|
||
preg_match_all($pattern, $text, $matches);
|
||
|
||
$persons = array_unique($matches[0]);
|
||
|
||
$report .= "<h2>Mentioned Individuals:</h2>";
|
||
if (empty($persons)) {
|
||
$report .= "<p>No individuals with titles (e.g., Mr., Dr.) were automatically detected.</p>";
|
||
} else {
|
||
$report .= "<ul>";
|
||
foreach ($persons as $person) {
|
||
$report .= "<li>" . htmlspecialchars(trim($person)) . "</li>";
|
||
}
|
||
$report .= "</ul>";
|
||
}
|
||
return $report;
|
||
}
|
||
|
||
/**
|
||
* Extracts a log of potential evidence markers.
|
||
*/
|
||
function generate_evidence_report($text) {
|
||
$report = "<h1>AI-Generated Evidence Log</h1>";
|
||
$report .= "<p>A log of items marked as potential evidence or exhibits.</p><br/>";
|
||
|
||
// Regex to find evidence-related keywords.
|
||
$pattern = '/\b(Exhibit|Appendix|Figure|Table|Attachment|Document|Photo|Video|Audio)\s+[A-Z0-9\-]+/i';
|
||
preg_match_all($pattern, $text, $matches);
|
||
|
||
$evidence_items = array_unique($matches[0]);
|
||
|
||
$report .= "<h2>Potential Evidence:</h2>";
|
||
if (empty($evidence_items)) {
|
||
$report .= "<p>No items matching standard evidence markers (e.g., 'Exhibit A', 'Document 123') were found.</p>";
|
||
} else {
|
||
$report .= "<ul>";
|
||
foreach ($evidence_items as $item) {
|
||
$report .= "<li>" . htmlspecialchars(trim($item)) . "</li>";
|
||
}
|
||
$report .= "</ul>";
|
||
}
|
||
return $report;
|
||
}
|
||
|
||
/**
|
||
* Generates a placeholder for a Case Diary Status Report.
|
||
*/
|
||
function generate_case_diary_report($text) {
|
||
$report = "<h1>Case Diary Status Report</h1>";
|
||
$report .= "<p>This is a placeholder for the Case Diary Status Report. Future development will populate this with relevant case diary entries from the text.</p><br/>";
|
||
$report .= "<h2>Extracted Text:</h2><p>" . htmlspecialchars(substr($text, 0, 500)) . "...</p>";
|
||
return $report;
|
||
}
|
||
|
||
/**
|
||
* Generates a placeholder for a Bail Reply.
|
||
*/
|
||
function generate_bail_reply_report($text) {
|
||
$report = "<h1>Bail Reply</h1>";
|
||
$report .= "<p>This is a placeholder for the Bail Reply. Future development will populate this with arguments and evidence against bail from the text.</p><br/>";
|
||
$report .= "<h2>Extracted Text:</h2><p>" . htmlspecialchars(substr($text, 0, 500)) . "...</p>";
|
||
return $report;
|
||
}
|
||
|
||
/**
|
||
* Generates a placeholder for a High Court Status Report.
|
||
*/
|
||
function generate_high_court_status_report($text) {
|
||
$report = "<h1>High Court Status Report</h1>";
|
||
$report .= "<p>This is a placeholder for the High Court Status Report. Future development will populate this with case status details for the High Court.</p><br/>";
|
||
$report .= "<h2>Extracted Text:</h2><p>" . htmlspecialchars(substr($text, 0, 500)) . "...</p>";
|
||
return $report;
|
||
}
|
||
|
||
/**
|
||
* Generates a placeholder for a Seizure Memo.
|
||
*/
|
||
function generate_seizure_memo_report($text) {
|
||
$report = "<h1>Seizure Memo</h1>";
|
||
$report .= "<p>This is a placeholder for the Seizure Memo. Future development will populate this with details of seized items from the text.</p><br/>";
|
||
$report .= "<h2>Extracted Text:</h2><p>" . htmlspecialchars(substr($text, 0, 500)) . "...</p>";
|
||
return $report;
|
||
}
|
||
|
||
/**
|
||
* Generates a placeholder for a Chargesheet.
|
||
*/
|
||
function generate_chargesheet_report($text) {
|
||
$report = "<h1>Chargesheet</h1>";
|
||
$report .= "<p>This is a placeholder for the Chargesheet. Future development will populate this with charges, evidence, and witness lists from the text.</p><br/>";
|
||
$report .= "<h2>Extracted Text:</h2><p>" . htmlspecialchars(substr($text, 0, 500)) . "...</p>";
|
||
return $report;
|
||
}
|
||
|
||
/**
|
||
* Generates a placeholder for a Disclosure Statement.
|
||
*/
|
||
function generate_disclosure_statement_report($text) {
|
||
$report = "<h1>Disclosure Statement</h1>";
|
||
$report .= "<p>This is a placeholder for the Disclosure Statement. Future development will populate this with disclosed information from the text.</p><br/>";
|
||
$report .= "<h2>Extracted Text:</h2><p>" . htmlspecialchars(substr($text, 0, 500)) . "...</p>";
|
||
return $report;
|
||
}
|
||
|
||
/**
|
||
* Generates a placeholder for an Exemption Request.
|
||
*/
|
||
function generate_exemption_request_report($text) {
|
||
$report = "<h1>Exemption Request</h1>";
|
||
$report .= "<p>This is a placeholder for the Exemption Request. Future development will populate this with reasons and justifications for exemption from the text.</p><br/>";
|
||
$report .= "<h2>Extracted Text:</h2><p>" . htmlspecialchars(substr($text, 0, 500)) . "...</p>";
|
||
return $report;
|
||
}
|
||
|
||
/**
|
||
* Generates a placeholder for an NBW (Non-Bailable Warrant).
|
||
*/
|
||
function generate_nbw_report($text) {
|
||
$report = "<h1>NBW (Non-Bailable Warrant)</h1>";
|
||
$report .= "<p>This is a placeholder for the NBW. Future development will populate this with details for the warrant application from the text.</p><br/>";
|
||
$report .= "<h2>Extracted Text:</h2><p>" . htmlspecialchars(substr($text, 0, 500)) . "...</p>";
|
||
return $report;
|
||
}
|
||
|
||
/**
|
||
* Generates a report for a Reply for Release of RC.
|
||
*/
|
||
function generate_release_of_rc_reply_report($text) {
|
||
$report = "<h1>Reply for Release of RC</h1>";
|
||
|
||
// Extract details using regex
|
||
preg_match('/IN THE HON’BLE COURT OF (.*)/', $text, $court);
|
||
preg_match('/FIR NO.: (.*)/', $text, $fir_no);
|
||
preg_match('/U\/S: (.*)/', $text, $us);
|
||
preg_match('/P.S.: (.*)/', $text, $ps);
|
||
preg_match('/State. vs. (.*)/', $text, $parties);
|
||
preg_match('/motorcycle bearing no. (.*?),/', $text, $vehicle_no);
|
||
preg_match('/applicant, (.*?)\./', $text, $applicant);
|
||
preg_match('/accused, (.*?)[,.]/', $text, $accused);
|
||
preg_match('/disposed of by this Hon'ble Court on (.*)\./', $text, $disposal_date);
|
||
|
||
$report .= "<p><strong>IN THE HON’BLE COURT OF " . htmlspecialchars($court[1] ?? '...') . "</strong></p>";
|
||
$report .= "<p><strong>FIR NO.:</strong> " . htmlspecialchars($fir_no[1] ?? '...') . "</p>";
|
||
$report .= "<p><strong>U/S:</strong> " . htmlspecialchars($us[1] ?? '...') . "</p>";
|
||
$report .= "<p><strong>P.S.:</strong> " . htmlspecialchars($ps[1] ?? '...') . "</p>";
|
||
$report .= "<p><strong>In the matter of:</strong> State. vs. " . htmlspecialchars($parties[1] ?? '...') . "</p>";
|
||
$report .= "<hr>";
|
||
$report .= "<p><strong>Subject- Reply for Release of RC</strong></p>";
|
||
$report .= "<p>HON’BLE SIR,</p>";
|
||
$report .= "<p>Most respectfully submitted that present application for the release of the registration certificate (RC) of motorcycle bearing no. <strong>" . htmlspecialchars($vehicle_no[1] ?? '...') . "</strong>, filed by the applicant, <strong>" . htmlspecialchars($applicant[1] ?? '...') . "</strong>.</p>";
|
||
$report .= "<p>The applicant stood as surety for the accused, <strong>" . htmlspecialchars($accused[1] ?? '...') . "</strong>, during the pendency of the case.</p>";
|
||
$report .= "<p>The case has been disposed of by this Hon'ble Court on <strong>" . htmlspecialchars($disposal_date[1] ?? '...') . "</strong>.</p>";
|
||
$report .= "<p>That no other appeal in this regard is pending in any other Hon’ble Court or High Court.</p>";
|
||
$report .= "<p>That undersigned have no objection on release of RC.</p>";
|
||
$report .= "<p>Submitted Please.</p>";
|
||
|
||
return $report;
|
||
}
|
||
|
||
/**
|
||
* Generates a report for a Request for Cartridges.
|
||
*/
|
||
function generate_request_for_cartridges_report($text) {
|
||
$report = "<h1>Request for Cartridges</h1>";
|
||
|
||
// Extract details using regex
|
||
preg_match('/FIR NO . (.*)/', $text, $fir_no);
|
||
preg_match('/PS (.*) DELHI/', $text, $ps);
|
||
preg_match('/TO (.*)/', $text, $to);
|
||
preg_match('/provide (.*) for test firing/', $text, $cartridges);
|
||
preg_match('/sent to (.*) Vide/', $text, $fsl);
|
||
preg_match('/Letter No. (.*) Dated/', $text, $letter_no);
|
||
preg_match('/Dated (.*) for expert/', $text, $letter_date);
|
||
|
||
|
||
$report .= "<p><strong>FIR NO.:</strong> " . htmlspecialchars($fir_no[1] ?? '...') . "</p>";
|
||
$report .= "<p><strong>P.S.:</strong> " . htmlspecialchars($ps[1] ?? '...') . "</p>";
|
||
$report .= "<p><strong>TO:</strong> " . htmlspecialchars($to[1] ?? '...') . "</p>";
|
||
$report .= "<hr>";
|
||
$report .= "<p><strong>Subject- Request to provide " . htmlspecialchars($cartridges[1] ?? '...') . " for test firing</strong></p>";
|
||
$report .= "<p>Sir,</p>";
|
||
$report .= "<p>Most respectfully submitted that recovered arms and ammunition were sent to <strong>" . htmlspecialchars($fsl[1] ?? '...') . "</strong> Vide Letter No. <strong>" . htmlspecialchars($letter_no[1] ?? '...') . "</strong> Dated <strong>" . htmlspecialchars($letter_date[1] ?? '...') . "</strong> for expert opinion.</p>";
|
||
$report .= "<p>That FSL raised objection and asked to provide <strong>" . htmlspecialchars($cartridges[1] ?? '...') . "</strong> for Test firing.</p>";
|
||
$report .= "<p>Therefore it is requested that for further proceedings <strong>" . htmlspecialchars($cartridges[1] ?? '...') . "</strong> may kindly be provided.</p>";
|
||
$report .= "<p>Submitted please.</p>";
|
||
|
||
return $report;
|
||
}
|
||
|
||
|
||
// --- Main Logic ---
|
||
|
||
$data = json_decode(file_get_contents('php://input'), true);
|
||
$text = $data['text'] ?? '';
|
||
$template = $data['template'] ?? 'summary'; // Default to 'summary'
|
||
|
||
if (empty($text)) {
|
||
echo json_encode(['report' => 'No text provided to generate a report.']);
|
||
exit;
|
||
}
|
||
|
||
$final_report = '';
|
||
|
||
switch ($template) {
|
||
case 'timeline':
|
||
$final_report = generate_timeline_report($text);
|
||
break;
|
||
case 'persons':
|
||
$final_report = generate_persons_report($text);
|
||
break;
|
||
case 'evidence':
|
||
$final_report = generate_evidence_report($text);
|
||
break;
|
||
case 'case_diary':
|
||
$final_report = generate_case_diary_report($text);
|
||
break;
|
||
case 'bail_reply':
|
||
$final_report = generate_bail_reply_report($text);
|
||
break;
|
||
case 'high_court_status':
|
||
$final_report = generate_high_court_status_report($text);
|
||
break;
|
||
case 'seizure_memo':
|
||
$final_report = generate_seizure_memo_report($text);
|
||
break;
|
||
case 'chargesheet':
|
||
$final_report = generate_chargesheet_report($text);
|
||
break;
|
||
case 'disclosure_statement':
|
||
$final_report = generate_disclosure_statement_report($text);
|
||
break;
|
||
case 'exemption_request':
|
||
$final_report = generate_exemption_request_report($text);
|
||
break;
|
||
case 'nbw':
|
||
$final_report = generate_nbw_report($text);
|
||
break;
|
||
case 'release_of_rc_reply':
|
||
$final_report = generate_release_of_rc_reply_report($text);
|
||
break;
|
||
case 'request_for_cartridges':
|
||
$final_report = generate_request_for_cartridges_report($text);
|
||
break;
|
||
case 'summary':
|
||
default:
|
||
$final_report = generate_summary_report($text);
|
||
break;
|
||
}
|
||
|
||
$final_report .= "<br/><p><strong>--- End of Report ---</strong></p>";
|
||
|
||
// Return the report as JSON
|
||
echo json_encode(['report' => $final_report]);
|