L15: Applied ChatGPT for Text Analysis

Bogdan G. Popescu

John Cabot University

Applications of ChatGPT in Text Analysis

ChatGPT, accessed via the OpenAI API, is a powerful tool for automating and enhancing text analysis tasks:

  • Summarization: condense long documents to key points
  • Topic Modeling: Identify recurring themes
  • Text Classification: Categorize text into predefined groups or labels.
  • Sentiment Analysis: Determine the tone or emotional sentiment of text
  • Data Augmentation: Generate synthetic text to expand training datasets for machine learning tasks.

Setup An OpenAI Developer Account

To use GPT via the API, you need to import the os and openai Python packages.

Python
from openai import OpenAI
key_bp= "sk-proj-XXXX-Long-Code"
client = OpenAI(api_key=key_bp)

The Code Pattern for Calling GPT via the API

The code pattern to call the OpenAI API and get a chat response is as follows:

Note: do not run. This is just for demonstration purposes:

Python
response = client.chat.completions.create(
           model="MODEL_NAME",
           messages=[{"role": "system", "content": 'SPECIFY HOW THE AI ASSISTANT SHOULD BEHAVE'},
                     {"role": "user", "content": 'SPECIFY WANT YOU WANT THE AI ASSISTANT TO SAY'}
              ])

Using ChatGPT for Summarizing

One of the typical uses of ChatGBT is summarizing texts.

This can be particularly useful in the context of long customer reviews.

Python
import pandas as pd
docs = pd.read_csv("./data/ps_5reviews.csv")

Using ChatGPT for Summarizing

This how we count the number of words:

Python
# Adding a column with word counts
docs['word_count'] = docs['review_text'].str.split().apply(len)

profile_name rating rating_date title review_text word_count
0 J.F. Carroll 5.0 out of 5 stars Reviewed in the United States on May 6, 2024 5.0 out of 5 stars\nIt's a PS5, everyone wants one, was there any doubt?\n \nNobody could get their hands on this until just recently. It's a PS5, the craftmanship is awesome.Operates well, system works well with store, everything is easy, this console is a masterpiece and I keep it close. Haven't got into too many games yet. It looks sexy as heck, it looks dumb in ads but in real life it's fantastic looking. And you can buy sexy plates for it as well.Controller feels great. Considering the upgrade controller, but I haven't decided. The game choices are phenomenal. It's official: XBOX lost, buy a Playstation.Playstation all the way.\n 95
1 Brandon 5.0 out of 5 stars Reviewed in the United States on April 17, 2024 5.0 out of 5 stars\nGreat Product, Great Price, Great Games!\n \nI was looking for a great deal. Luckily, I found this product on sale for $450! I love this new PlayStation 5! I've been waiting years to get my own and now I have it! I use it vertically instead of horizontally, so I had to buy a vertical stand for an extra $20. Overall, I would recommend you buy this!\n 61
2 Brock 5.0 out of 5 stars Reviewed in the United States on May 5, 2024 5.0 out of 5 stars\nAmazing Console\n \nAfter using the playstation for over 4 months; I can say, this is one of the best consoles I’ve played on. I had an Xbox Series S and after playing on the PS5 there is a world of difference in graphics, FPS, and loading times. Also the spiderman game that comes with this model is spectacular.\n 56

Using ChatGPT for Summarizing

We might be interested in summarizing really long customer reviews,

ChatGPT can ve particularly useful for this task.

Let us say that in a first instance we want to summarize the review with the largest number of words

Using ChatGPT for Summarizing

Python
# Sort the DataFrame by the 'word_count' column in ascending order
df_sorted = docs.sort_values(by='word_count', ascending=False)

# Get the review with the largest number of words
longest_review = df_sorted.loc[df_sorted['word_count'].idxmax(), 'review_text']
longest_review

Using ChatGPT for Summarizing

Review with the largest number of words

