-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentiment_analyzer.py
93 lines (74 loc) · 2.94 KB
/
sentiment_analyzer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import json
import re
from datetime import datetime
from typing import Dict, List
import pandas as pd
import streamlit as st
from langchain.chains import LLMChain
from langchain.chat_models import ChatOpenAI
from langchain.prompts import PromptTemplate
from tweety.types import Tweet
PROMPT_TEMPLATE = """
You're a cryptocurrency trader with 10+ years of experience. You always follow the trend
and follow and deeply understand crypto experts on Twitter. You always consider the historical predictions for each expert on Twitter.
You're given tweets and their view count from @{twitter_handle} for specific dates:
{tweets}
Tell how bullish or bearish the tweets for each date are. Use numbers between 0 and 100, where 0 is extremely bearish and 100 is extremely bullish.
Use a JSON using the format:
date: sentiment
Each record of the JSON should give the aggregate sentiment for that date. Return just the JSON. Do not explain.
"""
def clean_tweet(text: str) -> str:
text = re.sub(r"http\S+", "", text)
text = re.sub(r"www.\S+", "", text)
return re.sub(r"\s+", " ", text)
def create_dataframe_from_tweets(tweets: List[Tweet]) -> pd.DataFrame:
rows = []
for tweet in tweets:
clean_text = clean_tweet(tweet.text)
if len(clean_text) == 0:
continue
rows.append(
{
"id": tweet.id,
"text": clean_text,
"author": tweet.author.username,
"date": str(tweet.date.date()),
"created_at": tweet.date,
"views": tweet.views,
}
)
df = pd.DataFrame(
rows, columns=["id", "text", "author", "date", "views", "created_at"]
)
df.set_index("id", inplace=True)
if df.empty:
return df
df = df[df.created_at.dt.date > datetime.now().date() - pd.to_timedelta("7day")]
return df.sort_values(by="created_at", ascending=False)
def create_tweet_list_for_prompt(tweets: List[Tweet], twitter_handle: str) -> str:
df = create_dataframe_from_tweets(tweets)
user_tweets = df[df.author == twitter_handle]
if user_tweets.empty:
return ""
if len(user_tweets) > 100:
user_tweets = user_tweets.sample(n=100)
text = ""
for tweets_date, tweets in user_tweets.groupby("date"):
text += f"{tweets_date}:"
for tweet in tweets.itertuples():
text += f"\n{tweet.views} - {tweet.text}"
return text
def analyze_sentiment(twitter_handle: str, tweets: List[Tweet]) -> Dict[str, int]:
chat_gpt = ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo")
prompt = PromptTemplate(
input_variables=["twitter_handle", "tweets"], template=PROMPT_TEMPLATE
)
sentiment_chain = LLMChain(llm=chat_gpt, prompt=prompt)
response = sentiment_chain(
{
"twitter_handle": twitter_handle,
"tweets": create_tweet_list_for_prompt(tweets, twitter_handle),
}
)
return json.loads(response["text"])