Predict Sentiment with Allennlp TextClassifierPredictor

  • Share this:

Code introduction


This code defines a function to predict the sentiment of a given text. It uses the Allennlp library's TextClassifierPredictor to load a pre-trained model and tokenizer, then tokenize the input text and perform sentiment prediction.


Technology Stack : Allennlp, TextClassifierPredictor, Tokenizer, Sentence, Vocabulary, TextClassifier, Params

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
def predict_sentiment(text):
    from allennlp.predictors import TextClassifierPredictor
    from allennlp.data import Tokenizer, Sentence
    from allennlp.data.vocabulary import Vocabulary
    from allennlp.models import TextClassifier
    from allennlp.common import Params

    # Load the trained model and tokenizer
    model_path = "/path/to/trained/model"  # Replace with the actual path to the model
    params = Params.from_file(model_path + "/config.json")
    model = TextClassifier.load(model_path)
    tokenizer = Tokenizer.from_file(model_path + "/vocab.json", params)
    vocab = Vocabulary.from_file(model_path + "/vocab.json")
    predictor = TextClassifierPredictor.from_path(model_path)

    # Tokenize the input text
    tokens = tokenizer.tokenize(text)
    tokenized_text = Sentence(tokens, vocabulary=vocab)

    # Predict the sentiment
    prediction = predictor.predict(tokenized_text)
    return prediction.label