Book Image

Natural Language Processing with Flair

By : Tadej Magajna
Book Image

Natural Language Processing with Flair

By: Tadej Magajna

Overview of this book

Flair is an easy-to-understand natural language processing (NLP) framework designed to facilitate training and distribution of state-of-the-art NLP models for named entity recognition, part-of-speech tagging, and text classification. Flair is also a text embedding library for combining different types of embeddings, such as document embeddings, Transformer embeddings, and the proposed Flair embeddings. Natural Language Processing with Flair takes a hands-on approach to explaining and solving real-world NLP problems. You'll begin by installing Flair and learning about the basic NLP concepts and terminology. You will explore Flair's extensive features, such as sequence tagging, text classification, and word embeddings, through practical exercises. As you advance, you will train your own sequence labeling and text classification models and learn how to use hyperparameter tuning in order to choose the right training parameters. You will learn about the idea behind one-shot and few-shot learning through a novel text classification technique TARS. Finally, you will solve several real-world NLP problems through hands-on exercises, as well as learn how to deploy Flair models to production. By the end of this Flair book, you'll have developed a thorough understanding of typical NLP problems and you’ll be able to solve them with Flair.
Table of Contents (15 chapters)
1
Part 1: Understanding and Solving NLP with Flair
6
Part 2: Deep Dive into Flair – Training Custom Models
11
Part 3: Real-World Applications with Flair

Setting up the development environment

Now that you have a basic understanding of features offered by the framework, as well as an understanding of the basic NLP concepts, you are now ready to move to the next step of setting up your development environment for Flair.

To be able to follow the instructions in this section, first make sure you have Python 3.6+ installed on your device as described in the Technical requirements section.

Creating the virtual environment

In Python, it's generally good practice to install packages in virtual environments so that the project dependencies you are currently working on will not affect your global Python dependencies or other projects you may work on in the future.

We will use the venv tool that is part of the Python Standard Library and requires no installation. To create a virtual environment, simply create a new directory, move into it, then run the following command:

$ python3 -m venv learning-flair

Then, to activate the virtual environment on Linux or macOS, run the following:

$ source learning-flair/bin/activate

If you are running Windows, run the following:

$ learning-flair\Scripts\activate.bat

Your command line should become prefixed with (learning-flair) $ and your virtual environment is now active.

Installing a published version of Flair in a virtual environment

You should now be ready to install Flair version 0.11 with this single command:

(learning-flair) $ pip install flair==0.11

The installation should now commence and finish within a minute or so depending on the speed of your internet connection.

You can verify the installation by running the following command, which will display a list of package properties including its version:

(learning-flair) $ pip show flair
Name: flair
Version: 0.11
Summary: A very simple framework for state-of-the-art NLP
Home-page: https://github.com/flairNLP/flair

A command output like the preceding indicates the package has been successfully installed in your virtual environment.

Installing directly from the GitHub repository (optional)

In some cases, the features we aim to make use of in Flair may already be implemented in a branch on GitHub, but those changes may not yet be released as part of a Python package published on PyPI. We can install Flair with those features directly from the Git repository branch.

For example, here is how you can install Flair from the master branch:

(learning-flair) $ git clone https://github.com/flairNLP/flair.git
(learning-flair) $ cd flair
(learning-flair) $ git checkout master
(learning-flair) $ pip install .

Important note

Installing code from non-reviewed branches can introduce unreliable or unsafe code. When installing Flair from development branches, make sure the code you are installing comes from a trusted source. Also note that the future versions of Flair (versions larger than 0.11) may not be compatible with the code snippets found in this book.

Replace the term master with any other branch name to install the package from a branch of your choice.

Running code that uses Flair

Running code that makes use of the Flair Python package is no different from running any other type of Python code.

The recommended way for you to run the code snippets in this book is to execute them as code cells in a Jupyter notebook, which you can install and run as follows:

(learning-flair) $ pip install notebook
(learning-flair) $ jupyter notebook

You can then create a new Python 3 notebook and run your first Flair script to verify the package is imported successfully:

import flair
print(flair.__version__)

After executing, the preceding code should print out the version of Flair you are currently using, indicating that the Flair package has been imported successfully and you are ready to start.