Named Entity Recognition (NER) is a task in Natural Language Processing (NLP) that involves identifying and classifying named entities in text into predefined categories such as person names, locations, organizations, dates, and more. The goal of NER is to extract relevant information and understand the structure of the text by recognizing entities and their types.
from transformers import pipeline
ner = pipeline("ner", grouped_entities=True)
ner("My name is Sylvain and I work at Hugging Face in Brooklyn.")
Copied
[{'entity_group': 'PER', 'score': 0.99816, 'word': 'Sylvain', 'start': 11, 'end': 18},
{'entity_group': 'ORG', 'score': 0.97960, 'word': 'Hugging Face', 'start': 33, 'end': 45},
{'entity_group': 'LOC', 'score': 0.99321, 'word': 'Brooklyn', 'start': 49, 'end': 57}
]
Implement NER
Step 1: Install the required libraries
pip install transformers
Step 2: Create a Django view function to handle the POST request
from transformers import pipeline
from django.http import JsonResponse
def ner(request):
if request.method == 'POST':
# Get the input text from the POST request
input_text = request.POST.get('text', '')
# Load the NER pipeline
ner_pipeline = pipeline("ner")
# Process the input text with the NER pipeline
ner_results = ner_pipeline(input_text)
# Extract named entities from the NER results
entities = []
for result in ner_results:
entities.append({
'text': result['word'],
'label': result['entity']
})
# Return the named entities as JSON response
return JsonResponse(entities, safe=False)
Step 3: Define a URL pattern in your Django project's urls.py file to map the view function
from django.urls import path
from .views import ner
urlpatterns = [
path('ner/', ner, name='ner'),
]
Step 4: Start the Django development server and send a POST request to the /ner/ endpoint with the input text as the payload. You can use tools like Postman or cURL for this purpose.
Example output:
Input text: "Apple Inc. is planning to open a new store in New York City next month
."
Output JSON response:
[
{
"text": "Apple",
"label": "ORG"
},
{
"text": "Inc.",
"label": "ORG"
},
{
"text": "New York City",
"label": "LOC"
}
]
In this example, the NER pipeline identifies "Apple" and "Inc." as an organization (label: ORG) and "New York City" as a location (label: LOC).
Note: Ensure that you have proper error handling, input validation, and security measures in place when implementing this code in a production environment.
Top comments (0)