Advertisement
Python253

nlp_tokenization_nltk

Mar 8th, 2024 (edited)
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: nlp_tokenization_nltk.py
  4. # Author: Jeoi Reqi
  5.  
  6. """
  7. This script performs tokenization on a given text using NLTK.
  8.  
  9. Requirements:
  10. - Python 3
  11. - NLTK library with the 'punkt' resource
  12.  
  13. Usage:
  14. - Run the script, and it will print the tokenized words and sentences of the provided text.
  15.  
  16. Example:
  17. python tokenization_nltk.py
  18.  
  19. Output:
  20. Tokenized Words: ['Natural', 'Language', 'Processing', 'is', 'a', 'fascinating', 'field', '.', 'It', 'involves', 'the', 'use', 'of', 'computers', 'to', 'understand', 'and', 'process', 'human', 'language', '.']
  21. Tokenized Sentences: ['Natural Language Processing is a fascinating field.', 'It involves the use of computers to understand and process human language.']
  22. """
  23.  
  24. import nltk
  25. from nltk.tokenize import word_tokenize, sent_tokenize
  26.  
  27. # Download the 'punkt' resource
  28. nltk.download('punkt')
  29.  
  30. # Sample text
  31. text = "Natural Language Processing is a fascinating field. It involves the use of computers to understand and process human language."
  32.  
  33. # Tokenize the text
  34. words = word_tokenize(text)
  35. sentences = sent_tokenize(text)
  36.  
  37. print("Tokenized Words:", words)
  38. print("Tokenized Sentences:", sentences)
  39.  
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement