Skip to content

Commit c8dcbe6

Browse files
committed
prettify: better saxon genitive handing + fixed a couple of false positive tests
1 parent 9dcd112 commit c8dcbe6

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

string_utils.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@
131131
r')',
132132
re.MULTILINE | re.DOTALL
133133
),
134-
'NO_SPACES': re.compile(
134+
'SAXON_GENITIVE': re.compile(
135135
r'('
136-
r'(?<=\w)\'\s(?=s)|\s\'\s(?=s)' # saxon genitive
136+
r'(?<=\w)\'\ss\s|(?<=\w)\s\'s(?=\w)|(?<=\w)\s\'s\s(?=\w)'
137137
r')',
138138
re.MULTILINE | re.UNICODE
139139
)
@@ -482,8 +482,9 @@ def uppercase_after_sign(regex_match):
482482
p = PRETTIFY_RE['SPACES_AROUND'].sub(lambda m: ' ' + m.group(1).strip() + ' ', p)
483483
p = PRETTIFY_RE['SPACES_INSIDE'].sub(lambda m: m.group(1).strip(), p)
484484
p = PRETTIFY_RE['UPPERCASE_AFTER_SIGN'].sub(uppercase_after_sign, p)
485-
p = PRETTIFY_RE['NO_SPACES'].sub(lambda m: m.group(1).strip(), p)
485+
p = PRETTIFY_RE['SAXON_GENITIVE'].sub(lambda m: m.group(1).replace(' ', '') + ' ', p)
486486
p = p.strip()
487-
if len(p) > 1:
488-
p = p[0].capitalize() + p[1:]
489-
return p
487+
try:
488+
return p[0].capitalize() + p[1:]
489+
except IndexError:
490+
return p

tests.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,8 +1071,10 @@ def test_should_replace_multiple_single_quotes_with_single_ones(self):
10711071
self.assertEqual('Dave\'s job', prettify("Dave''s job"))
10721072
self.assertEqual("'destiny'", prettify("'''destiny'''"))
10731073

1074-
def should_fix_saxon_genitive_spaces(self):
1075-
self.assertEqual('Dave\'s dog', prettify('Dave\' s dog'))
1074+
def test_should_fix_saxon_genitive_spaces(self):
1075+
self.assertEqual("Dave's dog", prettify("Dave' s dog"))
1076+
self.assertEqual("Dave's dog", prettify("Dave 's dog"))
1077+
self.assertEqual("Dave's dog", prettify("Dave 'sdog"))
10761078

10771079
def test_should_replace_multiple_percentage_with_single_ones(self):
10781080
self.assertEqual('%', prettify('%%%'))
@@ -1156,12 +1158,12 @@ def test_should_add_spaces_around_equal_if_missing(self):
11561158
def test_should_add_spaces_around_division_if_missing(self):
11571159
self.assertEqual('5 / 2 = 2.5', prettify('5/ 2 = 2.5'))
11581160
self.assertEqual('5 / 2 = 2.5', prettify('5 /2 = 2.5'))
1159-
self.assertEqual('5 / 2 = 2.5', prettify('5 / 2 = 2.5'))
1161+
self.assertEqual('5 / 2 = 2.5', prettify('5/2 = 2.5'))
11601162

11611163
def test_should_add_spaces_around_multiplication_if_missing(self):
11621164
self.assertEqual('5 * 2 = 10', prettify('5* 2 = 10'))
11631165
self.assertEqual('5 * 2 = 10', prettify('5 *2 = 10'))
1164-
self.assertEqual('5 * 2 = 10', prettify('5 * 2 = 10'))
1166+
self.assertEqual('5 * 2 = 10', prettify('5*2 = 10'))
11651167

11661168
def test_should_prettify_string_as_expected(self):
11671169
original = ' unprettified string ,, like this one,will be"prettified" .it\' s awesome!( like python)) '
@@ -1172,7 +1174,9 @@ def test_should_work_as_expected_for_multiple_lines_string(self):
11721174
original = '''
11731175
11741176
unprettified string ,,
1177+
11751178
like this one,will be"prettified"
1179+
11761180
.it' s awesome!( like python))
11771181
11781182
'''

0 commit comments

Comments
 (0)