'Print sentence based a dictionary

I would like to print a sentence where 2 words change based on some conditions in a dictionary.

If the noun ends with the letter a, then the article having any "any" key with feminine and singular value is used in the sentence. BASICALLY I want to search for any "feminine" and "singular" in all keys. Once the key that has "female" and "singular" is found, then the key "la" is returned

article = {
  "il" : {
    "gender" : "male",
    "unit" : "singular"
  },
  "la" : {
    "gender" : "female",
    "unit" : "singular"
  },
  "le" : {
    "gender" : "female",
    "unit" : "plural"
  },
}

noun = { "torta" : { "genere" : "female", "unità" : "singular" } }

if noun["noun"].endswith('a'):
  article is ['gender'] == ['female'] and ['unit'] == ['singular'] 

text = (f"{''.join(article)} {noun} {è buona}")

print(text)


Solution 1:[1]

If you restructure article dictionary like:

article = { 'female': { 'singular': 'la', 'plural': 'le' },
            'male': { 'singular': 'il' }}

You can use it like this:

word = "torta"
if word.endswith('a'):
   article_ = article['female']['singular']
print(f"{article_} {word} e buena") 

If you want to use the noun dictionary you can use:

article_ = article[nount[word]['gender']][noun[word]['unita']]

To use article_words as is:

article = next(filter(lambda x: article_words[x]['gender'] == 'female' and
                      article_words[x]['unit'] == 'singular', article_words.keys()))

will give you the article 'la'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1