, , , , , . , , , . , !
import re
def plural(noun):
if re.search('[sxz]$', noun): UNIQd2f4acf57a9a5326-ref-00000003-QINU
return re.sub('$', 'es', noun) UNIQd2f4acf57a9a5326-ref-00000006-QINU
elif re.search('[^aeioudgkprt]h$', noun):
return re.sub('$', 'es', noun)
elif re.search('[^aeiou]y$', noun):
return re.sub('y$', 'ies', noun)
else:
return noun + 's'
- ↑ , , . . [sxz] s x, z, . $ . . , noun s, x z.
- ↑ re.sub() .
.
>>> import re
>>> re.search('[abc]', 'Mark') ①
<_sre.SRE_Match object at 0x001C1FA8>
>>> re.sub('[abc]', 'o', 'Mark') ②
'Mork'
>>> re.sub('[abc]', 'o', 'rock') ③
'rook'
>>> re.sub('[abc]', 'o', 'caps') ④
'oops'
- Mark a, b c? , a.
- , a, b c o. Mark Mork.
- rock rook.
- , caps oaps, . re.sub , . , caps oops, c a o.
plural()
def plural(noun):
if re.search('[sxz]$', noun):
return re.sub('$', 'es', noun) ①
elif re.search('[^aeioudgkprt]h$', noun): ②
return re.sub('$', 'es', noun)
elif re.search('[^aeiou]y$', noun): ③
return re.sub('y$', 'ies', noun)
else:
return noun + 's'
- ( $) es. , es . , , noun + 'es', , .
- -, - . ^ : . [^abc] a, b c. [^aeioudgkprt] a, e, i, o, u, d, g, k, p, r t. h, . , H, .
- : , Y, Y a, e, i, o u. , Y, I.
.
|
|
>>> import re
>>> re.search('[^aeiou]y$', 'vacancy') ①
<_sre.SRE_Match object at 0x001C1FA8>
>>> re.search('[^aeiou]y$', 'boy') ②
>>>
>>> re.search('[^aeiou]y$', 'day')
>>>
>>> re.search('[^aeiou]y$', 'pita') ③
>>>
- vacancy , cy, c a, e, i, o u.
- boy , oy, , y o. day , ay.
- pita , y.
>>> re.sub('y$', 'ies', 'vacancy') ①
'vacancies'
>>> re.sub('y$', 'ies', 'agency') 'agencies'
>>> re.sub('([^aeiou])y$', r'\1ies', 'vacancy') ②
'vacancies'
- vacancy vacancies, agency agencies, . , boy boies, , re.search , re.sub.
- , ( , ) . . : , : . y. , , \1, , , ? . , c y; , c c, ies y. ( , \2 \3 .)
, \1 . , , , , . S, X Z, ES. , , S, X Z, ES. .
. : , , . , .
import re
def match_sxz(noun):
return re.search('[sxz]$', noun)
def apply_sxz(noun):
return re.sub('$', 'es', noun)
def match_h(noun):
return re.search('[^aeioudgkprt]h$', noun)
def apply_h(noun):
return re.sub('$', 'es', noun)
def match_y(noun): ①
return re.search('[^aeiou]y$', noun)
def apply_y(noun): ②
return re.sub('y$', 'ies', noun)
def match_default(noun):
return True
def apply_default(noun):
return noun + 's'
|
|
rules = ((match_sxz, apply_sxz), ③
(match_h, apply_h),
(match_y, apply_y),
(match_default, apply_default)
)
def plural(noun):
for matches_rule, apply_rule in rules: ④
if matches_rule(noun):
return apply_rule(noun)
- - re.search().
- - , re.sub() .
- (plural()) rules, .
- , plural() . for, rules . for , match_rules match_sxz, apply_rule apply_sxz. , , matches_rule match_h, apply_rule apply_h. - , (match_default) True, , (apply_default) .
, , , Python , . rules , -. for , matches_rule apply_rule , . for , matches_sxz(noun), , apply_sxz(noun).
-> rules . [wa-robin.om]
, , , . for :
def plural(noun):
if match_sxz(noun):
return apply_sxz(noun)
if match_h(noun):
return apply_h(noun)
if match_y(noun):
return apply_y(noun)
if match_default(noun):
return apply_default(noun)
, plural() . , -, .
- ? .
- ? 1.
, . plural() .
, ? - . , . if plural(). , , match_foo() apply_foo(), rules , .
, .
. ; rules . , . re.search(), re.sub(). , .
import re
def build_match_and_apply_functions(pattern, search, replace):
def matches_rule(word): ①
return re.search(pattern, word)
def apply_rule(word): ②
return re.sub(search, replace, word)
return (matches_rule, apply_rule) ③
- build_match_and_apply_functions() , . pattern, search replace, matches_rule(), re.search() pattern, build_match_and_apply_functions() , word, matches_rule(), .
- apply . apply , , re.sub() search replace , build_match_and_apply_functions, word, apply_rule(), . , . : (word), (search replace), .
- build_match_and_apply_functions() , , . , (pattern match_rule()), search replace apply_rule()) , . .
( , ), , .
|
|
patterns = \ ①
(
('[sxz]$', '$', 'es'),
('[^aeioudgkprt]h$', '$', 'es'),
('(qu|[^aeiou])y$', 'y$', 'ies'),
('$', '$', 's') ②
)
rules = [build_match_and_apply_functions(pattern, search, replace) ③
for (pattern, search, replace) in patterns]
- ( ). , re.search() , . , re.sub() .
- . match_default() True, , , s . . , ($ ). , , , . , , match_default(), True: , , s .
- . patterns . ? build_and_apply_functions(). build_match_and_apply_functions() . build_match_and_apply_functions() . , rules : , . , re.search(), (), re.sub().
, plural().
def plural(noun):
for matches_rule, apply_rule in rules: ①
if matches_rule(noun):
return apply_rule(noun)
|
|
- rules , (, ), , plural() . ; - . , . . build_match_and_apply_functions() . . plural() .
. , .
-, , . , . plural4-rules.txt
[sxz]$ $ es
[^aeioudgkprt]h$ $ es
[^aeiou]y$ y$ ies
$ $ s
, .
import re
def build_match_and_apply_functions(pattern, search, replace): ①
def matches_rule(word):
return re.search(pattern, word)
def apply_rule(word):
return re.sub(search, replace, word)
return (matches_rule, apply_rule)
rules = []
with open('plural4-rules.txt', encoding='utf-8') as pattern_file: ②
for line in pattern_file: ③
pattern, search, replace = line.split(None, 3) ④
rules.append(build_match_and_apply_functions(⑤
pattern, search, replace))
- build_match_and_apply_functions() . - , , .
- open() . , , - . with , : with , Python , with . with .
- for line in <fileobject> line. .
- , ( , ). , split(). split() None, ( , ). 3, 3 , [sxz]$ $ es ['[sxz]$', '$', 'es'], pattern '[sxz]$', search '$', replace 'es'.
- , pattern, search replace build_match_and_apply_function(), . rules, rules , plural().
, , . , , .
plural() ? , , , . , plural() , .
def rules(rules_filename):
with open(rules_filename, encoding='utf-8') as pattern_file:
for line in pattern_file:
pattern, search, replace = line.split(None, 3)
yield build_match_and_apply_functions(pattern, search, replace)
def plural(noun, rules_filename='plural5-rules.txt'):
for matches_rule, apply_rule in rules(rules_filename):
if matches_rule(noun):
return apply_rule(noun)
raise ValueError('no matching rule for {0}'.format(noun))
? .
>>> def make_counter(x):
... print('entering make_counter')
... while True:
... yield x ①
... print('incrementing x')
... x = x + 1
...
>>> counter = make_counter(2) ②
>>> counter ③
<generator object at 0x001C9C10>
>>> next(counter) ④
entering make_counter
2
>>> next(counter) ⑤
incrementing x
3
>>> next(counter) ⑥
incrementing x
4
|
|
- yield make_counter , . , . . Ÿ , x.
- make_counter, . , . , make_counter() print(), .
- make_counter() -.
- next() . , next() counter, make_counter() yield, , yield. , 2, make_counter(2).
- next() , , , yield. , . . yield, next(). , , print(), incrementing x. x = x + 1. while, , yield x, x ( 3).
- next(counter) , x 4.
make_counter , , x . .