Search code examples
pythonmachine-learningnlpallennlp

Access field values of an instance in AllenNLP


I´m using AllenNLP for a combined classifier (one of its task is NER tagging), and while designing some tests, I've come across this doubt: how do I access the values of the different fields of my AllenNLP instance? I want to compared them with the values introduced, to make sure nothing got lost in the preprocessing.

I managed to get to the text field of the instance using instance.__getitem__("text"), but I dont know how to get the value from there, or if there is anything quicker directly from the Instance class.

What I'm trying to do is something like assert instance.getValueFromField("text) == training_dataset["text"][0]


Solution

  • You are right that instance.__getitem__("text") will let you access the field named "text", but you shouldn't use this method directly. Instead, just do instance["text"].

    Either way, this gives you a TextField, with which you can inspect the tokens used to construct this field by looking at instance["text"].tokens.