Home > OS >  Assign value from the second dictionary whose key is the value of the 1st dictionary
Assign value from the second dictionary whose key is the value of the 1st dictionary

Time:06-09

I have two dictionaries:

# ModName : Details for the Modules Present and Intent information
{'ClickUrl': ['HTTP'],
 'AngryUser': ['HATE'],
 'HelloGoodBye': ['HELLO'],
 'Social': ['TEAM_WELLNESS', 'TEAM_MEMBERS_ORIGINAL', 'TEAM_MEMBERS', 'TEAM_MEMBERS_QUERY'],
 'AgentMemory': ['WHAT_IS']}

And the other one:

# Details for the Description Mapped with each Module
{'HTTP': 'http*',
 'HATE': 'Hate',
 'HELLO': 'Hello',
 'TEAM_WELLNESS': 'How is the Alchemy team doing?',
 'TEAM_MEMBERS_ORIGINAL': "I'd like to learn more about the original proof-of-concept team",
 'TEAM_MEMBERS': "I'd like to learn more about the team",
 'TEAM_MEMBERS_QUERY': "I want to know if someone's on the team",
 'WHAT_IS': 'what is'}

In these two, the value from the 1st dictionary is the same as the key for the second dictionary.

Now, the requirement is to set the value (sentences) from the second dictionary to the node tree element created for the trsx file but somehow the logic isn't being set to be correct as the output for the trsx file should be like:

<sample intentref="MANAGE_URLS_HTTP" count="1" excluded="true">http*</sample>
    <sample intentref="NEGATIVE_HATE" count="1" excluded="true">Hate</sample>
    <sample intentref="GREETING_HELLO" count="1" excluded="true">Hello</sample>
    <sample intentref="SOCIAL_TEAM_WELLNESS" count="1" excluded="true">How is the Alchemy team doing?</sample>
    <sample intentref="SOCIAL_TEAM_MEMBERS_ORIGINAL" count="1" excluded="true">I'd like to learn more about the original proof-of-concept team</sample>
    <sample intentref="SOCIAL_TEAM_MEMBERS" count="1" excluded="true">I'd like to learn more about the team</sample>
    <sample intentref="SOCIAL_TEAM_MEMBERS_QUERY" count="1" excluded="true">I want to know if someone's on the team</sample>
    <sample intentref="AGENT_MEMORY_WHAT_IS" count="1" excluded="true">what is</sample>

Where <sample> is the node element being created.

Actual output:

 <sample intentref="MANAGE_URLS_HTTP" count="1" excluded="true">http*HateHelloHow is the Alchemy team doing?I'd like to learn more about the original proof-of-concept teamI'd like to learn more about the teamI want to know if someone's on the teamwhat is</sample>
    <sample intentref="GREETING_HATE" count="1" excluded="true">http*HateHelloHow is the Alchemy team doing?I'd like to learn more about the original proof-of-concept teamI'd like to learn more about the teamI want to know if someone's on the teamwhat is</sample>
    <sample intentref="NEGATIVE_HELLO" count="1" excluded="true">http*HateHelloHow is the Alchemy team doing?I'd like to learn more about the original proof-of-concept teamI'd like to learn more about the teamI want to know if someone's on the teamwhat is</sample>
    <sample intentref="SOCIAL_TEAM_WELLNESS" count="1" excluded="true">http*HateHelloHow is the Alchemy team doing?I'd like to learn more about the original proof-of-concept teamI'd like to learn more about the teamI want to know if someone's on the teamwhat is</sample>
    <sample intentref="SOCIAL_TEAM_MEMBERS_ORIGINAL" count="1" excluded="true">http*HateHelloHow is the Alchemy team doing?I'd like to learn more about the original proof-of-concept teamI'd like to learn more about the teamI want to know if someone's on the teamwhat is</sample>
    <sample intentref="SOCIAL_TEAM_MEMBERS" count="1" excluded="true">http*HateHelloHow is the Alchemy team doing?I'd like to learn more about the original proof-of-concept teamI'd like to learn more about the teamI want to know if someone's on the teamwhat is</sample>
    <sample intentref="SOCIAL_TEAM_MEMBERS_QUERY" count="1" excluded="true">http*HateHelloHow is the Alchemy team doing?I'd like to learn more about the original proof-of-concept teamI'd like to learn more about the teamI want to know if someone's on the teamwhat is</sample>
    <sample intentref="AGENT_MEMORY_WHAT_IS" count="1" excluded="true">http*HateHelloHow is the Alchemy team doing?I'd like to learn more about the original proof-of-concept teamI'd like to learn more about the teamI want to know if someone's on the teamwhat is</sample>

Python code:

