How is it possible to replace words in a sentence with their respective PoS tags generated with SpaCy in an efficient way?
You can not replace tags in a sentence because Python strings are immutable.
You can make another string with just the tags:
>>> import spacy >>> nlp = spacy.load('en_core_web_sm') >>> doc = nlp("Apple is looking at buying U.K. startup for $1 billion") >>> " ".join(token.tag_ for token in doc) 'NNP VBZ VBG IN VBG NNP NN IN $ CD CD'
That example is based on the spaCy documentation.
If the tokens are split by several different non-space delimiters (called "multiple infix tokenization" in SpaCy), you would have to track that and the code would be more complex.