Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduced prioritization and penalties for TM keys #3704

Open
wants to merge 55 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
52260f4
Introduced prioritization and penalties for TM keys
mauretto78 Nov 26, 2024
a7882db
TM Key sanitization
mauretto78 Nov 26, 2024
f30f7a2
TM key validation
mauretto78 Nov 26, 2024
1dc305f
updateJobKeysController refactoring and validation
mauretto78 Nov 27, 2024
eeb7098
schema
mauretto78 Nov 27, 2024
062b34b
Fixed updating job keys
mauretto78 Nov 27, 2024
fa32336
tm_keys validation
mauretto78 Nov 27, 2024
dd6de0d
TMAnalysisWorker
mauretto78 Nov 28, 2024
3b58325
Layout with switch prioritization
piedicianni Nov 28, 2024
da08863
Fix
piedicianni Nov 28, 2024
58e1129
Adding cache
mauretto78 Nov 28, 2024
646ef0d
Merge branch 'tm-prioritization' of github.com:matecat/MateCat into t…
mauretto78 Nov 28, 2024
2fd1805
UI integration
piedicianni Dec 2, 2024
470f558
Create numeric stepper and row grid adaptation
piedicianni Dec 3, 2024
b4cc775
Added `tm_prioritization` to project templates
mauretto78 Dec 3, 2024
e81bd1a
TMKeyStruct json
mauretto78 Dec 3, 2024
a131e34
Integration with BE
piedicianni Dec 3, 2024
e662cd6
Saving template fix
mauretto78 Dec 3, 2024
ced05c4
Fix
piedicianni Dec 3, 2024
46f21e3
Fix saving mt_prioritization
mauretto78 Dec 3, 2024
c7b3474
cast strings
mauretto78 Dec 3, 2024
faaf6c3
Behaviour cattol page
piedicianni Dec 3, 2024
6f667a6
Fix saving job metadata
mauretto78 Dec 3, 2024
bf37356
Fix
piedicianni Dec 4, 2024
6b26abc
Fix
piedicianni Dec 4, 2024
5a0b751
GetContribution
mauretto78 Dec 4, 2024
526bead
Merge branch 'tm-prioritization' of github.com:matecat/MateCat into t…
mauretto78 Dec 4, 2024
fa5d6aa
Engines_MyMemory
mauretto78 Dec 4, 2024
b1778e3
MyMemory config
mauretto78 Dec 4, 2024
32e659d
Fix
piedicianni Dec 4, 2024
2d17e7f
fix tests
mauretto78 Dec 4, 2024
96db2a5
Fixed tests
mauretto78 Dec 4, 2024
2b33246
Debug
mauretto78 Dec 5, 2024
4131163
Fix PHP errors
mauretto78 Dec 5, 2024
7ebf618
GetContribution
mauretto78 Dec 5, 2024
1660b90
Fix
piedicianni Dec 5, 2024
fe622ba
GetContributionWorker fix
mauretto78 Dec 5, 2024
08e1483
Merge branch 'tm-prioritization' of github.com:matecat/MateCat into t…
mauretto78 Dec 5, 2024
35fc7e1
Fix MyMemory client
mauretto78 Dec 5, 2024
cc79341
Fix
piedicianni Dec 6, 2024
4ba8a4c
Fix update job keys
mauretto78 Dec 6, 2024
faee912
Css fixes
piedicianni Dec 10, 2024
ec2e957
Css fixes
piedicianni Dec 10, 2024
8cd44b0
Fix
piedicianni Dec 10, 2024
3229553
Fix
piedicianni Dec 10, 2024
f7d6881
TMAnalysis Worker fix
mauretto78 Dec 19, 2024
20f4007
TMAnalysisWorker fixes
mauretto78 Dec 19, 2024
ca0c1bc
Fixed fetch of tm_prioritization value
mauretto78 Dec 19, 2024
f026c96
TMKeyManagementController: sort keys in the right order
mauretto78 Dec 20, 2024
09f5caa
typo
mauretto78 Dec 20, 2024
3435fed
Fix metadata cache
mauretto78 Dec 20, 2024
3e294f0
refactoring
mauretto78 Dec 23, 2024
bbd7a6f
Modify copy
piedicianni Dec 30, 2024
1a9aa75
Fix
piedicianni Dec 30, 2024
6c124ac
Fix tm order when tm prioritization change
piedicianni Dec 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions inc/validation/schema/job_keys.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"type": "object",
"additionalProperties": false,
"properties": {
"mine": {
"type": "array",
"nullable": true,
"items": {
"type": "object",
"$ref": "#/definitions/key"
}
},
"ownergroup": {
"type": "array",
"nullable": true,
"items": {
"type": "object",
"$ref": "#/definitions/key"
}
},
"anonymous": {
"type": "array",
"nullable": true,
"items": {
"type": "object",
"$ref": "#/definitions/key"
}
}
},
"required": [
"mine",
"ownergroup",
"anonymous"
],
"definitions": {
"key": {
"type": "object",
"additionalProperties": false,
"properties": {
"name": {
"type": "string"
},
"key": {
"type": "string"
},
"glos": {
"type": ["boolean", "integer", "string"]
},
"owner": {
"type": ["boolean", "integer", "string"]
},
"tm": {
"type": ["boolean", "integer", "string"]
},
"r": {
"type": ["boolean", "integer", "string"]
},
"w": {
"type": ["boolean", "integer", "string"]
},
"penalty": {
"type": "integer",
"nullable": true,
"maximum": 100,
"minimum": 0
}
},
"required": [
"name",
"key",
"glos",
"tm",
"r",
"w"
]
}
}
}
49 changes: 49 additions & 0 deletions inc/validation/schema/private_tm_key_json.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"type": "object",
"additionalProperties": false,
"properties": {
"tm_prioritization": {
"type": "boolean"
},
"keys": {
"type": "array",
"nullable": true,
"items": {
"type": "object",
"$ref": "#/definitions/key"
}
}
},
"required": [
"tm_prioritization",
"keys"
],
"definitions": {
"key": {
"type": "object",
"properties": {
"key": {
"type": "string"
},
"read": {
"type": "boolean"
},
"write": {
"type": "boolean"
},
"penalty": {
"type": "integer",
"nullable": true,
"maximum": 100,
"minimum": 0
}
},
"additionalProperties": false,
"required": [
"key",
"read",
"write"
]
}
}
}
9 changes: 9 additions & 0 deletions inc/validation/schema/project_template.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"id_team": {
"type": ["integer", "string"]
},
"tm_prioritization": {
"type": "boolean"
},
"pretranslate_100": {
"type": "boolean"
},
Expand Down Expand Up @@ -92,6 +95,12 @@
},
"w": {
"type": "boolean"
},
"penalty": {
"type": "integer",
"nullable": true,
"maximum": 100,
"minimum": 0
}
}
},
Expand Down
36 changes: 34 additions & 2 deletions lib/Controller/API/App/TmKeyManagementController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ public function getByJob(){
exit();
}

