-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentiment.py
79 lines (60 loc) · 2.07 KB
/
sentiment.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
import os
import tweetnlp
import argparse
import pandas as pd
from tqdm import tqdm
def preprocess(text):
"""
Preprocess the given text by replacing mentions and URLs with placeholders.
Args:
text (str): The text to be preprocessed.
Returns:
str: The preprocessed text.
"""
new_text = []
for t in text.split(" "):
t = '@user' if t.startswith('@') and len(t) > 1 else t
t = 'http' if t.startswith('http') else t
new_text.append(t)
return " ".join(new_text)
def analyze(language):
"""
Analyze the sentiment of the tweets in a given language using a pre-trained multilingual sentiment analysis model.
Args:
language (str): The language of the tweets to be analyzed.
Returns:
None
Raises:
FileNotFoundError: If the data file for the given language is not found.
"""
file_path = f"data/{language}.csv"
if not os.path.isfile(file_path):
raise FileNotFoundError(f"Missing data file: {file_path}")
if not os.path.exists("results/"):
os.mkdir("results")
data = pd.read_csv(file_path)
model_name = "cardiffnlp/twitter-xlm-roberta-base-sentiment-multilingual"
model = tweetnlp.Classifier(model_name, max_length=128)
predictions = []
# We analyze in chunks of 1000 tweets
for start in tqdm(range(0, len(data), 1000)):
if start + 1000 > len(data):
tweets = data[start:].tweet
else:
tweets = data[start:start+1000].tweet
tweets = [preprocess(tweet) for tweet in tweets]
preds = model.predict(tweets)
predictions += [pred["label"] for pred in preds]
data["sentiment"] = predictions
data.to_csv(f"results/{language}_sentiment.csv")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-l", "--language",
type=str,
default="IT",
choices=["IT", "EN", "DE", "FR", "ES", "PT", "NO"],
help="The language of the experiment"
)
args = parser.parse_args()
analyze(args.language)