To make a comparable study, I am working with data that has already been tokenised (not with spacy). I need to use these tokens as input to ensure that I work with the same data across the board. I wish to feed these tokens into spaCy's tagger, but the following fails:
import spacy
nlp = spacy.load('en', disable=['tokenizer', 'parser', 'ner', 'textcat'])
sent = ['I', 'like', 'yellow', 'bananas']
doc = nlp(sent)
for i in doc:
print(i)
with the following trace
Traceback (most recent call last):
File "C:/Users/bmvroy/.PyCharm2018.2/config/scratches/scratch_6.py", line 6, in <module>
doc = nlp(sent)
File "C:\Users\bmvroy\venv\lib\site-packages\spacy\language.py", line 346, in __call__
doc = self.make_doc(text)
File "C:\Users\bmvroy\venv\lib\site-packages\spacy\language.py", line 378, in make_doc
return self.tokenizer(text)
TypeError: Argument 'string' has incorrect type (expected str, got list)
First of all, I'm not sure why spaCy tries to tokenize the input as I disabled the tokenizer in the load() statement. Second, evidently this is not the way to go.
I am looking for a way to feed the tagger a list of tokens. Is that possible with spaCy?
I tried the solution provided by @aab combined with info from the documentation but to no avail:
from spacy.tokens import Doc
from spacy.lang.en import English
from spacy.pipeline import Tagger
nlp = English()
tagger = Tagger(nlp.vocab)
words = ['Listen', 'up', '.']
spaces = [True, False, False]
doc = Doc(nlp.vocab, words=words, spaces=spaces)
processed = tagger(doc)
print(processed)
This code didn't run, and gave the following error:
processed = tagger(doc)
File "pipeline.pyx", line 426, in spacy.pipeline.Tagger.__call__
File "pipeline.pyx", line 438, in spacy.pipeline.Tagger.predict
AttributeError: 'bool' object has no attribute 'tok2vec'