help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • Blog
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
nlp-text-analysis-bot
RSK World
nlp-text-analysis-bot
NLP Text Analysis Bot - Python + NLP + Flask + Machine Learning + Text Analysis + AI
nlp-text-analysis-bot
  • static
  • templates
  • .gitignore393 B
  • ADVANCED_FEATURES.md5.4 KB
  • CHANGELOG.md1.3 KB
  • FINAL_CHECK.md4.6 KB
  • GITHUB_RELEASE_INSTRUCTIONS.md4.1 KB
  • LICENSE1.2 KB
  • PROJECT_INFO.md2.7 KB
  • PROJECT_STATUS.md4 KB
  • QUICKSTART.md3.1 KB
  • README.md5.8 KB
  • RELEASE_NOTES.md3.8 KB
  • advanced_keywords.py3.9 KB
  • app.py3 KB
  • config.py668 B
  • emotion_detection.py4.3 KB
  • entity_recognition.py3 KB
  • example_usage.py2.7 KB
  • install.bat853 B
  • install.sh808 B
  • language_detection.py2.7 KB
  • nlp_pipeline.py7.1 KB
  • pos_tagging.py2.9 KB
  • readability_analysis.py3.5 KB
  • requirements.txt334 B
  • semantic_understanding.py4 KB
  • sentiment_analysis.py3.9 KB
  • setup.py1.4 KB
  • test_analysis.py2.5 KB
  • text_classification.py5 KB
  • text_preprocessing.py4.2 KB
  • text_similarity.py4.1 KB
  • text_summarization.py5 KB
  • validate_project.py4.2 KB
conversation_analytics.cpython-313.pycweather_api.cpython-313.pycapp.cpython-313.pyc.pre-commit-config.yamlsentiment_analysis.py
sentiment_analysis.py
Raw Download
Find: Go to:
"""
Sentiment Analysis Module
Analyzes sentiment using NLTK and transformers

Developer: RSK World
Website: https://rskworld.in
Email: help@rskworld.in
Phone: +91 93305 39277
Year: 2026
"""

from transformers import pipeline
from nltk.sentiment import SentimentIntensityAnalyzer
import nltk

# Download required NLTK data
try:
    nltk.data.find('vader_lexicon')
except LookupError:
    nltk.download('vader_lexicon', quiet=True)

class SentimentAnalyzer:
    """
    Sentiment analysis class using multiple methods
    Developer: RSK World - https://rskworld.in
    """
    
    def __init__(self):
        """Initialize sentiment analyzers"""
        # VADER sentiment analyzer (NLTK)
        self.vader_analyzer = SentimentIntensityAnalyzer()
        
        # Transformer-based sentiment analyzer
        try:
            self.transformer_analyzer = pipeline(
                "sentiment-analysis",
                model="cardiffnlp/twitter-roberta-base-sentiment-latest",
                device=-1  # Use CPU
            )
        except Exception as e:
            print(f"Warning: Could not load transformer model: {e}")
            self.transformer_analyzer = None
    
    def analyze_vader(self, text):
        """
        Analyze sentiment using VADER
        
        Args:
            text (str): Input text
            
        Returns:
            dict: VADER sentiment scores
        """
        scores = self.vader_analyzer.polarity_scores(text)
        
        # Determine label
        if scores['compound'] >= 0.05:
            label = 'positive'
        elif scores['compound'] <= -0.05:
            label = 'negative'
        else:
            label = 'neutral'
        
        return {
            'label': label,
            'compound': scores['compound'],
            'positive': scores['pos'],
            'neutral': scores['neu'],
            'negative': scores['neg']
        }
    
    def analyze_transformer(self, text):
        """
        Analyze sentiment using transformer model
        
        Args:
            text (str): Input text
            
        Returns:
            dict: Transformer sentiment results or None
        """
        if self.transformer_analyzer is None:
            return None
        
        try:
            # Truncate text if too long
            max_length = 512
            if len(text) > max_length:
                text = text[:max_length]
            
            result = self.transformer_analyzer(text)[0]
            
            # Map labels
            label_mapping = {
                'LABEL_0': 'negative',
                'LABEL_1': 'neutral',
                'LABEL_2': 'positive'
            }
            
            label = label_mapping.get(result['label'], result['label'].lower())
            
            return {
                'label': label,
                'score': result['score']
            }
        except Exception as e:
            print(f"Error in transformer analysis: {e}")
            return None
    
    def analyze(self, text):
        """
        Complete sentiment analysis using multiple methods
        
        Args:
            text (str): Input text
            
        Returns:
            dict: Combined sentiment analysis results
        """
        vader_result = self.analyze_vader(text)
        transformer_result = self.analyze_transformer(text)
        
        # Combine results
        result = {
            'vader': vader_result,
            'label': vader_result['label'],
            'score': vader_result['compound']
        }
        
        if transformer_result:
            result['transformer'] = transformer_result
            # Use transformer result if available (more accurate)
            result['label'] = transformer_result['label']
            result['score'] = transformer_result['score']
        
        return result

138 lines•3.9 KB
python
🚀 Support RSK World

Subscribe to our YouTube channel for latest tutorials & updates!



Click subscribe & support our work ❤️

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer