PHP code example of arnaudleroy-studio / mohitkhare

1. Go to this page and download the library: Download arnaudleroy-studio/mohitkhare 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/ */

    

arnaudleroy-studio / mohitkhare example snippets


use MohitKhare\TokenEstimator;

$estimator = new TokenEstimator();

// Quick estimate for a single string
$count = $estimator->estimate('The quick brown fox jumps over the lazy dog.');
echo "{$count} tokens"; // ~10 tokens

// Batch estimation with named arguments
$results = $estimator->estimateBatch(
    texts: ['Hello world', 'PHP is a general-purpose scripting language', $longArticle],
    model: 'gpt-4',
);

use MohitKhare\TextAnalyzer;

$analyzer = new TextAnalyzer();

$stats = $analyzer->analyze('Your input text goes here...');

// Array destructuring for readability
['words' => $words, 'sentences' => $sentences, 'readability' => $score] = $stats;

echo "Flesch-Kincaid readability: {$score}";

// Fit a prompt within model context limits
$truncated = $estimator->truncate(
    text: $longDocument,
    maxTokens: 4096,
    strategy: 'end',
);

// Null-safe access when source might be empty
$preview = $estimator->truncate(
    text: $input?->getContent() ?? '',
    maxTokens: 200,
)?->getText();

$articles = ['First article body...', 'Second article body...', 'Third...'];

// Map to token counts in one pass
$counts = array_map(fn(string $text) => $estimator->estimate($text), $articles);

// Filter articles that fit within budget
$withinBudget = array_filter(
    $articles,
    fn(string $text) => $estimator->estimate($text) <= 2048
);