Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: nlp_named_entity_nltk.py
- # Author: Jeoi Reqi
- """
- This script performs Named Entity Recognition (NER) using NLTK.
- Requirements:
- - Python 3
- - NLTK library
- - 'averaged_perceptron_tagger' and 'maxent_ne_chunker' NLTK resources
- Usage:
- - Run the script and provide input text when prompted.
- - The script will tokenize the text, perform part-of-speech tagging, and identify named entities using NLTK.
- Example:
- python named_entity_nltk.py
- Enter the text: Natural Language Processing is a fascinating field.
- Part-of-Speech Tags: [('Natural', 'JJ'), ('Language', 'NNP'), ('Processing', 'NNP'), ('is', 'VBZ'), ('a', 'DT'), ('fascinating', 'JJ'), ('field', 'NN'), ('.', '.')]
- Named Entities: (GPE Natural/NNP Language/NNP Processing/NNP)
- """
- import nltk
- from nltk import pos_tag, ne_chunk
- from nltk.tokenize import word_tokenize
- # Download the 'averaged_perceptron_tagger' and 'maxent_ne_chunker' resources
- nltk.download('averaged_perceptron_tagger')
- nltk.download('maxent_ne_chunker')
- # Sample text
- text = "Natural Language Processing is a fascinating field. It involves the use of computers to understand and process human language."
- # Tokenize the text
- words = word_tokenize(text)
- # Part-of-speech tagging
- pos_tags = pos_tag(words)
- print("Part-of-Speech Tags:", pos_tags)
- # Named Entity Recognition using NLTK
- ner_result = ne_chunk(pos_tags)
- print("Named Entities:", ner_result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement