Natural Language Generation Explained & 2 How To Tutorials In Python

by | Sep 26, 2023 | Artificial Intelligence, Machine Learning, Natural Language Processing

What is natural language generation?

Natural Language Generation (NLG) is a subfield of artificial intelligence (AI) and natural language processing (NLP) that focuses on the automatic generation of natural language text or speech from structured data or other forms of non-linguistic input. NLG systems use algorithms and linguistic rules to convert data into coherent, human-readable text that sounds as if it were written or spoken by a human.

What are the key components of natural language generation systems?

  1. Data Input: NLG systems take structured or semi-structured data as input. This data can come from various sources, such as databases, spreadsheets, or APIs.
  2. Natural Language Processing: NLG systems use NLP techniques to analyze and understand the input data. This involves tasks like data preprocessing, entity recognition, and determining the relationships between different pieces of information.
  3. Content Planning: NLG systems decide what information to include in the generated text and how to organize it. This step involves selecting and structuring the most relevant data in a coherent narrative.
  4. Text Generation: In this phase, NLG systems use linguistic rules, templates, or machine learning models to create the actual natural language text. This text can be in sentences, paragraphs, or longer documents.
  5. Post-processing: After generating the text, NLG systems may perform additional processing to improve the text’s readability, coherence, and fluency. This can include tasks like spell-checking, grammar correction, and style adjustments.

What is natural language generation used for?

  • Automated Reporting: NLG can automatically generate news articles, financial reports, and other business documents from data.
  • Chatbots and Virtual Assistants: NLG can enable chatbots and virtual assistants to generate human-like responses in natural language.
  • E-commerce: NLG can create product descriptions and reviews for e-commerce websites.
  • Healthcare: NLG can generate patient reports, medical summaries, and explanations of lab results.
  • Content Generation: NLG can be used to create content for websites, marketing materials, and social media.
  • Language Translation: NLG can assist in translating text from one language to another.
  • Accessibility: NLG can help convert text-based information into speech for visually impaired individuals.
Natural Language Generation can be used by the visually impaired

NLG can help convert text-based information into speech for visually impaired individuals.

NLG technology continues advancing, significantly automating content creation and enhancing communication between humans and machines.

What is the difference between NLP, NLU and NLG?

NLP (Natural Language Processing), NLU (Natural Language Understanding), and NLG (Natural Language Generation) are three closely related subfields of artificial intelligence (AI) and natural language processing, each with distinct focuses and purposes:

Natural Language Processing (NLP):

Natural Language Understanding (NLU):

  • NLU is a subset of NLP that focuses explicitly on human language comprehension.
  • It involves extracting meaning and context from text or speech data.
  • NLU systems aim to understand the intent, sentiment, entities, and relationships within a text or spoken language.
  • Applications of NLU include sentiment analysis, chatbot understanding, voice assistants, and information retrieval.

Natural Language Generation (NLG):

  • NLG is another NLP subset that generates human-like language from structured or non-linguistic data.
  • It involves converting data into coherent, contextually relevant natural language text or speech.
  • NLG systems automatically create reports, articles, product descriptions, and other textual content forms.
  • These systems use algorithms and linguistic rules to produce text that sounds like a human wrote.

NLP deals with the overall processing of human language. NLU focuses on understanding the meaning and context within language, and NLG is concerned with generating human-like language from data. These three areas often overlap and complement each other, and together, they enable machines to interact with and understand human language in various applications.

How does natural language generation work?

