/
v.bolshakov
/
AIEcosystem-Testing
Обзор
Документация
Войти
/
v.bolshakov
/
AIEcosystem-Testing
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
common/models/tm/Tm.php
221 строка
5 KB
Developer
Initial commit
03 авг 2026, 17:43
03 авг 2026, 17:43
7433917
Код
Авторство
О чём код?
<?php namespace common\models\tm; use common\helpers\GlossaryHelper; use common\helpers\Utf8Helper; use common\kdf\KLanguage; use common\models\TextSegment; use common\models\TextSegmentMongo; use common\traits\TitleRecordTrait; use Yii; use yii\db\ActiveQuery; use yii\helpers\ArrayHelper; use yii\helpers\Json; use yii\helpers\VarDumper; /** * Таблица - глоссарий * * @property string $name Заголовок * @property int $source_language_id * @property int $target_language_id * @property int $term_count * * @author Dmitry E. Semenov <sde.tomsk@gmail.com> * @copyright Self (c) 2024 */ class Tm extends \common\models\base\BaseModel { use TitleRecordTrait; /** * @inheritdoc */ public $softDelete = true; /** * {@inheritdoc} */ public static function tableName() { return 'tm'; } /** * {@inheritdoc} */ public function rules() { return ArrayHelper::merge(parent::rules(), [ ['term_count', 'default', 'value' => 0], ['name', 'trim'], [['name', 'term_count'], 'required'], [['name'], 'string', 'max' => 255], [['source_language_id', 'target_language_id', 'term_count'], 'number'], [['source_language_id', 'target_language_id'], 'required'], ]); } /** * {@inheritdoc} */ public function attributeLabels() { return ArrayHelper::merge(parent::attributeLabels(), [ 'name' => Yii::t('app', 'Glossary name'), 'term_count' => Yii::t('app', 'Term count'), 'source_language_id' => Yii::t('app', 'Source language'), 'target_language_id' => Yii::t('app', 'Target language'), ]); } /** * @return ActiveQuery */ public function getSourceLanguage() { return $this->hasOne(KLanguage::class, ['id' => 'source_language_id']); } /** * @return ActiveQuery */ public function getTargetLanguage() { return $this->hasOne(KLanguage::class, ['id' => 'target_language_id']); } /** * @inheritdoc */ public static function getTitleFields() { return ['name']; } /** * @inheritdoc */ public function __toString() { return $this->getTitle(); } /** * Получить количество элементов * @return mixed * @throws \yii\base\InvalidConfigException */ public function getTermCount() { return TmTermMongo::find() ->andWhere([ 'tm_id' => $this->id ]) ->count(); } /** * Получить путь к файлу * @return false|string */ public function getFilename() { return Yii::getAlias('@common/runtime/tm/tm' . $this->id . '.txt'); } /** * Получить массив терминов * @return array * @throws \yii\base\InvalidConfigException */ public function toTermArray($search = null) { $search_lite = Utf8Helper::substr($search, 0, 50); Yii::debug(VarDumper::dumpAsString([ 'search' => $search, 'search_lite' => $search_lite, 'tm_id' => $this->id ])); $query = TmTermMongo::find() ->select([ '_id', 'source' ]) ->andWhere([ 'tm_id' => $this->id ]); if ($search) { $query ->andWhere([ '$text' => [ '$search' => $search, ] ]) ->limit(10) ->orderBy(['score' => ['$meta' => 'textScore']]); } $data = $query->all(); $result = []; foreach ($data as $row) { $result[] = $row['_id'] . ';' . $row['source']; } return $result; } /** * Преобразовать в JSONL глоссарий * @return string * @throws \yii\base\InvalidConfigException */ public function toJsonl($request_id = null) { $query = TmTermMongo::find() ->select([ 'source', 'target', ]) ->andWhere([ 'tm_id' => $this->id ]); if ($request_id) { $ids = LinkRequestTm::find() ->select('tm_term_id') ->andWhere([ 'request_id' => $request_id ]) ->column(); $cnt = count($ids); $max = min($cnt, 50); $query ->limit($max) ->andWhere([ '_id' => $ids ]); } $data = $query ->asArray() ->all(); $result = []; foreach ($data as $row) { $result[] = Json::encode([ 'segment' => $row['source'], 'translation' => $row['target'] ]); } return implode(PHP_EOL, $result); } }