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,36 +1052,53 @@ def detect_dogwhistles(content: str, dogwhistles: {}) -> {}:
} }
else: else:
result[whistle]['count'] += 1 result[whistle]['count'] += 1
else: continue
if whistle.lower().endswith('-x'):
whistle = whistle[:len(whistle)-2]
starting = True
elif (whistle.endswith('*') or
whistle.endswith('~') or
whistle.endswith('-')):
whistle = whistle[:len(whistle)-1]
starting = True
if starting: if whistle.lower().endswith('-x'):
for wrd in words: whistle = whistle[:len(whistle)-2]
if wrd.startswith(whistle): starting = True
if not result.get(whistle): elif (whistle.endswith('*') or
result[whistle] = { whistle.endswith('~') or
"count": 1, whistle.endswith('-')):
"category": category whistle = whistle[:len(whistle)-1]
} starting = True
else:
result[whistle]['count'] += 1 if starting:
else: for wrd in words:
for wrd in words: if wrd.startswith(whistle):
if wrd == whistle: if not result.get(whistle):
if not result.get(whistle): result[whistle] = {
result[whistle] = { "count": 1,
"count": 1, "category": category
"category": category }
} 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:
result[whistle]['count'] += 1
continue
for wrd in words:
if wrd == whistle:
if not result.get(whistle):
result[whistle] = {
"count": 1,
"category": category
}
else:
result[whistle]['count'] += 1
return result return result

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