Views
No views yet
1def preprocess(text):
2 new_text = []
3 for t in text.split(" "):
4 t = '@user' if t.startswith('@') and len(t) > 1 else t
5 t = 'http' if t.startswith('http') else t
6 new_text.append(t)
7 return " ".join(new_text)
8
9def get_embedding(text):
10 text = preprocess(text)
11 encoded_input = tokenizer(text, return_tensors='pt')
12 features = model(**encoded_input)
13 features = features[0].detach().numpy()
14 features_mean = np.mean(features[0], axis=0)
15 return features_mean
16
17query = "Acabo de pedir pollo frito 🐣" #spanish
18
19tweets = ["We had a great time! ⚽️", # english
20 "We hebben een geweldige tijd gehad! ⛩", # dutch
21 "Nous avons passé un bon moment! 🎥", # french
22 "Ci siamo divertiti! 🍝"] # italian
23
24d = defaultdict(int)
25for tweet in tweets:
26 sim = 1-cosine(get_embedding(query),get_embedding(tweet))
27 d[tweet] = sim
28
29print('Most similar to: ',query)
30print('----------------------------------------')
31for idx,x in enumerate(sorted(d.items(), key=lambda x:x[1], reverse=True)):
32 print(idx+1,x[0])Most similar to: Acabo de pedir pollo frito 🐣
----------------------------------------
1 Ci siamo divertiti! 🍝
2 Nous avons passé un bon moment! 🎥
3 We had a great time! ⚽️
4 We hebben een geweldige tijd gehad! ⛩1@inproceedings{barbieri-etal-2022-xlm,
2 title = "{XLM}-{T}: Multilingual Language Models in {T}witter for Sentiment Analysis and Beyond",
3 author = "Barbieri, Francesco and
4 Espinosa Anke, Luis and
5 Camacho-Collados, Jose",
6 booktitle = "Proceedings of the Thirteenth Language Resources and Evaluation Conference",
7 month = jun,
8 year = "2022",
9 address = "Marseille, France",
10 publisher = "European Language Resources Association",
11 url = "https://aclanthology.org/2022.lrec-1.27",
12 pages = "258--266",
13 abstract = "Language models are ubiquitous in current NLP, and their multilingual capacity has recently attracted considerable attention. However, current analyses have almost exclusively focused on (multilingual variants of) standard benchmarks, and have relied on clean pre-training and task-specific corpora as multilingual signals. In this paper, we introduce XLM-T, a model to train and evaluate multilingual language models in Twitter. In this paper we provide: (1) a new strong multilingual baseline consisting of an XLM-R (Conneau et al. 2020) model pre-trained on millions of tweets in over thirty languages, alongside starter code to subsequently fine-tune on a target task; and (2) a set of unified sentiment analysis Twitter datasets in eight different languages and a XLM-T model trained on this dataset.",
14}