PHP Classes

File: examples/case-studies/macros/semantic_macro_trigger/semantic_macro_trigger.php

Recommend this page to a friend!
  Packages of Christos Drogidis   Ascoos OS   examples/case-studies/macros/semantic_macro_trigger/semantic_macro_trigger.php   Download  
File: examples/case-studies/macros/semantic_macro_trigger/semantic_macro_trigger.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Ascoos OS
A PHP Web 5.0 Kernel for decentralized web and IoT
Author: By
Last change:
Date: 9 months ago
Size: 2,608 bytes
 

Contents

Class file image Download
<?php
/**
 * @ASCOOS-NAME : Ascoos OS
 * @ASCOOS-VERSION : 26.0.0
 * @ASCOOS-SUPPORT : support@ascoos.com
 * @ASCOOS-BUGS : https://issues.ascoos.com
 *
 * @desc <English> Executes macros based on NLP analysis of editorial content using sentiment and topic detection.
 * @desc <Greek> ??????? macros ????? NLP ???????? ??????????? ???????????? ?? ????????? ????????????? ??? ?????????.
 *
 * @since PHP 8.2.0
 */
declare(strict_types=1);

use
ASCOOS\OS\Kernel\{
   
AI\NLP\TLanguageProcessingAIHandler,
   
Parsers\DSL\AbstractDslAstBuilder,
   
Parsers\DSL\AstMacroTranslator
};

// <English> Sample editorial text.
// <Greek> ?????? ??????????? ??????.
$text = <<<TEXT
The economy continues to struggle under inflationary pressure.
Unemployment is rising, and consumer confidence is falling.
TEXT;

// <English> Initialize NLP handler and extract sentiment and topic.
// <Greek> ???????????? NLP ???????? ??? ??????? ????????????? ??? ?????????.
$nlp = new TLanguageProcessingAIHandler();
$sentiment = $nlp->naiveBayesSentiment($text); // 'positive' or 'negative' or 'neutral'
$concepts = $nlp->conceptActivationVector(['economy', 'inflation', 'jobs'], [$text]);

// <English> DSL script defining macro logic.
// <Greek> DSL script ??? ?????? ?? ?????? ??? macros.
$dsl = <<<DSL
WHEN sentiment = negative AND topic = "economy" THEN
    TAG "alert"
    NOTIFY "Editor"
    FLAG "Review"
DSL;

// <English> Build AST from DSL.
// <Greek> ?????????? AST ??? DSL.
$astBuilder = new class extends AbstractDslAstBuilder {};
$ast = $astBuilder->buildAst($dsl);

// <English> Translate AST into macro container.
// <Greek> ????????? AST ?? container ????????????.
$translator = new class([
   
'TAG' => fn(string $tag) => print("?? Tagged: $tag\n"),
   
'NOTIFY' => fn(string $who) => print("? Notification sent to: $who\n"),
   
'FLAG' => fn(string $flag) => print("? Flagged for: $flag\n"),
   
'sentiment' => fn() => $sentiment,
   
'topic' => fn() => in_array('economy', $concepts) ? 'economy' : 'other'
]) extends AstMacroTranslator {};

$macroContainer = $translator->translateAst($ast);

// <English> Execute macros if NLP conditions are met.
// <Greek> ???????? macros ??? ?????????? ?? NLP ????????.
$macroContainer->executeIfTrue();
?>