PHP code example of watheqalshowaiter / quran-validator
1. Go to this page and download the library: Download watheqalshowaiter/quran-validator library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
watheqalshowaiter / quran-validator example snippets
use Watheq\QuranValidator\LlmIntegration;
$systemPrompt = LlmIntegration::SYSTEM_PROMPTS['xml']
."\n\n"
.$yourOtherInstructions;
// The LLM should now output Quran quotes like:
// <quran ref="1:1">بِسْمِ ٱللَّهِ ٱلرَّحْمَٰنِ ٱلرَّحِيمِ</quran>
use Watheq\QuranValidator\LlmIntegration;
LlmIntegration::SYSTEM_PROMPTS['xml'];
// <quran ref="1:1">...</quran>
LlmIntegration::SYSTEM_PROMPTS['markdown'];
// A fenced quran code block with a reference
LlmIntegration::SYSTEM_PROMPTS['bracket'];
// [[Q:1:1|...]]
LlmIntegration::SYSTEM_PROMPTS['minimal'];
// ... (1:1)
use Watheq\QuranValidator\LlmIntegration;
use Watheq\QuranValidator\ValueObjects\LlmIntegrationOptions;
$processor = LlmIntegration::create(new LlmIntegrationOptions(
autoCorrect: true,
scanUntagged: true,
tagFormat: 'xml',
));
use Watheq\QuranValidator\LlmIntegration;
$result = LlmIntegration::quickValidate($llmResponse);
var_dump($result['has_quran_content']);
var_dump($result['all_valid']);
print_r($result['issues']);
use Watheq\QuranValidator\ArabicNormalizer;
$normalizer = new ArabicNormalizer();
$normalizer->normalize('السَّلَامُ عَلَيْكُمُ'); // السلام عليكم
$normalizer->removeDiacritics('بِسْمِ اللَّهِ'); // بسم الله
$normalizer->containsArabic('Hello مرحبا world'); // true
$segments = $normalizer->extractArabicSegments('Say بسم الله and continue');
foreach ($segments as $segment) {
echo "{$segment->text}: {$segment->start}-{$segment->end}\n";
}
use Watheq\QuranValidator\LlmIntegration;
function validateLlmResponse(string $response): string
{
$result = LlmIntegration::create()->process($response);
if ($result->hasErrors()) {
throw new RuntimeException('The response contains an invalid Quran quotation.');
}
return $result->correctedText();
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.