【发布时间】:2019-06-23 22:43:20
【问题描述】:
加载 spacy 模型会减慢我的单元测试的运行速度。有没有办法模拟 spacy 模型或 Doc 对象来加速单元测试?
当前慢速测试示例
import spacy
nlp = spacy.load("en_core_web_sm")
def test_entities():
text = u"Google is a company."
doc = nlp(text)
assert doc.ents[0].text == u"Google"
根据文档,我的方法是
手动构建 Vocab 和 Doc 并将实体设置为元组。
from spacy.vocab import Vocab
from spacy.tokens import Doc
def test()
alphanum_words = u"Google Facebook are companies".split(" ")
labels = [u"ORG"]
words = alphanum_words + [u"."]
spaces = len(words) * [True]
spaces[-1] = False
spaces[-2] = False
vocab = Vocab(strings=(alphanum_words + labels))
doc = Doc(vocab, words=words, spaces=spaces)
def get_hash(text):
return vocab.strings[text]
entity_tuples = tuple([(get_hash(labels[0]), 0, 1)])
doc.ents = entity_tuples
assert doc.ents[0].text == u"Google"
是否有更简洁的 Pythonic 解决方案来模拟 spacy 对象以进行实体的单元测试?
【问题讨论】:
标签: unit-testing spacy