Beispiel #1
0
def test_classifier_server_train_post():
    data = {'hotel': ['book a hotel', 'need a nice place to stay', 'any motels near by'],
    'weather': ['what is the weather like', 'is it hot outside'],
    'place': ['which place is this', 'where are we', 'where are you going', 'which place is it'],
    'name': ['What is your name', 'Who are you', 'What do i call you', 'what are you called']
    }
    server = NLUServer().serve(test=True)
    clf = Classifier()
    y = tuple(data.keys())
    x = tuple(data.values())
    server.requests.post("/models/classifier/train", json={"data": {"inputs": x, "targets": y}})
    clf.fit(x, y)
    r = server.requests.get("/models/classifier/predict?input=will it rain today")
    assert r.text == 'weather'
Beispiel #2
0
def test_classifier_server_train_get():
    data = {'hotel': ['book a hotel', 'need a nice place to stay', 'any motels near by'],
    'weather': ['what is the weather like', 'is it hot outside'],
    'place': ['which place is this', 'where are we', 'where are you going', 'which place is it'],
    'name': ['What is your name', 'Who are you', 'What do i call you', 'what are you called']
    }
    server = NLUServer().serve(test=True)
    clf = Classifier()
    for y, v in data.items():
       for x in v:
           server.requests.get("/models/classifier/train?input={}&target={}".format(x, y))
           clf.fit(x, y)
    r = server.requests.get("/models/classifier/predict?input=will it rain today")
    assert r.text == 'weather'
Beispiel #3
0
def test_classifier_serialization():
    x_hotel = ['book a hotel', 'need a nice place to stay', 'any motels near by']
    x_weather = ['what is the weather like', 'is it hot outside']

    clf1 = Classifier()
    clf1.fit(x_hotel, 'hotel')
    clf1.fit(x_weather, 'weather')

    config = clf1.serialize()
    clf2 = Classifier.deserialize(config)

    assert clf1.classes == clf2.classes

    test_inputs = ['will it rain today', 'find a place to stay']

    for test_input in test_inputs:
        clf1_out = clf1.predict(test_input, return_probs=True)
        clf2_out = clf2.predict(test_input, return_probs=True)
        assert clf1_out == clf2_out
Beispiel #4
0
    def adapt(self, q, inttype=False, priority=False):
        clf = Classifier()
        clauses = self.clauses
        int_clauses = self.int_clauses
        clauses.update(int_clauses)
        for i, tup in enumerate(clauses.items()):
            clause = tup[0]
            clf.fit(clause, clauses[clause])
            if inttype and priority:
                clf.fit(("how many", "number of", "who all", "how much",
                         "sum of", "total"), "sum of {} in {}")

        return clf.predict(q)
Beispiel #5
0
    def test_classifier_serialization(self):
        x_hotel = ['book a hotel', 'need a nice place to stay', 'any motels near by']
        x_weather = ['what is the weather like', 'is it hot outside']

        clf1 = Classifier()
        clf1.fit(x_hotel, 'hotel')
        clf1.fit(x_weather, 'weather')

        config = clf1.serialize()
        clf2 = Classifier.deserialize(config)

        assert clf1.classes == clf2.classes

        test_inputs = ['will it rain today', 'find a place to stay']

        for test_input in test_inputs:
            clf1_out = clf1.predict(test_input, return_probs=True)
            clf2_out = clf2.predict(test_input, return_probs=True)
            assert clf1_out == clf2_out
Beispiel #6
0
def test_classifier_basic():
    x_hotel = [
        'book a hotel', 'need a nice place to stay', 'any motels near by'
    ]
    x_weather = ['what is the weather like', 'is it hot outside']
    x_place = [
        'which place is this', 'where are we', 'where are you going',
        'which place is it'
    ]
    x_name = [
        'What is your name', 'Who are you', 'What do i call you',
        'what are you called'
    ]

    clf1 = Classifier()
    clf2 = Classifier()
    clfs = [clf1, clf2]
    for cl in clfs:
        cl.fit(x_hotel, 'hotel')
        cl.fit(x_weather, 'weather')
        cl.fit(x_place, 'place')
        cl.fit(x_name, 'name')

    assert clf1.predict('will it rain today') == 'weather'
    assert clf1.predict('find a place to stay') == 'hotel'
    assert clf1.predict('where am I') == 'place'
    assert clf1.predict('may i know your name') == 'name'

    # Testing return probablities for each of the above tests
    assert clf1.predict('will it rain today',
                        True) == clf2.predict('will it rain today', True)
    assert clf1.predict('find a place to stay',
                        True) == clf2.predict('find a place to stay', True)
    assert clf1.predict('where am I', True) == clf2.predict('where am I', True)
    assert clf1.predict('may i know your name',
                        True) == clf2.predict('may i know your name', True)
