PHP code example of ali-apadana / persian-keyword

1. Go to this page and download the library: Download ali-apadana/persian-keyword 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/ */

    

ali-apadana / persian-keyword example snippets


use PersianKeyword\Facades\Keyword;

$result = Keyword::extract($title, $body);

$keywords = $result->keywords();
$tokens = $result->tokens();
$phrases = $result->phrases();
$entities = $result->entities();

$array = $result->toArray();
$json = $result->toJson();

$result = Keyword::extract($title, $body, [
    'max_keywords' => 5,
    'remove_stopwords' => true,
    'detect_entities' => false,
]);

foreach ($result->keywordScores() as $keyword) {
    echo $keyword->keyword();
    echo $keyword->score();
}

foreach ($result->entities() as $entity) {
    echo $entity->name();   // e.g. تهران
    echo $entity->type();   // e.g. location
    echo $entity->source(); // title or body
}

$normalizedTitle = $result->meta()['normalized_title'];
$normalizedBody = $result->meta()['normalized_body'];

$result = Keyword::extract($title, $body, ['remove_stopwords' => false]);

use PersianKeyword\PersianKeyword;

$result = app(PersianKeyword::class)->extract($title, $body);

return [
    'max_keywords' => 10,
    'min_keyword_length' => 2,
    'normalize' => true,
    'normalizer' => [
        'decode_html' => true,
        'strip_html' => true,
        'convert_arabic_characters' => true,
        'convert_digits' => true,
        'remove_diacritics' => true,
        'collapse_whitespace' => true,
    ],
    'tokenizer' => [
        'remove_stopwords' => true,
    ],
    'phrase_extractor' => [
        'min_terms' => 2,
        'max_terms' => 3,
    ],
    'scoring' => [
        'title_weight' => 2.0,
        'body_weight' => 1.0,
        'title_phrase_weight' => 5.0,
        'body_phrase_weight' => 2.0,
        'entity_boost' => 3.0,
    ],
    'entity_recognition' => [
        'enabled' => true,
    ],
];
bash
php artisan vendor:publish --tag=persian-keyword-config