-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced support for sentiment analysis (#81)
* Create nlp.py & add_sentiment(df) function * Added optional NLP dependencies * Closes #60 --------- Co-authored-by: joweich <[email protected]>
- Loading branch information
1 parent
e348a4c
commit b1daa4e
Showing
4 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from transformers import pipeline | ||
import pandas as pd | ||
|
||
|
||
def add_sentiment(df: pd.DataFrame, lang: str = "en") -> pd.DataFrame: | ||
""" | ||
Add sentiment column to the input dataframe | ||
Parameters: | ||
df (pd.DataFrame): The input dataframe | ||
lang (str): Language of the messages, defaults to "en" | ||
Returns: | ||
pd.DataFrame: The input dataframe with an additional column "sentiment" | ||
""" | ||
if "message" not in df.columns: | ||
raise ValueError("Input dataframe does not contain a 'message' column") | ||
|
||
model_path = ( | ||
"cardiffnlp/twitter-roberta-base-sentiment-latest" | ||
if lang == "en" | ||
else "cardiffnlp/twitter-xlm-roberta-base-sentiment" | ||
) | ||
sentiment_pipeline = pipeline("sentiment-analysis", model=model_path) | ||
|
||
def extract_sentiment(message: str) -> str: | ||
""" | ||
Extract sentiment from message | ||
Parameters: | ||
message (str): The input message | ||
Returns: | ||
str: The sentiment of the message | ||
""" | ||
try: | ||
return sentiment_pipeline(message)[0]["label"] | ||
except: | ||
print(f"Error processing message: {message}") | ||
return None | ||
|
||
df["sentiment"] = df["message"].apply(extract_sentiment) | ||
|
||
return df |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ numpy | |
matplotlib | ||
wordcloud | ||
python-dateutil | ||
tqdm | ||
tqdm | ||
transformers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,3 +38,7 @@ install_requires = | |
wordcloud | ||
python-dateutil | ||
tqdm | ||
|
||
[options.extras_require] | ||
NLP = | ||
transformers |