I thought i would post my final approach here incase someone else finds it usefull in the future
i need to add some more tags->genre mappings (like football->sports etc) but you get the idea
Code:
def evaluate(self, formatter, kwargs, mi, locals, val):
# turn the tags into an array and create a lowercase version
tagslist = [l.strip() for l in val.split(',') if l.strip()]
tagslistlcase = [icu_lower(l) for l in tagslist]
# my list of genres i want, and create a lowercase version
genrelist = ['Adult', 'Adventure', 'Anthologies', 'Biography', 'Childrens', 'Classics', 'Drugs', 'Fantasy', 'Food', 'Football', 'Health', 'History', 'Historical', 'Horror', 'Humour', 'Inspirational', 'Modern', 'Music', 'Mystery', 'Non-Fiction', 'Poetry', 'Political', 'Philosophy', 'Psychological', 'Reference', 'Religion', 'Romance', 'Science', 'Science Fiction', 'Self Help', 'Short Stories', 'Sociology', 'Spirituality', 'Suspense', 'Thriller', 'Travel', 'Vampires', 'War', 'Western', 'Writing', 'Young Adult']
genrelistlcase = [icu_lower(l) for l in genrelist]
res = set()
# loop through the genres
for idx,genre in enumerate(genrelistlcase):
# loop through the tags and see if the genre is contained in a tag
for tag in tagslistlcase:
if genre in tag:
# dont add science if it was found in science fiction
if genre != 'science' or (genre == 'science' and 'science fiction' not in tag):
# add to array
res.add(genrelist[idx])
break
# final loop through the tags to look for specific tags i want to map to a genre
for tag in tagslistlcase:
if 'religious' in tag or 'christian' in tag:
res.add('Religion')
if 'children' in tag:
res.add('Childrens')
# join the array into a string and return
return ', '.join(res)