A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://flairnlp.github.io/docs/intro below:

Quick Start | flair

Quick Start

Let's discover Flair in less than 5 minutes.

Requirements and Installation

In your favorite virtual environment, simply do:

Flair requires Python 3.8+.

Example 1: Tag Entities in Text

Let's run named entity recognition (NER) over the following example sentence: "I love Berlin and New York."

Our goal is to identify names in this sentence, and their types.

To do this, all you need is to make a Sentence for this text, load a pre-trained model and use it to predict tags for the sentence:

from flair.data import Sentence
from flair.nn import Classifier


sentence = Sentence('I love Berlin and New York.')


tagger = Classifier.load('ner')


tagger.predict(sentence)


print(sentence)

This should print:

Sentence[7]: "I love Berlin and New York." → ["Berlin"/LOC, "New York"/LOC]

The output shows that both "Berlin" and "New York" were tagged as location entities (LOC) in this sentence.

Example 2: Detect Sentiment

Let's run sentiment analysis over the same sentence to determine whether it is POSITIVE or NEGATIVE.

You can do this with essentially the same code as above. Just instead of loading the 'ner' model, you now load the 'sentiment' model:

from flair.data import Sentence
from flair.nn import Classifier


sentence = Sentence('I love Berlin and New York.')


tagger = Classifier.load('sentiment')


tagger.predict(sentence)


print(sentence)

This should print:

Sentence[7]: "I love Berlin and New York." → POSITIVE (0.9982)

The output shows that the sentence "I love Berlin and New York." was tagged as having POSITIVE sentiment.

Summary

Congrats, you now know how to use Flair to find entities and detect sentiment!


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4