Exclude text within quotation marks and words beginning with a capital letter

Peter Otten __peter__ at web.de
Tue Dec 1 07:43:56 EST 2015


Kevin Glover wrote:

> I am working on a program that is written in Python 2.7 to be compatible
> with the POS tagger that I import from Pattern. The tagger identifies all
> the nouns in a text. I need to exclude from the tagger any text that is
> within quotation marks, and also any word that begins with an upper case
> letter (including words at the beginning of sentences).
> 
> Any advice on coding that would be gratefully received. Thanks.

How about removing them afterwards?

>>> def skip_quoted(pairs):
...     quoted = False
...     for a, b in pairs:
...             if a == '"':
...                     quoted = not quoted
...             elif not quoted:
...                     yield a, b
... 
>>> from pattern.en import tag
>>> [p for p in skip_quoted(tag('Did you say "Hello world"?')) if not p[0]
[0].isupper()]
[(u'you', u'PRP'), (u'say', u'VB'), (u'?', u'.')]





More information about the Python-list mailing list