Пример #1
0
def message_to_bot(H, clf, learn_response):
    if learn_response == LearnResponse.ORIGIN.name:
        location_dict["origin"] = H
        B = "Can you help me with the destination location?"
        learn_response = LearnResponse.DESTINATION.name
        return B, learn_response
    if learn_response == LearnResponse.DESTINATION.name:
        location_dict["destination"] = H
        origin, destination = location_dict["origin"], location_dict["destination"]          
        googleMapsApiModule.direction(origin, destination)
        B = "I will certainly help you with that."
        learn_response = LearnResponse.MESSAGE.name
        return B, learn_response
    if "bye" in H.lower().split(" "):   # check in words within H
        B = "Bye! I'll miss you!"
        return B, learn_response      # exit loop
    if not H:
        B = "Please say something!"
        return B, learn_response          # empty input
    # grammar parsing
    subj = set()
    obj = set()
    verb = set()
    triples, root = utilities.parse_sentence(H)
    triples = list(triples)
    for t in triples:
        if t[0][1][:2] == 'VB':
            verb.add(t[0][0])
        relation = t[1]
        if relation[-4:] == 'subj':
            subj.add(t[2][0])
        if relation[-3:] == 'obj':
            obj.add(t[2][0])
    logging.debug("\t"+"Subject: "+str(subj)+"\n"+"\t"+"Object: "+str(obj)+"\n"+"\t"+"Topic: "+str(root)+"\n"+"\t"+"Verb: "+str(verb))  # noqa: E501
    subj = list(subj)
    obj = list(obj)
    verb = list(verb)
    proper_nouns = set()
    for t in triples:
        if t[0][1] == 'NNP':
            proper_nouns.add(t[0][0])
        if t[2][1] == 'NNP':
            proper_nouns.add(t[2][0])
    proper_nouns == list(proper_nouns)
    logging.debug("\t"+"Proper Nouns: "+str(proper_nouns))
    # classification
    classification = utilities.classify_sentence(clf, H)
    # logging.debug(classification)
    if learn_response == LearnResponse.MESSAGE.name:
        databaseconnect.add_to_database(classification, subj, root, verb, H)
        if (classification == 'C'):
            B = databaseconnect.get_chat_response()
        elif (classification == 'Q'):
            B, learn_response = databaseconnect.get_question_response(subj, root, verb)
            if learn_response == LearnResponse.TRAIN_ME.name and (len(proper_nouns) == 0 or (len(proper_nouns) == 1 and H.split(" ", 1)[0] != "Where")):
                databaseconnect.add_learnt_statement_to_database(subj, root, verb)
            if learn_response == LearnResponse.TRAIN_ME.name and (len(proper_nouns) >= 2 or (len(proper_nouns) == 1 and H.split(" ", 1)[0] == "Where")):
                learn_response = LearnResponse.MESSAGE.name
                B = "I will certainly help you with that."
        else:
            B = "Oops! I'm not trained for this yet."
    else:
        B, learn_response = databaseconnect.learn_question_response(H)
    if (len(proper_nouns) >= 2 or (len(proper_nouns) >= 1 and H.split(" ", 1)[0] == "Where")) and len(subj) != 0:   # noqa: E501
        if subj[0] == "distance":
            if len(proper_nouns) == 2:
                location_dict["origin"] = proper_nouns.pop()
                location_dict["destination"] = proper_nouns.pop()
                origin, destination = location_dict["origin"], location_dict["destination"]
                googleMapsApiModule.direction(origin, destination)
            else:
                B = "I didn't get that. Can you please give me the origin location?"
                learn_response = LearnResponse.ORIGIN.name
        if len(proper_nouns) == 1:
            location = proper_nouns.pop()
            if subj[0] == "geocoding" or subj[0] == location:
                googleMapsApiModule.geocoding(location)
                learn_response = LearnResponse.MESSAGE.name
                B = "I will certainly help you with that."
    return B, learn_response
 def test_geocoding_with_invalid_input(self):
     with pytest.raises(IndexError):
         result = googleMapsApiModule.geocoding('kahakd...')
         assert result == "https://www.google.com/maps/search/?api=1&query=kahakd..."  # noqa: E501
Пример #3
0
def message_to_bot(H, clf, learn_response):
    if learn_response == 2:
        add_to_maps_database(H, "")
        B = "Can you help me with the destination location?"
        learn_response = 3
        return B, learn_response
    if learn_response == 3:
        add_to_maps_database("", H)
        origin, destination = get_from_maps_database()
        direction(origin, destination)
        B = "I will certainly help you with that."
        learn_response = 0
        return B, learn_response
    if H.lower() == "bye" or H.lower() == "bye." or H.lower(
    ) == "bye!":  #empty input
        B = "Bye! I'll miss you!"
        return B, learn_response  #exit loop
    #grammar parsing
    subj = set()
    obj = set()
    verb = set()
    triples, root = parse_sentence(H)
    triples = list(triples)
    for t in triples:
        if t[0][1][:2] == 'VB':
            verb.add(t[0][0])
        relation = t[1]
        if relation[-4:] == 'subj':
            subj.add(t[2][0])
        if relation[-3:] == 'obj':
            obj.add(t[2][0])
    print("\t" + "Subject: " + str(subj) + "\n" + "\t" + "Object: " +
          str(obj) + "\n" + "\t" + "Topic: " + str(root) + "\n" + "\t" +
          "Verb: " + str(verb))
    subj = list(subj)
    obj = list(obj)
    verb = list(verb)
    proper_nouns = set()
    for t in triples:
        if t[0][1] == 'NNP':
            proper_nouns.add(t[0][0])
        if t[2][1] == 'NNP':
            proper_nouns.add(t[2][0])
    proper_nouns == list(proper_nouns)
    print("\t" + "Proper Nouns: " + str(proper_nouns))
    #classification
    classification = classify_sentence(clf, H)
    #print(classification)
    if learn_response == 0:
        add_to_database(classification, subj, root, verb, H)
        if (classification == 'C'):
            B = get_chat_response()
        elif (classification == 'Q'):
            B, learn_response = get_question_response(subj, root, verb)
            if learn_response == 1 and (len(proper_nouns) == 0 or
                                        (len(proper_nouns) == 1
                                         and H.split(" ", 1)[0] != "Where")):
                add_learnt_statement_to_database(subj, root, verb)
            if learn_response == 1 and (len(proper_nouns) >= 2 or
                                        (len(proper_nouns) == 1
                                         and H.split(" ", 1)[0] == "Where")):
                learn_response = 0
                B = "I will certainly help you with that."
        else:
            B = "Oops! I'm not trained for this yet."
    else:
        B, learn_response = learn_question_response(H)
    if (len(proper_nouns) >= 2 or
        (len(proper_nouns) >= 1
         and H.split(" ", 1)[0] == "Where")) and len(subj) != 0:
        if subj[0] == "distance":
            if len(proper_nouns) == 2:
                add_to_maps_database(proper_nouns.pop(), proper_nouns.pop())
                origin, destination = get_from_maps_database()
                direction(origin, destination)
            else:
                B = "I didn't get that. Can you please give me the origin location?"
                learn_response = 2
        if len(proper_nouns) == 1:
            location = proper_nouns.pop()
            if subj[0] == "geocoding" or subj[0] == location:
                geocoding(location)
    return B, learn_response
 def test_geocoding_with_valid_input(self):
     result = googleMapsApiModule.geocoding('denver')
     assert result == "https://www.google.com/maps/search/?api=1&query=denver"  # noqa: E501