Programmer_nltk
Programmer_nltk

Reputation: 915

Replace tags in place of words - spacy

I am trying to replace the sentence with their tags. I want tags to replace words and form as a sentence.

from __future__ import unicode_literals
import spacy,en_core_web_sm
import textacy
nlp = en_core_web_sm.load()
sentence = 'The cat sat on the mat. the dog sleeps.'
doc = nlp(sentence)
for token in doc:
print(token.dep_)

output.

det
nsubj
ROOT
prep
det
pobj
punct
det
nsubj
ROOT
punct

Expected output:

det nsubj ROOT prep det pobj det punct det nsubj ROOT punct

Upvotes: 1

Views: 632

Answers (1)

Rakesh
Rakesh

Reputation: 82765

You can use str.join

Ex:

print(" ".join([token.dep_ for token in doc]))   #List comprehension 

Upvotes: 3

Related Questions