Greetings, fellow gamers! Gather ’round as I recount my leap into the realm of PlayStation with the illustrious PS5 Bundle. As someone who’s long been immersed in the world of Xbox, I couldn’t resist the allure of Sony’s latest offering, and boy, am I glad I made the jump.Let’s kick things off with a nod to the PS5’s design. This thing is a marvel to behold, sleek and futuristic, commanding attention in my entertainment setup like a true centerpiece. And with a whopping 1TB of SSD storage, I’ve got all the room I need for my ever-expanding library of games, without having to endure those dreaded load times.Now, onto the real meat of the matter – the performance. The PS5 doesn’t pull any punches, delivering jaw-dropping 4K visuals and buttery-smooth frame rates that breathe new life into every game I play. And let’s talk about that SSD – it’s like greased lightning, ensuring I spend less time waiting and more time doing what I love: gaming.But hold onto your controllers, because the real star of the show is the DualSense. This thing is a game-changer in every sense of the word, with its immersive haptic feedback and adaptive triggers that elevate every gaming experience to new heights. And with backward compatibility, I can revisit old favorites with a fresh perspective, like catching up with old friends over a round of drinks.Of course, no review would be complete without addressing a few bumps along the road. The limited storage space is a bit of a downer, especially in today’s world of massive game downloads. And let’s not forget the price tag – while the PS5 is worth every penny in my book, it’s definitely an investment.And then there’s the hunt for the elusive console itself. Scouring the internet for availability felt like navigating a labyrinth, but let me tell you, the payoff was worth it. Sure, the PS5’s size might be a tad imposing for some setups, but once you fire up that first game, you’ll forget all about it.In conclusion, the PS5 Bundle has exceeded my wildest expectations, converting this die-hard Xbox fan into a true believer. So if you’re on the fence, take the plunge – you won’t regret it. Happy gaming, folks! .

Using ChatGPT for Summarizing

We first load the library and define our key:

Python
from openai import OpenAI
key_bp= "sk-proj-XXXX-Long-Code"
client = OpenAI(api_key=key_bp)

This is how we define our function:

Python
# Import the necessary OpenAI client library
# Make sure you have installed the OpenAI Python package: `pip install openai`

def get_completion(prompt):
    response = client.chat.completions.create(
        model="gpt-3.5-turbo-0125",
        messages=[{"role": "user", "content": prompt}],
        temperature=0,  # this is the degree of randomness of the model's output
    )
    return response.choices[0].message.content

Using ChatGPT for Summarizing

Here is an example:

Python
get_completion("Tell me a joke")
"Why couldn't the bicycle stand up by itself?\n\nBecause it was two tired!"

Let us now imagine that we want to summarize the long review

Python
# Assuming longest_review is defined and contains text
prompt_template = """
Task: Summarize the e-commerce product review below.

Instructions:

1. Create a shortened version of the review in 50 words or fewer.
2. Use first person and immitate the reviewer's tone directly.
3. Do not add any opinions, explanations, conversational tone, or new information.
4. Simply condense the main points as written by the reviewer.

Review: 
```{review_text_here}```
"""

Using ChatGPT for Summarizing

This is how we create a function o help us

Python
def summarize_review(prompt, review_text):
    # Replace placeholder in the prompt with the review text
    formatted_prompt = prompt.replace("{review_text_here}", review_text)
    
    # Generate the summary using get_completion
    summary = get_completion(formatted_prompt)
    return summary

Here is our function in action:

Python
short_longest_review = summarize_review(prompt=prompt_template, review_text=longest_review)
print(short_longest_review)
I switched from Xbox to the PS5 Bundle and it's been amazing. The design is sleek, the performance is top-notch with 4K visuals and fast load times, and the DualSense controller is a game-changer. Limited storage and price are downsides, but overall, it's worth the investment. Happy gaming!

Using ChatGPT for Summarizing

Review with the largest number of words