Natural Language Generation (NLG) models are a subset of natural language processing (NLP) models designed to generate human-like natural language text or speech. NLG models, including rule-based, statistical, and neural network-based approaches, transform structured data or other input into coherent, contextually relevant text. Here’s an overview of how neural network-based NLG models, like GPT-3 or BERT, work:

  1. Pre-training: NLG models are typically pre-trained on massive text corpora to learn language patterns, grammar, semantics, and world knowledge. During pre-training, the model learns to predict the next word in a sentence or to fill in masked words based on the context of the surrounding words. This process allows the model to capture a broad range of linguistic knowledge.
  2. Architecture: NLG models are often based on transformer architectures, which have proven highly effective for NLP tasks. These architectures allow the model to process input data in parallel and capture long-range dependencies in text, making them suitable for NLG.
  3. Fine-tuning: NLG models are fine-tuned on specific NLG tasks using labelled or structured data after pre-training. Fine-tuning adapts the model to a particular task, domain, or language style. For example, if the goal is to generate news headlines, the model may be fine-tuned on a dataset of news headlines.
  4. Input Encoding: To generate text, the model first encodes the input data into numerical representations (embeddings) that the neural network can process. For instance, input text or data might be tokenized into a sequence of subword units (e.g., words or subword pieces) and converted into embeddings.
  5. Decoding: NLG models generate text step-by-step, where each step involves predicting the next word or subword. Decoding strategies can vary, but some common approaches include:
    • Autoregressive Generation: The model predicts words one at a time, conditioning each prediction on the previously generated words. In autoregressive generation, a token is sampled at each step, and the model’s predictions influence the next token.
    • Greedy Decoding: The model selects the token with the highest predicted probability at each step. This approach often results in coherent text but may not always produce the most contextually appropriate output.
    • Beam Search: Beam search explores multiple possible token sequences in parallel, keeping track of a fixed number of top candidates. It can improve the diversity and quality of generated text but may be computationally expensive.
    • Top-K Sampling: Instead of choosing the token with the highest probability, this method randomly samples from the top-K most likely tokens at each step. It introduces randomness into the generation process and can produce more varied output.
  6. Post-processing: The generated text may undergo post-processing steps to ensure coherence and readability. This can include capitalization, punctuation, and grammar corrections.
  7. Output: The final output of the NLG model is the generated natural language text or speech, which can be used for various applications, such as chatbots, content generation, translation, and more.

In essence, neural network-based NLG models leverage pre-trained language representations, fine-tuning, and decoding strategies to convert structured data or input prompts into coherent, contextually appropriate natural language text. The architecture and scale of these models, along with their ability to capture intricate language patterns, contribute to their success in NLG tasks.

7 useful models for natural language generation

Natural Language Generation (NLG) models have advanced significantly in recent years thanks to the development of large-scale pre-trained language models like GPT-3 and its successors. These models have enabled the generation of highly coherent and contextually relevant natural language text. Here are some notable NLG models:

1. GPT-3/4 (Generative Pre-trained Transformer 3/4):

  • Developed by OpenAI, GPT-3/4 is one of the most prominent NLG models.
  • GPT-3 has 175 billion parameters, making it one of the most significant language models at its release.
  • GPT-3/4 can perform various NLG tasks, including text generation, language translation, summarization, and question answering.

2. BERT (Bidirectional Encoder Representations from Transformers):

  • While BERT is primarily known for its Natural Language Understanding (NLU) capabilities, it can also be fine-tuned for NLG tasks.
  • Researchers have adapted BERT for tasks like text completion and generation by conditioning it on specific input data.

3. T5 (Text-to-Text Transfer Transformer):

  • T5 is a model developed by Google Research that frames all NLP tasks as a text-to-text problem.
  • It has shown strong performance across various NLG tasks, including translation, summarization, and question-answering.

4. CTRL (Conditional Transformer Language Model):

  • CTRL, developed by Salesforce Research, is designed for generating text with specific styles or attributes.
  • It allows users to condition the model to render text in a particular style, tone, or domain.

5. XLNet:

  • XLNet, developed by Google AI and Carnegie Mellon University, is another transformer-based model used for NLG tasks.
  • It addresses some limitations of traditional autoregressive models like GPT-2 using a permutation-based training approach.

6. BART (Bidirectional and Auto-Regressive Transformers):

  • BART, developed by Facebook AI, is designed for text generation and comprehension.
  • It has been used for tasks such as text summarization, completion, and text generation with specific prompts.

7. Turing-NLG:

  • Turing-NLG, developed by Microsoft, is designed to generate conversational responses that are contextually relevant and human-like.
  • It is intended for chatbot and virtual assistant applications.

Please note that the field of NLP and NLG is rapidly evolving. We have a list of top open-source large language models available here.

A natural language generation example

Here’s an example of Natural Language Generation (NLG) in action. In this example, we’ll generate a product description for a smartphone based on structured data:

Structured Data:

  • Product Name: “Smartphone XZ-2000”
  • Price: $599.99
  • Display Size: 6.5 inches
  • Camera: 16 MP dual-camera setup
  • Battery Life: Up to 2 days
  • Operating System: Android 12
  • Storage: 128 GB
  • Colour Options: Black, Silver, and Blue
nlg can generate product descriptions

NLG-Generated Product Description:

“Introducing the Smartphone XZ-2000, your perfect companion for staying connected and capturing life’s moments. This smartphone is stylish and functional, with a sleek design and a vibrant 6.5-inch display.

Capture stunning photos and videos with the advanced 16 MP dual-camera setup, ensuring that your memories are preserved in high quality. Whether it’s a scenic landscape or a group selfie, you’ll be impressed with the clarity and detail.

