To create a custom detector that responds to specific locales, you must implement a supported_locale(cls, locale) class method.
supported_locale(cls, locale): This method must return True if the provided locale is supported by the detector, and False otherwise. If it returns False, the Scrubber will emit a warning and skip this detector for that document.locale_split(locale): Use this helper method within your detector to split the xx_YY locale string into its language and region components.
If supported_locale is not defined, the detector is assumed to be location-independent.
import scrubadub, re
class EmployeeNameFilth(scrubadub.filth.Filth):
type = 'employee_name'
class EmployeeDetector(scrubadub.detectors.Detector):
name = 'employee_detector'
def __init__(self, *args, **kwargs):
super(EmployeeDetector, self).__init__(*args, **kwargs)
# Example: mapping regions to specific names
self.employees = {'DE': ['Walther'], 'US': ['Georgina'] }
# Note: In a real implementation, self.region would be derived from self.locale
self.region = self.locale.split('_')[1]
self.regex = re.compile('|'.join(self.employees[self.region]))
@classmethod
def supported_locale(cls, locale):
# Use locale_split to get language and region
language, region = cls.locale_split(locale)
return region in ['DE', 'US']
def iter_filth(self, text, document_name=None):
for match in self.regex.finditer(text):
yield EmployeeNameFilth(match=match, detector_name=self.name, document_name=document_name, locale=self.locale)