Greetings, fellow gamers! Gather ’round as I recount my leap into the realm of PlayStation with the illustrious PS5 Bundle. As someone who’s long been immersed in the world of Xbox, I couldn’t resist the allure of Sony’s latest offering, and boy, am I glad I made the jump.Let’s kick things off with a nod to the PS5’s design. This thing is a marvel to behold, sleek and futuristic, commanding attention in my entertainment setup like a true centerpiece. And with a whopping 1TB of SSD storage, I’ve got all the room I need for my ever-expanding library of games, without having to endure those dreaded load times.Now, onto the real meat of the matter – the performance. The PS5 doesn’t pull any punches, delivering jaw-dropping 4K visuals and buttery-smooth frame rates that breathe new life into every game I play. And let’s talk about that SSD – it’s like greased lightning, ensuring I spend less time waiting and more time doing what I love: gaming.But hold onto your controllers, because the real star of the show is the DualSense. This thing is a game-changer in every sense of the word, with its immersive haptic feedback and adaptive triggers that elevate every gaming experience to new heights. And with backward compatibility, I can revisit old favorites with a fresh perspective, like catching up with old friends over a round of drinks.Of course, no review would be complete without addressing a few bumps along the road. The limited storage space is a bit of a downer, especially in today’s world of massive game downloads. And let’s not forget the price tag – while the PS5 is worth every penny in my book, it’s definitely an investment.And then there’s the hunt for the elusive console itself. Scouring the internet for availability felt like navigating a labyrinth, but let me tell you, the payoff was worth it. Sure, the PS5’s size might be a tad imposing for some setups, but once you fire up that first game, you’ll forget all about it.In conclusion, the PS5 Bundle has exceeded my wildest expectations, converting this die-hard Xbox fan into a true believer. So if you’re on the fence, take the plunge – you won’t regret it. Happy gaming, folks! .

Using ChatGPT for Summarizing

Let’s say that we want to summarize all the reviews that have more than 100 words:

Python
count_more_than_200 = (docs["word_count"] > 200).sum()
print(count_more_than_200)
7

Using ChatGPT for Summarizing

This is how we summarize those:

Python
# Apply the function only where word_count > 200
def process_row(row):
    if row["word_count"] > 200:
        summary = summarize_review(prompt=prompt_template, review_text=row["review_text"])
        print(f"Processed row {row.name}: Summary created")
        print(summary)
        return summary
    else:
        print(f"Processed row {row.name}: No summary needed (word_count <= 200)")
        return None
      
# Apply the function to process each row
docs["summary"] = docs.apply(process_row, axis=1)

Using ChatGPT for Summarizing

These are the first 3 entries:

Python
docs2=docs.head(4)
docs2