Don’t worry about constantly recharging your phone; the Smartphone XZ-2000 boasts an impressive battery life lasting up to 2 days on a single charge. Say goodbye to those midday charging sessions.

Running on the latest Android 12 operating system, you’ll experience a smooth and intuitive user interface. With 128 GB of storage, you’ll have ample space to store your apps, photos, and videos.

Choose from a range of stylish colours, including Black, Silver, and Blue, to match your style.

Get the Smartphone XZ-2000 today for only $599.99 and experience the perfect blend of style, performance, and affordability.”

In this example, the NLG system takes structured data about the smartphone. It generates a coherent and informative product description that could be used on a website or in marketing materials. The generated text is tailored to highlight the smartphone’s essential features, benefits, and pricing, making it more appealing to potential customers.

How to implement natural language generation in Python [2 ways]

1. Hugging Face Transformers

Let us get straight into implementing Natural Language Generation (NLG) using the Hugging Face Transformers library. In this example, we’ll use the GPT-2 model to generate text based on a given prompt.

Before running the code, make sure you have the transformers library installed:

pip install transformers 

Now, let’s generate text using Hugging Face Transformers:

from transformers import GPT2LMHeadModel, GPT2Tokenizer

# Load the pre-trained GPT-2 model and tokenizer
model_name = "gpt2"  # You can use different GPT-2 variants like "gpt2-medium," "gpt2-large," etc.
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
model = GPT2LMHeadModel.from_pretrained(model_name)

# Prompt for text generation
prompt = "Once upon a time in a faraway land,"

# Generate text
input_ids = tokenizer.encode(prompt, return_tensors="pt")
output = model.generate(input_ids, max_length=100, num_return_sequences=1, no_repeat_ngram_size=2, top_k=50)

# Decode and print the generated text
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
print("Generated Text:")


Generated Text:
Once upon a time in a faraway land, the world was a land of peace and harmony. The world of the gods was the land that was to be the home of all.

The world that had been the center of civilization was now a world where the Gods were to rule. They were the ones who had to make the most of their power. And they were not the only ones. There were many other gods, too. But the one who was most powerful was none other than

In this example:

We load a pre-trained GPT-2 model and tokenizer from the Hugging Face Transformers library. Depending on your requirements, you can choose a different GPT-2 variant or even other models.

We provide a prompt to the model, which serves as the starting point for text generation.

We use the model.generate method to generate text based on the prompt and specify parameters like max_length to control the length of the generated text, num_return_sequences to determine how many different sequences to generate, and no_repeat_ngram_size and top_k for text generation constraints.

Finally, we decode and print the generated text.

You can adjust the prompt and generation parameters to generate text according to your needs.

2. OpenAI

Let’s create a more complex example of Natural Language Generation (NLG) using Python. In this example, we’ll generate news headlines and summaries based on some sample data. We’ll use the GPT-3 model from OpenAI for more advanced NLG.

Before you proceed, you’ll need to set up an OpenAI account and get access to the GPT-3 API. You’ll also need to install the openai Python package.

pip install openai 

Here’s the Python code for generating news headlines and summaries:

import openai

# Set your OpenAI API key
api_key = "YOUR_API_KEY_HERE"  # Replace with your actual API key

# Initialize the OpenAI API client
openai.api_key = api_key

