Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions Emoji_Sentiment_Analyzer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Emoji Sentiment Analyzer

A Python tool that analyzes the sentiment of text based on emojis present in it.

## Description

This Emoji Sentiment Analyzer extracts emojis from text and determines the overall sentiment (positive, negative, or neutral) based on emoji classifications. The tool uses regex patterns to identify emojis and maintains dictionaries of emoji categories to calculate sentiment scores.

## Features

- **Emoji Extraction**: Automatically extracts all emojis from input text using regex patterns
- **Sentiment Analysis**: Categorizes emojis as positive, negative, or neutral
- **Sentiment Scoring**: Calculates a normalized sentiment score based on emoji distribution
- **Detailed Breakdown**: Provides counts of each emoji category found
- **Multiple Text Support**: Can analyze multiple texts in batch

## Requirements

- Python 3.6+
- No external dependencies required (uses only standard library)

## Usage

```python
from emoji_sentiment_analyzer import EmojiSentimentAnalyzer

# Create analyzer instance
analyzer = EmojiSentimentAnalyzer()

# Analyze text
text = "I love this! 😍🥰❤️"
result = analyzer.analyze_sentiment(text)

print(f"Sentiment: {result['sentiment']}")
print(f"Score: {result['score']}")
print(f"Emojis found: {result['emojis_found']}")
```

## Running the Script

```bash
python emoji_sentiment_analyzer.py
```

## Output Format

The analyzer returns a dictionary containing:
- `sentiment`: Overall sentiment classification ('positive', 'negative', or 'neutral')
- `score`: Normalized sentiment score between -1 and 1
- `emoji_count`: Total number of emojis found
- `positive_emojis`: Count of positive emojis
- `negative_emojis`: Count of negative emojis
- `neutral_emojis`: Count of neutral emojis
- `emojis_found`: List of all emojis extracted

## Example Output

```
Emoji Sentiment Analysis Results:
==================================================

Text: I love this! 😍🥰❤️
Sentiment: POSITIVE
Score: 1.0
Emojis found: 😍 🥰 ❤️
Breakdown - Positive: 3, Negative: 0, Neutral: 0
```

## Author

Created as part of the 100 Lines of Python Code project.

## Issue Reference

Resolves #759 - Emoji Sentiment Analyzer
116 changes: 116 additions & 0 deletions Emoji_Sentiment_Analyzer/emoji_sentiment_analyzer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#!/usr/bin/env python3
"""
Emoji Sentiment Analyzer
Analyzes the sentiment of text based on emojis present in it.
"""

import re
from collections import Counter
from typing import Dict, List, Tuple


class EmojiSentimentAnalyzer:
"""Analyzes sentiment based on emojis in text."""

def __init__(self):
"""Initialize the analyzer with emoji sentiment mappings."""
self.positive_emojis = {
'😀', '😃', '😄', '😁', '😆', '😊', '☺️', '😇', '🙂', '🙃',
'😉', '😌', '😍', '🥰', '😘', '😗', '😙', '😚', '🤗', '🤩',
'🥳', '😎', '🤓', '🥸', '👍', '👏', '🙌', '👌', '🤝', '💪',
'❤️', '🧡', '💛', '💚', '💙', '💜', '🤎', '🖤', '🤍', '💖',
'💗', '💓', '💞', '💕', '💟', '❣️', '✨', '⭐', '🌟', '💫'
}

self.negative_emojis = {
'😠', '😡', '🤬', '😤', '😭', '😢', '😥', '😰', '😨', '😱',
'😖', '😣', '😞', '😓', '😩', '😫', '🥺', '😿', '😾', '💔',
'👎', '🙁', '☹️', '😦', '😧', '😮', '😯', '😲', '😳', '🥵',
'🥶', '😬', '🙄', '😑', '😐', '😒', '🙃', '😔', '😕', '🙁'
}

self.neutral_emojis = {
'😐', '😑', '🤔', '🤨', '🧐', '🤫', '🤭', '🙊', '🙉', '🙈',
'💭', '💬', '🗨️', '🗯️', '💤', '👀', '👁️'
}

def extract_emojis(self, text: str) -> List[str]:
"""Extract all emojis from the given text."""
emoji_pattern = re.compile(
"["
"\U0001F600-\U0001F64F" # emoticons
"\U0001F300-\U0001F5FF" # symbols & pictographs
"\U0001F680-\U0001F6FF" # transport & map symbols
"\U0001F1E0-\U0001F1FF" # flags
"\U00002702-\U000027B0"
"\U000024C2-\U0001F251"
"]+", flags=re.UNICODE
)
return emoji_pattern.findall(text)

def analyze_sentiment(self, text: str) -> Dict[str, any]:
"""Analyze sentiment based on emojis in the text."""
emojis = self.extract_emojis(text)

if not emojis:
return {
'sentiment': 'neutral',
'score': 0.0,
'emoji_count': 0,
'details': 'No emojis found'
}

positive_count = sum(1 for emoji in emojis if emoji in self.positive_emojis)
negative_count = sum(1 for emoji in emojis if emoji in self.negative_emojis)
neutral_count = sum(1 for emoji in emojis if emoji in self.neutral_emojis)

total_emojis = len(emojis)
sentiment_score = (positive_count - negative_count) / total_emojis

if sentiment_score > 0.2:
sentiment = 'positive'
elif sentiment_score < -0.2:
sentiment = 'negative'
else:
sentiment = 'neutral'

return {
'sentiment': sentiment,
'score': round(sentiment_score, 2),
'emoji_count': total_emojis,
'positive_emojis': positive_count,
'negative_emojis': negative_count,
'neutral_emojis': neutral_count,
'emojis_found': emojis
}


def main():
"""Main function to demonstrate emoji sentiment analysis."""
analyzer = EmojiSentimentAnalyzer()

# Example texts
test_texts = [
"I love this! 😍🥰❤️",
"This is terrible 😡😤😠",
"Not sure about this 🤔🤨",
"Great day today! 😊👍✨",
"Feeling sad 😢😭💔"
]

print("Emoji Sentiment Analysis Results:")
print("=" * 50)

for text in test_texts:
result = analyzer.analyze_sentiment(text)
print(f"\nText: {text}")
print(f"Sentiment: {result['sentiment'].upper()}")
print(f"Score: {result['score']}")
print(f"Emojis found: {' '.join(result['emojis_found'])}")
print(f"Breakdown - Positive: {result['positive_emojis']}, "
f"Negative: {result['negative_emojis']}, "
f"Neutral: {result['neutral_emojis']}")


if __name__ == "__main__":
main()