profile_name rating rating_date title review_text word_count summary
0 J.F. Carroll 5.0 out of 5 stars Reviewed in the United States on May 6, 2024 5.0 out of 5 stars\nIt's a PS5, everyone wants one, was there any doubt?\n \nNobody could get their hands on this until just recently. It's a PS5, the craftmanship is awesome.Operates well, system works well with store, everything is easy, this console is a masterpiece and I keep it close. Haven't got into too many games yet. It looks sexy as heck, it looks dumb in ads but in real life it's fantastic looking. And you can buy sexy plates for it as well.Controller feels great. Considering the upgrade controller, but I haven't decided. The game choices are phenomenal. It's official: XBOX lost, buy a Playstation.Playstation all the way.\n 95 None
1 Brandon 5.0 out of 5 stars Reviewed in the United States on April 17, 2024 5.0 out of 5 stars\nGreat Product, Great Price, Great Games!\n \nI was looking for a great deal. Luckily, I found this product on sale for $450! I love this new PlayStation 5! I've been waiting years to get my own and now I have it! I use it vertically instead of horizontally, so I had to buy a vertical stand for an extra $20. Overall, I would recommend you buy this!\n 61 None
2 Brock 5.0 out of 5 stars Reviewed in the United States on May 5, 2024 5.0 out of 5 stars\nAmazing Console\n \nAfter using the playstation for over 4 months; I can say, this is one of the best consoles I’ve played on. I had an Xbox Series S and after playing on the PS5 there is a world of difference in graphics, FPS, and loading times. Also the spiderman game that comes with this model is spectacular.\n 56 None
3 Whispering Whiskers 5.0 out of 5 stars Reviewed in the United States on March 18, 2024 5.0 out of 5 stars\nEmbarking on a Next-Gen Adventure: An Honest Review of the PS5 Bundle\n \nGreetings, fellow gamers! Gather 'round as I recount my leap into the realm of PlayStation with the illustrious PS5 Bundle. As someone who's long been immersed in the world of Xbox, I couldn't resist the allure of Sony's latest offering, and boy, am I glad I made the jump.Let's kick things off with a nod to the PS5's design. This thing is a marvel to behold, sleek and futuristic, commanding attention in my entertainment setup like a true centerpiece. And with a whopping 1TB of SSD storage, I've got all the room I need for my ever-expanding library of games, without having to endure those dreaded load times.Now, onto the real meat of the matter – the performance. The PS5 doesn't pull any punches, delivering jaw-dropping 4K visuals and buttery-smooth frame rates that breathe new life into every game I play. And let's talk about that SSD – it's like greased lightning, ensuring I spend less time waiting and more time doing what I love: gaming.But hold onto your controllers, because the real star of the show is the DualSense. This thing is a game-changer in every sense of the word, with its immersive haptic feedback and adaptive triggers that elevate every gaming experience to new heights. And with backward compatibility, I can revisit old favorites with a fresh perspective, like catching up with old friends over a round of drinks.Of course, no review would be complete without addressing a few bumps along the road. The limited storage space is a bit of a downer, especially in today's world of massive game downloads. And let's not forget the price tag – while the PS5 is worth every penny in my book, it's definitely an investment.And then there's the hunt for the elusive console itself. Scouring the internet for availability felt like navigating a labyrinth, but let me tell you, the payoff was worth it. Sure, the PS5's size might be a tad imposing for some setups, but once you fire up that first game, you'll forget all about it.In conclusion, the PS5 Bundle has exceeded my wildest expectations, converting this die-hard Xbox fan into a true believer. So if you're on the fence, take the plunge – you won't regret it. Happy gaming, folks!\n 374 I recently switched from Xbox to the PS5 Bundle and I'm thrilled. The design is sleek, storage is ample, and performance is top-notch with 4K visuals and fast load times. The DualSense controller is a game-changer. Limited storage and high price are drawbacks, but overall, it's worth the investment. Happy gaming!
4 Carson Morby 5.0 out of 5 stars Reviewed in the United States on May 8, 2024 5.0 out of 5 stars\nI finally go one!\n \nFinally I got a PS5! I've been waiting forever, but I made the purchase because I heard NCAA Football 24 isn't coming out on PS4. Yep. I literally upgraded for that reason only :) So far it has been super easy to use. Navigation is pretty similar to the PS4 so there hasn't been much of an adjustment. The controller battery lasts wayyyyy longer than the ps4 controllers. The image is crystal clear, especially where I have it paired with a Sony TV. Can't ask for better quality than that.It runs quiet, and I literally have no complaints about the PS5. I'm excited to playing around and experiences more!\n 109 None
5 Chris 5.0 out of 5 stars Reviewed in the United States on April 17, 2024 5.0 out of 5 stars\nAwesome - Bad Packaging\n \nThe unit arrived in good condition and works as intended, but the Amazon packaging was an oversized box and some paper. I would’ve like to see the air packaging surrounding this for shipping. Amazon should know if something is so expensive they even require a one time password for delivery that maybe the expensive product should have extra care taken to make sure the box is properly insulated for rough shipping and handling. I worked for FedEx, and packages are tossed like garbage throughout the process because of the pace required to keep up with the load. They really should ensure that sensitive or expensive items are packaged securely for perfect condition arrival to the customer.5 stars for the product, but 0 stars for Amazon on the packaging. It was delivered two days ahead of schedule, so that was a nice bonus. It seems like these are stocked locally in an Amazon near you.\n 154 None

Using ChatGPT for Targeted Summaries

Let’s say that we want more targetted summaries to inform specific departments within companies.

Python
# Assuming longest_review is defined and contains text
prompt_template2 = """
Task: Summarize the e-commerce product review below, delimited by triple backticks,\
to provide feedback to the engineering department responsible for the product's design.

Instructions:

1. Create a concise summary in at most 30 words.
2. Focus specifically on aspects relevant to the product's design or engineering.
3. If the review does not mention design or engineering, respond with exactly the\
word "None" (without quotes or additional text).

Review: 
```{review_text_here}```
"""

