Two minutes NLP — Relation Extraction with OpenNRE

Relation Extraction, Knowledge Graphs, Entities, and OpenNRE

Fabio Chiusano
NLPlanet
2 min readDec 16, 2021

--

Photo by matthew Feeney on Unsplash

Relation Extraction is a Natural Language Processing task aiming at extracting relations between entities. For example, from the sentence Romeo and Juliet was written by William Shakespeare, we can extract the relation triple (William Shakespeare, is the writer of, Romeo and Juliet).

Relation Extraction is a crucial technique in automatic knowledge graph construction. By using Relation Extraction, we can accumulatively extract new relation facts and expand the knowledge graph, which, as a way for machines to understand the human world, has many downstream applications like question answering, recommender systems, and search engines.

OpenNRE is one of the most used libraries to do relation extraction on text.

Relation extraction with OpenNRE

OpenNRE is an open-source and extensible toolkit that provides a unified framework to implement relation extraction models.

The library comes with two pre-trained models which can be put into production without any training:

  • wiki80_cnn_softmax: trained on Wiki80 dataset with a CNN encoder.
  • wiki80_bert_softmax: trained on Wiki80 dataset with a BERT encoder.

Both models are trained on the Wiki80 dataset, which consists of 80 relations, each having 700 instances.

In order to use the OpenNRE library, you must at first install it from its repo:

The next steps are:

  • Import the library in your Python code.
  • Load a pre-trained model.
  • Call the infer function of the model passing (1) a paragraph, (2) the first entity position, and (3) the second entity position. The function returns the predicted relation for the pair of entities, using the paragraph as context.

Despite the name Relation Extraction, the example we have seen here is actually a multi-label classification problem, where the possible types of relations that can be extracted are the ones present in the training set.

--

--

Fabio Chiusano
NLPlanet

Freelance data scientist — Top Medium writer in Artificial Intelligence