Skip to content

Commit 5dbe80c

Browse files
committed
CorrectBot: Remove spaces inside quotation marks (#87)
Strip whitespace immediately inside opening and closing quote marks in QuotationMarkCorrector._correct_quotes(), so all languages using it benefit without per-language changes Close #87
1 parent 0d98fd0 commit 5dbe80c

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

pywikitools/correctbot/correctors/universal.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,8 @@ def _correct_quotes(
371371
372372
We do this by replacing all of „“”" with start and end quotation mark (alternating).
373373
This only works if we have an even amount of quotation marks. If not, we issue a warning
374-
and don't change anything.
374+
and don't change anything. After normalization, redundant whitespace immediately inside
375+
the quotation marks is removed (e.g. „ word ” becomes „word”).
375376
376377
Args:
377378
start_quotation_mark: The character used at the start of a quotation
@@ -391,6 +392,10 @@ def _correct_quotes(
391392
for counter in range(1, len(splitted_text)):
392393
text += start_quotation_mark if counter % 2 == 1 else end_quotation_mark
393394
text += splitted_text[counter]
395+
start = re.escape(start_quotation_mark)
396+
end = re.escape(end_quotation_mark)
397+
text = re.sub(start + r"\s+", start_quotation_mark, text)
398+
text = re.sub(r"\s+" + end, end_quotation_mark, text)
394399
return text
395400

396401

pywikitools/test/test_correctbot.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from pywikitools.correctbot.correctors.base import CorrectorBase
2020
from pywikitools.correctbot.correctors.de import GermanCorrector
2121
from pywikitools.correctbot.correctors.fr import FrenchCorrector
22+
from pywikitools.correctbot.correctors.ro import RomanianCorrector
2223
from pywikitools.correctbot.correctors.tr import TurkishCorrector
2324
from pywikitools.correctbot.correctors.universal import (
2425
NoSpaceBeforePunctuationCorrector,
@@ -545,6 +546,14 @@ def test_correct_quotes(self):
545546
correct(corrector, '"Something" wrong"'), '"Something" wrong"'
546547
)
547548

549+
def test_correct_quotes_spacing(self):
550+
corrector = QuotationMarkCorrectorTester()
551+
self.assertEqual(correct(corrector, "» Test«"), "»Test«")
552+
self.assertEqual(correct(corrector, "»Test «"), "»Test«")
553+
self.assertEqual(
554+
correct(corrector, "Start » word « end"), "Start »word« end"
555+
)
556+
548557

549558
class RTLCorrectorTester(CorrectorBase, RTLCorrector):
550559
"""With this class we can test the rules of RTLCorrector"""
@@ -610,6 +619,9 @@ def test_correct_quotes(self):
610619
correct(self.corrector, '"Das ist" seltsam"'), '"Das ist" seltsam"'
611620
)
612621

622+
for spaced in ["„ Test “", "„Test “", "„ Test“"]:
623+
self.assertEqual(correct(self.corrector, spaced), "„Test“")
624+
613625
valid_strings: List[str] = [ # Some more complex examples
614626
"(siehe Arbeitsblatt „[[Forgiving Step by Step/de|Schritte der Vergebung]]“)",
615627
"[[How to Continue After a Prayer Time/de|„Wie es nach einer Gebetszeit weitergeht“]]",
@@ -665,6 +677,24 @@ def test_suffix_for_print_version(self):
665677
self.assertNotEqual(self.corrector._suffix_for_print_version(), "_print")
666678

667679

680+
class TestRomanianCorrector(CorrectorTestCase):
681+
@classmethod
682+
def setUpClass(cls):
683+
cls.corrector = RomanianCorrector()
684+
685+
def test_correct_quotes(self):
686+
for spaced in ["„ cuvânt”", "„cuvânt ”", "„ cuvânt ”"]:
687+
self.assertEqual(correct(self.corrector, spaced), "„cuvânt”")
688+
self.assertEqual(correct(self.corrector, "„cuvânt”"), "„cuvânt”")
689+
self.assertEqual(
690+
correct(self.corrector, "Spune: „ da ” și „ nu ”"),
691+
"Spune: „da” și „nu”",
692+
)
693+
694+
def test_get_language_code(self):
695+
self.assertEqual(self.corrector._get_language_code(), "ro")
696+
697+
668698
"""TODO
669699
class TestEnglishCorrector(unittest.TestCase):
670700
def test_correct_apostrophe(self):

0 commit comments

Comments
 (0)