Prompt Engineering | Text conversion prompt

LLM is very good at converting input into different formats, such as multilingual text translation, spelling and grammar correction, tone adjustment, format conversion, etc.

Article directory

  • 1. Text translation
    • 1.1, Chinese to Spanish
    • 1.2. Language recognition
    • 1.3. Multilingual translation
    • 1.4. Translation + formal tone
    • 1.4. Universal translator
  • 2. Adjust tone/style
  • 3. Format conversion
  • 4. Correct spelling and grammar
  • 5. A comprehensive example: text translation + spelling correction + style adjustment + format conversion

1. Text translation

1.1, Chinese to Spanish

prompt = f"""
Translate the following Chinese into Spanish: \
```Hello, I would like to order a blender. ```
"""

output:
Hola, me gustaría ordenar una batidora.

1.2, Language recognition

prompt = f"""
Please tell me what language the following text is in:
```Combien co?te le lampadaire?```
"""

output:
This is French.

1.3, multilingual translation

prompt = f"""
Please translate the following texts into Chinese, English, French and Spanish respectively:
```I want to order a basketball.```
"""

output:
English: I want to order a basketball.
English: I want to order a basketball.
French: Je veux commander un ballon de basket.
Spanish: Quiero pedir una pelota de baloncesto.

1.4, translation + formal tone

prompt = f"""
Please translate the following text into Chinese, showing both formal and informal tone:
```Would you like to order a pillow?```
"""

output:
Formal tone: Do you need to order pillows?
Informal tone: Would you like to order a pillow?

1.4, Universal Translator

  • With the development of globalization and cross-border business, communication users may come from different countries and use different languages. Therefore, we need a universal translator to identify the language of each message and translate it into the native language of the target user, so as to realize More convenient cross-border communication.
user_messages = [
  "La performance du système est plus lente que d'habitude.", # System performance is slower than normal
  "Mi monitor tiene píxeles que no se iluminan.", # My monitor has pixels that are not lighting
  "Il mio mouse non funziona", # My mouse is not working
  "Mój klawisz Ctrl jest zepsuty", # My keyboard has a broken control key
  "My screen is flashing" # My screen is flashing
]

