Description
Hi Mark and All,
I've been working through your book and it has been going really well. Thank you for the clear instruction, witty humour, and clear communication.
I was wondering if you could explain an issue I am having with the File of Patterns example.
Code:
`import re
def build_match_and_apply_functions(pattern, search, replace):
def matches_rule(word):
return re.search(pattern, word)
def apply_rule(word):
fixed = re.sub(search, replace, word)
if
return fixed[:-1]
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, 2)
rules.append(build_match_and_apply_functions(
pattern, search, replace))
def plural(noun):
for matches_rule, apply_rule in rules:
if matches_rule(noun):
return apply_rule(noun)`
Data File:
[sxz]$ $ es
[^aeioudgkprt]h$ $ es
[^aeiou]y$ y$ ies
$ $ s
Shell Output:
IN: MakePlural.plural('match')
OUT: 'matches\n'
IN: MakePlural.plural('tax')
OUT: 'taxes\n'
IN: MakePlural.plural('happy')
OUT: 'happies\n' *Yes I know this is not a real word.
IN: MakePlural.plural('tent')
OUT: 'tents'
So, the output is including the next line character ('\n') when it uses the data file. But only for the first three rules, it doesn't have this issue for the catch all last rule. The code works perfectly fine when it was the more basic version (having the patterns as strings within the code), but has issues when using the text file. I have tried tabs, spaces, not that it should matter, but it still includes the '\n. I am on a Mac and used text editor to make the text file.
Would love to figure out this mystery. Cheers,
Adam Rogerson
- Update, Thanks! First time poster, used the <> (still isn't showing the tabbing, not sure how to use it properly) and implemented the .strip. Didn't know it read the \n.