Advertisement
Python253

nlp_named_entity_nltk

Mar 8th, 2024 (edited)
734
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.45 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: nlp_named_entity_nltk.py
  4. # Author: Jeoi Reqi
  5.  
  6. """
  7. This script performs Named Entity Recognition (NER) using NLTK.
  8.  
  9. Requirements:
  10. - Python 3
  11. - NLTK library
  12. - 'averaged_perceptron_tagger' and 'maxent_ne_chunker' NLTK resources
  13.  
  14. Usage:
  15. - Run the script and provide input text when prompted.
  16. - The script will tokenize the text, perform part-of-speech tagging, and identify named entities using NLTK.
  17.  
  18. Example:
  19. python named_entity_nltk.py
  20. Enter the text: Natural Language Processing is a fascinating field.
  21. Part-of-Speech Tags: [('Natural', 'JJ'), ('Language', 'NNP'), ('Processing', 'NNP'), ('is', 'VBZ'), ('a', 'DT'), ('fascinating', 'JJ'), ('field', 'NN'), ('.', '.')]
  22. Named Entities: (GPE Natural/NNP Language/NNP Processing/NNP)
  23. """
  24.  
  25. import nltk
  26. from nltk import pos_tag, ne_chunk
  27. from nltk.tokenize import word_tokenize
  28.  
  29. # Download the 'averaged_perceptron_tagger' and 'maxent_ne_chunker' resources
  30. nltk.download('averaged_perceptron_tagger')
  31. nltk.download('maxent_ne_chunker')
  32.  
  33. # Sample text
  34. text = "Natural Language Processing is a fascinating field. It involves the use of computers to understand and process human language."
  35.  
  36. # Tokenize the text
  37. words = word_tokenize(text)
  38.  
  39. # Part-of-speech tagging
  40. pos_tags = pos_tag(words)
  41. print("Part-of-Speech Tags:", pos_tags)
  42.  
  43. # Named Entity Recognition using NLTK
  44. ner_result = ne_chunk(pos_tags)
  45. print("Named Entities:", ner_result)
  46.  
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement