I am trying to make a chatbot using python and for that i am using Spacy for Entity recognition so i have installed pre build Spacy english language model(medium) to extract the entities from user utterance, but the problem is that when i load the model to extract entites from user utterance it takes 31 seconds to load the model, because i am making a chatbot time really matter in my case. Need some guidance from all of you, any alternative? any help would be really appreciated
Here is the code that extracts entities from user utterance:
import spacy
import time
def extractEntity(userUtterance):
''' This funtion returns a list of tuple a tuple contain
(entity Name, Entity Type)
We use pre build spacy english language model to extract entities
'''
start_time = time.process_time()
nlp = spacy.load("en")
print(time.process_time() - start_time, "seconds") # prints the time taken to load the model
docx = nlp(userUtterance)
listOfTyples = [(word.text, spacy.explain(word.label_)) for word in docx.ents]
return listOfTyples
if __name__ == "__main__":
print(extractEntity("I want to go to London, can you book my flight for wednesday"))
Output:
31.0 seconds
[('London', 'Countries, cities, states'), ('wednesday', 'Absolute or relative dates or periods')]
This is really slow because it loads the model for every sentence:
import spacy
def dostuff(text):
nlp = spacy.load("en")
return nlp(text)
This is not slow because it loads the model once and re-uses it for every function call:
import spacy
nlp = spacy.load("en")
def dostuff(text):
return nlp(text)
You should change your application to look like the second example. This is not specific to spaCy but will be the case with any kind of model you choose to use.