epicyon/semantic.py

75 lines
2.5 KiB
Python
Raw Normal View History

2020-06-12 11:50:49 +00:00
__filename__ = "semantic.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.1.0"
__maintainer__ = "Bob Mottram"
__email__ = "bob@freedombone.net"
__status__ = "Production"
def isAccusatory(content: str, translate: {}, threshold=3) -> bool:
"""Indicates whether the given content is an accusatory post
"""
2020-06-14 09:21:37 +00:00
words = ('your', "you're", 'if you', 'you are', 'you')
2020-06-12 11:50:49 +00:00
if translate:
wordsTranslated = []
for wrd in words:
translated = translate[wrd]
if '|' not in translated:
if translated not in wordsTranslated:
wordsTranslated.append(translated)
else:
# handle differing genders
words2 = translated.split('|')
for wrd2 in words2:
if wrd2.strip() not in wordsTranslated:
wordsTranslated.append(translated)
else:
wordsTranslated = words
contentLower = content.lower()
ctr = 0
for wrd in wordsTranslated:
2020-06-14 09:21:37 +00:00
wordCount = contentLower.count(wrd + ' ')
if wordCount > 0:
ctr += wordCount
contentLower = contentLower.replace(wrd + ' ', '')
2020-06-12 11:50:49 +00:00
if ctr >= threshold:
return True
return False
def labelAccusatoryPost(postJsonObject: {}, translate: {}, threshold=3):
"""If a post is accusatory and it doesn't mention anyone
specific and isn't a reply and it doesn't have a content
warning then add a default 'accusatory' content warning
"""
if not postJsonObject.get('object'):
return
if not isinstance(postJsonObject['object'], dict):
return
if not postJsonObject['object'].get('content'):
return
2020-06-12 13:19:12 +00:00
if not postJsonObject['object'].get('type'):
return
if postJsonObject['object']['type'] == 'Article':
return
2020-06-12 11:50:49 +00:00
if postJsonObject['object'].get('inReplyTo'):
return
if not isinstance(postJsonObject['object']['content'], str):
return
if '@' in postJsonObject['object']['content']:
return
if not isAccusatory(postJsonObject['object']['content'],
translate, threshold):
return
cwStr = translate['Accusatory']
if postJsonObject['object'].get('summary'):
if cwStr not in postJsonObject['object']['summary']:
postJsonObject['object']['summary'] = \
cwStr + ', ' + postJsonObject['object']['summary']
else:
postJsonObject['object']['summary'] = cwStr
postJsonObject['object']['sensitive'] = True