A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-list/2005-September/296128.html below:

Writing a parser the right way?

Writing a parser the right way? Writing a parser the right way?beza1e1 andreas.zwinkau at googlemail.com
Wed Sep 21 07:04:21 EDT 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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4