Using ChatGPT for Targeted Summaries

Let’s do this for the first 10 entries:

Python
docs_10 = docs.head(10).copy()

And these is how we define the function to process the reviews:

Python
def process_row(row):
    formatted_prompt = prompt_template2.replace("{review_text_here}", row["review_text"])
    summary = get_completion(formatted_prompt).strip()
    return summary

Using ChatGPT for Targeted Summaries

And this is how we apply it to those entries:

Python
# Apply the function to process each row
docs_10["targeted_summary"] = docs_10.apply(process_row, axis=1)
docs_10_filtered = docs_10[docs_10["targeted_summary"] != "None"]
docs_10_filtered

Using ChatGPT for Targeted Summaries

And this is what the processed entried look like:

profile_name rating rating_date title review_text word_count summary targeted_summary
0 J.F. Carroll 5.0 out of 5 stars Reviewed in the United States on May 6, 2024 5.0 out of 5 stars\nIt's a PS5, everyone wants one, was there any doubt?\n \nNobody could get their hands on this until just recently. It's a PS5, the craftmanship is awesome.Operates well, system works well with store, everything is easy, this console is a masterpiece and I keep it close. Haven't got into too many games yet. It looks sexy as heck, it looks dumb in ads but in real life it's fantastic looking. And you can buy sexy plates for it as well.Controller feels great. Considering the upgrade controller, but I haven't decided. The game choices are phenomenal. It's official: XBOX lost, buy a Playstation.Playstation all the way.\n 95 None The review praises the PS5's craftsmanship, ease of use, and game choices. Positive feedback on controller and aesthetics. Suggests considering an upgraded controller. No negative comments on design or engineering.
1 Brandon 5.0 out of 5 stars Reviewed in the United States on April 17, 2024 5.0 out of 5 stars\nGreat Product, Great Price, Great Games!\n \nI was looking for a great deal. Luckily, I found this product on sale for $450! I love this new PlayStation 5! I've been waiting years to get my own and now I have it! I use it vertically instead of horizontally, so I had to buy a vertical stand for an extra $20. Overall, I would recommend you buy this!\n 61 None The customer is satisfied with the PlayStation 5 purchase but had to buy an additional vertical stand. Consider including a built-in stand in future designs.

Using ChatGPT for Inferring

ChatGPT OpenAI can also be used for some inferrence task:

Examples:

  • deciding on the sentiment of a text
  • extracting labels
  • extracting names or locations

Using ChatGPT for Inferring

1. Sentiment Analysis

Here is how we can infer sentiment.

Python
review = """
The PS5 is overpriced, has limited storage, poor backward compatibility,\ 
and lacks exclusive games to justify its hype.
"""

This is how we write a prompt to evaluate sentiment:

Python
prompt = f"""
What is the sentiment of the following product review, which is delimited\
with triple backticks?

Review: 
```{review}```
"""

Using ChatGPT for Inferring

1. Sentiment Analysis

And this is the reponse

Python
response = get_completion(prompt)
print(response)
The sentiment of the review is negative.

Using ChatGPT for Inferring

1. Sentiment Analysis

We can make the prompt more concise

Python
prompt = f"""
What is the sentiment of the following product review, which is delimited with triple backticks?

Give the answer as a single word: "positive", "negative", or "neutral"

Review: 
```{review}```
"""

And this is the reponse:

Python
response = get_completion(prompt)
print(response)
negative

Using ChatGPT for Inferring

2. Emotion Analysis

We might also be interested in identifying emotions in text.

Python
text = """
Needed a nice lamp for my bedroom, and this one had \
additional storage and not too high of a price point. \
Got it fast.  The string to our lamp broke during the \
transit and the company happily sent over a new one. \
Came within a few days as well. It was easy to put \
together.  I had a missing part, so I contacted their \
support and they very quickly got me the missing piece! \
Lumina seems to me to be a great company that cares \
about their customers and products!!
"""

Using ChatGPT for Inferring

2. Emotion Analysis

This is what our prompt could look like:

Python
prompt = f"""
Identify a list of emotions that the writer of the \
following review is expressing. Include no more than \
five items in the list. Format your answer as a list of \
lower-case words separated by commas.

Review text: '''{text}'''
"""

