Ejemplo n.º 1
0
def answer(cleaned_input, product=None, general=True):
    if not general:
        # print(cleaned_input)
        attrs = product.get_attr()["attrs"]
        p_type = product.get_attr()["type"]
        attrs["cost price"] = product.retail_price
        attrs["much"] = product.retail_price
        try:
            attrs["big"] = attrs["display_size"]
        except KeyError:
            pass
        if p_type == "powerbank":
            mapping["charge"] = "charging"
            mapping["battery"] = "battery life"
        elif p_type == "TV":
            mapping["type"] = ""
        cosine_sims = []
        for key in attrs.keys():
            separated = key.replace("_", " ")
            rephrased_sep = rephrase(separated, mapping)
            cosine_sim = cosine_comparision(rephrased_sep, cleaned_input)
            # print(f"key:{rephrased_sep}, question:{cleaned_input}, score:{cosine_sim}")
            cosine_sims.append(cosine_sim)
        if cleaned_input == "long last":
            reply = gen_reply(None, product, cleaned_input)
            return reply
        if "big" in cleaned_input:
            reply = gen_reply(attrs["big"], product, "display size")
            return reply
        h_p = get_h_possibility(cosine_sims)
        h_possibility = h_p["highest"]
        if h_possibility == 0:
            l_dis_list = []
            for key in attrs.keys():
                separated = key.replace("_", " ")
                rephrased_sep = rephrase(separated, mapping)
                l_dis = Levenshtein.distance(rephrased_sep, cleaned_input)
                # print(f"key:{rephrased_sep}, question:{cleaned_input}, score:{l_dis}")
                l_dis_list.append(l_dis)
            h_possibility = min(l_dis_list)
            if h_possibility > 4:
                return "Sorry, I do not understand your question."
            for key in attrs.keys():
                separated = key.replace("_", " ")
                rephrased_sep = rephrase(separated, mapping)
                l_dis = Levenshtein.distance(rephrased_sep, cleaned_input)
                if l_dis == h_possibility:
                    ans = attrs.get(key)
                    reply = gen_reply(ans, product, rephrased_sep)
                    return reply
        for key in attrs.keys():
            separated = key.replace("_", " ")
            rephrased_sep = rephrase(separated, mapping)
            cosine_sim = cosine_comparision(rephrased_sep, cleaned_input)
            if cosine_sim == h_possibility:
                ans = attrs.get(key)
                reply = gen_reply(ans, product, rephrased_sep)
                return reply
Ejemplo n.º 2
0
def get_stored_attrs():
    product_list = dat_loader.load_data("Products")["data"]
    attributes = []
    products = []
    product_title = []
    for product in product_list:
        attrs = product.get_attr()["attrs"]
        p_type = product.get_attr()["type"]
        title_tok = product.get_title().split()
        title_tok.remove("Eclectic")
        title = " ".join(title_tok)
        product_title.append(title)
        if p_type == "powerbank":
            mapping["charge"] = "charging"
            mapping["battery"] = "battery life"
        elif p_type == "TV":
            mapping["type"] = ""
        for key in attrs.keys():
            separated = key.replace("_", " ")
            rephrased_sep = rephrase(separated, mapping)
            attributes.append(rephrased_sep)
        p_split = p_type.split("-")
        for x in p_split:
            products.append(x)
    attributes.append("price")
    return {
        "attributes": attributes,
        "products": products,
        "title": product_title
    }
Ejemplo n.º 3
0
async def main(websocket, path):
    await websocket.send("Hello, My name is Zoro and I am here to help! :)")
    print("> Hello, My name is Zoro and I am here to help! :)")
    context = None
    while True:
        try:
            user_input = await websocket.recv()
            print(f"< {user_input}")
        except ConnectionClosedError:
            print("< Client close connection")
            break
        except ConnectionClosedOK:
            print("< Client close connection")
            break
        user_input_tok = nltk.word_tokenize(user_input)
        skip_logic = False
        exit_loop = False
        for word in user_input_tok:
            if word.lower() in ("thank", "you", "thanks"):
                await websocket.send(
                    "You are welcome, If you need further assistance I will be here to help!"
                )
                print(
                    "> You are welcome, If you need further assistance I will be here to help!"
                )
                skip_logic = True
                exit_loop = True
                break
            elif word.lower() in ("hello", "hi"):
                await websocket.send("Hi, good day to you!")
                print("> Hi, good day to you!")
                await websocket.send("How may I help you today?")
                print("> How may I help you today?")
                skip_logic = True
                break
        if skip_logic and exit_loop:
            break
        elif skip_logic:
            continue
        parsed_input = rephrase(user_input, mappings)
        if is_question(parsed_input):
            product = identify_product(parsed_input)
            if isinstance(product, str):
                line_split = product.split(".")
                line1 = line_split[0] + "."
                line2 = line_split[1] + "."
                await websocket.send(line1)
                print(f"> {line1}")
                await websocket.send(line2)
                print(f"> {line2}")
                context = None
            elif product is not None:
                context = product
                cleaned = clean_sentence(parsed_input)
                ans = answer(cleaned, product, general=False)
                await websocket.send(ans)
                print(f"> {ans}")
            elif product is None and context is not None:
                for word, tag in nltk.pos_tag(
                        nltk.word_tokenize(parsed_input)):
                    if tag == "PRP":
                        cleaned = clean_sentence(parsed_input)
                        ans = answer(cleaned, context, general=False)
                        await websocket.send(ans)
                        print(f"> {ans}")
            # print(cleaned)
            elif product is None and context is None:
                if "products" in nltk.word_tokenize(parsed_input):
                    products_type = get_stored_attrs()["products"]
                    seen = {}
                    dupes = []
                    for x in products_type:
                        if x not in seen:
                            seen[x] = 1
                        else:
                            if seen[x] == 1:
                                dupes.append(x)
                            seen[x] += 1
                    for x in dupes:
                        products_type.remove(x)
                    for i, x in enumerate(products_type):
                        products_type[i] = pluralize(x)
                    products_str = ", ".join(products_type)
                    products = get_stored_attrs()["title"]
                    random_product = []
                    for x in range(0, 3):
                        random_product.append(random.choice(products))
                    products_t_str = ", ".join(random_product)
                    await websocket.send(f"We sell {products_str}")
                    print(f"> We sell {products_str}")
                    await websocket.send(
                        f"Some of the products we sell are {products_t_str}.")
                    print(
                        f"> Some of the products we sell are {products_t_str}."
                    )
                else:
                    await websocket.send(
                        "Sorry I do not understand your question")
                    print("> Sorry I do not understand your question")
                    await websocket.send(
                        "You may contact our support team by opening a new request for assistance"
                    )
                    print(
                        "> You may contact our support team by opening a new request for assistance"
                    )
            else:
                await websocket.send(
                    "Sorry, I can only answer questions regarding the products we sell."
                )
                print(
                    "> Sorry, I can only answer questions regarding the products we sell."
                )
                await websocket.send(
                    "You may contact our support team by opening a new request for assistance"
                )
                print(
                    "> You may contact our support team by opening a new request for assistance"
                )
        else:
            await websocket.send(
                "Sorry, I can only answer questions regarding the products we sell."
            )
            print(
                "> Sorry, I can only answer questions regarding the products we sell."
            )
            await websocket.send(
                "You may contact our support team by opening a new request for assistance"
            )
            print(
                "> You may contact our support team by opening a new request for assistance"
            )