1
$\begingroup$

How is it possible to replace words in a sentence with their respective PoS tags generated with SpaCy in an efficient way?

$\endgroup$
1
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.