Gender detaction for SSML

main
Bob Mottram 2021-03-03 13:02:47 +00:00
parent 7c20406d3f
commit d96cdd6c85
3 changed files with 45 additions and 8 deletions

View File

@ -14,6 +14,7 @@ import html
import urllib.parse import urllib.parse
from linked_data_sig import verifyJsonSignature from linked_data_sig import verifyJsonSignature
from utils import getDisplayName from utils import getDisplayName
from utils import getGenderFromBio
from utils import removeHtml from utils import removeHtml
from utils import getConfigParam from utils import getConfigParam
from utils import hasUsersPath from utils import hasUsersPath
@ -2194,6 +2195,7 @@ def _updateSpeaker(baseDir: str, nickname: str, domain: str,
speakerName = \ speakerName = \
getDisplayName(baseDir, postJsonObject['actor'], personCache) getDisplayName(baseDir, postJsonObject['actor'], personCache)
gender = getGenderFromBio(baseDir, postJsonObject['actor'], personCache)
if not speakerName: if not speakerName:
return return
if announcingActor: if announcingActor:
@ -2204,7 +2206,7 @@ def _updateSpeaker(baseDir: str, nickname: str, domain: str,
translate['announces'] + ' ' + announcedHandle + '. ' + content translate['announces'] + ' ' + announcedHandle + '. ' + content
speakerJson = speakerEndpointJson(speakerName, summary, speakerJson = speakerEndpointJson(speakerName, summary,
content, imageDescription, content, imageDescription,
detectedLinks) detectedLinks, gender)
saveJson(speakerJson, speakerFilename) saveJson(speakerJson, speakerFilename)

View File

@ -194,16 +194,19 @@ def getSpeakerFromServer(baseDir: str, session,
def speakerEndpointJson(displayName: str, summary: str, def speakerEndpointJson(displayName: str, summary: str,
content: str, imageDescription: str, content: str, imageDescription: str,
links: []) -> {}: links: [], gender: str) -> {}:
"""Returns a json endpoint for the TTS speaker """Returns a json endpoint for the TTS speaker
""" """
return { speakerJson = {
"name": displayName, "name": displayName,
"summary": summary, "summary": summary,
"say": content, "say": content,
"imageDescription": imageDescription, "imageDescription": imageDescription,
"detectedLinks": links "detectedLinks": links
} }
if gender:
speakerJson['gender'] = gender
return speakerJson
def _speakerEndpointSSML(displayName: str, summary: str, def _speakerEndpointSSML(displayName: str, summary: str,
@ -223,13 +226,10 @@ def _speakerEndpointSSML(displayName: str, summary: str,
else: else:
if langShort == 'en': if langShort == 'en':
gender = gender.lower() gender = gender.lower()
if 'him' in gender or 'male' in gender: if 'he/him' in gender:
gender = 'male' gender = 'male'
elif 'her' in gender or 'she' in gender or \ elif 'she/her' in gender:
'fem' in gender or 'woman' in gender:
gender = 'female' gender = 'female'
elif 'man' in gender:
gender = 'male'
else: else:
gender = 'neutral' gender = 'neutral'

View File

@ -669,6 +669,41 @@ def getDisplayName(baseDir: str, actor: str, personCache: {}) -> str:
return nameFound return nameFound
def getGenderFromBio(baseDir: str, actor: str, personCache: {}) -> str:
"""Tries to ascertain gender from bio description
"""
if '/statuses/' in actor:
actor = actor.split('/statuses/')[0]
if not personCache.get(actor):
return None
bioFound = None
if personCache[actor].get('actor'):
if personCache[actor]['actor'].get('summary'):
bioFound = personCache[actor]['actor']['summary']
else:
# Try to obtain from the cached actors
cachedActorFilename = \
baseDir + '/cache/actors/' + (actor.replace('/', '#')) + '.json'
if os.path.isfile(cachedActorFilename):
actorJson = loadJson(cachedActorFilename, 1)
if actorJson:
if actorJson.get('summary'):
bioFound = actorJson['summary']
if not bioFound:
return None
gender = 'They/Them'
bioFoundOrig = bioFound
bioFound = bioFound.lower()
if 'him' in bioFound or 'male' in bioFound:
gender = 'He/Him'
elif 'her' in bioFound or 'she' in bioFound or \
'fem' in bioFound or 'woman' in bioFound:
gender = 'She/Her'
elif 'man' in bioFound or 'He' in bioFoundOrig:
gender = 'He/Him'
return gender
def getNicknameFromActor(actor: str) -> str: def getNicknameFromActor(actor: str) -> str:
"""Returns the nickname from an actor url """Returns the nickname from an actor url
""" """