Write a state-of-the-art code (using a transformer model) for extracting…
- User
Write a state-of-the-art code (using a transformer model) for extracting subject-verb-object triples from the Wiki texts.
Assistant
Here is a code for extracting subject-verb-object triples from the Wiki texts using a transformer model:
import transformers from transformers import BertTokenizer, BertModel # Load the tokenizer tokenizer = BertTokenizer.from_pretrained('bert-base-uncased') # Load the model model = BertModel.from_pretrained('bert-base-uncased') # Define a function to extract subject-verb-object triples def extract_triples(text): # Tokenize the text encoded_inputs = tokenizer(text, return_tensors='pt') # Get the output of the model outputs = model(**encoded_inputs) # Get the subject, verb, and object subject = outputs[0][0].argmax(dim=1).item() verb = outputs[0][1].argmax(dim=1).item() object = outputs[0][2].argmax(dim=1).item() # Return the triple return (subject, verb, object) # Get the text of the Wiki article text = """ The Titanic was a British passenger liner that sank in the North Atlantic Ocean in the early morning of 15 April 1912 after colliding with an iceberg. The sinking of the Titanic caused the deaths of an estimated 1,500 people, making it one of the deadliest maritime disasters in history. """ # Extract the subject-verb-object triples triples = extract_triples(text) # Print the triples print(triples)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.