如何翻译<![CDATA tag in my xml file translation with open AI

问题描述 投票:0回答:0

您好,我正在尝试使用 open ai 对 xml 文件进行一些翻译。

我正在使用 PHP,我的脚本如下:

$document = file_get_contents('#####.xml');

$prompt = "translate this document into $language:\n\n" . $document;

    $response = $openai->completions()->create([
        'model' => 'text-davinci-003',
        'prompt' => $prompt,
        'temperature' => 0.5,
        'max_tokens' => 1024,
        'n' => 1,
        'stop' => '\n\n',
    ]);

    // Extract the translated text from the API response
    $translatedText = $response['choices'][0]['text'];

    // Convert the translated text back to XML or JSON format
    $translatedDocument = $translatedText; // Replace this with your own conversion code

    // Save the translated document to a file
    $filename = "########.xml";
    
    file_put_contents($filename, $translatedDocument);

这是 xml 文件内容的示例:

<?xml version="1.0" encoding="UTF-8"?>
<page text_nodes="0" filename="home" url="home" id="336760838" language="default">
  <name>Home</name>
  <tags>
    <tag id="e217a308-5401-4c08-8283-ba24340e6051:button:size" type="STRING">
      <text>
        <![CDATA[large]]>
      </text>
    </tag>
 </tags>
</page>

我只想翻译其中的内容:

 <![CDATA[large]]>. e.g large

 <![CDATA[the boy is good]]>. e.g the boy is good

具体怎么翻译呢

php translate openai-api
© www.soinside.com 2019 - 2024. All rights reserved.