for issue in user_messages:
    prompt = f"Tell me what language the following text is, and output the language directly, such as French, without outputting punctuation marks: ```{<!-- -->issue}```"
    lang = chatgpt(prompt)
    print(f"Original message ({<!-- -->lang}): {<!-- -->issue}\\
")

    prompt = f"""
    Translate the following messages into English and Chinese respectively, and write them as
    Chinese translation: xxx
    English translation: yyy
    of the format:
    ```{<!-- -->issue}```
    """
    response = chatgpt(prompt)
    print(response, "\\
============================================== ")

Output:

Original message (French): La performance du système est plus lente que d'habitude.

Chinese translation: System performance is slower than usual.
English translation: The system performance is slower than usual.
============================================
Original message (Spanish): Mi monitor tiene píxeles que no se iluminan.

Chinese translation: Some pixels of my monitor are not lit.
English translation: My monitor has pixels that don't light up.
============================================
Original message (Italian): Il mio mouse non funziona

Chinese translation: My mouse doesn't work anymore.
English translation: My mouse is not working.
============================================
Original message (Polish): Mój klawisz Ctrl jest zepsuty

Chinese translation: My Ctrl key is broken
English translation: My Ctrl key is broken.
============================================
Original message (in Chinese): My screen is flickering

Chinese translation: My screen is flickering.
English translation: My screen is flickering.
============================================

2. Tone/style adjustment

The tone of writing tends to vary depending on the audience.

prompt = f"""
Translate the following text into the format of a business letter:
```Little brother, my lamb, last time you said how many inches are the monitors that our department wants to purchase? ```
"""

output:
Dear XXX (recipient's name):

Hello! I'm XXX (sender's name) and I have a question for you. The last time we spoke, you mentioned that our department needed to purchase monitors, but I forgot what size you needed in inches. I hope you can get back to me so that we can procure the required equipment in time.

thank you for your help!

Sincerely,

salute

XXX (sender's name)

3. Format conversion

ChatGPT is very good at converting between different formats, such as JSON to HTML, XML, Markdown, etc.

json to html:

data_json = {<!-- --> "resturant employees" :[
    {<!-- -->"name":"Shyam", "email":"[email protected]"},
    {<!-- -->"name":"Bob", "email":"[email protected]"},
    {<!-- -->"name":"Jai", "email":"[email protected]"}
]}
prompt = f"""
Convert the following Python dictionary from JSON to an HTML table, preserving table headers and column names: {<!-- -->data_json}
"""


output:

<table>
  <caption>restaurant employees</caption>
  <thead>
    <tr>
      <th>name</th>
      <th>email</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Shyam</td>
      <td>[email protected]</td>
    </tr>
    <tr>
      <td>Bob</td>
      <td>[email protected]</td>
    </tr>
    <tr>
      <td>Jai</td>
      <td>[email protected]</td>
    </tr>
  </tbody>
</table>

4. Spelling and grammar correction

An example is given below. There is a list of sentences, some of which have spelling or grammatical problems, and some do not. We loop through each sentence and ask the model to proofread the text. If it is correct, it will output “no errors found”, if Errors output the corrected text.

text = [
  "The girl with the black and white puppies have a ball.", # The girl has a ball.
  "Yolanda has her notebook.", # ok
  "Its going to be a long day. Does the car need it’s oil changed?", # Homonyms
  "Their goes my freedom. There going to bring they’re suitcases.", # Homonyms
  "Your going to need you’re notebook.", # Homonyms
  "That medicine effects my ability to sleep. Have you heard of the butterfly affect?", # Homonyms
  "This phrase is to cherck chatGPT for spelling abilitty" # spelling
]
for i in range(len(text)):
    prompt = f"""Please proofread and correct the following text, note that the corrected text remains in the original language, and there is no need to output the original text.
    Say "No errors found" if you don't find any errors.
    
    For example:
    Enter: I are happy.
    Output: I am happy.
    ```{<!-- -->text[i]}```"""
    response = chatgpt(prompt)
    print(i, response)


output:
0 The girl with the black and white puppies has a ball.
1 No errors were found.
2 It's going to be a long day. Does the car need its oil changed?
3 Their goes my freedom. They're going to bring their suitcases.
4 Output: You're going to need your notebook.
5 That medicine affects my ability to sleep. Have you heard of the butterfly effect?
6 This phrase is to check chatGPT for spelling ability.

5. A comprehensive example: text translation + spelling correction + style adjustment + format conversion

text = f"""
Got this for my daughter for her birthday cuz she keeps taking \
mine from my room. Yes, adults also like pandas too. She takes \
it everywhere with her, and it's super soft and cute. One of the \
ears is a bit lower than the other, and I don't think that was \
designed to be asymmetrical. It's a bit small for what I paid for it \
though. I think there might be other options that are bigger for \
the same price. It arrived a day earlier than expected, so I got \
to play with it myself before I gave it to my daughter.
"""
prompt = f"""
For the comment text in English between the following three backticks,
Correct spelling and grammar first.
Then convert it into Chinese,
Then transform it into a high-quality Taobao review style, explain the advantages and disadvantages of the product from various angles, and make a summary.
Polish up the description to make the review more engaging.
The output format is:
【Advantages】xxx
【Disadvantages】xxx
【Summary】xxx
Note that you only need to fill in the xxx part and output it in sections.
Output the result in Markdown format.
```{<!-- -->text}```
"""

output:
[Advantages] - Super soft and cute, very popular as a birthday gift for daughters. - Adults love pandas too, and I love it too. - Arrived a day early to give me time to play with it.
[Disadvantages] - One ear is lower than the other, asymmetrical. - The price is a bit expensive, but the size is a bit small, there may be larger options at the same price.
[Summary] This panda toy is very suitable as a birthday gift. It is soft and cute, and is loved by children. While the price is a bit pricey, the size is a bit small and the asymmetrical design is a bit of a let down. If you want a larger, similarly priced option, you may want to consider other options. Overall, this is a good panda toy, worth buying.

?
?
?
?
?
?
Reference link:
[1] OpenAI
[2] Teacher Wu Enda’s: DeepLearning.AI
[3] DataWhale
[4] https://learn.deeplearning.ai/