Search code examples
nlpnltkspacyspacy-3

Spacy nl is not capitalizing the sentence correctly


I am using Spacy model and want to capitalize the plain text with the starting sentence and proper noun only.

I am using the code below

nlp = spacy.load("en_core_news_lg")
doc = nlp(text)
output_text = ""
for sent in doc.sents:
    for index, token in enumerate(sent):
        token_text = token.text
        if index == 0 or token.pos in (PROPN):
            token_text = token_text.capitalize()
        output_text += token_text + token.whitespace_
        
        output_text = output_text.strip() + " "

Now the error is like below if index == 0 or token.pos in (PROPN):

TypeError: argument of type 'univ_pos_t' is not iterable

Is it possible to use this only for proper noun?


Solution

  • You need to use

    if index == 0 or token.pos_ == 'PROPN':
    

    This line will check if either index is set to 0, or if the current token POS is PROPN.