.


:




:

































 

 

 

 


, !




, , , , , . , , , . , !

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'

  1. ↑ , , . . [sxz] s x, z, . $ . . , noun s, x z.
  2. ↑ 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'

  1. Mark a, b c? , a.
  2. , a, b c o. Mark Mork.
  3. rock rook.
  4. , 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'

  1. ( $) es. , es . , , noun + 'es', , .
  2. -, - . ^ : . [^abc] a, b c. [^aeioudgkprt] a, e, i, o, u, d, g, k, p, r t. h, . , H, .
  3. : , 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') ③
>>>

  1. vacancy , cy, c a, e, i, o u.
  2. boy , oy, , y o. day , ay.
  3. pita , y.

>>> re.sub('y$', 'ies', 'vacancy') ①
'vacancies'
>>> re.sub('y$', 'ies', 'agency') 'agencies'
>>> re.sub('([^aeiou])y$', r'\1ies', 'vacancy') ②
'vacancies'

  1. vacancy vacancies, agency agencies, . , boy boies, , re.search , re.sub.
  2. , ( , ) . . : , : . 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)

  1. - re.search().
  2. - , re.sub() .
  3. (plural()) rules, .
  4. , 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. ? .
  2. ? 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) ③

  1. build_match_and_apply_functions() , . pattern, search replace, matches_rule(), re.search() pattern, build_match_and_apply_functions() , word, matches_rule(), .
  2. apply . apply , , re.sub() search replace , build_match_and_apply_functions, word, apply_rule(), . , . : (word), (search replace), .
  3. 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]

  1. ( ). , re.search() , . , re.sub() .
  2. . match_default() True, , , s . . , ($ ). , , , . , , match_default(), True: , , s .
  3. . 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)

  1. 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))

  1. build_match_and_apply_functions() . - , , .
  2. open() . , , - . with , : with , Python , with . with .
  3. for line in <fileobject> line. .
  4. , ( , ). , split(). split() None, ( , ). 3, 3 , [sxz]$ $ es ['[sxz]$', '$', 'es'], pattern '[sxz]$', search '$', replace 'es'.
  5. , 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

  1. yield make_counter , . , . . Ÿ , x.
  2. make_counter, . , . , make_counter() print(), .
  3. make_counter() -.
  4. next() . , next() counter, make_counter() yield, , yield. , 2, make_counter(2).
  5. next() , , , yield. , . . yield, next(). , , print(), incrementing x. x = x + 1. while, , yield x, x ( 3).
  6. next(counter) , x 4.

make_counter , , x . .





:


: 2016-11-18; !; : 472 |


:

:

, .
==> ...

1684 - | 1606 -


© 2015-2024 lektsii.org - -

: 0.033 .