-1

I tried writing this code:

def stopWords(text, stopwords):
  stopwords = map(to_lower(x),stopwords)
  pattern = '/[0-9\W]/'
  text = re.sub(pattern, ',', text)
  text_array = text.partition(',');
  text_array = map(to_lower(x), text_array);
  keywords = []
for term in text_array:
  if(term in stopwords):
     keywords.append(term)
 return filter(None, keywords)
  
stopwords = open('stop_words.txt','r').read()
text = "All words in the English language can be classified as one of the eight different parts of speech."
print(stopWords(text, stopwords))

But I get an error that says IndentationError: unindent does not match any outer indentation level. What i am doing wrong?

5
  • 1
    You did not properly intent your code lines. Unlike in PHP indentation is very important in Python. Commented Jan 9, 2018 at 10:04
  • Do you know why you need indentation in Python? Are you aware that your line for term in text_array: ends the function? Commented Jan 9, 2018 at 10:04
  • @Matthias no, i am not properly aware about that, but i am interested to know about that Commented Jan 9, 2018 at 10:06
  • @Matthias ok got it, no. of spaces (enter) matter in python Commented Jan 9, 2018 at 10:08
  • Indentation is the way to define things that belong together. In PHP you use { and } and the indentation of the code is optional. In Python the indentation of the code defines the code structure. Commented Jan 9, 2018 at 10:10

1 Answer 1

1

The for should be indented, as you write it, it seems to be out of the function. Moreover, the last return is not aligned both with the for or the function.

A correct indentation should look like this:

def stopWords(text, stopwords):
  stopwords = map(to_lower(x),stopwords)
  pattern = '/[0-9\W]/'
  text = re.sub(pattern, ',', text)
  text_array = text.partition(',');
  text_array = map(to_lower(x), text_array);
  keywords = []
  for term in text_array:
    if(term in stopwords):
      keywords.append(term)
  return filter(None, keywords)
Sign up to request clarification or add additional context in comments.

1 Comment

@LovepreetSinghBatth as long as you correctly indent the function, it should work

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.