Making WordPress.org


Ignore:
Timestamp:
05/09/2023 08:55:27 AM (3 years ago)
Author:
amieiro
Message:

Translate: Do not use the OpenAI and the DeepL suggestions if the TM has a translation with 100% of accuracy

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/routes/class-translation-memory.php

    r12527 r12579  
    1111class Translation_Memory extends GP_Route {
    1212
     13        /**
     14         * Get the suggestions from the translation memory and return a JSON template.
     15         *
     16         * @param string $project_path Project path.
     17         * @param string $locale_slug  Locale slug.
     18         * @param string $set_slug     Set slug.
     19         *
     20         * @return void
     21         */
    1322        public function get_suggestions( $project_path, $locale_slug, $set_slug ) {
    14                 $type                                  = 'Translation';
    15                 $original_id                           = gp_get( 'original' );
    16                 $translation_id                        = gp_get( 'translation', 0 );
    17                 $nonce                                 = gp_get( 'nonce' );
    18                 $gp_default_sort                       = get_user_option( 'gp_default_sort' );
    19                 $external_services_exclude_some_status = gp_array_get( $gp_default_sort, 'external_services_exclude_some_status', 0 );
    20                 $translation                           = null;
    21                 $openai_suggestions                    = array();
    22                 $deepl_suggestions                     = array();
     23                $type        = 'Translation';
     24                $original_id = gp_get( 'original' );
     25                $nonce       = gp_get( 'nonce' );
    2326
    2427                if ( ! wp_verify_nonce( $nonce, 'translation-memory-suggestions-' . $original_id ) ) {
     
    5558        }
    5659
     60        /**
     61         * Get a suggestion from OpenAI and return a JSON template.
     62         *
     63         * @param string $project_path Project path.
     64         * @param string $locale_slug  Locale slug.
     65         * @param string $set_slug     Set slug.
     66         *
     67         * @return void
     68         */
    5769        public function get_openai_suggestions( $project_path, $locale_slug, $set_slug ) {
    5870                $type                                  = 'OpenAI';
     
    8799                        }
    88100                        if ( ! $translation || ( 'current' != $translation->status && 'rejected' != $translation->status && 'old' != $translation->status ) ) {
    89                                 $suggestions = $this->get_openai_suggestion( $original->singular, $locale_slug, $locale_glossary );
     101                                $suggestions = $this->get_openai_suggestion( $original->singular, $locale_slug, $locale_glossary, $current_set_slug );
    90102                        }
    91103                } else {
    92                         $suggestions = $this->get_openai_suggestion( $original->singular, $locale_slug, $locale_glossary );
     104                        $suggestions = $this->get_openai_suggestion( $original->singular, $locale_slug, $locale_glossary, $current_set_slug );
    93105                }
    94106
     
    102114        }
    103115
     116        /**
     117         * Get a suggestion from DeepL and return a JSON template.
     118         *
     119         * @param string $project_path Project path.
     120         * @param string $locale_slug  Locale slug.
     121         * @param string $set_slug     Set slug.
     122         *
     123         * @return void
     124         */
    104125        public function get_deepl_suggestions( $project_path, $locale_slug, $set_slug ) {
    105126                $type                                  = 'DeepL';
     
    156177         * @param string       $locale            The locale.
    157178         * @param \GP_Glossary $locale_glossary   The glossary for the locale.
     179         * @param string       $set_slug          The set slug.
    158180         *
    159181         * @return array
    160182         */
    161         private function get_openai_suggestion( $original_singular, $locale, $locale_glossary ): array {
     183        private function get_openai_suggestion( $original_singular, $locale, $locale_glossary, string $set_slug ): array {
    162184                $openai_query    = '';
    163185                $glossary_query  = '';
     
    165187                $openai_key      = gp_array_get( $gp_default_sort, 'openai_api_key' );
    166188                if ( empty( trim( $openai_key ) ) ) {
     189                        return array();
     190                }
     191                if ( $this->is_TM_translation_100_accurate( $original_singular, $locale, $set_slug ) ) {
    167192                        return array();
    168193                }
     
    242267        }
    243268
    244         /**
    245          * Updates the number of tokens used by OpenAI.
    246          *
    247          * @param int $tokens_used The number of tokens used.
    248          */
    249         private function update_openai_tokens_used( int $tokens_used ) {
    250                 $gp_external_translations = get_user_option( 'gp_external_translations' );
    251                 $openai_tokens_used       = gp_array_get( $gp_external_translations, 'openai_tokens_used' );
    252                 if ( ! is_int( $openai_tokens_used ) || $openai_tokens_used < 0 ) {
    253                         $openai_tokens_used = 0;
    254                 }
    255                 $openai_tokens_used                            += $tokens_used;
    256                 $gp_external_translations['openai_tokens_used'] = $openai_tokens_used;
    257                 update_user_option( get_current_user_id(), 'gp_external_translations', $gp_external_translations );
    258         }
    259269
    260270        /**
     
    276286                $target_lang = $this->get_deepl_locale( $locale );
    277287                if ( empty( $target_lang ) ) {
     288                        return array();
     289                }
     290                if ( $this->is_TM_translation_100_accurate( $original_singular, $locale, $set_slug ) ) {
    278291                        return array();
    279292                }
     
    303316                $this->update_deepl_chars_used( $original_singular );
    304317                return $response;
     318        }
     319
     320        /**
     321         * Indicate whether there is a translation in the translation memory with full similarity (100%).
     322         *
     323         * @param string $project_path The project path.
     324         * @param string $locale       The locale.
     325         * @param string $set_slug     The set slug.
     326         *
     327         * @return bool
     328         */
     329        private function is_TM_translation_100_accurate( $project_path, $locale, $set_slug ): bool {
     330                $original_id = gp_get( 'original' );
     331                if ( empty( $original_id ) ) {
     332                        return false;
     333                }
     334
     335                $original = GP::$original->get( $original_id );
     336                if ( ! $original ) {
     337                        return false;
     338                }
     339
     340                $suggestions = Translation_Memory_Client::query( $original->singular, $locale );
     341                if ( is_wp_error( $suggestions ) ) {
     342                        return false;
     343                }
     344
     345                foreach ( $suggestions as $suggestion ) {
     346                        if ( 1 == $suggestion['similarity_score'] ) {
     347                                return true;
     348                        }
     349                }
     350
     351                return false;
     352        }
     353
     354        /**
     355         * Updates the number of tokens used by OpenAI.
     356         *
     357         * @param int $tokens_used The number of tokens used.
     358         */
     359        private function update_openai_tokens_used( int $tokens_used ) {
     360                $gp_external_translations = get_user_option( 'gp_external_translations' );
     361                $openai_tokens_used       = gp_array_get( $gp_external_translations, 'openai_tokens_used' );
     362                if ( ! is_int( $openai_tokens_used ) || $openai_tokens_used < 0 ) {
     363                        $openai_tokens_used = 0;
     364                }
     365                $openai_tokens_used                            += $tokens_used;
     366                $gp_external_translations['openai_tokens_used'] = $openai_tokens_used;
     367                update_user_option( get_current_user_id(), 'gp_external_translations', $gp_external_translations );
    305368        }
    306369
Note: See TracChangeset for help on using the changeset viewer.