Allennlp Entity Recognition with Pre-trained Model

  • Share this:

Code introduction


This function uses the pre-trained model and predictor from the Allennlp library for entity recognition in text. It first loads a pre-trained model and predictor, then tokenizes the input text, creates an instance, and uses the predictor to predict entities in the text.


Technology Stack : Allennlp, BERT, Tokenizer, Vocabulary, TextField, Instance, Predictor

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
def random_entity_recognition(text):
    from allennlp.models import Model
    from allennlp.predictors import Predictor
    from allennlp.data import Instance, Tokenizer, Vocabulary
    from allennlp.data.fields import TextField

    # Load the pre-trained model and predictor for entity recognition
    model_path = 'https://storage.googleapis.com/allennlp-public-models/bert-base-uncased-srl-2020.11.09.tar.gz'
    predictor = Predictor.from_path(model_path)

    # Tokenize the input text
    tokenizer = Tokenizer()
    tokens = tokenizer.tokenize(text)

    # Create an instance from the tokens
    instance = Instance(
        TextField(tokens)
    )

    # Use the predictor to predict entities in the text
    result = predictor.predict(instance)

    return result