$job_keyList = json_decode( $chunk->tm_keys, true );

if(!$this->isLoggedIn()){

$tmKeys = [];
$job_keyList = json_decode( $chunk->tm_keys, true );

foreach ( $job_keyList as $jobKey ) {
$jobKey = new TmKeyManagement_ClientTmKeyStruct( $jobKey );
Expand Down Expand Up @@ -63,7 +64,7 @@ public function getByJob(){
$keys = $userKeys->getKeys( $chunk->tm_keys );

$this->response->json( [
'tm_keys' => $keys['job_keys']
'tm_keys' => $this->sortKeysInTheRightOrder($keys['job_keys'], $job_keyList)
] );
}

Expand All @@ -76,4 +77,35 @@ public function getByJob(){
private function isJobRevision($idJob, $password) {
return CatUtils::getIsRevisionFromIdJobAndPassword( $idJob, $password );
}

/**
* This function sorts the $keys array based on $job_keyList.
* $keys can contain shared and/or hidden keys
*
* @param $keys
* @param $jobKeyList
* @return mixed
*/
private function sortKeysInTheRightOrder($keys, $jobKeyList)
{
$sortedKeys = [];

foreach ($jobKeyList as $jobKey){
$filter = array_filter($keys, function ($key) use($jobKey){

if($jobKey['key'] === $key->key){
return true;
}

// compare only last 5 chars (hidden keys)
return substr($jobKey['key'], -5) === substr($key->key, -5);
});

if(!empty($filter)){
$sortedKeys[] = array_values($filter)[0];
}
}

return $sortedKeys;
}
}
95 changes: 69 additions & 26 deletions lib/Controller/API/NewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ class NewController extends ajaxController {
*/
private $private_tm_key;

/**
* @var array
*/
private $tm_prioritization = false;

private $private_tm_user = null;
private $private_tm_pass = null;

Expand Down Expand Up @@ -189,6 +194,7 @@ public function __construct() {
'options' => [ 'default' => 1, 'min_range' => 0 ]
],
'private_tm_key' => [ 'filter' => FILTER_SANITIZE_STRING, 'flags' => FILTER_FLAG_STRIP_LOW ],
'private_tm_key_json' => [ 'filter' => FILTER_SANITIZE_STRING, 'flags' => FILTER_FLAG_STRIP_LOW ],
'subject' => [
'filter' => FILTER_SANITIZE_STRING, 'flags' => FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH
],
Expand Down Expand Up @@ -246,7 +252,7 @@ public function __construct() {
* Note 2022-10-13
* ----------------------------------
*
* We trim every space private_tm_key
* We trim every space in instructions
* in order to avoid mispelling errors
*
*/
Expand Down Expand Up @@ -657,21 +663,22 @@ public function doAction() {
$projectStructure[ 'project_name' ] = $this->postInput[ 'project_name' ];
$projectStructure[ 'job_subject' ] = $this->postInput[ 'subject' ];

$projectStructure[ 'private_tm_key' ] = $this->private_tm_key;
$projectStructure[ 'private_tm_user' ] = $this->private_tm_user;
$projectStructure[ 'private_tm_pass' ] = $this->private_tm_pass;
$projectStructure[ 'uploadToken' ] = $uploadFile->getDirUploadToken();
$projectStructure[ 'array_files' ] = $arFiles; //list of file name
$projectStructure[ 'array_files_meta' ] = $arMeta; //list of file metadata
$projectStructure[ 'source_language' ] = $this->postInput[ 'source_lang' ];
$projectStructure[ 'target_language' ] = explode( ',', $this->postInput[ 'target_lang' ] );
$projectStructure[ 'mt_engine' ] = $this->postInput[ 'mt_engine' ];
$projectStructure[ 'tms_engine' ] = $this->postInput[ 'tms_engine' ];
$projectStructure[ 'status' ] = Constants_ProjectStatus::STATUS_NOT_READY_FOR_ANALYSIS;
$projectStructure[ 'owner' ] = $this->user->email;
$projectStructure[ 'metadata' ] = $this->metadata;
$projectStructure[ 'pretranslate_100' ] = (int)!!$this->postInput[ 'pretranslate_100' ]; // Force pretranslate_100 to be 0 or 1
$projectStructure[ 'pretranslate_101' ] = isset( $this->postInput[ 'pretranslate_101' ] ) ? (int)$this->postInput[ 'pretranslate_101' ] : 1;
$projectStructure[ 'private_tm_key' ] = $this->private_tm_key;
$projectStructure[ 'private_tm_user' ] = $this->private_tm_user;
$projectStructure[ 'private_tm_pass' ] = $this->private_tm_pass;
$projectStructure[ 'uploadToken' ] = $uploadFile->getDirUploadToken();
$projectStructure[ 'array_files' ] = $arFiles; //list of file name
$projectStructure[ 'array_files_meta' ] = $arMeta; //list of file metadata
$projectStructure[ 'source_language' ] = $this->postInput[ 'source_lang' ];
$projectStructure[ 'target_language' ] = explode( ',', $this->postInput[ 'target_lang' ] );
$projectStructure[ 'mt_engine' ] = $this->postInput[ 'mt_engine' ];
$projectStructure[ 'tms_engine' ] = $this->postInput[ 'tms_engine' ];
$projectStructure[ 'tm_prioritization' ] = $this->tm_prioritization;
$projectStructure[ 'status' ] = Constants_ProjectStatus::STATUS_NOT_READY_FOR_ANALYSIS;
$projectStructure[ 'owner' ] = $this->user->email;
$projectStructure[ 'metadata' ] = $this->metadata;
$projectStructure[ 'pretranslate_100' ] = (int)!!$this->postInput[ 'pretranslate_100' ]; // Force pretranslate_100 to be 0 or 1
$projectStructure[ 'pretranslate_101' ] = isset( $this->postInput[ 'pretranslate_101' ] ) ? (int)$this->postInput[ 'pretranslate_101' ] : 1;

$projectStructure[ 'dictation' ] = $this->postInput[ 'dictation' ] ?? null;
$projectStructure[ 'show_whitespace' ] = $this->postInput[ 'show_whitespace' ] ?? null;
Expand Down Expand Up @@ -1030,10 +1037,44 @@ private static function __parseTmKeyInput( $tmKeyString ) {
protected function __validateTmAndKeys() {

try {
$this->private_tm_key = array_map(
if(!empty($this->postInput[ 'private_tm_key_json' ])){
$json = html_entity_decode( $this->postInput[ 'private_tm_key_json' ] );

// first check if `filters_extraction_parameters` is a valid JSON
if ( !Utils::isJson( $json ) ) {
throw new Exception( "filters_extraction_parameters is not a valid JSON" );
}

$schema = file_get_contents( INIT::$ROOT . '/inc/validation/schema/private_tm_key_json.json' );

$validatorObject = new JSONValidatorObject();
$validatorObject->json = $json;

$validator = new JSONValidator( $schema );
$validator->validate( $validatorObject );

$privateTmKeyJsonObject = json_decode($json);

$this->tm_prioritization = $privateTmKeyJsonObject->tm_prioritization;

$this->private_tm_key = array_map(
function ($item){
return [
'key' => $item->key,
'r' => $item->read,
'w' => $item->write,
'penalty' => $item->penalty,
];
},
$privateTmKeyJsonObject->keys
);

} else {
$this->private_tm_key = array_map(
[ 'NewController', '__parseTmKeyInput' ],
explode( ",", $this->postInput[ 'private_tm_key' ] )
);
);
}
} catch ( Exception $e ) {
throw new Exception( $e->getMessage(), -6 );
}
Expand Down Expand Up @@ -1071,10 +1112,11 @@ protected function __validateTmAndKeys() {

$this->private_tm_key[ $__key_idx ] =
[
'key' => $newUser->key,
'name' => null,
'r' => $tm_key[ 'r' ],
'w' => $tm_key[ 'w' ]
'key' => $newUser->key,
'name' => null,
'penalty' => $tm_key[ 'penalty' ] ?? null,
'r' => $tm_key[ 'r' ],
'w' => $tm_key[ 'w' ],

];
$this->new_keys[] = $newUser->key;
Expand All @@ -1089,10 +1131,11 @@ protected function __validateTmAndKeys() {
$uid = $this->user->uid;

$this_tm_key = [
'key' => $tm_key[ 'key' ],
'name' => null,
'r' => $tm_key[ 'r' ],
'w' => $tm_key[ 'w' ]
'key' => $tm_key[ 'key' ],
'name' => null,
'penalty' => $tm_key[ 'penalty' ] ?? null,
'r' => $tm_key[ 'r' ],
'w' => $tm_key[ 'w' ]
];

/**
Expand Down
8 changes: 4 additions & 4 deletions lib/Controller/API/V3/MetaDataController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private function getProjectInfo( Projects_ProjectStruct $project ) {

foreach ( $project->getMetadata() as $metadatum ) {
$key = $metadatum->key;
$metadata->$key = $metadatum->getValue();
$metadata->$key = is_numeric($metadatum->getValue()) ? (int)$metadatum->getValue() : $metadatum->getValue();
}

return $metadata;
Expand All @@ -64,16 +64,16 @@ private function getJobMetaData( Jobs_JobStruct $job ) {

foreach ( $jobMetaDataDao->getByJobIdAndPassword( $job->id, $job->password, 60 * 5 ) as $metadatum ) {
$key = $metadatum->key;
$metadata->$key = $metadatum->value;
$metadata->$key = is_numeric($metadatum->value) ? (int)$metadatum->value : $metadatum->value;
}

return $metadata;
}

/**
* @param Jobs_JobStruct $job
*
* @return array
* @throws \ReflectionException
*/
private function getJobFilesMetaData( Jobs_JobStruct $job ) {

Expand All @@ -84,7 +84,7 @@ private function getJobFilesMetaData( Jobs_JobStruct $job ) {
$metadatum = new stdClass();
foreach ( $filesMetaDataDao->getByJobIdProjectAndIdFile( $job->getProject()->id, $file->id, 60 * 5 ) as $meta ) {
$key = $meta->key;
$metadatum->$key = $meta->value;
$metadatum->$key = is_numeric($meta->value) ? (int)$meta->value : $meta->value;
}

$metadataObject = new \stdClass();
Expand Down
Loading