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)1from transformers import pipeline, AutoTokenizer
2import numpy as np
3
4MODEL = "cardiffnlp/twitter-roberta-base"
5fill_mask = pipeline("fill-mask", model=MODEL, tokenizer=MODEL)
6tokenizer = AutoTokenizer.from_pretrained(MODEL)
7
8def print_candidates():
9 for i in range(5):
10 token = tokenizer.decode(candidates[i]['token'])
11 score = np.round(candidates[i]['score'], 4)
12 print(f"{i+1}) {token} {score}")
13
14texts = [
15 "I am so <mask> 😊",
16 "I am so <mask> 😢"
17]
18for text in texts:
19 t = preprocess(text)
20 print(f"{'-'*30}\n{t}")
21 candidates = fill_mask(t)
22 print_candidates()------------------------------
I am so <mask> 😊
1) happy 0.402
2) excited 0.1441
3) proud 0.143
4) grateful 0.0669
5) blessed 0.0334
------------------------------
I am so <mask> 😢
1) sad 0.2641
2) sorry 0.1605
3) tired 0.138
4) sick 0.0278
5) hungry 0.02321from transformers import AutoTokenizer, AutoModel, TFAutoModel
2import numpy as np
3from scipy.spatial.distance import cosine
4from collections import defaultdict
5
6tokenizer = AutoTokenizer.from_pretrained(MODEL)
7model = AutoModel.from_pretrained(MODEL)
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().cpu().numpy()
14 features_mean = np.mean(features[0], axis=0)
15 return features_mean
16
17MODEL = "cardiffnlp/twitter-roberta-base"
18
19query = "The book was awesome"
20
21tweets = ["I just ordered fried chicken 🐣",
22 "The movie was great",
23 "What time is the next game?",
24 "Just finished reading 'Embeddings in NLP'"]
25
26d = defaultdict(int)
27for tweet in tweets:
28 sim = 1-cosine(get_embedding(query),get_embedding(tweet))
29 d[tweet] = sim
30
31print('Most similar to: ',query)
32print('----------------------------------------')
33for idx,x in enumerate(sorted(d.items(), key=lambda x:x[1], reverse=True)):
34 print(idx+1,x[0])Most similar to: The book was awesome
----------------------------------------
1 The movie was great
2 Just finished reading 'Embeddings in NLP'
3 I just ordered fried chicken 🐣
4 What time is the next game?1from transformers import AutoTokenizer, AutoModel, TFAutoModel
2import numpy as np
3
4MODEL = "cardiffnlp/twitter-roberta-base"
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)
251@inproceedings{barbieri-etal-2020-tweeteval,
2 title = "{T}weet{E}val: Unified Benchmark and Comparative Evaluation for Tweet Classification",
3 author = "Barbieri, Francesco and
4 Camacho-Collados, Jose and
5 Espinosa Anke, Luis and
6 Neves, Leonardo",
7 booktitle = "Findings of the Association for Computational Linguistics: EMNLP 2020",
8 month = nov,
9 year = "2020",
10 address = "Online",
11 publisher = "Association for Computational Linguistics",
12 url = "https://aclanthology.org/2020.findings-emnlp.148",
13 doi = "10.18653/v1/2020.findings-emnlp.148",
14 pages = "1644--1650"
15}