Wildcard in middle

main
Bob Mottram 2022-07-05 20:35:38 +01:00
parent 81c78e2b0e
commit 85780153dc
2 changed files with 50 additions and 30 deletions

View File

@ -1052,7 +1052,8 @@ def detect_dogwhistles(content: str, dogwhistles: {}) -> {}:
} }
else: else:
result[whistle]['count'] += 1 result[whistle]['count'] += 1
else: continue
if whistle.lower().endswith('-x'): if whistle.lower().endswith('-x'):
whistle = whistle[:len(whistle)-2] whistle = whistle[:len(whistle)-2]
starting = True starting = True
@ -1072,7 +1073,23 @@ def detect_dogwhistles(content: str, dogwhistles: {}) -> {}:
} }
else: else:
result[whistle]['count'] += 1 result[whistle]['count'] += 1
continue
if '*' in whistle:
whistle_start = whistle.split('*', 1)[0]
whistle_end = whistle.split('*', 1)[1]
for wrd in words:
if wrd.startswith(whistle_start) and \
wrd.endswith(whistle_end):
if not result.get(whistle):
result[whistle] = {
"count": 1,
"category": category
}
else: else:
result[whistle]['count'] += 1
continue
for wrd in words: for wrd in words:
if wrd == whistle: if wrd == whistle:
if not result.get(whistle): if not result.get(whistle):

View File

@ -7332,7 +7332,8 @@ def _test_dogwhistles():
dogwhistles = { dogwhistles = {
"X-hamstered": "hamsterism", "X-hamstered": "hamsterism",
"gerbil": "rodent", "gerbil": "rodent",
"*snake": "slither" "*snake": "slither",
"start*end": "something"
} }
content = 'This text does not contain any dogwhistles' content = 'This text does not contain any dogwhistles'
assert not detect_dogwhistles(content, dogwhistles) assert not detect_dogwhistles(content, dogwhistles)
@ -7340,6 +7341,8 @@ def _test_dogwhistles():
assert detect_dogwhistles(content, dogwhistles) assert detect_dogwhistles(content, dogwhistles)
content = 'A rattlesnake.' content = 'A rattlesnake.'
assert detect_dogwhistles(content, dogwhistles) assert detect_dogwhistles(content, dogwhistles)
content = 'A startthingend.'
assert detect_dogwhistles(content, dogwhistles)
content = 'This content is unhamstered and yhamstered.' content = 'This content is unhamstered and yhamstered.'
result = detect_dogwhistles(content, dogwhistles) result = detect_dogwhistles(content, dogwhistles)
assert result assert result