Click here to Skip to main content
16,018,264 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tried to update the dictionary values but output is not as expected also i wanted to reduce the code complexity of the code. Could someone help me to reduce the cognitive complexity with optimized code.


What I have tried:

Python
def post_process_slot(query_split , predict_labels):
    # print(query_split)
    d = {
    "product" :[],
    "quantity" :[]
    }
    ls = []
    for i,j in zip(query_split,predict_label):
        if j == "0":
            continue
        elif j == "quantity":
            d["quantity"].append(i)
        elif j == "product":
            ls.append(i)
        d["product"].append(ls)

        elif j == "B-product":
            ls.append("-")
            ls.append(i)
        elif j == "I-product":
            ls.append(i)
            d["product"].append(ls)
    ls1  = d["product"][0]
    ls2 = " ".join((ls1))
    ls3 =ls2.split("-")
    ls3.remove("")
    d["product"] = ls3
    return d

    query = "i want to order 7 Mountain Dew Spark bottles and 5 pepsi"
    predict_label = ['O', 'O', 'O', 'O', 'quantity', 'B-product', 'I- 
    product', 'I- 
    product','0','O' , 'quantity','product']
    query_split = query.split(" ")
    result = post_process_slot(query_split , predict_label)
    print(result)


Expected output:
    {'product': ['Mountain Dew Spark','pepsi'], 'quantity': ['7', '5']}

    Actual output:
    {'product': [' Mountain Dew Spark  pepsi'], 'quantity': ['7', '5']}
Posted
Updated 12-May-22 4:36am

1 solution

When you read the '0' character your product entry is:
[['-', 'Mountain', 'Dew', 'Spark'], ['-', 'Mountain', 'Dew', 'Spark']]

which does not seem correct; should there be only a single item in that list? I am not sure why the leading hyphen is in there either. But, at that point you should combine those words into a single string. Then when you get to "pepsi", that should be added as a second entry in the list.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900