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:
result[whistle]['count'] += 1
else:
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
continue
if starting:
for wrd in words:
if wrd.startswith(whistle):
if not result.get(whistle):
result[whistle] = {
"count": 1,
"category": category
}
else:
result[whistle]['count'] += 1
else:
for wrd in words:
if wrd == whistle:
if not result.get(whistle):
result[whistle] = {
"count": 1,
"category": category
}
else:
result[whistle]['count'] += 1
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:
for wrd in words:
if wrd.startswith(whistle):
if not result.get(whistle):
result[whistle] = {
"count": 1,
"category": category
}
else:
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

View File

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