val1 = list(Description_list.values())  # extracting the values
       
for key in ModName_Social.keys():
    val = ModName_Social[key]
    for i in val:
        i_x_0 = SubElement(samples, NODE_CONSTANTS['sample_node'])  # creating the sub-element
        if key == 'ClickUrl':
            
            i_x_0.text = val1[0]
                                                              
        if key == 'AngryUser':
           
            i_x_0.text = val1[1]  # have to hard code, which isn't right

        if key == 'HelloGoodBye':
           i_x_0.text = val1[1]
                                                            
        if key == 'Social':
            i_x_0.text = val1[2]
                                                            
        if key == 'AgentMemory':
            i_x_0.text = val1[3]

In this, as the 2nd dictionary suggests, the Key : Social has more than one entry so can't hard-code it to be as val1[2] always.

What can be done to achieve the actual output?

Even though when i Modified the code like :

 for k in combined_dict:
            for v in combined_dict[k]: 
              
              if k =='Social':
                i_x_0.text = v
              elif k =='AgentMemory':
                i_x_0.text = v
              elif k =='HelloGoodBye':
                i_x_0.text = v
              elif k =='AngryUser':
                i_x_0.text = v
              elif k =='ClickUrl':
                i_x_0.text = v

The output stays:

 <sample intentref="MANAGE_URLS_HTTP" count="1" excluded="true"/>
    <sample intentref="GREETING_HATE" count="1" excluded="true"/>
    <sample intentref="NEGATIVE_HELLO" count="1" excluded="true"/>
    <sample intentref="SOCIAL_TEAM_WELLNESS" count="1" excluded="true"/>
    <sample intentref="SOCIAL_TEAM_MEMBERS_ORIGINAL" count="1" excluded="true"/>
    <sample intentref="SOCIAL_TEAM_MEMBERS" count="1" excluded="true"/>
    <sample intentref="SOCIAL_TEAM_MEMBERS_QUERY" count="1" excluded="true"/>
    <sample intentref="AGENT_MEMORY_WHAT_IS" count="1">what is</sample>

CodePudding user response:

The below minor thing fixed the issue of setting the value of the second dictionary as the text in the elementTree

for key in ModName_Social.keys():
    val = ModName_Social[key]
    for i in val:
        i_x_0 = SubElement(samples, NODE_CONSTANTS['sample_node'])  # creating the sub-element
        if key == 'ClickUrl':
            i_x_0.text = Description_list[i]                                             
        if key == 'AngryUser':
            i_x_0.text = Description_list[i]  
        if key == 'HelloGoodBye':
           i_x_0.text = Description_list[i]                                                   
        if key == 'Social':
            i_x_0.text = Description_list[i]                                                    
        if key == 'AgentMemory':
            i_x_0.text = Description_list[i]

Output :

 <sample intentref="GREETING_HATE" count="1" excluded="true">Hate</sample>
    <sample intentref="NEGATIVE_HELLO" count="1" excluded="true">Hello</sample>
    <sample intentref="SOCIAL_TEAM_WELLNESS" count="1" excluded="true">How is the Alchemy team doing?</sample>
    <sample intentref="SOCIAL_TEAM_MEMBERS_ORIGINAL" count="1" excluded="true">I'd like to learn more about the original proof-of-concept team</sample>
    <sample intentref="SOCIAL_TEAM_MEMBERS" count="1" excluded="true">I'd like to learn more about the team</sample>
    <sample intentref="SOCIAL_TEAM_MEMBERS_QUERY" count="1" excluded="true">I want to know if someone's on the team</sample>
    <sample intentref="AGENT_MEMORY_WHAT_IS" count="1">what is</sample>

CodePudding user response:

You could combine the two dictionaries in the way you want using a dictionary comprehension -

combined_dict = {k:[Description_list[_] for _ in ModName_Social[k]] for k in ModName_Social.keys()}

Output

{'ClickUrl': ['http*'],
 'AngryUser': ['Hate'],
 'HelloGoodBye': ['Hello'],
 'Social': ['How is the Alchemy team doing?',
  "I'd like to learn more about the original proof-of-concept team",
  "I'd like to learn more about the team",
  "I want to know if someone's on the team"],
 'AgentMemory': ['what is']}

Your node creation code will then look something like -

for k in combined_dict:
    for v in combined_dict[k]:
        # create_text(v)
        print(k, v, sep='\t', end='\n')
ClickUrl        http*
AngryUser       Hate
HelloGoodBye    Hello
Social  How is the Alchemy team doing?
Social  I'd like to learn more about the original proof-of-concept team
Social  I'd like to learn more about the team
Social  I want to know if someone's on the team
AgentMemory     what is
  • Related