Name: ______________ Net id: _____________ || Name: ______________ Net id: _____________ Name: ______________ Net id: _____________ || Name: ______________ Net id: _____________Snarf code for lab 12.
rsgModel.py
Why does it print two
different excuses? Be brief.
#print m.group()
in the function
initialize
is commented out. Remove the # so that the line
is executed when the program is run. Run the program and
provide a high-level explanation of what the six-lines printed that
begin with a curly-brace are.
"\s+"
matches any sequence of
adjacent white-space characters include space, tab,
newline. What's the purpose of the code below in
initialize
. How did you figure this out?
flat = re.sub(r"\s+", " ", text)
[^}]+
which represents one or more occurrences of any
character other than a right curly brace. Explain the
purpose of this regular expression in variable
gram
in the function initialize
.
parse_rule
the regular expression
pattern = r"{\s*(<[^>]+>)(.*)}"
is somewhat complex. This
matches "{" followed by \s* which is 0 or more white-space characters,
followed by <[^>]+>
.
mat.group(1)
in the program/code because
it's the first regular expression surrounded by parentheses.
pattern
is the regular expression
.* which matches anything (none or more occurrences of . which is
any character). This is mat.group(2)
, what is its purpose
in the program?
partPattern
at a high-level in how it's used to parse
the grammar definition and append things to the list created in the
dictionary. You may want to add print statements to the
program to help you understand things.
generate_from
uses two list methods
to add elements to the list words
: both append
and extend
. Explain when each is used (look at the code)
and why extend
is used instead of
append
-- use concepts, don't explain the Python
code so
much as why the Python code is written.
URLreader
. Explain
conceptually how the call to
rsgModel.initialize
works in initializing the model with a
grammar read from a URL.