Gender detaction for SSML

merge-requests/21/head
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
from linked_data_sig import verifyJsonSignature
from utils import getDisplayName
from utils import getGenderFromBio
from utils import removeHtml
from utils import getConfigParam
from utils import hasUsersPath
@ -2194,6 +2195,7 @@ def _updateSpeaker(baseDir: str, nickname: str, domain: str,
speakerName = \
getDisplayName(baseDir, postJsonObject['actor'], personCache)
gender = getGenderFromBio(baseDir, postJsonObject['actor'], personCache)
if not speakerName:
return
if announcingActor:
@ -2204,7 +2206,7 @@ def _updateSpeaker(baseDir: str, nickname: str, domain: str,
translate['announces'] + ' ' + announcedHandle + '. ' + content
speakerJson = speakerEndpointJson(speakerName, summary,
content, imageDescription,
detectedLinks)
detectedLinks, gender)
saveJson(speakerJson, speakerFilename)

View File

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

View File

@ -669,6 +669,41 @@ def getDisplayName(baseDir: str, actor: str, personCache: {}) -> str:
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:
"""Returns the nickname from an actor url
"""