Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: spacy_nlp_program.py
- # Author: Jeoi Reqi
- """
- Welcome to the SpaCy NLP Tool!
- This interactive script provides a Natural Language Processing (NLP) tool powered by the SpaCy library.
- It offers various text analysis functionalities through a user-friendly menu.
- Requirements:
- - Python 3
- - SpaCy library
- - SpacyTextBlob library for sentiment analysis
- Usage:
- 1. Run the script.
- 2. Follow the menu prompts to select the desired analysis.
- 3. Enter the text for analysis.
- Menu Options:
- 1. Tokenization: Breaks text into words and sentences.
- 2. Named Entity Recognition: Identifies entities such as persons, organizations, and locations.
- 3. Part-of-Speech Tagging: Identifies grammatical parts of speech for each word.
- 4. Sentiment Analysis: Determines sentiment polarity (negative, neutral, positive).
- Examples:
- 1. Tokenization
- Enter the text: Hello, World!
- Tokenized Words: ['Hello', ',', 'World', '!']
- Tokenized Sentences: ['Hello, World!']
- 2. Part-of-Speech Tagging
- Enter the text: Hello, World!
- Part-of-Speech Tags: [('Hello', 'NNP'), (',', ','), ('World', 'NNP'), ('!', '.')]
- 3. Named Entity Recognition
- Enter the text: Hello, World!
- Named Entity Recognition: (S (GPE Hello/NNP) ,/, (PERSON World/NNP) !/.)
- 4. Sentiment Analysis
- Enter the text: Hello, World!
- Sentiment Analysis: {'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound': 0.0}
- Enjoy exploring the world of Natural Language Processing with SpaCy!
- """
- # Imports
- import spacy
- from spacytextblob.spacytextblob import SpacyTextBlob
- from spacy.tokens import Doc
- from textblob import TextBlob
- # Tokenization Function
- def tokenization_spacy(text):
- """
- Tokenizes the input text into words and sentences using SpaCy.
- Parameters:
- - text (str): Input text for tokenization.
- Returns:
- Tuple[List[str], List[str]]: Tokenized words and sentences.
- """
- nlp = spacy.load("en_core_web_sm")
- doc = nlp(text)
- tokens = [token.text for token in doc]
- sentences = [sent.text for sent in doc.sents]
- print("Tokenized Words:", tokens)
- print("Tokenized Sentences:", sentences)
- # Named Entity Recognition Function
- def named_entity_recognition_spacy(text):
- """
- Identifies named entities in the input text using SpaCy.
- Parameters:
- - text (str): Input text for named entity recognition.
- Returns:
- List[Tuple[str, str]]: Named entities and their labels.
- """
- nlp = spacy.load("en_core_web_sm")
- doc = nlp(text)
- entities = [(ent.text, ent.label_) for ent in doc.ents]
- print("Named Entities:", entities)
- # Part-of-Speech Tagging Function
- def part_of_speech_tagging_spacy(text):
- """
- Performs part-of-speech tagging on the input text using SpaCy.
- Parameters:
- - text (str): Input text for part-of-speech tagging.
- Returns:
- List[Tuple[str, str]]: Part-of-speech tags for each word.
- """
- nlp = spacy.load("en_core_web_sm")
- doc = nlp(text)
- pos_tags = [(token.text, token.pos_) for token in doc]
- print("Part-of-Speech Tags:", pos_tags)
- # Sentiment Analysis Function
- def sentiment_analysis_spacy(text):
- """
- Analyzes the sentiment of the input text using SpaCyTextBlob.
- Parameters:
- - text (str): Input text for sentiment analysis.
- Returns:
- float: Sentiment polarity score.
- """
- nlp = spacy.load("en_core_web_sm")
- nlp.add_pipe('spacytextblob')
- doc = nlp(text)
- # Access sentiment polarity from the 'doc' object
- sentiment_score = doc._.sentiment.polarity
- print("Sentiment Score:", sentiment_score)
- # Menu & Options Function
- def main_spacy():
- """
- Main function for the SpaCy NLP tool, providing a user-friendly menu for text analysis.
- """
- print("Welcome to the SpaCy NLP Tool!\n")
- while True:
- print_menu()
- choice = input("Enter your choice (0-4): ")
- if choice == '0':
- print("Exiting the program. Goodbye!")
- break
- elif choice == '1':
- text = input("Enter the text: ")
- tokenization_spacy(text)
- elif choice == '2':
- text = input("Enter the text: ")
- named_entity_recognition_spacy(text)
- elif choice == '3':
- text = input("Enter the text: ")
- part_of_speech_tagging_spacy(text)
- elif choice == '4':
- text = input("Enter the text: ")
- sentiment_analysis_spacy(text)
- else:
- print("Invalid choice. Please enter a number between 0 and 4.")
- # Menu Printing Function
- def print_menu():
- """
- Prints the menu options for the user.
- """
- print("\nMenu:")
- print("1. Tokenization")
- print("2. Named Entity Recognition")
- print("3. Part-of-Speech Tagging")
- print("4. Sentiment Analysis")
- print("0. Exit")
- if __name__ == "__main__":
- main_spacy()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement