Advertisement
Python253

spacy_nlp_program

Mar 8th, 2024 (edited)
807
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.91 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: spacy_nlp_program.py
  4. # Author: Jeoi Reqi
  5.  
  6. """
  7. Welcome to the SpaCy NLP Tool!
  8.  
  9. This interactive script provides a Natural Language Processing (NLP) tool powered by the SpaCy library.
  10. It offers various text analysis functionalities through a user-friendly menu.
  11.  
  12. Requirements:
  13. - Python 3
  14. - SpaCy library
  15. - SpacyTextBlob library for sentiment analysis
  16.  
  17. Usage:
  18. 1. Run the script.
  19. 2. Follow the menu prompts to select the desired analysis.
  20. 3. Enter the text for analysis.
  21.  
  22. Menu Options:
  23. 1. Tokenization: Breaks text into words and sentences.
  24. 2. Named Entity Recognition: Identifies entities such as persons, organizations, and locations.
  25. 3. Part-of-Speech Tagging: Identifies grammatical parts of speech for each word.
  26. 4. Sentiment Analysis: Determines sentiment polarity (negative, neutral, positive).
  27.  
  28. Examples:
  29.  
  30. 1. Tokenization
  31. Enter the text: Hello, World!
  32. Tokenized Words: ['Hello', ',', 'World', '!']
  33. Tokenized Sentences: ['Hello, World!']
  34.  
  35. 2. Part-of-Speech Tagging
  36. Enter the text: Hello, World!
  37. Part-of-Speech Tags: [('Hello', 'NNP'), (',', ','), ('World', 'NNP'), ('!', '.')]
  38.  
  39. 3. Named Entity Recognition
  40. Enter the text: Hello, World!
  41. Named Entity Recognition: (S (GPE Hello/NNP) ,/, (PERSON World/NNP) !/.)
  42.  
  43. 4. Sentiment Analysis
  44. Enter the text: Hello, World!
  45. Sentiment Analysis: {'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound': 0.0}
  46.  
  47. Enjoy exploring the world of Natural Language Processing with SpaCy!
  48. """
  49.  
  50. # Imports
  51. import spacy
  52. from spacytextblob.spacytextblob import SpacyTextBlob
  53. from spacy.tokens import Doc
  54. from textblob import TextBlob
  55.  
  56. # Tokenization Function
  57. def tokenization_spacy(text):
  58.     """
  59.    Tokenizes the input text into words and sentences using SpaCy.
  60.  
  61.    Parameters:
  62.    - text (str): Input text for tokenization.
  63.  
  64.    Returns:
  65.    Tuple[List[str], List[str]]: Tokenized words and sentences.
  66.    """
  67.     nlp = spacy.load("en_core_web_sm")
  68.     doc = nlp(text)
  69.     tokens = [token.text for token in doc]
  70.     sentences = [sent.text for sent in doc.sents]
  71.     print("Tokenized Words:", tokens)
  72.     print("Tokenized Sentences:", sentences)
  73.  
  74. # Named Entity Recognition Function
  75. def named_entity_recognition_spacy(text):
  76.     """
  77.    Identifies named entities in the input text using SpaCy.
  78.  
  79.    Parameters:
  80.    - text (str): Input text for named entity recognition.
  81.  
  82.    Returns:
  83.    List[Tuple[str, str]]: Named entities and their labels.
  84.    """
  85.     nlp = spacy.load("en_core_web_sm")
  86.     doc = nlp(text)
  87.     entities = [(ent.text, ent.label_) for ent in doc.ents]
  88.     print("Named Entities:", entities)
  89.  
  90. # Part-of-Speech Tagging Function
  91. def part_of_speech_tagging_spacy(text):
  92.     """
  93.    Performs part-of-speech tagging on the input text using SpaCy.
  94.  
  95.    Parameters:
  96.    - text (str): Input text for part-of-speech tagging.
  97.  
  98.    Returns:
  99.    List[Tuple[str, str]]: Part-of-speech tags for each word.
  100.    """
  101.     nlp = spacy.load("en_core_web_sm")
  102.     doc = nlp(text)
  103.     pos_tags = [(token.text, token.pos_) for token in doc]
  104.     print("Part-of-Speech Tags:", pos_tags)
  105.  
  106. # Sentiment Analysis Function
  107. def sentiment_analysis_spacy(text):
  108.     """
  109.    Analyzes the sentiment of the input text using SpaCyTextBlob.
  110.  
  111.    Parameters:
  112.    - text (str): Input text for sentiment analysis.
  113.  
  114.    Returns:
  115.    float: Sentiment polarity score.
  116.    """
  117.     nlp = spacy.load("en_core_web_sm")
  118.     nlp.add_pipe('spacytextblob')
  119.     doc = nlp(text)
  120.  
  121.     # Access sentiment polarity from the 'doc' object
  122.     sentiment_score = doc._.sentiment.polarity
  123.     print("Sentiment Score:", sentiment_score)
  124.  
  125. # Menu & Options Function
  126. def main_spacy():
  127.     """
  128.    Main function for the SpaCy NLP tool, providing a user-friendly menu for text analysis.
  129.    """
  130.     print("Welcome to the SpaCy NLP Tool!\n")
  131.  
  132.     while True:
  133.         print_menu()
  134.         choice = input("Enter your choice (0-4): ")
  135.  
  136.         if choice == '0':
  137.             print("Exiting the program. Goodbye!")
  138.             break
  139.         elif choice == '1':
  140.             text = input("Enter the text: ")
  141.             tokenization_spacy(text)
  142.         elif choice == '2':
  143.             text = input("Enter the text: ")
  144.             named_entity_recognition_spacy(text)
  145.         elif choice == '3':
  146.             text = input("Enter the text: ")
  147.             part_of_speech_tagging_spacy(text)
  148.         elif choice == '4':
  149.             text = input("Enter the text: ")
  150.             sentiment_analysis_spacy(text)
  151.         else:
  152.             print("Invalid choice. Please enter a number between 0 and 4.")
  153.            
  154. # Menu Printing Function
  155. def print_menu():
  156.     """
  157.    Prints the menu options for the user.
  158.    """
  159.     print("\nMenu:")
  160.     print("1. Tokenization")
  161.     print("2. Named Entity Recognition")
  162.     print("3. Part-of-Speech Tagging")
  163.     print("4. Sentiment Analysis")
  164.     print("0. Exit")
  165.  
  166. if __name__ == "__main__":
  167.     main_spacy()
  168.  
  169.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement