Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

regexp_tokenize() handling of paranthesis #181

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 6 additions & 7 deletions book/ch03.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1855,16 +1855,15 @@ NLTK's Regular Expression Tokenizer

The function ``nltk.regexp_tokenize()`` is similar to ``re.findall()`` (as
we've been using it for tokenization). However, ``nltk.regexp_tokenize()``
is more efficient for this task, and avoids the need for special treatment of parentheses.
For readability we break up the regular expression over several lines
and add a comment about each line. The special ``(?x)`` "verbose flag"
tells Python to strip out the embedded whitespace and comments.
is more efficient for this task. For readability we break up the regular
expression over several lines and add a comment about each line. The special
``(?x)`` "verbose flag" tells Python to strip out the embedded whitespace and comments.

>>> text = 'That U.S.A. poster-print costs $12.40...'
>>> pattern = r'''(?x) # set flag to allow verbose regexps
... ([A-Z]\.)+ # abbreviations, e.g. U.S.A.
... | \w+(-\w+)* # words with optional internal hyphens
... | \$?\d+(\.\d+)?%? # currency and percentages, e.g. $12.40, 82%
... (?:[A-Z]\.)+ # abbreviations, e.g. U.S.A.
... | \w+(?:-\w+)* # words with optional internal hyphens
... | \$?\d+(?:\.\d+)?%? # currency and percentages, e.g. $12.40, 82%
... | \.\.\. # ellipsis
... | [][.,;"'?():-_`] # these are separate tokens; includes ], [
... '''
Expand Down