Views
No views yet
1def preprocess(text):
2 preprocessed_text = []
3 for t in text.split():
4 if len(t) > 1:
5 t = '@user' if t[0] == '@' and t.count('@') == 1 else t
6 t = 'http' if t.startswith('http') else t
7 preprocessed_text.append(t)
8 return ' '.join(preprocessed_text)1from transformers import pipeline, AutoTokenizer
2
3MODEL = "cardiffnlp/twitter-roberta-base-2022-154m"
4fill_mask = pipeline("fill-mask", model=MODEL, tokenizer=MODEL)
5tokenizer = AutoTokenizer.from_pretrained(MODEL)
6
7def pprint(candidates, n):
8 for i in range(n):
9 token = tokenizer.decode(candidates[i]['token'])
10 score = candidates[i]['score']
11 print("%d) %.5f %s" % (i+1, score, token))
12
13texts = [
14 "So glad I'm <mask> vaccinated.",
15 "I keep forgetting to bring a <mask>.",
16 "Looking forward to watching <mask> Game tonight!",
17]
18for text in texts:
19 t = preprocess(text)
20 print(f"{'-'*30}\n{t}")
21 candidates = fill_mask(t)
22 pprint(candidates, 5)------------------------------
So glad I'm <mask> vaccinated.
1) 0.26251 not
2) 0.25460 a
3) 0.12611 in
4) 0.11036 the
5) 0.04210 getting
------------------------------
I keep forgetting to bring a <mask>.
1) 0.09274 charger
2) 0.04727 lighter
3) 0.04469 mask
4) 0.04395 drink
5) 0.03644 camera
------------------------------
Looking forward to watching <mask> Game tonight!
1) 0.57683 Squid
2) 0.17419 The
3) 0.04198 the
4) 0.00970 Spring
5) 0.00921 Big1from transformers import AutoTokenizer, AutoModel, TFAutoModel
2import numpy as np
3from scipy.spatial.distance import cosine
4from collections import Counter
5
6def get_embedding(text): # naive approach for demonstration
7 text = preprocess(text)
8 encoded_input = tokenizer(text, return_tensors='pt')
9 features = model(**encoded_input)
10 features = features[0].detach().cpu().numpy()
11 return np.mean(features[0], axis=0)
12
13
14MODEL = "cardiffnlp/twitter-roberta-base-2022-154m"
15tokenizer = AutoTokenizer.from_pretrained(MODEL)
16model = AutoModel.from_pretrained(MODEL)
17
18query = "The book was awesome"
19tweets = ["I just ordered fried chicken 🐣",
20 "The movie was great",
21 "What time is the next game?",
22 "Just finished reading 'Embeddings in NLP'"]
23
24sims = Counter()
25for tweet in tweets:
26 sim = 1 - cosine(get_embedding(query), get_embedding(tweet))
27 sims[tweet] = sim
28
29print('Most similar to: ', query)
30print(f"{'-'*30}")
31for idx, (tweet, sim) in enumerate(sims.most_common()):
32 print("%d) %.5f %s" % (idx+1, sim, tweet))Most similar to: The book was awesome
------------------------------
1) 0.99403 The movie was great
2) 0.98006 Just finished reading 'Embeddings in NLP'
3) 0.97314 What time is the next game?
4) 0.92448 I just ordered fried chicken 🐣1from transformers import AutoTokenizer, AutoModel, TFAutoModel
2import numpy as np
3
4MODEL = "cardiffnlp/twitter-roberta-base-2022-154m"
5tokenizer = AutoTokenizer.from_pretrained(MODEL)
6
7text = "Good night 😊"
8text = preprocess(text)
9
10# Pytorch
11model = AutoModel.from_pretrained(MODEL)
12encoded_input = tokenizer(text, return_tensors='pt')
13features = model(**encoded_input)
14features = features[0].detach().cpu().numpy()
15features_mean = np.mean(features[0], axis=0)
16#features_max = np.max(features[0], axis=0)
17
18# # Tensorflow
19# model = TFAutoModel.from_pretrained(MODEL)
20# encoded_input = tokenizer(text, return_tensors='tf')
21# features = model(encoded_input)
22# features = features[0].numpy()
23# features_mean = np.mean(features[0], axis=0)
24# #features_max = np.max(features[0], axis=0)1@article{loureiro2023tweet,
2 title={Tweet Insights: A Visualization Platform to Extract Temporal Insights from Twitter},
3 author={Loureiro, Daniel and Rezaee, Kiamehr and Riahi, Talayeh and Barbieri, Francesco and Neves, Leonardo and Anke, Luis Espinosa and Camacho-Collados, Jose},
4 journal={arXiv preprint arXiv:2308.02142},
5 year={2023}
6}