<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[12579] sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/routes/class-translation-memory.php: Translate: Do not use the OpenAI and the DeepL suggestions if the TM has a translation with 100% of accuracy</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://meta.trac.wordpress.org/changeset/12579">12579</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/12579","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>amieiro</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-05-09 08:55:27 +0000 (Tue, 09 May 2023)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Translate: Do not use the OpenAI and the DeepL suggestions if the TM has a translation with 100% of accuracy</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionsincroutesclasstranslationmemoryphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/routes/class-translation-memory.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporggptranslationsuggestionsincroutesclasstranslationmemoryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/routes/class-translation-memory.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/routes/class-translation-memory.php 2023-05-08 05:46:57 UTC (rev 12578)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/routes/class-translation-memory.php   2023-05-09 08:55:27 UTC (rev 12579)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -10,16 +10,19 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> class Translation_Memory extends GP_Route {
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * Get the suggestions from the translation memory and return a JSON template.
+        *
+        * @param string $project_path Project path.
+        * @param string $locale_slug  Locale slug.
+        * @param string $set_slug     Set slug.
+        *
+        * @return void
+        */
</ins><span class="cx" style="display: block; padding: 0 10px">         public function get_suggestions( $project_path, $locale_slug, $set_slug ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $type                                  = 'Translation';
-               $original_id                           = gp_get( 'original' );
-               $translation_id                        = gp_get( 'translation', 0 );
-               $nonce                                 = gp_get( 'nonce' );
-               $gp_default_sort                       = get_user_option( 'gp_default_sort' );
-               $external_services_exclude_some_status = gp_array_get( $gp_default_sort, 'external_services_exclude_some_status', 0 );
-               $translation                           = null;
-               $openai_suggestions                    = array();
-               $deepl_suggestions                     = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $type        = 'Translation';
+               $original_id = gp_get( 'original' );
+               $nonce       = gp_get( 'nonce' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! wp_verify_nonce( $nonce, 'translation-memory-suggestions-' . $original_id ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        wp_send_json_error( 'invalid_nonce' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -54,6 +57,15 @@
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * Get a suggestion from OpenAI and return a JSON template.
+        *
+        * @param string $project_path Project path.
+        * @param string $locale_slug  Locale slug.
+        * @param string $set_slug     Set slug.
+        *
+        * @return void
+        */
</ins><span class="cx" style="display: block; padding: 0 10px">         public function get_openai_suggestions( $project_path, $locale_slug, $set_slug ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $type                                  = 'OpenAI';
</span><span class="cx" style="display: block; padding: 0 10px">                $original_id                           = gp_get( 'original' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -86,10 +98,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                $translation = GP::$translation->get( $translation_id );
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( ! $translation || ( 'current' != $translation->status && 'rejected' != $translation->status && 'old' != $translation->status ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $suggestions = $this->get_openai_suggestion( $original->singular, $locale_slug, $locale_glossary );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $suggestions = $this->get_openai_suggestion( $original->singular, $locale_slug, $locale_glossary, $current_set_slug );
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px">                } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $suggestions = $this->get_openai_suggestion( $original->singular, $locale_slug, $locale_glossary );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $suggestions = $this->get_openai_suggestion( $original->singular, $locale_slug, $locale_glossary, $current_set_slug );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                wp_send_json_success(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -101,6 +113,15 @@
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * Get a suggestion from DeepL and return a JSON template.
+        *
+        * @param string $project_path Project path.
+        * @param string $locale_slug  Locale slug.
+        * @param string $set_slug     Set slug.
+        *
+        * @return void
+        */
</ins><span class="cx" style="display: block; padding: 0 10px">         public function get_deepl_suggestions( $project_path, $locale_slug, $set_slug ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $type                                  = 'DeepL';
</span><span class="cx" style="display: block; padding: 0 10px">                $original_id                           = gp_get( 'original' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -155,10 +176,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @param string       $original_singular The singular from the original string.
</span><span class="cx" style="display: block; padding: 0 10px">         * @param string       $locale            The locale.
</span><span class="cx" style="display: block; padding: 0 10px">         * @param \GP_Glossary $locale_glossary   The glossary for the locale.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @param string       $set_slug          The set slug.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @return array
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        private function get_openai_suggestion( $original_singular, $locale, $locale_glossary ): array {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ private function get_openai_suggestion( $original_singular, $locale, $locale_glossary, string $set_slug ): array {
</ins><span class="cx" style="display: block; padding: 0 10px">                 $openai_query    = '';
</span><span class="cx" style="display: block; padding: 0 10px">                $glossary_query  = '';
</span><span class="cx" style="display: block; padding: 0 10px">                $gp_default_sort = get_user_option( 'gp_default_sort' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -166,6 +188,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                if ( empty( trim( $openai_key ) ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return array();
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( $this->is_TM_translation_100_accurate( $original_singular, $locale, $set_slug ) ) {
+                       return array();
+               }
</ins><span class="cx" style="display: block; padding: 0 10px">                 $openai_prompt      = gp_array_get( $gp_default_sort, 'openai_custom_prompt' );
</span><span class="cx" style="display: block; padding: 0 10px">                $openai_temperature = gp_array_get( $gp_default_sort, 'openai_temperature', 0 );
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! is_float( $openai_temperature ) || $openai_temperature < 0 || $openai_temperature > 2 ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -241,21 +266,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return $response;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        /**
-        * Updates the number of tokens used by OpenAI.
-        *
-        * @param int $tokens_used The number of tokens used.
-        */
-       private function update_openai_tokens_used( int $tokens_used ) {
-               $gp_external_translations = get_user_option( 'gp_external_translations' );
-               $openai_tokens_used       = gp_array_get( $gp_external_translations, 'openai_tokens_used' );
-               if ( ! is_int( $openai_tokens_used ) || $openai_tokens_used < 0 ) {
-                       $openai_tokens_used = 0;
-               }
-               $openai_tokens_used                            += $tokens_used;
-               $gp_external_translations['openai_tokens_used'] = $openai_tokens_used;
-               update_user_option( get_current_user_id(), 'gp_external_translations', $gp_external_translations );
-       }
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Gets a translation suggestion from DeepL.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -277,6 +287,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                if ( empty( $target_lang ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return array();
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( $this->is_TM_translation_100_accurate( $original_singular, $locale, $set_slug ) ) {
+                       return array();
+               }
</ins><span class="cx" style="display: block; padding: 0 10px">                 $deepl_response = wp_remote_post(
</span><span class="cx" style="display: block; padding: 0 10px">                        $free_url,
</span><span class="cx" style="display: block; padding: 0 10px">                        array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -305,6 +318,56 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Indicate whether there is a translation in the translation memory with full similarity (100%).
+        *
+        * @param string $project_path The project path.
+        * @param string $locale       The locale.
+        * @param string $set_slug     The set slug.
+        *
+        * @return bool
+        */
+       private function is_TM_translation_100_accurate( $project_path, $locale, $set_slug ): bool {
+               $original_id = gp_get( 'original' );
+               if ( empty( $original_id ) ) {
+                       return false;
+               }
+
+               $original = GP::$original->get( $original_id );
+               if ( ! $original ) {
+                       return false;
+               }
+
+               $suggestions = Translation_Memory_Client::query( $original->singular, $locale );
+               if ( is_wp_error( $suggestions ) ) {
+                       return false;
+               }
+
+               foreach ( $suggestions as $suggestion ) {
+                       if ( 1 == $suggestion['similarity_score'] ) {
+                               return true;
+                       }
+               }
+
+               return false;
+       }
+
+       /**
+        * Updates the number of tokens used by OpenAI.
+        *
+        * @param int $tokens_used The number of tokens used.
+        */
+       private function update_openai_tokens_used( int $tokens_used ) {
+               $gp_external_translations = get_user_option( 'gp_external_translations' );
+               $openai_tokens_used       = gp_array_get( $gp_external_translations, 'openai_tokens_used' );
+               if ( ! is_int( $openai_tokens_used ) || $openai_tokens_used < 0 ) {
+                       $openai_tokens_used = 0;
+               }
+               $openai_tokens_used                            += $tokens_used;
+               $gp_external_translations['openai_tokens_used'] = $openai_tokens_used;
+               update_user_option( get_current_user_id(), 'gp_external_translations', $gp_external_translations );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Updates the number of characters used by DeepL.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param string $original_singular The singular from the original string.
</span></span></pre>
</div>
</div>

</body>
</html>