Beispiel #7
0
    def test_classifier_basic(self):
        x_hotel = ['book a hotel', 'need a nice place to stay', 'any motels near by']
        x_weather = ['what is the weather like', 'is it hot outside']

        clf = Classifier()
        clf.fit(x_hotel, 'hotel')
        clf.fit(x_weather, 'weather')

        assert clf.predict('will it rain today') == 'weather'
        assert clf.predict('find a place to stay') == 'hotel'

        x_greetings = ['hi', 'hello', 'hello there', 'hey']
        x_not_greetings = x_hotel + x_weather

        clf2 = Classifier()
        clf2.fit(x_greetings, 'greetings')
        clf2.fit(x_not_greetings, 'not_greetings')
        assert clf2.predict('flight information') == 'not_greetings'
        assert clf2.predict('hey there') == 'greetings'
Beispiel #8
0
# Classifier, predicts the class of user input.

CONV_SAMPLES = {
    'greetings': ['Hi', 'hello', 'How are you', 'hey there', 'hey'],
    'taxi': ['book a cab', 'need a ride', 'find me a cab'],
    'weather': [
        'what is the weather in tokyo', 'weather germany',
        'what is the weather like in kochi'
    ],
    'datetime': [
        'what day is today', 'todays date', 'what time is it now', 'time now',
        'what is the time'
    ]
}

CLF = Classifier()
for key in CONV_SAMPLES:
    CLF.fit(CONV_SAMPLES[key], key)

# Entitiy Extractor, gets the required entities from the input.

X_WEATHER = [
    'what is the weather in tokyo', 'weather germany',
    'what is the weather like in kochi'
]
Y_WEATHER = [{
    'intent': 'weather',
    'place': 'tokyo'
}, {
    'intent': 'weather',
    'place': 'germany'
Beispiel #9
0
    def test_classifier_basic(self):
        x_hotel = [
            'book a hotel', 'need a nice place to stay', 'any motels near by'
        ]
        x_weather = ['what is the weather like', 'is it hot outside']

        clf = Classifier()
        clf.fit(x_hotel, 'hotel')
        clf.fit(x_weather, 'weather')

        assert clf.predict('will it rain today') == 'weather'
        assert clf.predict('find a place to stay') == 'hotel'

        x_greetings = ['hi', 'hello', 'hello there', 'hey']
        x_not_greetings = x_hotel + x_weather

        clf2 = Classifier()
        clf2.fit(x_greetings, 'greetings')
        clf2.fit(x_not_greetings, 'not_greetings')
        assert clf2.predict('flight information') == 'not_greetings'
        assert clf2.predict('hey there') == 'greetings'
Beispiel #10
0
def test_classifier_server_get():
    x_hotel = ['book a hotel', 'need a nice place to stay', 'any motels near by']
    x_weather = ['what is the weather like', 'is it hot outside']
    x_place = ['which place is this', 'where are we', 'where are you going', 'which place is it']
    x_name = ['What is your name', 'Who are you', 'What do i call you', 'what are you called']
    clf = Classifier()
    clf.fit(x_hotel, 'hotel')
    clf.fit(x_weather, 'weather')
    clf.fit(x_place, 'place')
    clf.fit(x_name, 'name')
    clf_serialized = clf.serialize()
    server = NLUServer(clf).serve(test=True)
    r = server.requests.get("/models/classifier/predict?input=will it rain today")
    assert r.text == 'weather'
    r = server.requests.get("/models/classifier/config")
    assert r.json() == clf_serialized
with open("dataset.txt", "r+") as file:
    text = file.readlines()
who = []
what = []
when = []
affirmation = []
unknown = []
for i in range(len(text)):
    line = text[i].split(",,,")
    if line[1] == " who\n":
        who.append(line[0])
    if line[1] == " what\n":
        what.append(line[0])
    if line[1] == " when\n":
        when.append(line[0])
    if line[1] == " affirmation\n":
        affirmation.append(line[0])
    if line[1] == " unknown\n":
        unknown.append(line[0])

from eywa.nlu import Classifier

clf = Classifier()
clf.fit(who, 'who')
clf.fit(what, 'what')
clf.fit(when, 'when')
clf.fit(affirmation, 'affirmation')
clf.fit(unknown, 'unknown')
x = input("Enter string :")
print(clf.predict(str(x)))