docstring blocks elif statement

645 Views Asked by At

Let me past the exact code I have: This is the short module

class SentenceSplitter:

def __init__(self, filename=None):
    self._raw_text = self.raw_text(filename)
    self._sentences = self.to_sentences()


def raw_text(self, filename):
    text = ''
    with open(filename, 'r') as file:
        for line in file.readlines():
            line = line.strip()
            text += ''.join(line.replace(line, line+' '))
    file.close()
    text = text.strip() # Deal with the last whitespace
    return text

def to_sentences(self):
    """ Sentence boundaries occur at '.', '!', '?' except that,
    there are some not-sentence boundaries that
    may occur before/after the period.
    """
    raw_text = self._raw_text
    sentences = []
    sentence = ''
    boundary = None

    for char in raw_text:
        sentence += ''.join(char)
        if char == '!' or char == '?':
            sentences.append(sentence)
            sentence = ''

        """ The sign -> refers to 'followed by' """
        elif char == '.':
            i = raw_text.index(char) # slicing previous/following characters
            boundary = True

        if boundary:
            sentences.append(sentence)
            sentence = ''

    return sentences

And the main:

import textchange

ss = textchange.SentenceSplitter(filename='text.txt')
print(ss._sentences)

The docstring after the first if statement

""" The sign -> refers to 'followed by' """

I commented it out and the program runs, else does not. There is more code in the elif statement but removed it after making sure it still throwing error. Here is the traceback:

Traceback (most recent call last):
File "D:\Programs\Python 3.3.2\Tutorials\46 Simple Python Exercises.py", line 26, in        
<module>
import textchange
File "D:\Programs\Python 3.3.2\Tutorials\textchange.py", line 51
elif char == '.':
   ^
SyntaxError: invalid syntax
1

There are 1 best solutions below

0
On

Docstrings are just string literals that are found at the start of the function. They still have to follow indentation rules.

Your string is not correctly indented for the elif block; by being de-dented from the if block before, you ended the if-elif-else blocks altogether and no elif is permitted to follow.

Use a regular, normal comment instead, a line starting with #; lines that contain only comments are exempt from the indentation rules:

if char == '!' or char == '?':
    sentences.append(sentence)
    sentence = ''

# The sign -> refers to 'followed by'
elif char == '.':
    i = raw_text.index(char) # slicing previous/following characters
    boundary = True

or indent the string (which is entirely still executed by Python as code, but otherwise not assigned and thus discarded again):

if char == '!' or char == '?':
    sentences.append(sentence)
    sentence = ''

elif char == '.':
    """ The sign -> refers to 'followed by' """
    i = raw_text.index(char) # slicing previous/following characters
    boundary = True