# Sample data
news_data = [
        "headline": "Scientists Make Breakthrough in Fusion Energy",
        "summary": "Researchers at the Fusion Energy Institute have achieved a major milestone in nuclear fusion, bringing us one step closer to clean and limitless energy sources."
        "headline": "SpaceX Launches Crewed Mission to Mars",
        "summary": "SpaceX successfully launched its first crewed mission to Mars today, marking a historic moment in space exploration. The crew of six astronauts will conduct experiments and pave the way for future interplanetary missions."
        "headline": "Tech Giant Unveils Quantum Computing Breakthrough",
        "summary": "A leading technology company has revealed a groundbreaking quantum computing platform that promises to revolutionize industries from cryptography to drug discovery. Experts are calling it a game-changer in the world of computing."

# Generate news articles
generated_articles = []

for news_item in news_data:
    prompt = f"Generate a news headline and summary:\n\nHeadline: {news_item['headline']}\nSummary: {news_item['summary']}\n\nArticle:"
    response = openai.Completion.create(
        max_tokens=150  # Adjust the length of the generated text as needed
    article_text = response.choices[0].text

# Print the generated news articles
for i, article in enumerate(generated_articles, start=1):
    print(f"Generated Article {i}:\n{article}\n")

We use OpenAI’s GPT-3 model in this example to generate news articles based on sample headlines and summaries. The code sends a prompt to the GPT-3 API, asking it to generate a news article for each news item. The generated articles are then printed to the console.

Remember to replace “YOUR_API_KEY_HERE” with your actual OpenAI API key. Additionally, you can customize the max_tokens parameter to control the length of the generated text.

This example demonstrates more complex NLG by generating human-like news articles based on structured input data.


Natural Language Generation (NLG) is a fascinating field within natural language processing (NLP) and artificial intelligence (AI) that focuses on the automatic generation of human-like natural language text or speech from structured or non-linguistic data. NLG has many applications, from chatbots and virtual assistants to content generation, automated reporting, and more.

Key points:

  1. Components of NLG: NLG systems typically consist of data input, natural language processing, content planning, text generation, and post-processing.
  2. NLP, NLU, and NLG: NLG is closely related to Natural Language Processing (NLP) and Natural Language Understanding (NLU). While NLP deals with the overall processing of language, NLU focuses on understanding language, and NLG is about generating language.
  3. NLG Models: There are various NLG models and approaches, including rule-based systems, template-based systems, and more advanced deep learning models like GPT-3 and BERT.
  4. Use Cases: NLG finds applications in automating content creation, generating personalized recommendations, creating chatbot responses, producing automated reports, assisting with language translation, and enhancing accessibility for people with disabilities, among others.
  5. Hugging Face Transformers: The library is famous for working with state-of-the-art language models like GPT-2 and BERT for NLG tasks.

NLG continues to evolve with the development of larger and more capable language models. As technology advances, NLG will likely play an increasingly important role in automating content creation and communication between humans and machines.

About the Author

Neri Van Otten

Neri Van Otten

Neri Van Otten is the founder of Spot Intelligence, a machine learning engineer with over 12 years of experience specialising in Natural Language Processing (NLP) and deep learning innovation. Dedicated to making your projects succeed.

Recent Articles

One class SVM anomaly detection plot

How To Implement Anomaly Detection With One-Class SVM In Python

What is One-Class SVM? One-class SVM (Support Vector Machine) is a specialised form of the standard SVM tailored for unsupervised learning tasks, particularly anomaly...

decision tree example of weather to play tennis

Decision Trees In ML Complete Guide [How To Tutorial, Examples, 5 Types & Alternatives]

What are Decision Trees? Decision trees are versatile and intuitive machine learning models for classification and regression tasks. It represents decisions and their...

graphical representation of an isolation forest

Isolation Forest For Anomaly Detection Made Easy & How To Tutorial

What is an Isolation Forest? Isolation Forest, often abbreviated as iForest, is a powerful and efficient algorithm designed explicitly for anomaly detection. Introduced...

Illustration of batch gradient descent

Batch Gradient Descent In Machine Learning Made Simple & How To Tutorial In Python

What is Batch Gradient Descent? Batch gradient descent is a fundamental optimization algorithm in machine learning and numerical optimisation tasks. It is a variation...

Techniques for bias detection in machine learning

Bias Mitigation in Machine Learning [Practical How-To Guide & 12 Strategies]

In machine learning (ML), bias is not just a technical concern—it's a pressing ethical issue with profound implications. As AI systems become increasingly integrated...

text similarity python

Full-Text Search Explained, How To Implement & 6 Powerful Tools

What is Full-Text Search? Full-text search is a technique for efficiently and accurately retrieving textual data from large datasets. Unlike traditional search methods...

the hyperplane in a support vector regression (SVR)

Support Vector Regression (SVR) Simplified & How To Tutorial In Python

What is Support Vector Regression (SVR)? Support Vector Regression (SVR) is a machine learning technique for regression tasks. It extends the principles of Support...

Support vector Machines (SVM) work with decision boundaries

Support Vector Machines (SVM) In Machine Learning Made Simple & How To Tutorial

What are Support Vector Machines? Machine learning algorithms transform raw data into actionable insights. Among these algorithms, Support Vector Machines (SVMs) stand...

underfitting vs overfitting vs optimised fit

Weight Decay In Machine Learning And Deep Learning Explained & How To Tutorial

What is Weight Decay in Machine Learning? Weight decay is a pivotal technique in machine learning, serving as a cornerstone for model regularisation. As algorithms...


Submit a Comment

Your email address will not be published. Required fields are marked *

nlp trends

2024 NLP Expert Trend Predictions

Get a FREE PDF with expert predictions for 2024. How will natural language processing (NLP) impact businesses? What can we expect from the state-of-the-art models?

Find out this and more by subscribing* to our NLP newsletter.

You have Successfully Subscribed!