Spacy 英语语言模型加载时间过长
Spacy english language model take too long to load
我正在尝试使用 python 制作聊天机器人,为此我使用 Spacy 进行实体识别,因此我安装了预构建的 Spacy 英语语言模型(中)以从用户话语中提取实体,但是问题是,当我加载模型以从用户话语中提取实体时,加载模型需要 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')]
这真的很慢,因为它为每个句子加载模型:
import spacy
def dostuff(text):
nlp = spacy.load("en")
return nlp(text)
这并不慢,因为它加载一次模型并在每次函数调用时重新使用它:
import spacy
nlp = spacy.load("en")
def dostuff(text):
return nlp(text)
您应该将您的应用程序更改为类似于第二个示例。这不是 spaCy 特有的,但您选择使用的任何类型的模型都会出现这种情况。
我正在尝试使用 python 制作聊天机器人,为此我使用 Spacy 进行实体识别,因此我安装了预构建的 Spacy 英语语言模型(中)以从用户话语中提取实体,但是问题是,当我加载模型以从用户话语中提取实体时,加载模型需要 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')]
这真的很慢,因为它为每个句子加载模型:
import spacy
def dostuff(text):
nlp = spacy.load("en")
return nlp(text)
这并不慢,因为它加载一次模型并在每次函数调用时重新使用它:
import spacy
nlp = spacy.load("en")
def dostuff(text):
return nlp(text)
您应该将您的应用程序更改为类似于第二个示例。这不是 spaCy 特有的,但您选择使用的任何类型的模型都会出现这种情况。