0% found this document useful (0 votes)
62 views6 pages

4.1.6.relation Extraction

NLP Topics
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views6 pages

4.1.6.relation Extraction

NLP Topics
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Relation

Extraction
What is RELATION EXTRACTION?
• Relation extraction is a task in NLP that aims to identify and extract
the relationships between named entities mentioned in text. The goal
is to extract structured information from unstructured text and
represent it in the form of triples, consisting of two entities and a
relationship between them.
• Relation extraction has a wide range of applications, such as
information extraction, question answering, and knowledge base
construction. It plays an important role in many NLP tasks by providing
a structured representation of information in unstructured text.
• Once named entities have been identified in a text, we then want to
extract the relations that exist between them.
• One way of approaching this task is to initially look for all triples of the
form (X, α, Y), where X and Y are named entities of the required types,
and α is the string of words that intervenes between X and Y.

RELATION EXTRACTION 2
• Here's an example of relation extraction in action:

• Input: "Barack Obama was the 44th President of the


United States."

• In this example, the relation extraction algorithm has


identified the relationship "44th President of the
United States" between the entities "Barack Obama"
and "United States". The output is represented as a
triple, consisting of two entities and the relationship
between them.

• Output: (Barack Obama, 44th President, of the United


States)
RELATION EXTRCTION 3
• Here are the steps involved in relation extraction:
1.Named Entity Recognition (NER): The first step is to
identify the named entities in the text using NER.
2.Candidate relation identification: After the named
entities are identified, the next step is to identify potential
relationships between them. This is done by extracting all
the pairs of named entities that appear together in the text.
3.Feature extraction: Once the candidate relations are
identified, the next step is to extract features from the text
that can help predict the relationships between the entities.
4.Relation classification: The final step is to classify the
candidate relationships into predefined categories, such as
"works for," "located in," "born on," etc. This is usually done
using machine learning algorithms.
RELATION EXTRACTION 4
import spacy

# Load the English language model


nlp = spacy.load("en_core_web_sm")

# Define the text


text = “Vaishnav is a student in Osmania university."

# Apply the NLP pipeline to the text


doc = nlp(text)

# Iterate over the entities in the document


for ent in doc.ents:
# Check if the entity is a person
if ent.label_ == "PERSON":
# Iterate over the remaining entities in the document
for ent2 in doc.ents:
# Check if the second entity is an organization
if ent2.label_ == "ORG":
# Check if the entities appear together in the text
if ent.end < ent2.start:
# Extract the relationship between the entities
relationship = doc[ent.end:ent2.start].text
# Print the result
print("(%s, %s, %s)" % (ent, relationship, ent2))

RELATION EXTRCTION 5
• The output of the code would be:
(vaishnav, is a student in, Osmania
University)

This code performs named entity recognition on a sample


text and then performs relation extraction between two
entities. The code loads the "en_core_web_sm" English
language model using spaCy and then applies the NLP
pipeline to the text. The code iterates over the entities in
the document and checks if the entity is a "PERSON". If it
is, it then iterates over the remaining entities in the
document and checks if the second entity is an "ORG". If
both entities are present in the text, the code extracts the
text between the two entities and prints the result.
RELATION EXTRCTION 6

You might also like