Using ChatGPT for Inferring

2. Emotion Analysis

An this is the response:

Python
response = get_completion(prompt)
print(response)
happy, satisfied, grateful, impressed, content

Using ChatGPT for Inferring

3. Detailed Emotion Analysis

We can also obtain more information related to emotions from text.

Python
text = """
Needed a nice lamp for my bedroom, and this one had \
additional storage and not too high of a price point. \
Got it fast.  The string to our lamp broke during the \
transit and the company happily sent over a new one. \
Came within a few days as well. It was easy to put \
together.  I had a missing part, so I contacted their \
support and they very quickly got me the missing piece! \
Lumina seems to me to be a great company that cares \
about their customers and products!!
"""

Using ChatGPT for Inferring

3. Detailed Emotion Analysis

This is what our prompt could look like:

Python
prompt = f"""
Identify the following items from the review text: 
- Item purchased by reviewer
- Company that made the item

The review is delimited with triple backticks. \
Format your response as a JSON object with \
"Item" and "Brand" as the keys. 
If the information isn't present, use "unknown" \
as the value.
Make your response as short as possible.
  
Review text: '''{text}'''
"""

Using ChatGPT for Inferring

3. Detailed Emotion Analysis

This is what our response looks like:

Python
response = get_completion(prompt)
print(response)
{
  "Item": "lamp",
  "Brand": "Lumina"
}

Using ChatGPT for Inferring

3. Detailed Emotion Analysis

We can thus perform multiple tasks within the same prompt:

Python
prompt = f"""
Identify the following items from the review text: 
- Sentiment (positive or negative)
- Is the reviewer expressing anger? (true or false)
- Item purchased by reviewer
- Company that made the item

The review is delimited with triple backticks. \
Format your response as a JSON object with \
"Sentiment", "Anger", "Item" and "Brand" as the keys.
If the information isn't present, use "unknown" \
as the value.
Make your response as short as possible.
Format the Anger value as a boolean.

Review text: '''{text}'''
"""

Using ChatGPT for Inferring

3. Detailed Emotion Analysis

And this is the response:

Python
response = get_completion(prompt)
print(response)
{
    "Sentiment": "positive",
    "Anger": false,
    "Item": "lamp",
    "Brand": "Lumina"
}

Using ChatGPT for Inferring

4. Topics

Similar to LDA and word-embeddings, we can easily identify topics within a text.

Speech

Python
speech = """I am encouraged by the honourable Gentleman’s anger. His intervention bears no relationship\
to the reality in Britain or to reality for our European Union partners. His comment was important.\ 
I hope that it will be well circulated to his constituents. It is clear that this whole process is \
in a state of disarray. I welcomed the new Secretary of State for Transport. His main challenge is \
to persuade a number of his colleagues in the Cabinet that the whole process should be dumped. I want\
to finish on two points that sum up this fiasco. I am sure that the honourable Member for Ross,\ 
Cromarty and Skye will deal with the issue of sleeper services to Scotland. The Government and\
the British Railways Board were dragged into the courts in Scotland to be exposed for what they\
had shown to Scots - utter contempt for their rail network. They thought that, through stealth\ 
and the incompetence of the director of franchising, Mr. Roger Salmon, they could close services\ 
under the guise of consultation on a draft passenger service requirement. They were found out.\
Is it not a disgrace that railway policy is now going to be partly dictated in the courts of\
England and Scotland? That is a fiasco and the Government know it. The second issue is trans-European\
networks. It is an important issue for Britain because it involves integrating the major routes in Britain\
with those in Europe. It is absolutely right that that should be done. What did Ministers do\
when they went to Brussels? They sabotaged the proposals. We find that the European Union is\
giving no priority to schemes. It has thrown out environmental impact assessments and of course\
no cash is being provided by the European Union for such developments..
"""

Using ChatGPT for Inferring

4. Topics

This is what our prompt looks like:

Python
prompt = f"""
Determine five topics that are being discussed in the \
following text, which is delimited by triple backticks.

Make each item one or two words long. 

Format your response as a list of uncounted items separated by commas.

Text sample: 
'''{speech}'''
"""

