Search code examples
pythonstanford-nlp

Run custom trained stanford ner model as server


I trained my own model for NER using Stanford NER. I can interface to it from my python code using the following code and it annotates named entities in the input text correctly.

st = NERTagger('stanford_classifiers/ner-model-custom.ser.gz',
               'stanford_classifiers/stanford-ner.jar', 'utf-8')
        entities = st.tag(input_text.split())

But since, it is too slow for each text input , I run the model as a server and access the result by querying from python using following code. But I get no output from my trained model.

I start the server like this :

java -mx1000m -cp stanford-ner.jar edu.stanford.nlp.ie.NERServer -loadClassifier ner-m
odel-custom.ser.gz -port 8080 -outputFormat inlineXML

I get this message on server :

Loading classifier from XX\XX\stanford_classifiers\ner-model-custom.ser.gz ... done [6.1 sec].

On the client side (python code) :

tagger = ner.SocketNER(host='localhost', port=8080)
entities = tagger.get_entities(input_text)

The same code works when an existing model like english.all.3class.distsim.crf.ser.gz is started as a server. But not for the custom model in server mode.

Is there a specific way to start our custom trained ner models in server version ?

Could this be a problem with the training of the custom model ?


Solution

  • Thank you @ChristopherManning for responding. You are right. the model is working fine. The custom model also annotated the input text in the inlineXML format correctly.

    It just did not return the entities in the tagger.get_entities(input_text) function. I instead had to use another function in the python interface : tagger.tag_text(input_text) and then using an xml minidom parser to extract the annotated companies.

    Input text : "She is working at facebook"
    
    Received by tagger using tag_text function : "She is working at <B-company>facebook</B-company>"
    

    Parsed this output to get the list of entities.