From 5b2986d68505e8059f2a9867a77d6465baffa9b8 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Wed, 3 Mar 2021 13:37:18 +0000 Subject: [PATCH] Gender detection from profile tag --- inbox.py | 3 ++- translations/ar.json | 5 ++++- translations/ca.json | 5 ++++- translations/cy.json | 5 ++++- translations/de.json | 5 ++++- translations/en.json | 5 ++++- translations/es.json | 5 ++++- translations/fr.json | 5 ++++- translations/ga.json | 5 ++++- translations/hi.json | 5 ++++- translations/it.json | 5 ++++- translations/ja.json | 5 ++++- translations/oc.json | 5 ++++- translations/pt.json | 5 ++++- translations/ru.json | 5 ++++- translations/zh.json | 5 ++++- utils.py | 41 +++++++++++++++++++++++++++++++++++++---- 17 files changed, 99 insertions(+), 20 deletions(-) diff --git a/inbox.py b/inbox.py index b7c7b3e1b..80481c66f 100644 --- a/inbox.py +++ b/inbox.py @@ -2195,7 +2195,8 @@ def _updateSpeaker(baseDir: str, nickname: str, domain: str, speakerName = \ getDisplayName(baseDir, postJsonObject['actor'], personCache) - gender = getGenderFromBio(baseDir, postJsonObject['actor'], personCache) + gender = getGenderFromBio(baseDir, postJsonObject['actor'], + personCache, translate) if not speakerName: return if announcingActor: diff --git a/translations/ar.json b/translations/ar.json index 54f4adbac..0027e6346 100644 --- a/translations/ar.json +++ b/translations/ar.json @@ -381,5 +381,8 @@ "mentioning": "ذكر", "sad face": "وجه حزين", "thinking emoji": "التفكير الرموز التعبيرية", - "laughing": "يضحك" + "laughing": "يضحك", + "gender": "جنس تذكير أو تأنيث", + "He/Him": "هو", + "She/Her": "هي" } diff --git a/translations/ca.json b/translations/ca.json index 5112f57ab..5ad1772f0 100644 --- a/translations/ca.json +++ b/translations/ca.json @@ -381,5 +381,8 @@ "mentioning": "esmentant", "sad face": "cara trista", "thinking emoji": "emoji pensant", - "laughing": "rient" + "laughing": "rient", + "gender": "gènere", + "He/Him": "Ell", + "She/Her": "Ella" } diff --git a/translations/cy.json b/translations/cy.json index 8decbe17b..c8c2dfff5 100644 --- a/translations/cy.json +++ b/translations/cy.json @@ -381,5 +381,8 @@ "mentioning": "sôn", "sad face": "wyneb trist", "thinking emoji": "meddwl emoji", - "laughing": "chwerthin" + "laughing": "chwerthin", + "gender": "rhyw", + "He/Him": "Ef", + "She/Her": "Hi/Ei" } diff --git a/translations/de.json b/translations/de.json index 46bb450d8..aef2f428a 100644 --- a/translations/de.json +++ b/translations/de.json @@ -381,5 +381,8 @@ "mentioning": "Erwähnen", "sad face": "trauriges Gesicht", "thinking emoji": "Emowji denken", - "laughing": "Lachen" + "laughing": "Lachen", + "gender": "geschlecht", + "He/Him": "Er/ihm", + "She/Her": "Sie" } diff --git a/translations/en.json b/translations/en.json index 47997a169..eb8743f40 100644 --- a/translations/en.json +++ b/translations/en.json @@ -381,5 +381,8 @@ "mentioning": "mentioning", "sad face": "sad face", "thinking emoji": "thinking emowji", - "laughing": "laughing" + "laughing": "laughing", + "gender": "gender", + "He/Him": "He/Him", + "She/Her": "She/Her" } diff --git a/translations/es.json b/translations/es.json index bb12926f7..10c2729ea 100644 --- a/translations/es.json +++ b/translations/es.json @@ -381,5 +381,8 @@ "mentioning": "mencionar", "sad face": "cara triste", "thinking emoji": "pensando emowji", - "laughing": "risa" + "laughing": "risa", + "gender": "género", + "He/Him": "El", + "She/Her": "Ella" } diff --git a/translations/fr.json b/translations/fr.json index 84cd8b145..f959f284e 100644 --- a/translations/fr.json +++ b/translations/fr.json @@ -381,5 +381,8 @@ "mentioning": "mentionnant", "sad face": "visage triste", "thinking emoji": "penser emowji", - "laughing": "en riant" + "laughing": "en riant", + "gender": "le genre", + "He/Him": "Il/Lui", + "She/Her": "Elle" } diff --git a/translations/ga.json b/translations/ga.json index 123ef8c52..315448826 100644 --- a/translations/ga.json +++ b/translations/ga.json @@ -381,5 +381,8 @@ "mentioning": "ag lua", "sad face": "aghaidh brónach", "thinking emoji": "ag smaoineamh emowji", - "laughing": "ag gáire" + "laughing": "ag gáire", + "gender": "inscne", + "He/Him": "Sé/Eisean", + "She/Her": "Sí" } diff --git a/translations/hi.json b/translations/hi.json index ecb575420..f7ed05f0d 100644 --- a/translations/hi.json +++ b/translations/hi.json @@ -381,5 +381,8 @@ "mentioning": "उल्लेख", "sad face": "उदास चेहरा", "thinking emoji": "सोच रहे हैं इमोजी", - "laughing": "हस रहा" + "laughing": "हस रहा", + "gender": "लिंग", + "He/Him": "वह/उसे", + "She/Her": "वह/उसकी" } diff --git a/translations/it.json b/translations/it.json index e5a999fe6..52bb3e58f 100644 --- a/translations/it.json +++ b/translations/it.json @@ -381,5 +381,8 @@ "mentioning": "menzionando", "sad face": "faccia triste", "thinking emoji": "pensiero emoji", - "laughing": "ridendo" + "laughing": "ridendo", + "gender": "genere", + "He/Him": "Lui", + "She/Her": "Lei" } diff --git a/translations/ja.json b/translations/ja.json index efdc45a58..967673668 100644 --- a/translations/ja.json +++ b/translations/ja.json @@ -381,5 +381,8 @@ "mentioning": "言及する", "sad face": "悲しい顔", "thinking emoji": "絵文字を考える", - "laughing": "笑い" + "laughing": "笑い", + "gender": "性別", + "He/Him": "彼", + "She/Her": "彼女" } diff --git a/translations/oc.json b/translations/oc.json index 522ac2cdf..3d008db27 100644 --- a/translations/oc.json +++ b/translations/oc.json @@ -377,5 +377,8 @@ "mentioning": "mentioning", "sad face": "sad face", "thinking emoji": "thinking emowji", - "laughing": "laughing" + "laughing": "laughing", + "gender": "gender", + "He/Him": "He/Him", + "She/Her": "She/Her" } diff --git a/translations/pt.json b/translations/pt.json index 42414a9fc..e62387eec 100644 --- a/translations/pt.json +++ b/translations/pt.json @@ -381,5 +381,8 @@ "mentioning": "mencionando", "sad face": "rosto triste", "thinking emoji": "pensando emowji", - "laughing": "rindo" + "laughing": "rindo", + "gender": "gênero", + "He/Him": "Ele", + "She/Her": "Ela" } diff --git a/translations/ru.json b/translations/ru.json index a9e14625f..806da91da 100644 --- a/translations/ru.json +++ b/translations/ru.json @@ -381,5 +381,8 @@ "mentioning": "упоминание", "sad face": "грустное лицо", "thinking emoji": "думающий смайлик", - "laughing": "смеющийся" + "laughing": "смеющийся", + "gender": "Пол", + "He/Him": "Он/Его", + "She/Her": "Она/Ее" } diff --git a/translations/zh.json b/translations/zh.json index d7af314c0..5f149d144 100644 --- a/translations/zh.json +++ b/translations/zh.json @@ -381,5 +381,8 @@ "mentioning": "提及", "sad face": "悲伤的脸", "thinking emoji": "思维表情符号", - "laughing": "笑" + "laughing": "笑", + "gender": "", + "He/Him": "", + "She/Her": "" } diff --git a/utils.py b/utils.py index 9d467467d..9ccef742a 100644 --- a/utils.py +++ b/utils.py @@ -669,7 +669,8 @@ def getDisplayName(baseDir: str, actor: str, personCache: {}) -> str: return nameFound -def getGenderFromBio(baseDir: str, actor: str, personCache: {}) -> str: +def getGenderFromBio(baseDir: str, actor: str, personCache: {}, + translate: {}) -> str: """Tries to ascertain gender from bio description """ if '/statuses/' in actor: @@ -678,7 +679,21 @@ def getGenderFromBio(baseDir: str, actor: str, personCache: {}) -> str: return None bioFound = None if personCache[actor].get('actor'): - if personCache[actor]['actor'].get('summary'): + # is gender defined as a profile tag? + if personCache[actor]['actor'].get('attachment'): + tagsList = personCache[actor]['actor']['attachment'] + if isinstance(tagsList, list): + for tag in tagsList: + if not isinstance(tag, dict): + continue + if not tag.get('name') or not tag.get('value'): + continue + if tag['name'].lower() == \ + translate['gender'].lower(): + bioFound = tag['value'] + break + # if not then use the bio + if not bioFound and personCache[actor]['actor'].get('summary'): bioFound = personCache[actor]['actor']['summary'] else: # Try to obtain from the cached actors @@ -687,14 +702,32 @@ def getGenderFromBio(baseDir: str, actor: str, personCache: {}) -> str: if os.path.isfile(cachedActorFilename): actorJson = loadJson(cachedActorFilename, 1) if actorJson: - if actorJson.get('summary'): + # is gender defined as a profile tag? + if actorJson.get('attachment'): + tagsList = actorJson['attachment'] + if isinstance(tagsList, list): + for tag in tagsList: + if not isinstance(tag, dict): + continue + if not tag.get('name') or not tag.get('value'): + continue + if tag['name'].lower() == \ + translate['gender'].lower(): + bioFound = tag['value'] + break + # if not then use the bio + if not bioFound and 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: + if translate['He/Him'] in bioFound: + gender = 'He/Him' + elif translate['She/Her'] in bioFound: + gender = 'She/Her' + elif '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: