我正在尝试使用python制作聊天机器人,为此我使用Spacy进行实体识别,因此我安装了预构建Spacy英语语言模型(medium),以从用户话语中提取实体,但问题是,当我加载模型以从用户话语中提取实体时,加载模型需要31秒,因为我在做聊天机器人,时间对我来说真的很重要。 需要你们所有人的指导,还有其他选择吗?任何帮助都将不胜感激
下面是从用户话语中提取实体的代码:
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"))
输出:
31.0 seconds
[('London', 'Countries, cities, states'), ('wednesday', 'Absolute or relative dates or periods')]
这非常慢,因为它为每个句子加载模型:
这并不慢,因为它只加载一次模型,并在每次函数调用中重新使用它:
您应该将应用程序更改为第二个示例。这并不特定于spaCy,但适用于您选择使用的任何类型的模型
相关问题 更多 >
编程相关推荐