You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
(Please use the code formatting tool (<> above), it is difficult to read Python without indenting.)
Reading a file with for line in pattern_file, Python includes a line-end character in line. This ends up in replace, and in your output. I'm guessing you don't have a newline at the end of your pattern file, so the last pattern doesn't end in \n.
To correct the problem, you can do line = line.strip() before the split.
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
The text was updated successfully, but these errors were encountered: