简体   繁体   中英

Find gender(key of dictionary) from a dictionary of list in python

I need help, I have a string of a sentence. The sentence may have a word that may reflect the gender. lets suppose The sentence is passed to a function as a string. If any word from dictionary of lists matches with any of the word in sentense, return the key(Men or Women) of that list.

If there is nothing in the sentence that may give a hint of gender then return 'Unisex'

def gender_find(gender_data):
    men_list = ["men", "men's", "him", "his", "male"]
    women_list = ["women", "women's", "her", "female"]
    gender = {'Men': men_list, 'Women': women_list}
    #I have a logic in mind to loop the list using one key 
    #and check if any item of list is in the gender_data. 
    #If any better logic exists then please help.
    # Do something here that does not include indexing.
    #And don't use any,all functions
    #It should be something like list comprehension i.e. One line code

Here is my approach:

def gender_find(gender_data):
    gender = {
        "Women": ["women", "women's", "her", "female"],
        "Men": ["men", "men's", "him", "his", "male"],
    }

    gender_data = gender_data.lower()
    for gender_name, gender_list in gender.items():
        if any(word in gender_data for word in gender_list):
            return gender_name
    return "Gender Neutral"

Testing it

>>> gender_find("It's time for all men to take up responsibility!")
'Men'

>>> gender_find("WOMEN in WORKPLACE")
'Women'

>>> gender_find("Men and Women together")
'Women'

>>> gender_find("foo bar")
'Gender Neutral'

A couple of notes:

  • Since the word "women" contains the word "men", checking for men first will give false result
  • I convert the sentence to lower case for case insensitive comparison
  • My function will return "Women" if both men and women are found
  • The any() function will return True upon the first true test. It does not go through the entire list, which saves time and improves performance.

If you need to be mapping from the words in the lists to the gender, then your dictionary is set up backwards. You want the categories to be the values, and the words to be the keys.

I'd do:

gender = {}
for word in men_list:
    gender[word] = "Men"
for word in women_list:
    gender[word] = "Women"

Then you can examine the words of your input text one by one and look them up in the dictionary to see what (if anything) they tell you about gender.

seen_men = False
seen_women = False
for item in gender_data:
    if item in gender:
        if gender[item] == "Men":
            seen_men = True
        elif gender[item] == "Women":
            seen_women = True

if seen_men and not seen_woman:
    return "Men"
elif seen_women and not seen_men:
    return "Women"
else: # seen either both or neither
    return "Unisex"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM