Ver3.0 ๐ค ํ์ด์ฌ์ผ๋ก ํธ์ํฐ ์๋ ์์ฝ๋ด ๊ฐ๋ฐํ๊ธฐ: AI ๊ธฐ์ ๋ก ์ ๋ณด์ ํ์๋ฅผ ์ ๋ณตํ๋ ๋ฒ

๐ค ํ์ด์ฌ์ผ๋ก ํธ์ํฐ ์๋ ์์ฝ๋ด ๊ฐ๋ฐํ๊ธฐ: AI ๊ธฐ์ ๋ก ์ ๋ณด์ ํ์๋ฅผ ์ ๋ณตํ๋ ๋ฒ
SNS ์๋, ์์์ง๋ ํธ์์ AI๋ก ๋๋ํ๊ฒ ์์ฝํด๋ณด์ ใ ใ ใ
๐ก ์์ฆ ํธ์ํฐ(ํ X) ํ์๋ผ์ธ ๋ณด๋ฉด ์ง์ง ์ ๋ณด๊ฐ ๋ฏธ์น๋ฏ์ด ์์์ง์์์? ํ๋ฃจ์ ์๋ฐฑ ๊ฐ์ฉ ์ฌ๋ผ์ค๋ ํธ์๋ค ์ค์์ ํต์ฌ๋ง ์์ ๋ฝ์๋ด๊ณ ์ถ์๋ฐ, ์ผ์ผ์ด ๋ค ์ฝ๊ธฐ์ ์๊ฐ๋ ์๊ณ ... ๊ทธ๋์ ์ค๋์ ํ์ด์ฌ๊ณผ AI ๊ธฐ์ ์ ํ์ฉํด์ ํธ์ํฐ ์๋ ์์ฝ๋ด์ ๋ง๋๋ ๋ฐฉ๋ฒ์ ์๋ ค๋๋ฆด๊ฒ์ ใ
ใ
์ด ๊ธ์์๋ ์ค์ ๋ก ์๋ํ๋ ๋ด์ ๋ง๋ค๊ธฐ ์ํ ์ ์ฒด ํ๋ก์ธ์ค๋ฅผ ๋ค๋ฃฐ ๊ฑฐ์์. API ์ฐ๋๋ถํฐ ์์ฐ์ด ์ฒ๋ฆฌ, ๋จธ์ ๋ฌ๋ ๋ชจ๋ธ ์ ์ฉ๊น์ง! ํ๋ก๊ทธ๋๋ฐ ์ด๋ณด์๋ ๋ฐ๋ผํ ์ ์๊ฒ ์ต๋ํ ์ฝ๊ฒ ์ค๋ช
ํ ํ
๋๊น ๊ฑฑ์ ๋
ธ๋
ธ~ ๐
๐ฏ ์ ํธ์ํฐ ์๋ ์์ฝ๋ด์ด ํ์ํ ๊น?
์์งํ ๋งํด์ ์์ฆ SNS๋ ์ ๋ณด ๊ณผ๋ถํ ๊ทธ ์์ฒด์์์ ใ
ใ
ใ
ํนํ ํธ์ํฐ๋ ์ค์๊ฐ์ผ๋ก ์์ฒญ๋ ์์ ์ ๋ณด๊ฐ ์์์ง๋๋ฐ, ์ด๊ฑธ ๋ค ์ฝ์ผ๋ ค๋ฉด ํ๋ฃจ 24์๊ฐ๋ ๋ชจ์๋ผ์. ๊ทธ๋์ ๋ง์ ๊ธฐ์
๋ค๊ณผ ๊ฐ์ธ ๊ฐ๋ฐ์๋ค์ด ์๋ ์์ฝ ์์คํ
์ ์ฃผ๋ชฉํ๊ณ ์์ด์.
์ค์ ๋ก ๋ด์ค ๊ธฐ๊ด, ๋ง์ผํ
ํ์ฌ, ๋ฆฌ์์น ๊ธฐ๊ด๋ค์ ํธ์ํฐ์์ ํธ๋ ๋๋ฅผ ํ์
ํ๊ณ ์ฌ๋ก ์ ๋ถ์ํ๋๋ฐ ์ด๋ฐ ๋ด๋ค์ ํ์ฉํ๊ณ ์๊ฑฐ๋ ์. ๊ฐ์ธ์ ์ผ๋ก๋ ๊ด์ฌ ๋ถ์ผ์ ํต์ฌ ์ ๋ณด๋ง ๋น ๋ฅด๊ฒ ์บ์นํ ์ ์์ผ๋๊น ์์ ์ ์ฉํ์ฃ ๐
๐ ์๋ ์์ฝ๋ด์ ์ฃผ์ ํ์ฉ ์ฌ๋ก
1. ๋ด์ค ๋ชจ๋ํฐ๋ง: ํน์ ํค์๋๋ ํด์ํ๊ทธ ๊ด๋ จ ํธ์์ ์ค์๊ฐ์ผ๋ก ์์งํ๊ณ ์์ฝ
2. ๋ธ๋๋ ๋ชจ๋ํฐ๋ง: ์์ฌ ์ ํ์ด๋ ์๋น์ค์ ๋ํ ์๋น์ ๋ฐ์ ๋ถ์
3. ํธ๋ ๋ ๋ถ์: ์ค์๊ฐ ์ด์์ ์ฌ๋ก ์ ํ๋ฆ ํ์
4. ๊ฒฝ์์ฌ ๋ถ์: ๊ฒฝ์ ์
์ฒด์ ๋ง์ผํ
ํ๋๊ณผ ๊ณ ๊ฐ ๋ฐ์ ์ถ์
5. ๊ฐ์ธ ํ๋ ์ด์
: ๊ด์ฌ ๋ถ์ผ์ ํต์ฌ ์ ๋ณด๋ง ๊ณจ๋ผ์ ๋ฐ์๋ณด๊ธฐ
๐ ๏ธ ๊ฐ๋ฐ ํ๊ฒฝ ์ค๋นํ๊ธฐ
๋ณธ๊ฒฉ์ ์ผ๋ก ์ฝ๋ฉ์ ๋ค์ด๊ฐ๊ธฐ ์ ์ ํ์ํ ๋๊ตฌ๋ค์ ๋จผ์ ์ธํ ํด์ผ๊ฒ ์ฃ ? ์๊ฐ๋ณด๋ค ๊ฐ๋จํด์ ใ ใ
1๏ธโฃ ํ์ด์ฌ ์ค์น
ํ์ด์ฌ 3.8 ์ด์ ๋ฒ์ ์ ๊ถ์ฅํด์. ๊ณต์ ํํ์ด์ง(python.org)์์ ๋ค์ด๋ก๋ ๋ฐ์ผ์๋ฉด ๋ฉ๋๋ค. ์ด๋ฏธ ์ค์น๋์ด ์๋ค๋ฉด ํฐ๋ฏธ๋์์ python --version ๋ช
๋ น์ด๋ก ๋ฒ์ ํ์ธํด๋ณด์ธ์!
2๏ธโฃ ํ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น
์๋ ๋ช ๋ น์ด๋ก ํ์ํ ํจํค์ง๋ค์ ํ ๋ฒ์ ์ค์นํ ์ ์์ด์:
pip install tweepy
pip install transformers
pip install torch
pip install nltk
pip install pandas
pip install python-dotenv
pip install schedule
๐ก ๊ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์ญํ :
โข tweepy: ํธ์ํฐ API์ ํต์ ํ๊ธฐ ์ํ ํ์ด์ฌ ๋ํผ
โข transformers: Hugging Face์ ์ฌ์ ํ์ต๋ NLP ๋ชจ๋ธ ์ฌ์ฉ
โข torch: ๋ฅ๋ฌ๋ ํ๋ ์์ํฌ (transformers์ ๋ฐฑ์๋)
โข nltk: ์์ฐ์ด ์ฒ๋ฆฌ๋ฅผ ์ํ ๊ธฐ๋ณธ ๋๊ตฌ
โข pandas: ๋ฐ์ดํฐ ์ฒ๋ฆฌ ๋ฐ ๋ถ์
โข python-dotenv: ํ๊ฒฝ ๋ณ์ ๊ด๋ฆฌ
โข schedule: ์๋ํ ์ค์ผ์ค๋ง
3๏ธโฃ ํธ์ํฐ API ํค ๋ฐ๊ธ๋ฐ๊ธฐ
ํธ์ํฐ ๊ฐ๋ฐ์ ํฌํธ(developer.twitter.com)์ ์ ์ํด์ ์ฑ์ ๋ฑ๋กํ๊ณ API ํค๋ฅผ ๋ฐ๊ธ๋ฐ์์ผ ํด์. ์์ฆ์ ์ ๋ฃ ํ๋๋ ์์ง๋ง, ๊ธฐ๋ณธ์ ์ธ ํ
์คํธ์ฉ์ผ๋ก๋ ๋ฌด๋ฃ ํฐ์ด๋ก๋ ์ถฉ๋ถํฉ๋๋ค ใ
ใ
๋ฐ๊ธ๋ฐ์์ผ ํ ํค ์ ๋ณด๋ค:
โข API Key (Consumer Key)
โข API Secret Key (Consumer Secret)
โข Access Token
โข Access Token Secret
โข Bearer Token (API v2 ์ฌ์ฉ ์)
๐ API ์ธ์ฆ ์ค์ ํ๊ธฐ
API ํค๋ฅผ ์์ ํ๊ฒ ๊ด๋ฆฌํ๋ ๊ฒ ์ง์ง ์ค์ํด์! ์ ๋๋ก ์ฝ๋์ ์ง์ ํ๋์ฝ๋ฉํ๋ฉด ์ ๋ฉ๋๋ค. ๊นํ๋ธ์ ์ฌ๋ ธ๋ค๊ฐ ํค ํธ๋ฆฌ๋ฉด ์ง์ง ๋๋ฆฌ๋๊ฑฐ๋ ์ ใ ใ
๐ .env ํ์ผ ์์ฑ
ํ๋ก์ ํธ ๋ฃจํธ ๋๋ ํ ๋ฆฌ์ .env ํ์ผ์ ๋ง๋ค๊ณ ์๋์ฒ๋ผ ์์ฑํ์ธ์:
TWITTER_API_KEY=your_api_key_here
TWITTER_API_SECRET=your_api_secret_here
TWITTER_ACCESS_TOKEN=your_access_token_here
TWITTER_ACCESS_SECRET=your_access_secret_here
TWITTER_BEARER_TOKEN=your_bearer_token_here
๊ทธ๋ฆฌ๊ณ ํ์ด์ฌ ์ฝ๋์์๋ ์ด๋ ๊ฒ ๋ถ๋ฌ์์:
import os
from dotenv import load_dotenv
import tweepy
# ํ๊ฒฝ ๋ณ์ ๋ก๋
load_dotenv()
# API ์ธ์ฆ ์ค์
api_key = os.getenv('TWITTER_API_KEY')
api_secret = os.getenv('TWITTER_API_SECRET')
access_token = os.getenv('TWITTER_ACCESS_TOKEN')
access_secret = os.getenv('TWITTER_ACCESS_SECRET')
bearer_token = os.getenv('TWITTER_BEARER_TOKEN')
# Tweepy ํด๋ผ์ด์ธํธ ์์ฑ (API v2)
client = tweepy.Client(
bearer_token=bearer_token,
consumer_key=api_key,
consumer_secret=api_secret,
access_token=access_token,
access_token_secret=access_secret
)
print("ํธ์ํฐ API ์ฐ๊ฒฐ ์ฑ๊ณต! โ
")
โ ๏ธ ๋ณด์ ์ฃผ์์ฌํญ:
โข .env ํ์ผ์ ๋ฐ๋์ .gitignore์ ์ถ๊ฐํ์ธ์
โข API ํค๋ ์ ๋ ๊ณต๊ฐ ์ ์ฅ์์ ์
๋ก๋ํ์ง ๋ง์ธ์
โข ์ ๊ธฐ์ ์ผ๋ก ํค๋ฅผ ์ฌ๋ฐ๊ธํ๋ ๊ฒ๋ ์ข์ ์ต๊ด์
๋๋ค
๐ฆ ํธ์ ๋ฐ์ดํฐ ์์งํ๊ธฐ
์ด์ ๋ณธ๊ฒฉ์ ์ผ๋ก ํธ์์ ๊ฐ์ ธ์๋ณผ๊น์? Tweepy๋ฅผ ์ฌ์ฉํ๋ฉด ์๊ฐ๋ณด๋ค ๊ฐ๋จํด์ ใ ใ
๐ ํค์๋ ๊ธฐ๋ฐ ํธ์ ๊ฒ์
ํน์ ํค์๋๋ ํด์ํ๊ทธ๊ฐ ํฌํจ๋ ํธ์์ ๊ฒ์ํ๋ ๊ธฐ๋ณธ ์ฝ๋์์:
def search_tweets(query, max_results=100):
"""
ํน์ ํค์๋๋ก ํธ์ ๊ฒ์
Args:
query: ๊ฒ์ํ ํค์๋ (์: "ํ์ด์ฌ OR Python")
max_results: ๊ฐ์ ธ์ฌ ํธ์ ๊ฐ์ (์ต๋ 100)
Returns:
ํธ์ ๋ฐ์ดํฐ ๋ฆฌ์คํธ
"""
try:
tweets = client.search_recent_tweets(
query=query,
max_results=max_results,
tweet_fields=['created_at', 'author_id', 'public_metrics', 'lang'],
expansions=['author_id'],
user_fields=['username', 'name']
)
tweet_list = []
if tweets.data:
# ์ฌ์ฉ์ ์ ๋ณด ๋งคํ
users = {user.id: user for user in tweets.includes['users']}
for tweet in tweets.data:
author = users.get(tweet.author_id)
tweet_info = {
'id': tweet.id,
'text': tweet.text,
'created_at': tweet.created_at,
'author_name': author.name if author else 'Unknown',
'author_username': author.username if author else 'Unknown',
'likes': tweet.public_metrics['like_count'],
'retweets': tweet.public_metrics['retweet_count'],
'replies': tweet.public_metrics['reply_count'],
'language': tweet.lang
}
tweet_list.append(tweet_info)
print(f"โ
{len(tweet_list)}๊ฐ์ ํธ์์ ์์งํ์ต๋๋ค!")
return tweet_list
else:
print("๊ฒ์ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค.")
return []
except Exception as e:
print(f"โ ์๋ฌ ๋ฐ์: {e}")
return []
# ์ฌ์ฉ ์์
tweets = search_tweets("์ธ๊ณต์ง๋ฅ -is:retweet lang:ko", max_results=50)
๐ก ๊ฒ์ ์ฟผ๋ฆฌ ํ:
โข -is:retweet: ๋ฆฌํธ์ ์ ์ธ
โข lang:ko: ํ๊ตญ์ด ํธ์๋ง
โข "์ ํํ ๋ฌธ๊ตฌ": ๋ฐ์ดํ๋ก ์ ํํ ๋ฌธ๊ตฌ ๊ฒ์
โข OR: ์ฌ๋ฌ ํค์๋ ์ค ํ๋๋ผ๋ ํฌํจ
โข from:username: ํน์ ์ฌ์ฉ์์ ํธ์๋ง
โข has:images: ์ด๋ฏธ์ง๊ฐ ํฌํจ๋ ํธ์๋ง
๐ ๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ
์์งํ ํธ์ ๋ฐ์ดํฐ๋ฅผ ์์ฝํ๊ธฐ ์ ์ ์ ์ ์์ ์ด ํ์ํด์. URL, ๋ฉ์ , ํด์ํ๊ทธ ๊ฐ์ ๋ถํ์ํ ์์๋ค์ ์ ๊ฑฐํด์ผ ํ๊ฑฐ๋ ์:
import re
import nltk
from nltk.corpus import stopwords
# NLTK ๋ฐ์ดํฐ ๋ค์ด๋ก๋ (์ต์ด 1ํ๋ง)
nltk.download('stopwords', quiet=True)
nltk.download('punkt', quiet=True)
def clean_tweet(text):
"""
ํธ์ ํ
์คํธ ์ ์
"""
# URL ์ ๊ฑฐ
text = re.sub(r'http\S+|www\S+|https\S+', '', text, flags=re.MULTILINE)
# ๋ฉ์
(@username) ์ ๊ฑฐ
text = re.sub(r'@\w+', '', text)
# ํด์ํ๊ทธ ๊ธฐํธ๋ง ์ ๊ฑฐ (๋ด์ฉ์ ์ ์ง)
text = re.sub(r'#', '', text)
# ํน์๋ฌธ์ ์ ๊ฑฐ (ํ๊ธ, ์๋ฌธ, ์ซ์, ๊ณต๋ฐฑ๋ง ์ ์ง)
text = re.sub(r'[^\w\s๊ฐ-ํฃ]', '', text)
# ์ฐ์๋ ๊ณต๋ฐฑ ์ ๊ฑฐ
text = re.sub(r'\s+', ' ', text).strip()
return text
def preprocess_tweets(tweet_list):
"""
ํธ์ ๋ฆฌ์คํธ ์ ์ฒ๋ฆฌ
"""
processed_tweets = []
for tweet in tweet_list:
cleaned_text = clean_tweet(tweet['text'])
# ๋๋ฌด ์งง์ ํธ์์ ์ ์ธ (10์ ๋ฏธ๋ง)
if len(cleaned_text) >= 10:
tweet['cleaned_text'] = cleaned_text
processed_tweets.append(tweet)
print(f"โ
{len(processed_tweets)}๊ฐ์ ํธ์์ด ์ ์ฒ๋ฆฌ๋์์ต๋๋ค!")
return processed_tweets
# ์ฌ์ฉ ์์
processed_tweets = preprocess_tweets(tweets)
๐ง AI ์์ฝ ๋ชจ๋ธ ๊ตฌํํ๊ธฐ
๋๋์ด ํต์ฌ ํํธ! AI ๋ชจ๋ธ์ ์ฌ์ฉํด์ ํธ์์ ์์ฝํ๋ ๋ถ๋ถ์ด์์. ์ฌ๊ธฐ์๋ Hugging Face์ ์ฌ์ ํ์ต๋ ๋ชจ๋ธ์ ํ์ฉํ ๊ฑฐ์์ ใ ใ
๐ค Transformers ๋ชจ๋ธ ๋ก๋
ํ๊ตญ์ด ์์ฝ์ ํนํ๋ ๋ชจ๋ธ์ ์ฌ์ฉํ๋ฉด ๋ ์ข์ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์ด์. ์ฌ๊ธฐ์๋ ๋ค๊ตญ์ด ์ง์ ๋ชจ๋ธ๊ณผ ํ๊ตญ์ด ํนํ ๋ชจ๋ธ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ ์๊ฐํ ๊ฒ์:
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
import torch
class TweetSummarizer:
def __init__(self, model_name='facebook/bart-large-cnn'):
"""
์์ฝ ๋ชจ๋ธ ์ด๊ธฐํ
Args:
model_name: ์ฌ์ฉํ ๋ชจ๋ธ ์ด๋ฆ
- 'facebook/bart-large-cnn': ์์ด ํนํ (๊ธฐ๋ณธ)
- 'csebuetnlp/mT5_multilingual_XLSum': ๋ค๊ตญ์ด ์ง์
- 'eenzeenee/t5-base-korean-summarization': ํ๊ตญ์ด ํนํ
"""
print(f"๐ ๋ชจ๋ธ ๋ก๋ฉ ์ค: {model_name}")
self.device = 0 if torch.cuda.is_available() else -1
try:
self.summarizer = pipeline(
"summarization",
model=model_name,
device=self.device
)
print("โ
๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ!")
except Exception as e:
print(f"โ ๋ชจ๋ธ ๋ก๋ฉ ์คํจ: {e}")
print("๊ธฐ๋ณธ ๋ชจ๋ธ๋ก ์ฌ์๋ํฉ๋๋ค...")
self.summarizer = pipeline(
"summarization",
model='facebook/bart-large-cnn',
device=self.device
)
def summarize_text(self, text, max_length=130, min_length=30):
"""
๋จ์ผ ํ
์คํธ ์์ฝ
"""
try:
# ํ
์คํธ๊ฐ ๋๋ฌด ์งง์ผ๋ฉด ๊ทธ๋๋ก ๋ฐํ
if len(text.split()) < 20:
return text
summary = self.summarizer(
text,
max_length=max_length,
min_length=min_length,
do_sample=False
)
return summary[0]['summary_text']
except Exception as e:
print(f"์์ฝ ์ค ์๋ฌ: {e}")
return text[:100] + "..." # ์๋ฌ ์ ์๋ถ๋ถ๋ง ๋ฐํ
def summarize_multiple_tweets(self, tweets, combine=True):
"""
์ฌ๋ฌ ํธ์ ์์ฝ
Args:
tweets: ํธ์ ๋ฆฌ์คํธ
combine: True๋ฉด ๋ชจ๋ ํธ์์ ํฉ์ณ์ ํ๋๋ก ์์ฝ,
False๋ฉด ๊ฐ๊ฐ ์์ฝ
"""
if combine:
# ๋ชจ๋ ํธ์์ ํ๋๋ก ํฉ์น๊ธฐ
combined_text = " ".join([t['cleaned_text'] for t in tweets])
# ๋๋ฌด ๊ธธ๋ฉด ์๋ผ๋ด๊ธฐ (๋ชจ๋ธ์ ์ต๋ ์
๋ ฅ ๊ธธ์ด ์ ํ)
if len(combined_text.split()) > 1000:
words = combined_text.split()[:1000]
combined_text = " ".join(words)
summary = self.summarize_text(combined_text, max_length=200, min_length=50)
return {
'type': 'combined',
'summary': summary,
'tweet_count': len(tweets)
}
else:
# ๊ฐ ํธ์ ๊ฐ๋ณ ์์ฝ
summaries = []
for tweet in tweets:
summary = self.summarize_text(
tweet['cleaned_text'],
max_length=100,
min_length=20
)
summaries.append({
'original': tweet['text'],
'summary': summary,
'author': tweet['author_name'],
'likes': tweet['likes']
})
return {
'type': 'individual',
'summaries': summaries
}
# ์ฌ์ฉ ์์
summarizer = TweetSummarizer()
# ๋ฐฉ๋ฒ 1: ๋ชจ๋ ํธ์์ ํฉ์ณ์ ํ๋์ ์์ฝ ์์ฑ
combined_summary = summarizer.summarize_multiple_tweets(processed_tweets, combine=True)
print("\n๐ ํตํฉ ์์ฝ:")
print(combined_summary['summary'])
# ๋ฐฉ๋ฒ 2: ๊ฐ ํธ์์ ๊ฐ๋ณ์ ์ผ๋ก ์์ฝ
individual_summaries = summarizer.summarize_multiple_tweets(processed_tweets, combine=False)
print("\n๐ ๊ฐ๋ณ ์์ฝ:")
for i, item in enumerate(individual_summaries['summaries'][:5], 1):
print(f"\n{i}. {item['author']} (์ข์์ {item['likes']}๊ฐ)")
print(f" ์์ฝ: {item['summary']}")
๐ก ๋ชจ๋ธ ์ ํ ๊ฐ์ด๋:
์์ด ํธ์ ์์ฝ:
โข facebook/bart-large-cnn (์ถ์ฒ! ์ฑ๋ฅ ์ข์)
โข google/pegasus-xsum (๋ด์ค ์์ฝ์ ํนํ)
ํ๊ตญ์ด ํธ์ ์์ฝ:
โข eenzeenee/t5-base-korean-summarization
โข lcw99/t5-base-korean-text-summary
๋ค๊ตญ์ด ์ง์:
โข csebuetnlp/mT5_multilingual_XLSum
โข facebook/mbart-large-50 (50๊ฐ ์ธ์ด ์ง์)
โ ๏ธ ์ฑ๋ฅ ์ต์ ํ ํ:
โข GPU๊ฐ ์์ผ๋ฉด ํจ์ฌ ๋น ๋ฆ
๋๋ค (CUDA ์ค์น ๊ถ์ฅ)
โข ๋ชจ๋ธ ์ฒซ ๋ก๋ฉ ์ ์๊ฐ์ด ์ข ๊ฑธ๋ ค์ (์ธํฐ๋ท์์ ๋ค์ด๋ก๋)
โข ๋ฉ๋ชจ๋ฆฌ๊ฐ ๋ถ์กฑํ๋ฉด ๋ ์์ ๋ชจ๋ธ ์ฌ์ฉ (์: t5-small)
โข ๋ฐฐ์น ์ฒ๋ฆฌ๋ก ์ฌ๋ฌ ํธ์์ ํ ๋ฒ์ ์ฒ๋ฆฌํ๋ฉด ํจ์จ์
๐ ๊ณ ๊ธ ๊ธฐ๋ฅ: ๊ฐ์ฑ ๋ถ์ ์ถ๊ฐํ๊ธฐ
์์ฝ๋ง ํ๋ฉด ์ข ์ฌ์ฌํ์์์? ใ ใ ํธ์์ ๊ฐ์ (๊ธ์ /๋ถ์ /์ค๋ฆฝ)๋ ๋ถ์ํด์ ํจ๊ป ๋ณด์ฌ์ฃผ๋ฉด ํจ์ฌ ์ ์ฉํด์!
from transformers import pipeline
class SentimentAnalyzer:
def __init__(self):
"""๊ฐ์ฑ ๋ถ์ ๋ชจ๋ธ ์ด๊ธฐํ"""
print("๐ ๊ฐ์ฑ ๋ถ์ ๋ชจ๋ธ ๋ก๋ฉ ์ค...")
# ๋ค๊ตญ์ด ๊ฐ์ฑ ๋ถ์ ๋ชจ๋ธ
self.analyzer = pipeline(
"sentiment-analysis",
model="cardiffnlp/twitter-xlm-roberta-base-sentiment",
device=0 if torch.cuda.is_available() else -1
)
print("โ
๊ฐ์ฑ ๋ถ์ ๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ!")
def analyze(self, text):
"""
ํ
์คํธ์ ๊ฐ์ฑ ๋ถ์
Returns:
dict: {'label': 'positive/negative/neutral', 'score': 0.95}
"""
try:
result = self.analyzer(text[:512])[0] # ์ต๋ 512์๊น์ง
# ๋ผ๋ฒจ ํ๊ธํ
label_map = {
'LABEL_0': 'negative',
'LABEL_1': 'neutral',
'LABEL_2': 'positive',
'negative': '๋ถ์ ',
'neutral': '์ค๋ฆฝ',
'positive': '๊ธ์ '
}
label = label_map.get(result['label'], result['label'])
label_kr = label_map.get(label, label)
return {
'label': label,
'label_kr': label_kr,
'score': round(result['score'], 3)
}
except Exception as e:
print(f"๊ฐ์ฑ ๋ถ์ ์๋ฌ: {e}")
return {'label': 'unknown', 'label_kr': '์ ์ ์์', 'score': 0}
def analyze_tweets(self, tweets):
"""์ฌ๋ฌ ํธ์์ ๊ฐ์ฑ ๋ถ์ ๋ฐ ํต๊ณ"""
sentiments = []
for tweet in tweets:
sentiment = self.analyze(tweet['cleaned_text'])
sentiments.append(sentiment)
# ํต๊ณ ๊ณ์ฐ
positive_count = sum(1 for s in sentiments if s['label'] == 'positive')
negative_count = sum(1 for s in sentiments if s['label'] == 'negative')
neutral_count = sum(1 for s in sentiments if s['label'] == 'neutral')
total = len(sentiments)
stats = {
'positive': {'count': positive_count, 'ratio': positive_count/total if total > 0 else 0},
'negative': {'count': negative_count, 'ratio': negative_count/total if total > 0 else 0},
'neutral': {'count': neutral_count, 'ratio': neutral_count/total if total > 0 else 0},
'total': total
}
return sentiments, stats
# ์ฌ์ฉ ์์
sentiment_analyzer = SentimentAnalyzer()
sentiments, stats = sentiment_analyzer.analyze_tweets(processed_tweets)
print("\n๐ ๊ฐ์ฑ ๋ถ์ ๊ฒฐ๊ณผ:")
print(f"๊ธ์ : {stats['positive']['count']}๊ฐ ({stats['positive']['ratio']*100:.1f}%)")
print(f"์ค๋ฆฝ: {stats['neutral']['count']}๊ฐ ({stats['neutral']['ratio']*100:.1f}%)")
print(f"๋ถ์ : {stats['negative']['count']}๊ฐ ({stats['negative']['ratio']*100:.1f}%)")
๐จ ๊ฒฐ๊ณผ ์๊ฐํ ๋ฐ ๋ฆฌํฌํธ ์์ฑ
๋ถ์ ๊ฒฐ๊ณผ๋ฅผ ์์๊ฒ ์ ๋ฆฌํด์ ๋ณด์ฌ์ฃผ๋ ๊ฒ๋ ์ค์ํ์ฃ ! ๊ฐ๋จํ HTML ๋ฆฌํฌํธ๋ฅผ ์์ฑํ๋ ์ฝ๋์์:
from datetime import datetime
import json
class ReportGenerator:
def __init__(self):
self.timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
def generate_html_report(self, summary_data, sentiment_stats, tweets, filename="report.html"):
"""
HTML ํ์์ ๋ฆฌํฌํธ ์์ฑ
"""
html_template = f"""
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ํธ์ํฐ ์์ฝ ๋ฆฌํฌํธ</title>
<style>.tx-header{background:linear-gradient(135deg,#667eea 0%,#764ba2 100%);
color:#fff;
padding:40px 25px;
border-radius:16px;
text-align:center;
margin-bottom:35px;
box-shadow:0 8px 20px rgba(102,126,234,0.3)}
.tx-intro{background:#f8f9fa;
padding:25px;
border-radius:12px;
margin:25px 0;
border-left:5px solid #667eea}
.tx-section{margin:35px 0}
.tx-box{background:#fff;
border:2px solid #e9ecef;
padding:20px;
border-radius:12px;
margin:20px 0}
.tx-highlight{background:linear-gradient(120deg,#fef3c7 0%,#fde68a 100%);
padding:3px 8px;
border-radius:4px;
font-weight:600}
.tx-tip{background:#e0f2fe;
border-left:4px solid #0284c7;
padding:15px;
border-radius:8px;
margin:20px 0}
.tx-warning{background:#fef3c7;
border-left:4px solid #f59e0b;
padding:15px;
border-radius:8px;
margin:20px 0}
.tx-table{width:100%;
border-collapse:collapse;
margin:20px 0}
.tx-table th{background:#667eea;
color:#fff;
padding:12px;
text-align:left}
.tx-table td{padding:12px;
border-bottom:1px solid #e9ecef}
.tx-step{background:#f1f5f9;
padding:15px;
border-radius:8px;
margin:15px 0;
border-left:4px solid #8b5cf6}
.tx-emoji{font-size:1.3em;
margin:0 5px}</style>
</head>
<body>
<div class="header">
<h1>๐ฆ ํธ์ํฐ ์๋ ์์ฝ ๋ฆฌํฌํธ</h1>
<p>์์ฑ ์๊ฐ: {self.timestamp}</p>
<p>๋ถ์๋ ํธ์ ์: {len(tweets)}๊ฐ</p>
</div>
<div class="summary-box">
<h2>๐ ํต์ฌ ์์ฝ</h2>
<p style="font-size: 1.1em; line-height: 1.6;">
{summary_data['summary']}
</p>
</div>
<h2>๐ ๊ฐ์ฑ ๋ถ์ ๊ฒฐ๊ณผ</h2>
<div class="sentiment-stats">
<div class="stat-card positive">
<h3>๊ธ์ </h3>
<p style="font-size: 2em; margin: 10px 0;">{sentiment_stats['positive']['count']}</p>
<p>{sentiment_stats['positive']['ratio']*100:.1f}%</p>
</div>
<div class="stat-card neutral">
<h3>์ค๋ฆฝ</h3>
<p style="font-size: 2em; margin: 10px 0;">{sentiment_stats['neutral']['count']}</p>
<p>{sentiment_stats['neutral']['ratio']*100:.1f}%</p>
</div>
<div class="stat-card negative">
<h3>๋ถ์ </h3>
<p style="font-size: 2em; margin: 10px 0;">{sentiment_stats['negative']['count']}</p>
<p>{sentiment_stats['negative']['ratio']*100:.1f}%</p>
</div>
</div>
<div class="summary-box">
<h2>๐ฅ ์ธ๊ธฐ ํธ์ TOP 5</h2>
{self._generate_top_tweets_html(tweets[:5])}
</div>
<div style="text-align: center; margin-top: 30px; color: #6b7280;">
<p>Powered by Python & AI ๐ค</p>
</div>
</body>
</html>
"""
with open(filename, 'w', encoding='utf-8') as f:
f.write(html_template)
print(f"โ
๋ฆฌํฌํธ๊ฐ ์์ฑ๋์์ต๋๋ค: {filename}")
return filename
def _generate_top_tweets_html(self, tweets):
"""์ธ๊ธฐ ํธ์ HTML ์์ฑ"""
html = ""
for i, tweet in enumerate(tweets, 1):
html += f"""
<div class="tweet-item">
<strong>{i}. @{tweet['author_username']}</strong>
<p>{tweet['text'][:200]}...</p>
<small>โค๏ธ {tweet['likes']} | ๐ {tweet['retweets']} | ๐ฌ {tweet['replies']}</small>
</div>
"""
return html
def save_json_report(self, data, filename="report.json"):
"""JSON ํ์์ผ๋ก ์ ์ฅ"""
with open(filename, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=2)
print(f"โ
JSON ๋ฆฌํฌํธ ์ ์ฅ ์๋ฃ: {filename}")
# ์ฌ์ฉ ์์
reporter = ReportGenerator()
# ํธ์์ ์ข์์ ์์ผ๋ก ์ ๋ ฌ
sorted_tweets = sorted(processed_tweets, key=lambda x: x['likes'], reverse=True)
# HTML ๋ฆฌํฌํธ ์์ฑ
reporter.generate_html_report(
combined_summary,
stats,
sorted_tweets,
filename="twitter_summary_report.html"
)
# JSON ๋ฐ์ดํฐ๋ ์ ์ฅ
report_data = {
'timestamp': reporter.timestamp,
'summary': combined_summary,
'sentiment_stats': stats,
'top_tweets': sorted_tweets[:10]
}
reporter.save_json_report(report_data, "twitter_summary_data.json")
โก ์๋ํ ์ค์ผ์ค๋ง ๊ตฌํํ๊ธฐ
์ด์ ์ด ๋ชจ๋ ๊ฑธ ์๋์ผ๋ก ์คํ๋๊ฒ ๋ง๋ค์ด๋ณผ๊น์? ๋งค์ผ ์ ํด์ง ์๊ฐ์ ์๋์ผ๋ก ํธ์์ ์์งํ๊ณ ์์ฝํด์ฃผ๋ ๋ด์ ๋ง๋ค์ด๋ด ์๋ค ใ ใ
import schedule
import time
from datetime import datetime
class TwitterSummaryBot:
def __init__(self, search_query, max_tweets=50):
"""
์๋ ์์ฝ ๋ด ์ด๊ธฐํ
Args:
search_query: ๊ฒ์ํ ํค์๋
max_tweets: ์์งํ ์ต๋ ํธ์ ์
"""
self.search_query = search_query
self.max_tweets = max_tweets
self.summarizer = TweetSummarizer()
self.sentiment_analyzer = SentimentAnalyzer()
self.reporter = ReportGenerator()
print(f"๐ค ๋ด ์ด๊ธฐํ ์๋ฃ!")
print(f"๊ฒ์ ํค์๋: {search_query}")
print(f"์์ง ํธ์ ์: {max_tweets}๊ฐ")
def run_summary_job(self):
"""์์ฝ ์์
์คํ"""
print(f"\n{'='*50}")
print(f"๐ ์์
์์: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print(f"{'='*50}\n")
try:
# 1. ํธ์ ์์ง
print("1๏ธโฃ ํธ์ ์์ง ์ค...")
tweets = search_tweets(self.search_query, self.max_tweets)
if not tweets:
print("โ ์์ง๋ ํธ์์ด ์์ต๋๋ค.")
return
# 2. ์ ์ฒ๋ฆฌ
print("2๏ธโฃ ๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ ์ค...")
processed = preprocess_tweets(tweets)
# 3. ์์ฝ ์์ฑ
print("3๏ธโฃ AI ์์ฝ ์์ฑ ์ค...")
summary = self.summarizer.summarize_multiple_tweets(processed, combine=True)
# 4. ๊ฐ์ฑ ๋ถ์
print("4๏ธโฃ ๊ฐ์ฑ ๋ถ์ ์ค...")
sentiments, stats = self.sentiment_analyzer.analyze_tweets(processed)
# 5. ๋ฆฌํฌํธ ์์ฑ
print("5๏ธโฃ ๋ฆฌํฌํธ ์์ฑ ์ค...")
sorted_tweets = sorted(processed, key=lambda x: x['likes'], reverse=True)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
html_filename = f"report_{timestamp}.html"
json_filename = f"data_{timestamp}.json"
self.reporter.generate_html_report(
summary, stats, sorted_tweets, html_filename
)
report_data = {
'timestamp': datetime.now().isoformat(),
'query': self.search_query,
'summary': summary,
'sentiment_stats': stats,
'tweet_count': len(processed),
'top_tweets': sorted_tweets[:10]
}
self.reporter.save_json_report(report_data, json_filename)
print(f"\nโ
์์
์๋ฃ!")
print(f"๐ ์์ฝ: {summary['summary'][:100]}...")
print(f"๐ ๊ธ์ : {stats['positive']['ratio']*100:.1f}% | "
f"๐ ์ค๋ฆฝ: {stats['neutral']['ratio']*100:.1f}% | "
f"๐ ๋ถ์ : {stats['negative']['ratio']*100:.1f}%")
except Exception as e:
print(f"โ ์์
์ค ์๋ฌ ๋ฐ์: {e}")
import traceback
traceback.print_exc()
def start_scheduler(self, schedule_times=['09:00', '15:00', '21:00']):
"""
์ค์ผ์ค๋ฌ ์์
Args:
schedule_times: ์คํํ ์๊ฐ ๋ฆฌ์คํธ (์: ['09:00', '15:00'])
"""
print(f"\nโฐ ์ค์ผ์ค๋ฌ ์์!")
print(f"์คํ ์๊ฐ: {', '.join(schedule_times)}")
# ๊ฐ ์๊ฐ์ ์์
์ค์ผ์ค๋ง
for time_str in schedule_times:
schedule.every().day.at(time_str).do(self.run_summary_job)
print(f" - ๋งค์ผ {time_str}์ ์คํ ์์ฝ๋จ")
# ์ฆ์ ํ ๋ฒ ์คํ
print("\n๐ฌ ์ฒซ ์คํ์ ์์ํฉ๋๋ค...")
self.run_summary_job()
# ์ค์ผ์ค๋ฌ ๋ฃจํ
print("\nโณ ๋ค์ ์คํ ๋๊ธฐ ์ค... (Ctrl+C๋ก ์ข
๋ฃ)")
try:
while True:
schedule.run_pending()
time.sleep(60) # 1๋ถ๋ง๋ค ์ฒดํฌ
except KeyboardInterrupt:
print("\n\n๐ ๋ด์ ์ข
๋ฃํฉ๋๋ค.")
# ์ฌ์ฉ ์์
if __name__ == "__main__":
# ๋ด ์์ฑ
bot = TwitterSummaryBot(
search_query="ํ์ด์ฌ OR Python -is:retweet lang:ko",
max_tweets=50
)
# ์ค์ผ์ค๋ฌ ์์ (๋งค์ผ 9์, 15์, 21์์ ์คํ)
bot.start_scheduler(['09:00', '15:00', '21:00'])
# ๋๋ ์ฆ์ ํ ๋ฒ๋ง ์คํํ๋ ค๋ฉด:
# bot.run_summary_job()
๐ก ์ค์ผ์ค๋ง ์ต์
:
๋งค์ผ ํน์ ์๊ฐ:
schedule.every().day.at("09:00").do(job)
๋งค N์๊ฐ๋ง๋ค:
schedule.every(3).hours.do(job)
๋งค์ฃผ ํน์ ์์ผ:
schedule.every().monday.at("10:00").do(job)
๋งค N๋ถ๋ง๋ค:
schedule.every(30).minutes.do(job)
๐ง ์ค์ ๋ฐฐํฌ: ์๋ฒ์์ 24์๊ฐ ์คํํ๊ธฐ
๋ก์ปฌ ์ปดํจํฐ์์๋ง ๋๋ฆฌ๋ฉด ์ปดํจํฐ ๊บผ์ง๋ฉด ๋ด๋ ๋ฉ์ถ์์์ ใ ใ ํด๋ผ์ฐ๋ ์๋ฒ์ ๋ฐฐํฌํด์ 24์๊ฐ ๋์๊ฐ๊ฒ ๋ง๋ค์ด๋ด ์๋ค!
โ๏ธ AWS EC2 ๋๋ Google Cloud์ ๋ฐฐํฌํ๊ธฐ
๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ๋ฐฐํฌ ๋ฐฉ๋ฒ์ด์์. ๋ฌด๋ฃ ํฐ์ด๋ก๋ ์ถฉ๋ถํ ๋๋ฆด ์ ์์ด์!
1. ์๋ฒ ์ธ์คํด์ค ์์ฑ
โข AWS EC2 t2.micro (ํ๋ฆฌํฐ์ด) ๋๋ Google Cloud e2-micro ์ธ์คํด์ค ์์ฑ
โข Ubuntu 20.04 LTS ์ด๋ฏธ์ง ์ ํ
โข SSH ์ ์ ์ค์
2. ์๋ฒ ํ๊ฒฝ ์ค์
SSH๋ก ์ ์ํ ํ ์๋ ๋ช ๋ น์ด ์คํ:
# ์์คํ
์
๋ฐ์ดํธ
sudo apt update && sudo apt upgrade -y
# Python 3.9 ์ค์น
sudo apt install python3.9 python3-pip -y
# Git ์ค์น
sudo apt install git -y
# ํ๋ก์ ํธ ํด๋ก
git clone https://github.com/your-username/twitter-summary-bot.git
cd twitter-summary-bot
# ๊ฐ์ํ๊ฒฝ ์์ฑ
python3 -m venv venv
source venv/bin/activate
# ํจํค์ง ์ค์น
pip install -r requirements.txt
# .env ํ์ผ ์์ฑ (API ํค ์
๋ ฅ)
nano .env
3. systemd ์๋น์ค๋ก ๋ฑ๋ก (์๋ ์ฌ์์)
์๋ฒ๊ฐ ์ฌ๋ถํ ๋์ด๋ ์๋์ผ๋ก ๋ด์ด ์คํ๋๊ฒ ์ค์ :
# ์๋น์ค ํ์ผ ์์ฑ
sudo nano /etc/systemd/system/twitter-bot.service
# ์๋ ๋ด์ฉ ์
๋ ฅ:
[Unit]
Description=Twitter Summary Bot
After=network.target
[Service]
Type=simple
User=ubuntu
WorkingDirectory=/home/ubuntu/twitter-summary-bot
Environment="PATH=/home/ubuntu/twitter-summary-bot/venv/bin"
ExecStart=/home/ubuntu/twitter-summary-bot/venv/bin/python main.py
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
# ์๋น์ค ํ์ฑํ ๋ฐ ์์
sudo systemctl daemon-reload
sudo systemctl enable twitter-bot
sudo systemctl start twitter-bot
# ์ํ ํ์ธ
sudo systemctl status twitter-bot
# ๋ก๊ทธ ํ์ธ
sudo journalctl -u twitter-bot -f
๐ณ Docker๋ก ๋ฐฐํฌํ๊ธฐ (์ถ์ฒ!)
Docker๋ฅผ ์ฌ์ฉํ๋ฉด ํ๊ฒฝ ์ค์ ์ด ํจ์ฌ ๊ฐํธํด์:
# Dockerfile ์์ฑ
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "main.py"]
# docker-compose.yml ์์ฑ
version: '3.8'
services:
twitter-bot:
build: .
container_name: twitter-summary-bot
restart: unless-stopped
env_file:
- .env
volumes:
- ./reports:/app/reports
- ./data:/app/data
# ์คํ
docker-compose up -d
# ๋ก๊ทธ ํ์ธ
docker-compose logs -f
๐ฑ ๊ฒฐ๊ณผ ์๋ฆผ ๊ธฐ๋ฅ ์ถ๊ฐํ๊ธฐ
์์ฝ์ด ์๋ฃ๋๋ฉด ์๋์ผ๋ก ์๋ฆผ์ ๋ฐ์ผ๋ฉด ์ข๊ฒ ์ฃ ? ํ ๋ ๊ทธ๋จ ๋ด์ด๋ ์ด๋ฉ์ผ๋ก ๊ฒฐ๊ณผ๋ฅผ ์ ์กํ๋ ๊ธฐ๋ฅ์ ์ถ๊ฐํด๋ด ์๋ค!
๐จ ํ ๋ ๊ทธ๋จ ์๋ฆผ
import requests
class TelegramNotifier:
def __init__(self, bot_token, chat_id):
"""
ํ
๋ ๊ทธ๋จ ์๋ฆผ ์ด๊ธฐํ
Args:
bot_token: ํ
๋ ๊ทธ๋จ ๋ด ํ ํฐ (@BotFather์์ ๋ฐ๊ธ)
chat_id: ๋ฉ์์ง๋ฅผ ๋ฐ์ ์ฑํ
ID
"""
self.bot_token = bot_token
self.chat_id = chat_id
self.api_url = f"https://api.telegram.org/bot{bot_token}"
def send_message(self, text):
"""ํ
์คํธ ๋ฉ์์ง ์ ์ก"""
url = f"{self.api_url}/sendMessage"
data = {
'chat_id': self.chat_id,
'text': text,
'parse_mode': 'HTML'
}
try:
response = requests.post(url, data=data)
if response.status_code == 200:
print("โ
ํ
๋ ๊ทธ๋จ ์๋ฆผ ์ ์ก ์๋ฃ!")
else:
print(f"โ ์๋ฆผ ์ ์ก ์คํจ: {response.text}")
except Exception as e:
print(f"โ ์๋ฌ: {e}")
def send_summary_report(self, summary_data, sentiment_stats):
"""์์ฝ ๋ฆฌํฌํธ ์ ์ก"""
message = f"""
๐ฆ <b>ํธ์ํฐ ์์ฝ ๋ฆฌํฌํธ</b>
๐ <b>ํต์ฌ ์์ฝ:</b>
{summary_data['summary'][:500]}...
๐ <b>๊ฐ์ฑ ๋ถ์:</b>
๊ธ์ : {sentiment_stats['positive']['ratio']*100:.1f}%
์ค๋ฆฝ: {sentiment_stats['neutral']['ratio']*100:.1f}%
๋ถ์ : {sentiment_stats['negative']['ratio']*100:.1f}%
๐ ๋ถ์๋ ํธ์: {summary_data['tweet_count']}๊ฐ
โฐ ์์ฑ ์๊ฐ: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
"""
self.send_message(message)
def send_file(self, file_path):
"""ํ์ผ ์ ์ก (HTML ๋ฆฌํฌํธ ๋ฑ)"""
url = f"{self.api_url}/sendDocument"
with open(file_path, 'rb') as file:
files = {'document': file}
data = {'chat_id': self.chat_id}
try:
response = requests.post(url, data=data, files=files)
if response.status_code == 200:
print(f"โ
ํ์ผ ์ ์ก ์๋ฃ: {file_path}")
except Exception as e:
print(f"โ ํ์ผ ์ ์ก ์๋ฌ: {e}")
# .env ํ์ผ์ ์ถ๊ฐ
# TELEGRAM_BOT_TOKEN=your_bot_token
# TELEGRAM_CHAT_ID=your_chat_id
# ์ฌ์ฉ ์์
telegram = TelegramNotifier(
bot_token=os.getenv('TELEGRAM_BOT_TOKEN'),
chat_id=os.getenv('TELEGRAM_CHAT_ID')
)
# ์์ฝ ์๋ฃ ํ ์๋ฆผ ์ ์ก
telegram.send_summary_report(combined_summary, stats)
telegram.send_file("twitter_summary_report.html")
โ๏ธ ์ด๋ฉ์ผ ์๋ฆผ
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
class EmailNotifier:
def __init__(self, smtp_server, smtp_port, sender_email, sender_password):
"""
์ด๋ฉ์ผ ์๋ฆผ ์ด๊ธฐํ
Args:
smtp_server: SMTP ์๋ฒ (์: smtp.gmail.com)
smtp_port: ํฌํธ ๋ฒํธ (์ผ๋ฐ์ ์ผ๋ก 587)
sender_email: ๋ฐ์ ์ ์ด๋ฉ์ผ
sender_password: ์ด๋ฉ์ผ ๋น๋ฐ๋ฒํธ ๋๋ ์ฑ ๋น๋ฐ๋ฒํธ
"""
self.smtp_server = smtp_server
self.smtp_port = smtp_port
self.sender_email = sender_email
self.sender_password = sender_password
def send_email(self, recipient_email, subject, html_content, attachments=None):
"""
์ด๋ฉ์ผ ์ ์ก
Args:
recipient_email: ์์ ์ ์ด๋ฉ์ผ
subject: ์ ๋ชฉ
html_content: HTML ๋ณธ๋ฌธ
attachments: ์ฒจ๋ถ ํ์ผ ๋ฆฌ์คํธ
"""
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = self.sender_email
msg['To'] = recipient_email
# HTML ๋ณธ๋ฌธ ์ถ๊ฐ
html_part = MIMEText(html_content, 'html')
msg.attach(html_part)
# ์ฒจ๋ถ ํ์ผ ์ถ๊ฐ
if attachments:
for file_path in attachments:
with open(file_path, 'rb') as f:
attachment = MIMEApplication(f.read())
attachment.add_header(
'Content-Disposition',
'attachment',
filename=file_path.split('/')[-1]
)
msg.attach(attachment)
try:
# SMTP ์๋ฒ ์ฐ๊ฒฐ ๋ฐ ์ ์ก
with smtplib.SMTP(self.smtp_server, self.smtp_port) as server:
server.starttls()
server.login(self.sender_email, self.sender_password)
server.send_message(msg)
print(f"โ
์ด๋ฉ์ผ ์ ์ก ์๋ฃ: {recipient_email}")
except Exception as e:
print(f"โ ์ด๋ฉ์ผ ์ ์ก ์คํจ: {e}")
def send_summary_report(self, recipient_email, summary_data, sentiment_stats, report_file):
"""์์ฝ ๋ฆฌํฌํธ ์ด๋ฉ์ผ ์ ์ก"""
subject = f"๐ฆ ํธ์ํฐ ์์ฝ ๋ฆฌํฌํธ - {datetime.now().strftime('%Y-%m-%d')}"
html_content = f"""
<html>
<body style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
<div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white; padding: 20px; border-radius: 10px;">
<h1>๐ฆ ํธ์ํฐ ์๋ ์์ฝ ๋ฆฌํฌํธ</h1>
<p>{datetime.now().strftime('%Y๋
%m์ %d์ผ %H:%M')}</p>
</div>
<div style="padding: 20px; background: #f5f5f5; margin-top: 20px; border-radius: 10px;">
<h2>๐ ํต์ฌ ์์ฝ</h2>
<p style="line-height: 1.6;">{summary_data['summary']}</p>
</div>
<div style="padding: 20px; margin-top: 20px;">
<h2>๐ ๊ฐ์ฑ ๋ถ์ ๊ฒฐ๊ณผ</h2>
<table style="width: 100%; text-align: center;">
<tr>
<td style="padding: 10px; background: #d1fae5; border-radius: 5px;">
<strong>๊ธ์ </strong><br>
{sentiment_stats['positive']['ratio']*100:.1f}%
</td>
<td style="padding: 10px; background: #e5e7eb; border-radius: 5px;">
<strong>์ค๋ฆฝ</strong><br>
{sentiment_stats['neutral']['ratio']*100:.1f}%
</td>
<td style="padding: 10px; background: #fee2e2; border-radius: 5px;">
<strong>๋ถ์ </strong><br>
{sentiment_stats['negative']['ratio']*100:.1f}%
</td>
</tr>
</table>
</div>
<div style="text-align: center; margin-top: 30px; color: #6b7280;">
<p>์์ธ ๋ฆฌํฌํธ๋ ์ฒจ๋ถ ํ์ผ์ ํ์ธํด์ฃผ์ธ์!</p>
<p>Powered by Python & AI ๐ค</p>
</div>
</body>
</html>
"""
self.send_email(
recipient_email,
subject,
html_content,
attachments=[report_file]
)
# ์ฌ์ฉ ์์
email_notifier = EmailNotifier(
smtp_server='smtp.gmail.com',
smtp_port=587,
sender_email=os.getenv('SENDER_EMAIL'),
sender_password=os.getenv('EMAIL_PASSWORD')
)
email_notifier.send_summary_report(
recipient_email='your@email.com',
summary_data=combined_summary,
sentiment_stats=stats,
report_file='twitter_summary_report.html'
)
โ ๏ธ Gmail ์ฌ์ฉ ์ ์ฃผ์์ฌํญ:
โข 2๋จ๊ณ ์ธ์ฆ์ ํ์ฑํํ๊ณ '์ฑ ๋น๋ฐ๋ฒํธ'๋ฅผ ์์ฑํด์ผ ํด์
โข Google ๊ณ์ ์ค์ โ ๋ณด์ โ ์ฑ ๋น๋ฐ๋ฒํธ์์ ์์ฑ ๊ฐ๋ฅ
โข ์ผ๋ฐ ๋น๋ฐ๋ฒํธ๋ก๋ ๋ก๊ทธ์ธ์ด ์ ๋ ์ ์์ด์!
๐ ์ค์ ํ์ฉ ์ฌ๋ก ๋ฐ ํ
์ด์ ์ค์ ๋ก ์ด๋ป๊ฒ ํ์ฉํ ์ ์๋์ง ๊ตฌ์ฒด์ ์ธ ์์๋ค์ ์๋ ค๋๋ฆด๊ฒ์ ใ ใ
๐ผ ๋น์ฆ๋์ค ํ์ฉ ์ฌ๋ก
1. ๋ธ๋๋ ๋ชจ๋ํฐ๋ง ๋ด
์์ฌ ๋ธ๋๋๋ ์ ํ๋ช
์ ์ค์๊ฐ์ผ๋ก ๋ชจ๋ํฐ๋งํ๊ณ , ๊ณ ๊ฐ ๋ฐ์์ ์์ฝํด์ ๋ง์ผํ
ํ์ ์ ๋ฌํ ์ ์์ด์. ํนํ ์ ์ ํ ์ถ์ ํ ์ด๊ธฐ ๋ฐ์์ ๋น ๋ฅด๊ฒ ํ์
ํ๋ ๋ฐ ์ ์ฉํ์ฃ !
# ๋ธ๋๋ ๋ชจ๋ํฐ๋ง ์์
brand_bot = TwitterSummaryBot(
search_query="(์ฐ๋ฆฌํ์ฌ OR ์ฐ๋ฆฌ์ ํ) -is:retweet lang:ko",
max_tweets=100
)
# 1์๊ฐ๋ง๋ค ์ฒดํฌ
bot.start_scheduler(['09:00', '12:00', '15:00', '18:00', '21:00'])
2. ๊ฒฝ์์ฌ ๋ถ์ ๋ด
๊ฒฝ์์ฌ์ ๋ง์ผํ
ํ๋๊ณผ ๊ณ ๊ฐ ๋ฐ์์ ์ถ์ ํด์ ์ฐ๋ฆฌ ์ ๋ต์ ๋ฐ์ํ ์ ์์ด์.
competitor_bot = TwitterSummaryBot(
search_query="(๊ฒฝ์์ฌA OR ๊ฒฝ์์ฌB) (์ ์ ํ OR ์ถ์ OR ์ด๋ฒคํธ) -is:retweet",
max_tweets=50
)
3. ์
๊ณ ํธ๋ ๋ ๋ถ์
ํน์ ์ฐ์
์ด๋ ๊ธฐ์ ๋ถ์ผ์ ์ต์ ํธ๋ ๋๋ฅผ ๋งค์ผ ์์ฝํด์ ๋ฐ์๋ณผ ์ ์์ด์. ์ฌ๋ฅ๋ท ๊ฐ์ ํ๋ซํผ์์๋ ์ด๋ฐ ํธ๋ ๋ ์ ๋ณด๋ฅผ ํ์ฉํด์ ์๋ก์ด ์ฌ๋ฅ ์นดํ
๊ณ ๋ฆฌ๋ฅผ ๋ฐ๊ตดํ ์ ์๊ฒ ์ฃ ?
trend_bot = TwitterSummaryBot(
search_query="(AI OR ์ธ๊ณต์ง๋ฅ OR ๋จธ์ ๋ฌ๋) (ํธ๋ ๋ OR ๊ธฐ์ OR ๊ฐ๋ฐ) -is:retweet lang:ko",
max_tweets=80
)
๐ฏ ๊ฐ์ธ ํ์ฉ ์ฌ๋ก
1. ๊ด์ฌ ๋ถ์ผ ํ๋ ์ด์
๋ด๊ฐ ๊ด์ฌ ์๋ ์ฃผ์ ์ ํต์ฌ ์ ๋ณด๋ง ๊ณจ๋ผ์ ๋งค์ผ ์์นจ ๋ฐ์๋ณผ ์ ์์ด์. ๋ด์ค๋ ํฐ์ฒ๋ผ์!
2. ํฌ์ ์ ๋ณด ๋ชจ๋ํฐ๋ง
์ฃผ์, ์ฝ์ธ ๋ฑ ํฌ์ ๊ด๋ จ ์ ๋ณด๋ฅผ ์ค์๊ฐ์ผ๋ก ์์ฝํด์ ๋ฐ์๋ณด๋ฉด ๋น ๋ฅธ ์์ฌ๊ฒฐ์ ์ ๋์์ด ๋์ฃ .
3. ํ์ต ์๋ฃ ์์ง
ํ๋ก๊ทธ๋๋ฐ, ๋์์ธ ๋ฑ ํ์ตํ๊ณ ์ถ์ ๋ถ์ผ์ ํ๊ณผ ์๋ฃ๋ฅผ ์๋์ผ๋ก ์์งํ๊ณ ์์ฝํ ์ ์์ด์.
๐ก ํจ๊ณผ์ ์ธ ๊ฒ์ ์ฟผ๋ฆฌ ์์ฑ ํ:
์ ํ๋๋ฅผ ๋์ด๋ ค๋ฉด:
โข ๋ถํ์ํ ํค์๋๋ ๋ง์ด๋์ค(-) ๊ธฐํธ๋ก ์ ์ธ
โข ๋ฆฌํธ์ ์ ์ธ: -is:retweet
โข ์ธ์ด ์ง์ : lang:ko ๋๋ lang:en
๋ฒ์๋ฅผ ๋ํ๋ ค๋ฉด:
โข OR ์ฐ์ฐ์ ํ์ฉ: ํ์ด์ฌ OR Python OR ํ์ด์
โข ์ ์ฌ ํค์๋ ํฌํจ: (AI OR ์ธ๊ณต์ง๋ฅ OR ๋จธ์ ๋ฌ๋)
ํน์ ๊ณ์ ๋ชจ๋ํฐ๋ง:
โข from:username: ํน์ ์ฌ์ฉ์์ ํธ์๋ง
โข to:username: ํน์ ์ฌ์ฉ์์๊ฒ ๋ณด๋ธ ๋ฉ์
์ธ๊ธฐ ํธ์๋ง:
โข min_faves:100: ์ข์์ 100๊ฐ ์ด์
โข min_retweets:50: ๋ฆฌํธ์ 50๊ฐ ์ด์
โ ๏ธ ์ฃผ์์ฌํญ ๋ฐ ์ ํ์ฌํญ
์๋ฒฝํด ๋ณด์ด์ง๋ง ๋ช ๊ฐ์ง ์ฃผ์ํ ์ ๋ค์ด ์์ด์ ใ ใ
๐จ ํธ์ํฐ API ์ ํ์ฌํญ:
๋ฌด๋ฃ ํฐ์ด (Free Tier):
โข ์ 1,500๊ฐ ํธ์ ์ฝ๊ธฐ ์ ํ
โข ์ 50๊ฐ ํธ์ ์์ฑ ์ ํ
โข ์ฑ 1๊ฐ๋ง ์์ฑ ๊ฐ๋ฅ
Basic ํ๋ ($100/์):
โข ์ 10,000๊ฐ ํธ์ ์ฝ๊ธฐ
โข ์ 1,000๊ฐ ํธ์ ์์ฑ
โข ์ฑ 2๊ฐ ์์ฑ ๊ฐ๋ฅ
Pro ํ๋ ($5,000/์):
โข ์ 1,000,000๊ฐ ํธ์ ์ฝ๊ธฐ
โข ๋ฌด์ ํ ํธ์ ์์ฑ
โข ์ฑ ๋ฌด์ ํ
๐ก ํด๊ฒฐ์ฑ
: ๋ฌด๋ฃ ํฐ์ด๋ก ์์ํด์ ํ์์ ๋ฐ๋ผ ์
๊ทธ๋ ์ด๋ํ๊ฑฐ๋, ์ฌ๋ฌ ๊ณ์ ์ ๋ฒ๊ฐ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ๋ ์์ด์ (๊ท์ ์๋ฐ ์ฃผ์!).
โ๏ธ ๋ฒ์ /์ค๋ฆฌ์ ๊ณ ๋ ค์ฌํญ:
โข ๊ฐ์ธ์ ๋ณด ๋ณดํธ: ์์งํ ํธ์ ๋ฐ์ดํฐ๋ฅผ ์ธ๋ถ์ ๊ณต์ ํ๊ฑฐ๋ ์์
์ ์ผ๋ก ์ด์ฉํ ๋๋ ์ฃผ์
โข ์ ์๊ถ: ํธ์ ๋ด์ฉ๋ ์ ์๋ฌผ์ด๋ฏ๋ก ๋ฌด๋จ ๋ณต์ /๋ฐฐํฌ ๊ธ์ง
โข ์คํธ ๋ฐฉ์ง: ์๋ํ๋ ๋๋ ์์ง์ ํธ์ํฐ ์ ์ฑ
์๋ฐ ๊ฐ๋ฅ
โข Rate Limiting: API ํธ์ถ ์ ํ์ ์งํค์ง ์์ผ๋ฉด ๊ณ์ ์ ์ง๋ ์ ์์
โข ๋ด ํ์: ์๋ํ๋ ๊ณ์ ์์ ๋ช
์ํ๋ ๊ฒ ์ข์์
๐ง ์ฑ๋ฅ ์ต์ ํ ํ
1. ์บ์ฑ ํ์ฉ
์ด๋ฏธ ์ฒ๋ฆฌํ ํธ์์ ๋ค์ ์ฒ๋ฆฌํ์ง ์๋๋ก ์บ์ฑ ์์คํ
์ ๊ตฌ์ถํ์ธ์:
import json
import hashlib
class TweetCache:
def __init__(self, cache_file='tweet_cache.json'):
self.cache_file = cache_file
self.cache = self._load_cache()
def _load_cache(self):
try:
with open(self.cache_file, 'r') as f:
return json.load(f)
except FileNotFoundError:
return {}
def _save_cache(self):
with open(self.cache_file, 'w') as f:
json.dump(self.cache, f)
def is_processed(self, tweet_id):
return str(tweet_id) in self.cache
def mark_processed(self, tweet_id, summary):
self.cache[str(tweet_id)] = {
'summary': summary,
'timestamp': datetime.now().isoformat()
}
self._save_cache()
# ์ฌ์ฉ
cache = TweetCache()
for tweet in tweets:
if not cache.is_processed(tweet['id']):
summary = summarizer.summarize_text(tweet['cleaned_text'])
cache.mark_processed(tweet['id'], summary)
2. ๋ฐฐ์น ์ฒ๋ฆฌ
์ฌ๋ฌ ํธ์์ ํ ๋ฒ์ ์ฒ๋ฆฌํ๋ฉด ๋ ํจ์จ์ ์ด์์:
def batch_summarize(tweets, batch_size=10):
"""๋ฐฐ์น ๋จ์๋ก ์์ฝ ์ฒ๋ฆฌ"""
summaries = []
for i in range(0, len(tweets), batch_size):
batch = tweets[i:i+batch_size]
combined_text = " [SEP] ".join([t['cleaned_text'] for t in batch])
summary = summarizer.summarize_text(combined_text)
summaries.append(summary)
return summaries
3. ๋น๋๊ธฐ ์ฒ๋ฆฌ
asyncio๋ฅผ ์ฌ์ฉํ๋ฉด ์ฌ๋ฌ ์์
์ ๋์์ ์ฒ๋ฆฌํ ์ ์์ด์:
import asyncio
async def process_tweet_async(tweet):
"""๋น๋๊ธฐ ํธ์ ์ฒ๋ฆฌ"""
summary = await asyncio.to_thread(
summarizer.summarize_text,
tweet['cleaned_text']
)
return summary
async def process_all_tweets(tweets):
"""๋ชจ๋ ํธ์ ๋น๋๊ธฐ ์ฒ๋ฆฌ"""
tasks = [process_tweet_async(tweet) for tweet in tweets]
summaries = await asyncio.gather(*tasks)
return summaries
# ์ฌ์ฉ
summaries = asyncio.run(process_all_tweets(processed_tweets))
๐ ๊ณ ๊ธ ๊ธฐ๋ฅ: ๋ง์ถคํ ์์ฝ ์คํ์ผ
์ฌ์ฉ์๋ง๋ค ์ํ๋ ์์ฝ ์คํ์ผ์ด ๋ค๋ฅผ ์ ์์์์? ์์ฝ ๊ธธ์ด๋ ํค์ ์กฐ์ ํ ์ ์๋ ๊ธฐ๋ฅ์ ์ถ๊ฐํด๋ด ์๋ค!
class CustomSummarizer:
def __init__(self, model_name='facebook/bart-large-cnn'):
self.summarizer = TweetSummarizer(model_name)
def summarize_with_style(self, text, style='balanced'):
"""
์คํ์ผ๋ณ ์์ฝ ์์ฑ
Args:
text: ์์ฝํ ํ
์คํธ
style: 'brief' (๊ฐ๋จ), 'balanced' (๊ท ํ), 'detailed' (์์ธ)
Returns:
์์ฝ ํ
์คํธ
"""
style_configs = {
'brief': {
'max_length': 50,
'min_length': 20,
'description': 'ํต์ฌ๋ง ๊ฐ๋จํ๊ฒ'
},
'balanced': {
'max_length': 130,
'min_length': 50,
'description': '์ ๋นํ ๊ธธ์ด๋ก'
},
'detailed': {
'max_length': 250,
'min_length': 100,
'description': '์์ธํ๊ฒ'
}
}
config = style_configs.get(style, style_configs['balanced'])
summary = self.summarizer.summarize_text(
text,
max_length=config['max_length'],
min_length=config['min_length']
)
return summary
def summarize_with_bullets(self, tweets, max_bullets=5):
"""
๋ถ๋ฆฟ ํฌ์ธํธ ํ์์ผ๋ก ์์ฝ
"""
# ์ค์๋ ์์ผ๋ก ์ ๋ ฌ (์ข์์ + ๋ฆฌํธ์)
sorted_tweets = sorted(
tweets,
key=lambda x: x['likes'] + x['retweets'],
reverse=True
)[:max_bullets]
bullets = []
for tweet in sorted_tweets:
summary = self.summarizer.summarize_text(
tweet['cleaned_text'],
max_length=80,
min_length=30
)
bullets.append(f"โข {summary}")
return "\n".join(bullets)
def summarize_with_categories(self, tweets):
"""
์นดํ
๊ณ ๋ฆฌ๋ณ๋ก ๋ถ๋ฅํด์ ์์ฝ
"""
# ๊ฐ๋จํ ํค์๋ ๊ธฐ๋ฐ ๋ถ๋ฅ (์ค์ ๋ก๋ ๋ ์ ๊ตํ ๋ถ๋ฅ ํ์)
categories = {
'๊ธฐ์ ': ['๊ธฐ์ ', '๊ฐ๋ฐ', 'AI', 'ํ๋ก๊ทธ๋๋ฐ', '์ฝ๋ฉ'],
'๋น์ฆ๋์ค': ['๋น์ฆ๋์ค', '๋ง์ผํ
', '์คํํธ์
', 'ํฌ์'],
'๋ด์ค': ['๋ด์ค', '์๋ณด', '๋ฐํ', '๊ณต์'],
'๊ธฐํ': []
}
categorized = {cat: [] for cat in categories.keys()}
for tweet in tweets:
text = tweet['cleaned_text'].lower()
assigned = False
for category, keywords in categories.items():
if any(keyword in text for keyword in keywords):
categorized[category].append(tweet)
assigned = True
break
if not assigned:
categorized['๊ธฐํ'].append(tweet)
# ๊ฐ ์นดํ
๊ณ ๋ฆฌ๋ณ ์์ฝ
result = {}
for category, cat_tweets in categorized.items():
if cat_tweets:
combined_text = " ".join([t['cleaned_text'] for t in cat_tweets])
summary = self.summarizer.summarize_text(combined_text)
result[category] = {
'summary': summary,
'count': len(cat_tweets)
}
return result
# ์ฌ์ฉ ์์
custom_summarizer = CustomSummarizer()
# ์คํ์ผ๋ณ ์์ฝ
brief_summary = custom_summarizer.summarize_with_style(combined_text, style='brief')
print("๐ ๊ฐ๋จ ์์ฝ:", brief_summary)
detailed_summary = custom_summarizer.summarize_with_style(combined_text, style='detailed')
print("๐ ์์ธ ์์ฝ:", detailed_summary)
# ๋ถ๋ฆฟ ํฌ์ธํธ ์์ฝ
bullets = custom_summarizer.summarize_with_bullets(processed_tweets, max_bullets=5)
print("\n๐ฏ ํต์ฌ ํฌ์ธํธ:")
print(bullets)
# ์นดํ
๊ณ ๋ฆฌ๋ณ ์์ฝ
categorized = custom_summarizer.summarize_with_categories(processed_tweets)
print("\n๐ ์นดํ
๊ณ ๋ฆฌ๋ณ ์์ฝ:")
for category, data in categorized.items():
print(f"\n[{category}] ({data['count']}๊ฐ ํธ์)")
print(data['summary'])
๐ ๋ฐ์ดํฐ ๋ถ์ ๋ฐ ์๊ฐํ
์์งํ ๋ฐ์ดํฐ๋ฅผ ๋ถ์ํด์ ์ธ์ฌ์ดํธ๋ฅผ ์ป๋ ๊ฒ๋ ์ค์ํ์ฃ ! ๊ฐ๋จํ ํต๊ณ์ ์๊ฐํ ๊ธฐ๋ฅ์ ์ถ๊ฐํด๋ด ์๋ค:
import pandas as pd
from collections import Counter
import re
class TweetAnalyzer:
def __init__(self, tweets):
self.tweets = tweets
self.df = pd.DataFrame(tweets)
def get_basic_stats(self):
"""๊ธฐ๋ณธ ํต๊ณ"""
stats = {
'total_tweets': len(self.tweets),
'total_likes': self.df['likes'].sum(),
'total_retweets': self.df['retweets'].sum(),
'avg_likes': self.df['likes'].mean(),
'avg_retweets': self.df['retweets'].mean(),
'most_liked': self.df.loc[self.df['likes'].idxmax()].to_dict() if len(self.df) > 0 else None,
'most_retweeted': self.df.loc[self.df['retweets'].idxmax()].to_dict() if len(self.df) > 0 else None
}
return stats
def get_top_hashtags(self, top_n=10):
"""์ธ๊ธฐ ํด์ํ๊ทธ ์ถ์ถ"""
all_hashtags = []
for tweet in self.tweets:
hashtags = re.findall(r'#(\w+)', tweet['text'])
all_hashtags.extend(hashtags)
counter = Counter(all_hashtags)
return counter.most_common(top_n)
def get_top_mentions(self, top_n=10):
"""์์ฃผ ์ธ๊ธ๋ ์ฌ์ฉ์"""
all_mentions = []
for tweet in self.tweets:
mentions = re.findall(r'@(\w+)', tweet['text'])
all_mentions.extend(mentions)
counter = Counter(all_mentions)
return counter.most_common(top_n)
def get_hourly_distribution(self):
"""์๊ฐ๋๋ณ ํธ์ ๋ถํฌ"""
self.df['hour'] = pd.to_datetime(self.df['created_at']).dt.hour
hourly_counts = self.df['hour'].value_counts().sort_index()
return hourly_counts.to_dict()
def get_engagement_rate(self):
"""์ฐธ์ฌ์จ ๊ณ์ฐ (์ข์์ + ๋ฆฌํธ์ + ๋๊ธ)"""
self.df['engagement'] = self.df['likes'] + self.df['retweets'] + self.df['replies']
self.df['engagement_rate'] = self.df['engagement'] / len(self.df)
return {
'avg_engagement': self.df['engagement'].mean(),
'top_engaged': self.df.nlargest(5, 'engagement')[['text', 'engagement']].to_dict('records')
}
def generate_word_cloud_data(self):
"""์๋ ํด๋ผ์ฐ๋์ฉ ๋ฐ์ดํฐ ์์ฑ"""
all_words = []
for tweet in self.tweets:
words = tweet['cleaned_text'].split()
# ๋ถ์ฉ์ด ์ ๊ฑฐ (๊ฐ๋จํ ์์)
stopwords = ['์ด', '๊ทธ', '์ ', '๊ฒ', '์', '๋ฑ', '๋ฐ', '๋ฅผ', '์', '๊ฐ', '์ด']
words = [w for w in words if w not in stopwords and len(w) > 1]
all_words.extend(words)
counter = Counter(all_words)
return counter.most_common(50)
# ์ฌ์ฉ ์์
analyzer = TweetAnalyzer(processed_tweets)
# ๊ธฐ๋ณธ ํต๊ณ
stats = analyzer.get_basic_stats()
print("๐ ๊ธฐ๋ณธ ํต๊ณ:")
print(f"์ด ํธ์ ์: {stats['total_tweets']}๊ฐ")
print(f"์ด ์ข์์: {stats['total_likes']:,}๊ฐ")
print(f"ํ๊ท ์ข์์: {stats['avg_likes']:.1f}๊ฐ")
# ์ธ๊ธฐ ํด์ํ๊ทธ
top_hashtags = analyzer.get_top_hashtags(10)
print("\n#๏ธโฃ ์ธ๊ธฐ ํด์ํ๊ทธ TOP 10:")
for i, (tag, count) in enumerate(top_hashtags, 1):
print(f"{i}. #{tag} ({count}ํ)")
# ์๊ฐ๋๋ณ ๋ถํฌ
hourly = analyzer.get_hourly_distribution()
print("\nโฐ ์๊ฐ๋๋ณ ํธ์ ๋ถํฌ:")
for hour, count in sorted(hourly.items()):
print(f"{hour:02d}์: {'โ' * (count // 2)} ({count}๊ฐ)")
# ์ฐธ์ฌ์จ ๋ถ์
engagement = analyzer.get_engagement_rate()
print(f"\n๐ฌ ํ๊ท ์ฐธ์ฌ์จ: {engagement['avg_engagement']:.1f}")
print("\n๐ฅ ์ฐธ์ฌ์จ ๋์ ํธ์ TOP 5:")
for i, tweet in enumerate(engagement['top_engaged'], 1):
print(f"{i}. {tweet['text'][:80]}... (์ฐธ์ฌ: {tweet['engagement']})")
# ์๋ ํด๋ผ์ฐ๋ ๋ฐ์ดํฐ
word_freq = analyzer.generate_word_cloud_data()
print("\n๐ ์์ฃผ ๋์จ ๋จ์ด TOP 20:")
for word, freq in word_freq[:20]:
print(f"{word}: {freq}ํ")
๐ ๋ง๋ฌด๋ฆฌ ๋ฐ ๋ค์ ๋จ๊ณ
์! ์ฌ๊ธฐ๊น์ง ์ค๋๋ผ ์๊ณ ๋ง์ผ์ จ์ด์ ใ ใ ใ ์ด์ ์ฌ๋ฌ๋ถ๋ง์ ํธ์ํฐ ์๋ ์์ฝ๋ด์ ๋ง๋ค ์ ์๋ ๋ชจ๋ ์ง์์ ๊ฐ์ถ์ จ์ต๋๋ค ๐
โ ์ฐ๋ฆฌ๊ฐ ๋ฐฐ์ด ๋ด์ฉ ์ ๋ฆฌ
1. ํธ์ํฐ API ์ฐ๋: Tweepy๋ฅผ ์ฌ์ฉํ ํธ์ ์์ง
2. ๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ: ํ
์คํธ ์ ์ ๋ฐ ๋
ธ์ด์ฆ ์ ๊ฑฐ
3. AI ์์ฝ: Transformers ๋ชจ๋ธ์ ํ์ฉํ ์๋ ์์ฝ
4. ๊ฐ์ฑ ๋ถ์: ํธ์์ ๊ธ์ /๋ถ์ /์ค๋ฆฝ ํ๋จ
5. ์๋ํ: ์ค์ผ์ค๋ง์ผ๋ก ์ ๊ธฐ์ ์คํ
6. ์๋ฆผ ์์คํ
: ํ
๋ ๊ทธ๋จ/์ด๋ฉ์ผ ์๋ฆผ
7. ๋ฐฐํฌ: ํด๋ผ์ฐ๋ ์๋ฒ์์ 24์๊ฐ ์ด์
8. ๋ฐ์ดํฐ ๋ถ์: ํต๊ณ ๋ฐ ์ธ์ฌ์ดํธ ๋์ถ
๐ ๋ ๋ฐ์ ์ํฌ ์ ์๋ ๋ฐฉํฅ
1. ์น ๋์๋ณด๋ ๊ตฌ์ถ
Flask๋ Django๋ก ์น ์ธํฐํ์ด์ค๋ฅผ ๋ง๋ค์ด์ ์ค์๊ฐ์ผ๋ก ๊ฒฐ๊ณผ๋ฅผ ํ์ธํ ์ ์๊ฒ ๋ง๋ค์ด๋ณด์ธ์!
2. ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๋
PostgreSQL์ด๋ MongoDB์ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํด์ ์ฅ๊ธฐ์ ์ธ ํธ๋ ๋ ๋ถ์์ ํ ์ ์์ด์.
3. ๋จธ์ ๋ฌ๋ ๋ชจ๋ธ ํ์ธํ๋
ํน์ ๋๋ฉ์ธ์ ํนํ๋ ์์ฝ ๋ชจ๋ธ์ ์ง์ ํ์ต์์ผ๋ณด์ธ์. ์ฑ๋ฅ์ด ํจ์ฌ ์ข์์ง ๊ฑฐ์์!
4. ๋ค์ค SNS ์ง์
ํธ์ํฐ๋ฟ๋ง ์๋๋ผ ์ธ์คํ๊ทธ๋จ, ํ์ด์ค๋ถ, ์ ํ๋ธ ๋๊ธ ๋ฑ๋ ํจ๊ป ๋ถ์ํ๋ฉด ๋ ํ๋ถํ ์ธ์ฌ์ดํธ๋ฅผ ์ป์ ์ ์์ด์.
5. ์ค์๊ฐ ์คํธ๋ฆฌ๋ฐ
ํธ์ํฐ Streaming API๋ฅผ ์ฌ์ฉํ๋ฉด ์ค์๊ฐ์ผ๋ก ํธ์์ ๋ฐ์์ ์ฆ์ ์ฒ๋ฆฌํ ์ ์์ด์.
6. ์ด๋ฏธ์ง/๋น๋์ค ๋ถ์
์ปดํจํฐ ๋น์ ๊ธฐ์ ์ ์ถ๊ฐํด์ ํธ์์ ์ฒจ๋ถ๋ ์ด๋ฏธ์ง๋ ๋น๋์ค๋ ๋ถ์ํด๋ณด์ธ์!
๐ก ํ์ต ๋ฆฌ์์ค ์ถ์ฒ:
๊ณต์ ๋ฌธ์:
โข Tweepy: https://docs.tweepy.org
โข Hugging Face Transformers: https://huggingface.co/docs
โข Twitter API: https://developer.twitter.com/en/docs
์จ๋ผ์ธ ๊ฐ์:
โข Coursera: Natural Language Processing Specialization
โข Udemy: Python for NLP
โข YouTube: ๋ค์ํ NLP ํํ ๋ฆฌ์ผ
์ปค๋ฎค๋ํฐ:
โข Reddit: r/MachineLearning, r/LanguageTechnology
โข Stack Overflow: ๋งํ ๋ ์ง๋ฌธํ๊ธฐ
โข GitHub: ์คํ์์ค ํ๋ก์ ํธ ์ฐธ๊ณ ํ๊ธฐ
๊ทธ๋ฆฌ๊ณ ์ฌ๋ฅ๋ท์์๋ AI/๋จธ์ ๋ฌ๋ ๊ด๋ จ ์ฌ๋ฅ์ ๊ฐ์ง ์ ๋ฌธ๊ฐ๋ค์ ๋ง๋ ์ ์์ผ๋, ๋งํ๋ ๋ถ๋ถ์ด ์์ผ๋ฉด ๋์์ ๋ฐ์๋ณด๋ ๊ฒ๋ ์ข์ ๋ฐฉ๋ฒ์ด์์!
๐ค ํ๋ก์ ํธ ๊ณต์ ํ๊ธฐ
์ฌ๋ฌ๋ถ์ด ๋ง๋ ๋ด์ GitHub์ ์ฌ๋ ค์ ๋ค๋ฅธ ์ฌ๋๋ค๊ณผ ๊ณต์ ํด๋ณด์ธ์! ์คํ์์ค๋ก ๊ณต๊ฐํ๋ฉด ๋ค๋ฅธ ๊ฐ๋ฐ์๋ค์ ํผ๋๋ฐฑ๋ ๋ฐ์ ์ ์๊ณ , ํฌํธํด๋ฆฌ์ค๋ก๋ ํ์ฉํ ์ ์์ด์.
README.md์๋ ๋ค์ ๋ด์ฉ์ ํฌํจํ๋ฉด ์ข์์:
โข ํ๋ก์ ํธ ์๊ฐ ๋ฐ ๋ชฉ์
โข ์ค์น ๋ฐฉ๋ฒ (requirements.txt ํฌํจ)
โข ์ฌ์ฉ ๋ฐฉ๋ฒ ๋ฐ ์์
โข ์ฃผ์ ๊ธฐ๋ฅ ์ค๋ช
โข ์คํฌ๋ฆฐ์ท ๋๋ ๋ฐ๋ชจ ์์
โข ๋ผ์ด์ ์ค ์ ๋ณด
โข ๊ธฐ์ฌ ๋ฐฉ๋ฒ (Contributing Guidelines)
โ ๏ธ ๋ง์ง๋ง ๋น๋ถ:
โข ์ค๋ฆฌ์ ์ฌ์ฉ: ์์งํ ๋ฐ์ดํฐ๋ฅผ ์
์ฉํ์ง ๋ง์ธ์
โข ๊ฐ์ธ์ ๋ณด ๋ณดํธ: ๋ฏผ๊ฐํ ์ ๋ณด๋ ์ ๋ ์ ์ฅํ๊ฑฐ๋ ๊ณต์ ํ์ง ๋ง์ธ์
โข API ์ ํ ์ค์: ํธ์ํฐ ์ ์ฑ
์ ๊ผญ ์ง์ผ์ฃผ์ธ์
โข ์ง์์ ์ธ ํ์ต: AI ๊ธฐ์ ์ ๋น ๋ฅด๊ฒ ๋ฐ์ ํ๋ ๊ณ์ ๊ณต๋ถํ์ธ์
โข ์ปค๋ฎค๋ํฐ ๊ธฐ์ฌ: ๋ฐฐ์ด ๊ฒ์ ๋ค๋ฅธ ์ฌ๋๋ค๊ณผ ๋๋ ์ฃผ์ธ์!
๐ ์ง๊ธ ๋ฐ๋ก ์์ํด๋ณด์ธ์!
์ด ๊ธ์์ ๋ฐฐ์ด ๋ด์ฉ์ ๋ฐํ์ผ๋ก ์ฌ๋ฌ๋ถ๋ง์ ๋
ํนํ ํธ์ํฐ ๋ด์ ๋ง๋ค์ด๋ณด์ธ์.
๋งํ๋ ๋ถ๋ถ์ด ์๋ค๋ฉด ์ฌ๋ฅ๋ท์์ AI/๋จธ์ ๋ฌ๋ ์ ๋ฌธ๊ฐ๋ค์ ๋์์ ๋ฐ์ ์๋ ์์ด์!
Happy Coding! ๐
๐ ์ด ๊ธ์ด ๋์์ด ๋์ จ๋์?
๋ ๋ง์ AI/๋จธ์ ๋ฌ๋ ๊ฐ๋ฐ ์ง์์ ์ฌ๋ฅ๋ท์ '์ง์์ธ์ ์ฒ'์์ ๋ง๋๋ณด์ธ์!
ํ๋ก๊ทธ๋๋ฐ๋ถํฐ ๋ฐ์ดํฐ ๋ถ์๊น์ง, ๋ค์ํ ์ฌ๋ฅ์ ๊ฐ์ง ์ ๋ฌธ๊ฐ๋ค์ด ๊ธฐ๋ค๋ฆฌ๊ณ ์์ต๋๋ค ๐
๊ด๋ จ ํค์๋
๋๊ธ ์์ฑ
์ด ๊ธ์ ๋ํ ์ฌ๋ฌ๋ถ์ ์๊ฐ์ ๋ค๋ ค์ฃผ์ธ์
๋ก๊ทธ์ธ์ด ํ์ํฉ๋๋ค
๋๊ธ์ ์์ฑํ๋ ค๋ฉด ๋จผ์ ๋ก๊ทธ์ธํด์ฃผ์ธ์.
๋๊ธ 0
์ง์์ธ์ ์ฒ - ์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
- ์ ์๊ถ ๋ฐ ์์ ๊ถ: ๋ณธ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ๋ ์ AI ๊ธฐ์ ๋ก ์์ฑ๋์์ผ๋ฉฐ, ๋ํ๋ฏผ๊ตญ ์ ์๊ถ๋ฒ ๋ฐ ๊ตญ์ ์ ์๊ถ ํ์ฝ์ ์ํด ๋ณดํธ๋ฉ๋๋ค.
- AI ์์ฑ ์ปจํ ์ธ ์ ๋ฒ์ ์ง์: ๋ณธ AI ์์ฑ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ์ง์ ์ฐฝ์๋ฌผ๋ก ์ธ์ ๋๋ฉฐ, ๊ด๋ จ ๋ฒ๊ท์ ๋ฐ๋ผ ์ ์๊ถ ๋ณดํธ๋ฅผ ๋ฐ์ต๋๋ค.
- ์ฌ์ฉ ์ ํ: ์ฌ๋ฅ๋ท์ ๋ช ์์ ์๋ฉด ๋์ ์์ด ๋ณธ ์ปจํ ์ธ ๋ฅผ ๋ณต์ , ์์ , ๋ฐฐํฌ, ๋๋ ์์ ์ ์ผ๋ก ํ์ฉํ๋ ํ์๋ ์๊ฒฉํ ๊ธ์ง๋ฉ๋๋ค.
- ๋ฐ์ดํฐ ์์ง ๊ธ์ง: ๋ณธ ์ปจํ ์ธ ์ ๋ํ ๋ฌด๋จ ์คํฌ๋ํ, ํฌ๋กค๋ง, ๋ฐ ์๋ํ๋ ๋ฐ์ดํฐ ์์ง์ ๋ฒ์ ์ ์ฌ์ ๋์์ด ๋ฉ๋๋ค.
- AI ํ์ต ์ ํ: ์ฌ๋ฅ๋ท์ AI ์์ฑ ์ปจํ ์ธ ๋ฅผ ํ AI ๋ชจ๋ธ ํ์ต์ ๋ฌด๋จ ์ฌ์ฉํ๋ ํ์๋ ๊ธ์ง๋๋ฉฐ, ์ด๋ ์ง์ ์ฌ์ฐ๊ถ ์นจํด๋ก ๊ฐ์ฃผ๋ฉ๋๋ค.
์์ง ๋๊ธ์ด ์์ต๋๋ค.