/
v.bolshakov
/
AIEcosystem-Testing
Обзор
Документация
Войти
/
v.bolshakov
/
AIEcosystem-Testing
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
common/helpers/TmHelper.php
171 строка
5 KB
Developer
Initial commit
03 авг 2026, 17:43
03 авг 2026, 17:43
7433917
Код
Авторство
О чём код?
<?php namespace common\helpers; use common\models\Glossary; use common\models\tm\Tm; use common\shell\Commands\Builder; use common\shell\Runners\Exec; use common\shell\Runners\ResultCode; use common\shell\Runners\ReturnValue; use common\shell\Runners\StandardError; use common\shell\Runners\StandardOut; use Yii; use yii\helpers\Json; use yii\helpers\VarDumper; use yii\web\NotFoundHttpException; /** * Расширение функций для работы с TM * * @author Dmitry E. Semenov <sde.tomsk@gmail.com> * @copyright Self (c) 2026 */ class TmHelper { /** * Сформировать текстовый файл для глоссария * * @param Tm $tm * @return bool * @throws \yii\base\Exception */ public static function generateFile($tm) { $file_name = $tm->getFilename(); FileHelper::createDirectory(dirname($file_name)); $items = $tm->toTermArray(); if (false === file_put_contents($file_name, implode(PHP_EOL, $items))) { return false; } return true; } /** * Сохранить текст в файл * * @param $text * @return false|string * @throws \yii\base\Exception */ public static function generateText($text) { $string = Yii::$app->security->generateRandomString(8); $file_name = Yii::getAlias('@common/runtime/tm/text/text_' . $string . '.txt'); FileHelper::createDirectory(dirname($file_name)); if (false === file_put_contents($file_name, $text)) { return false; } return $file_name; } /** * Анализ текст и переводческой памяти * * @param string $text * @param Tm $tm * @return array * @throws \yii\base\Exception */ public static function doAnalysis($text, $tm) { $result = []; $is_ok = true; $message = 'OK'; $array = explode(PHP_EOL, $text); AppHelper::log('use tm: ' . $text); Yii::debug(VarDumper::dumpAsString([ 'array' => $array ])); foreach ($array as $segment) { $segment = Utf8Helper::trim($segment); $term = $tm->toTermArray($segment); if (count($term) > 0) { Yii::debug(VarDumper::dumpAsString([ 'segment' => $segment, 'term' => $term ])); $file_tm = TmHelper::generateText(implode(PHP_EOL, $term)); $file_text = TmHelper::generateText($segment); $search = Yii::getAlias('@root/script/chrf.py'); $command = new Builder(); $command ->setCommand('python3') ->addParam($search) ->addParam($file_tm) ->addParam($file_text); $cmd = (string)$command; $data = [ 'cmd' => $cmd, ]; $run = new Exec(); $run->run($command); if ($run instanceof ReturnValue) { $data['return_value'] = $run->getReturnValue(); } if ($run instanceof ResultCode) { $data['return_code'] = $run->getResultCode(); } if ($run instanceof StandardError) { $data['standard_error'] = $run->getStandardError(); } if ($run instanceof StandardOut) { $data['standard_out'] = $run->getStandardOut(); } Yii::debug(VarDumper::dumpAsString($data)); $json = ArrayHelper::getValue($data, 'return_value.0'); try { $items = Json::decode($json); foreach ($items as $index => $item) { $result[] = [ 'segment' => $item['segment'], 'source' => $item['source'], 'score' => (float)$item['score'], 'id' => $item['id'] ]; } } catch (\Exception $e) { $message = $e->getMessage(); $is_ok = false; } FileHelper::unlink($file_tm); FileHelper::unlink($file_text); if (!$is_ok) { break; } } } return [ 'is_ok' => $is_ok, 'message' => $message, 'raw' => $result, ]; } }