Using ChatGPT for Inferring

4. Topics

This is what our response looks like:

Python
response = get_completion(prompt)
print(response)
Brexit, Transportation, European Union, Railway Policy, European Networks

This is how we make a list out of those topics:

Python
response.split(sep=',')
['Brexit', ' Transportation', ' European Union', ' Railway Policy', ' European Networks']

Using ChatGPT for Inferring

5. Topics using Zero-Shot Learning

We might also be interested in identifying whether certain topics are present within texts.

Python
topic_list = ["brexit", "UK-EU relations", "rails", "transport", "aliens"]
Python
prompt = f"""
Determine whether each item in the following list of \
topics is a topic in the text below, which
is delimited with triple backticks.

Format your response as a JSON object with \
the topics as the keys.

Give your answer as follows:
item from the list: 0 or 1

List of topics: {", ".join(topic_list)}

Text sample: '''{speech}'''
"""

Using ChatGPT for Inferring

5. Topics using Zero-Shot Learning

And this is the response:

Python
response = get_completion(prompt)
print(response)
{
    "brexit": 0,
    "UK-EU relations": 0,
    "rails": 1,
    "transport": 1,
    "aliens": 0
}

Using ChatGPT for Inferring

6. Language

We can also use ChatGPT to translate.

Python
text_to_translate = "I love what I can do with ChatGPT OpenAI."
Python
prompt = f"""
Translate the following English text to Italian which
is delimited with triple backticks.:\

Text sample: '''{text_to_translate}'''
"""

Using ChatGPT for Inferring

6. Language

And this is what the response could look like:

Python
response = get_completion(prompt)
print(response)
'''Amo ciò che posso fare con ChatGPT OpenAI.'''

Using ChatGPT for Inferring

6. Language

We can also use ChatGPT to translate to multiple languages.

Python
text_to_translate = "I love what I can do with ChatGPT OpenAI."

This is what the prompt looks like:

Python
prompt = f"""
Translate the following English text to Italian and Spanish, which
is delimited with triple backticks.:\

Format your response as a JSON object with \
the languages as the keys.

Text sample: '''{text_to_translate}'''
"""
response = get_completion(prompt)
print(response)
{
  "Italian": {
    "text": "Amo ciò che posso fare con ChatGPT OpenAI."
  },
  "Spanish": {
    "text": "Amo lo que puedo hacer con ChatGPT OpenAI."
  }
}

Using ChatGPT for Inferring

7. Grammar and Spelling

We can also use ChatGPT to proofread and fix grammar mistakes.

Python
text_to_fix = """
This is a mock short paragraf to see how ChatGPT performs when it comes to\ 
grammer and spelling. These is a wrong sentence. Speling is of.
"""
Python
prompt = f"""
Proofread and correct the following text, which
is delimited with triple backticks.

Rewrite the corrected version a simple string.

If you don't find and errors, leave as such.

```{text_to_fix}```
"""
response = get_completion(prompt)
print(response)
This is a mock short paragraph to see how ChatGPT performs when it comes to grammar and spelling. There is a wrong sentence. Spelling is off.

Using ChatGPT for Inferring

7. Grammar and Spelling

This is how we can see better the fixes:

Python
import markdown
from IPython.display import display, HTML
from redlines import Redlines
diff = Redlines(text_to_fix, response)
html_obj = markdown.markdown(diff.output_markdown).strip('"')
HTML(html_obj)

This is a mock short paragraf paragraph to see how ChatGPT performs when it comes to\ ¶ grammer to grammar and spelling. These There is a wrong sentence. Speling Spelling is of.off.

Conclusion

  • Versatility: ChatGPT can be applied across diverse text analysis tasks.
  • Automation: Streamlines labor-intensive processes with efficient solutions.
  • Customizability: Flexible prompts cater to specific analytical needs.
  • Advanced Insights: Facilitates in-depth tasks like sentiment, topic, and emotion analysis.

Final Thoughts:

  • Experiment with prompts to optimize results.
  • Leverage ChatGPT’s strengths for text-heavy projects.
  • Balance automation with critical human judgment.