Views
No views yet
@inproceedings{huguet-cabot-et-al-2023-redfm-dataset,
title = "RED$^{\rm FM}$: a Filtered and Multilingual Relation Extraction Dataset",
author = "Huguet Cabot, Pere-Llu{\'\i}s and Tedeschi, Simone and Ngonga Ngomo, Axel-Cyrille and
Navigli, Roberto",
booktitle = "Proc. of the 61st Annual Meeting of the Association for Computational Linguistics: ACL 2023",
month = jul,
year = "2023",
address = "Toronto, Canada",
publisher = "Association for Computational Linguistics",
url = "https://arxiv.org/abs/2306.09802",
}1from transformers import pipeline
2
3triplet_extractor = pipeline('translation_xx_to_yy', model='Babelscape/mrebel-base', tokenizer='Babelscape/mrebel-base')
4# We need to use the tokenizer manually since we need special tokens.
5extracted_text = triplet_extractor.tokenizer.batch_decode([triplet_extractor("The Red Hot Chili Peppers were formed in Los Angeles by Kiedis, Flea, guitarist Hillel Slovak and drummer Jack Irons.", src_lang="en", return_tensors=True, return_text=False)[0]["translation_token_ids"]]) # change __en__ for the language of the source.
6print(extracted_text[0])
7# Function to parse the generated text and extract the triplets
8def extract_triplets_typed(text):
9 triplets = []
10 relation = ''
11 text = text.strip()
12 current = 'x'
13 subject, relation, object_, object_type, subject_type = '','','','',''
14
15 for token in text.replace("<s>", "").replace("<pad>", "").replace("</s>", "").replace("tp_XX", "").replace("__en__", "").split():
16 if token == "<triplet>" or token == "<relation>":
17 current = 't'
18 if relation != '':
19 triplets.append({'head': subject.strip(), 'head_type': subject_type, 'type': relation.strip(),'tail': object_.strip(), 'tail_type': object_type})
20 relation = ''
21 subject = ''
22 elif token.startswith("<") and token.endswith(">"):
23 if current == 't' or current == 'o':
24 current = 's'
25 if relation != '':
26 triplets.append({'head': subject.strip(), 'head_type': subject_type, 'type': relation.strip(),'tail': object_.strip(), 'tail_type': object_type})
27 object_ = ''
28 subject_type = token[1:-1]
29 else:
30 current = 'o'
31 object_type = token[1:-1]
32 relation = ''
33 else:
34 if current == 't':
35 subject += ' ' + token
36 elif current == 's':
37 object_ += ' ' + token
38 elif current == 'o':
39 relation += ' ' + token
40 if subject != '' and relation != '' and object_ != '' and object_type != '' and subject_type != '':
41 triplets.append({'head': subject.strip(), 'head_type': subject_type, 'type': relation.strip(),'tail': object_.strip(), 'tail_type': object_type})
42 return triplets
43extracted_triplets = extract_triplets_typed(extracted_text[0])
44print(extracted_triplets)1from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
2
3def extract_triplets_typed(text):
4 triplets = []
5 relation = ''
6 text = text.strip()
7 current = 'x'
8 subject, relation, object_, object_type, subject_type = '','','','',''
9
10 for token in text.replace("<s>", "").replace("<pad>", "").replace("</s>", "").replace("tp_XX", "").replace("__en__", "").split():
11 if token == "<triplet>" or token == "<relation>":
12 current = 't'
13 if relation != '':
14 triplets.append({'head': subject.strip(), 'head_type': subject_type, 'type': relation.strip(),'tail': object_.strip(), 'tail_type': object_type})
15 relation = ''
16 subject = ''
17 elif token.startswith("<") and token.endswith(">"):
18 if current == 't' or current == 'o':
19 current = 's'
20 if relation != '':
21 triplets.append({'head': subject.strip(), 'head_type': subject_type, 'type': relation.strip(),'tail': object_.strip(), 'tail_type': object_type})
22 object_ = ''
23 subject_type = token[1:-1]
24 else:
25 current = 'o'
26 object_type = token[1:-1]
27 relation = ''
28 else:
29 if current == 't':
30 subject += ' ' + token
31 elif current == 's':
32 object_ += ' ' + token
33 elif current == 'o':
34 relation += ' ' + token
35 if subject != '' and relation != '' and object_ != '' and object_type != '' and subject_type != '':
36 triplets.append({'head': subject.strip(), 'head_type': subject_type, 'type': relation.strip(),'tail': object_.strip(), 'tail_type': object_type})
37 return triplets
38
39# Load model and tokenizer
40tokenizer = AutoTokenizer.from_pretrained("Babelscape/mrebel-base", src_lang="en", tgt_lang="en")
41# Here we set English ("en") as source language. To change the source language swap the first token of the input for your desired language or change to supported language.
42model = AutoModelForSeq2SeqLM.from_pretrained("Babelscape/mrebel-base")
43gen_kwargs = {
44 "max_length": 256,
45 "length_penalty": 0,
46 "num_beams": 3,
47 "num_return_sequences": 3,
48 "forced_bos_token_id": None,
49}
50
51# Text to extract triplets from
52text = 'The Red Hot Chili Peppers were formed in Los Angeles by Kiedis, Flea, guitarist Hillel Slovak and drummer Jack Irons.'
53
54# Tokenizer text
55model_inputs = tokenizer(text, max_length=256, padding=True, truncation=True, return_tensors = 'pt')
56
57# Generate
58generated_tokens = model.generate(
59 model_inputs["input_ids"].to(model.device),
60 attention_mask=model_inputs["attention_mask"].to(model.device),
61 **gen_kwargs,
62)
63
64# Extract text
65decoded_preds = tokenizer.batch_decode(generated_tokens, skip_special_tokens=False)
66
67# Extract triplets
68for idx, sentence in enumerate(decoded_preds):
69 print(f'Prediction triplets sentence {idx}')
70 print(extract_triplets_typed(sentence))