Writing a parser the right way?

beza1e1 andreas.zwinkau at googlemail.com
Wed Sep 21 13:04:21 CEST 2005


I'm writing a parser for english language. This is a simple function to
identify, what kind of sentence we have. Do you think, this class
wrapping is right to represent the result of the function? Further
parsing then checks isinstance(text, Declarative).

-------------------
class Sentence(str): pass
class Declarative(Sentence): pass
class Question(Sentence): pass
class Command(Sentence): pass

def identify_sentence(text):
    text = text.strip()
    if text[-1] == '.':
        return Declarative(text)
    elif text[-1] == '!':
        return Command(text)
    elif text[-1] == '?':
        return Question(text)
    return text
-------------------

At first i just returned the class, then i decided to derive Sentence
from str, so i can insert the text as well.




More information about the Python-list mailing list