2020-07-31 15:02:54 +00:00
|
|
|
from typing import Optional
|
2020-02-27 17:42:27 +00:00
|
|
|
from thinc.api import chain, clone, list2ragged, reduce_mean, residual
|
|
|
|
from thinc.api import Model, Maxout, Linear
|
|
|
|
|
2020-02-28 10:57:41 +00:00
|
|
|
from ...util import registry
|
2020-05-20 09:41:12 +00:00
|
|
|
from ...kb import KnowledgeBase
|
|
|
|
from ...vocab import Vocab
|
2020-02-27 17:42:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
@registry.architectures.register("spacy.EntityLinker.v1")
|
2020-07-31 15:02:54 +00:00
|
|
|
def build_nel_encoder(tok2vec: Model, nO: Optional[int] = None) -> Model:
|
2020-02-27 17:42:27 +00:00
|
|
|
with Model.define_operators({">>": chain, "**": clone}):
|
|
|
|
token_width = tok2vec.get_dim("nO")
|
|
|
|
output_layer = Linear(nO=nO, nI=token_width)
|
|
|
|
model = (
|
|
|
|
tok2vec
|
|
|
|
>> list2ragged()
|
|
|
|
>> reduce_mean()
|
|
|
|
>> residual(Maxout(nO=token_width, nI=token_width, nP=2, dropout=0.0))
|
|
|
|
>> output_layer
|
|
|
|
)
|
|
|
|
model.set_ref("output_layer", output_layer)
|
|
|
|
model.set_ref("tok2vec", tok2vec)
|
|
|
|
return model
|
2020-05-20 09:41:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
@registry.assets.register("spacy.KBFromFile.v1")
|
2020-07-31 15:02:54 +00:00
|
|
|
def load_kb(vocab_path: str, kb_path: str) -> KnowledgeBase:
|
2020-07-22 11:42:59 +00:00
|
|
|
vocab = Vocab().from_disk(vocab_path)
|
2020-05-20 09:41:12 +00:00
|
|
|
kb = KnowledgeBase(vocab=vocab)
|
|
|
|
kb.load_bulk(kb_path)
|
|
|
|
return kb
|