Last Updated on July 20, 2023
ChatGPT has demonstrated its ability to handle translation tasks with surprisingly high accuracy. While Google Translate has been the go-to tool for multilingual translation for years, recent studies show that ChatGPT performs competitively with commercial translation products in high-resource European languages (Is ChatGPT A Good Translator?).
ChatGPT’s strength as a translator goes beyond just natural language text. You can also translate the code between different programming languages. Simply providing an initializing prompt describing the translation you want, ChatGPT can often generate the converted code within seconds with high accuracy.
In this post, you will learn how to use ChatGPT prompts to understand ChatGPT’s Translation Capabilities. Specifically, you will learn how to:
- Translate a poem from English to Swedish.
- Translate code from Julia to Python.
- Enhance translation results.
Get started and apply ChatGPT with my book Maximizing Productivity with ChatGPT. It provides real-world use cases and prompt examples designed to get you using ChatGPT quickly.
Let’s get started.
Overview
This post is divided into three parts; they are
- Language Translation
- Code Translation
- Tips on Effective Translation
Language Translation
Before crafting a translation prompt, you must establish an initialization prompt providing the necessary context for the translator and text style. For our current task, you are translating a poem by Robert Frost and will incorporate this information into the initialization prompt:
You are a translation helper that translates English poems into Swedish.
Without providing a poem, ChatGPT will simply acknowledge your intention:
Sure! I can help you with translating English poems into Swedish. Please provide me with the English poem you would like to have translated, and I’ll do my best to assist you.
Your translation bot is now fully prepared and has a comprehensive understanding of the context. You can proceed by requesting it to translate the poem into Swedish. You provide the poem in your second prompt:
Please convert the following poem from English to Swedish:
Nature’s first green is gold,
Her hardest hue to hold.
Her early leaf’s a flower;
But only so an hour.
Then leaf subsides to leaf.
So Eden sank to grief,
So dawn goes down to day.
Nothing gold can stay.
The following is what ChatGPT responded:
Sure! Here’s the translation of the poem “Nothing Gold Can Stay” by Robert Frost into Swedish:
Naturens första gröna är guld,
Hennes hårdaste nyans att behålla.
Hennes tidiga löv är en blomma;
Men endast så en timme.
Sedan försvinner bladet till blad.
Så sjönk Eden i sorg,
Så gryningen går ner till dagen.
Ingenting guld kan bestå.I hope this translation captures the essence of the original poem in Swedish. Let me know if there’s anything else I can assist you with!
Validation
To confirm the accuracy of the translation, you can paste the translated poem into Google Translate to get the English version. In this case, the translation is nearly flawless, which is impressive given the difficulty of translating poetry. It successfully captures both the essence and the meaning of the original poem.
Code Translation
In this section, you will ask ChatGPT to translate a simple number-guessing game from Julia into Python.
Many programming languages look similar to each other. To ensure a successful code translation, you must provide context through an initialization prompt. This prompt will introduce the code translator and specify the type of code being translated:
You are a professional software engineer that has mastered both Julia and Python programming languages. In this case, you will help me translate a simple Julia game into Python.
Similar to the previous example, ChatGPT will acknowledge your instructions and wait for the code from you:
Of course! I’d be happy to help you translate your Julia game into Python. Please provide me with the code for the game in Julia, and I’ll assist you in converting it to Python.
Now, ask ChatGPT to convert the following code into Python, using the following prompt:
Please convert the following Julia Code into Python:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
```
function number_game()
secret_number = rand(1:100) # Generate a random secret number between 1 and 100
attempts = 0
println(“Welcome to the Number Game!”)
println(“I’m thinking of a number between 1 and 100. Can you guess it?”)
while true
attempts += 1
guess = parse(Int, readline()) # Read the player’s guess from input
if guess < secret_number
println(“Too low! Try again.”)
elseif guess > secret_number
println(“Too high! Try again.”)
else
println(“Congratulations! You guessed the number in $attempts attempts!”)
break
end
end
end
# Call the number_game function to start the game
number_game()
```
The code above is deliberately quoted using three backticks following the convention of quoting codes in Markdown format. You shall see ChatGPT translated the code in response:
Sure! Here’s the equivalent Python code for the given Julia code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import random
def number_game():
secret_number = random.randint(1, 100) # Generate a random secret number between 1 and 100
attempts = 0
print(“Welcome to the Number Game!”)
print(“I’m thinking of a number between 1 and 100. Can you guess it?”)
while True:
attempts += 1
guess = int(input()) # Read the player’s guess from input
if guess < secret_number:
print(“Too low! Try again.”)
elif guess > secret_number:
print(“Too high! Try again.”)
else:
print(f“Congratulations! You guessed the number in {attempts} attempts!”)
break
# Call the number_game function to start the game
number_game()
Validation
As a language model, ChatGPT may produce some errors in the code. To verify the accuracy of the translation, you should create a Python file named number_game.py
, paste the converted code, and execute the file in the terminal, so that you let a real Python interpreter to tell you whether the code is working as expected.
In this case, ChatGPT has flawlessly translated the Julia code into Python with just a simple prompt.
Tips on Effective Translation
While it’s possible to write a simple translation prompt and obtain a result, there are several tips you can follow to enhance the quality of the translation output:
- Begin with an initialization prompt before crafting a translation request.
- Provide additional context to help the model understand the meaning behind the text. For instance, specify if the text is a poem or a technical document.
- Clearly state both the source language and target language in your prompt.
- Use simple and concise sentences in any follow-up prompts.
- Always proofread and verify the translations for accuracy.
- Ensure that your input text has correct punctuation and follows the appropriate formatting guidelines for better translation results.
Further Readings
Wenxiang Jiao, Wenxuan Wang, Jen-tse Huang, Xing Wang, and Zhaopeng Tu. “Is ChatGPT A Good Translator? Yes With GPT-4 As The Engine“, arXiv 2301.08745, 2023.
Summary
In this post, we have explored different use cases of machine translation with ChatGPT, highlighting effective strategies for achieving accurate translations. Specifically, we have discussed the following topics:
- Writing an effective initialization and translation prompt for language translation.
- Writing translation prompts for technical tasks, such as converting Julia code into Python.
- Enhancing translation results through context awareness, attention to punctuation, validation techniques, and optimizing ChatGPT
- prompts.
By implementing these techniques, you can leverage ChatGPT’s translation capabilities to their fullest potential and obtain high-quality translations for various use cases.