0

I am trying to convert the sentence into lower case with the below code

import re
temp =[]
snow = nltk.stem.SnowballStemmer('english')
for sentence in final_X:
    sentence = str(sentence.lower())                
    cleanr = re.compile('<.*?>')
    sentence = re.sub(cleanr, ' ', sentence)        
    sentence = re.sub(r'[?|!|\'|"|#]',r'',sentence)
    sentence = re.sub(r'[.|,|)|(|\|/]',r' ',sentence)        

    words = [snow.stem(word) for word in sentence.split() if word not in stopwords.words('english')]   # Stemming and removing stopwords
temp.append(words)

final_X = temp


I am getting the below error while executing the code

AttributeError                            Traceback (most recent call last)
<ipython-input-31-f0e602a068f6> in <module>()
      3 snow = nltk.stem.SnowballStemmer('english')
      4 for sentence in final_X:
----> 5     sentence = str(sentence.lower())
      6     cleanr = re.compile('<.*?>')
      7     sentence = re.sub(cleanr, ' ', sentence)

**AttributeError: 'list' object has no attribute 'lower'**
1
  • 1
    changing str(sentence.lower()) to str(sentence).lower() will make the error go away. But you could possibly prefer ''.join(sentence).lower() or a variation thereof. Commented Oct 22, 2019 at 18:06

1 Answer 1

1

It's impossible to give you an exact fix as your post is not a reproduce-able error. However the error message is pretty clear

sentence is of type list and has no method lower

One could imagine something like this

sentence = ['This', 'is', 'a', 'sentence']

If you want it in one string all lower case, you could do the following

' '.join(sentence).lower()

This will join all the strings in the list with a space into one string and then lowercase the result, yielding

'this is a sentence'

The caveat here is that based on your post I don't know what sentence looks like, just that it's a list

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.