Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 1.2.0 #21

Merged
merged 12 commits into from
Aug 29, 2019
69 changes: 30 additions & 39 deletions chatbot/api/v2/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
unknown_col = Config.get_mongo_collection("unknown")


direct_response_model = api.model('DirectResponse', {
response_model = api.model('Response', {
'user_input': fields.String(description='User chat input'),
'response': fields.String(description='Bot chat response')
'response': fields.String(description='Bot chat response'),
'style': fields.String
})

conflict_model = api.model('Conflict', {
Expand All @@ -40,30 +41,31 @@
})

keyword_model = api.model('Keyword', {
'keyword': fields.String,
'confidence': fields.Float
'keyword': fields.String,
'confidence': fields.Float
})

inner_content_model = api.model('InnerContent', {
'title': fields.String,
'keywords': fields.List(fields.Nested(keyword_model)),
'texts': fields.List(fields.String)
'title': fields.String,
'keywords': fields.List(fields.Nested(keyword_model)),
'text': fields.String,
'links': fields.List(fields.List(fields.String))
})

content_model = api.model('Content', {
'id': fields.String,
'url': fields.String,
'content': fields.Nested(inner_content_model)
'id': fields.String,
'url': fields.String,
'content': fields.Nested(inner_content_model)
})

content_collection_model = api.model('ContentCol', {
'prod': fields.Nested(content_model),
'manual': fields.Nested(content_model),
'url': fields.String
'prod': fields.Nested(content_model),
'manual': fields.Nested(content_model),
'url': fields.String
})

unknown_query_model = api.model('UnknownQuery', {
'query_text': fields.String
'query_text': fields.String
})


Expand All @@ -72,15 +74,17 @@ def get(self):
return {'hello': 'world'}


class Response(Resource):
@api.marshal_with(direct_response_model)
def get(self, query):
return models.DirectResponse(user_input=query)
style_parser = reqparse.RequestParser()
style_parser.add_argument('style', required=False)


class FullResponse(Resource):
def get(self):
pass
class Response(Resource):
@api.marshal_with(response_model)
@api.expect(style_parser)
def get(self, query):
args = style_parser.parse_args()
style = args['style'] if 'style' in args else 'plain'
return models.Response(query, style)


class ConflictIDs(Resource):
Expand Down Expand Up @@ -223,34 +227,21 @@ def delete(self, unknown_query):
abort(404, 'Unknown query not found')


api.add_resource(HelloWorld,
'/',
methods=['GET'])
api.add_resource(HelloWorld, '/', methods=['GET'])

api.add_resource(Response,
'/response/<string:query>/',
methods=['GET'])
api.add_resource(FullResponse,
'/response/',
methods=['GET'])
api.add_resource(Response, '/response/<string:query>/', methods=['GET'])

api.add_resource(ConflictIDs,
'/conflict_ids/',
methods=['GET'])
api.add_resource(ConflictIDs, '/conflict_ids/', methods=['GET'])
api.add_resource(ConflictIDs,
'/conflict_ids/<conflict_id>/',
methods=['DELETE'])

api.add_resource(Contents,
'/contents/',
methods=['GET'])
api.add_resource(Contents, '/contents/', methods=['GET'])
api.add_resource(Content,
'/content/<content_id>/',
methods=['GET', 'PUT', 'DELETE'])

api.add_resource(UnknownQueries,
'/unknown_queries/',
methods=['GET'])
api.add_resource(UnknownQueries, '/unknown_queries/', methods=['GET'])
api.add_resource(UnknownQueries,
'/unknown_queries/<unknown_query>/',
methods=['DELETE'])
17 changes: 7 additions & 10 deletions chatbot/api/v2/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@
handler = QueryHandler()


class DirectResponse(object):
def __init__(self, user_input=''):
self.user_input = user_input
self.response = handler.get_response(self.user_input)


class Response(object):
def __init__(self, user_input='', response_format='plain'):
def __init__(self, user_input, style):
self.user_input = user_input
self.response_format = response_format
self.response = handler.get_response(self.user_input,
self.response_format)
if style:
self.response = handler.get_response(self.user_input, style)
self.style = style
else:
self.response = handler.get_response(self.user_input)
self.style = 'plain'


class Conflict(object):
Expand Down
11 changes: 7 additions & 4 deletions chatbot/api/v2/test/test_v2_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ def test_get_content(client):
'content': {
'title': 'test title',
'keywords': [],
'texts': []
'text': '',
'links': []
},
'url': 'test_url'}
factory.post_document(content, prod_col)
Expand All @@ -103,7 +104,8 @@ def test_update_content(client):
"confidence": 0.2010
}
],
"texts": ["some test text"]
"text": "some test text",
"links": []
}
}
factory.post_document(content.copy(), prod_col)
Expand Down Expand Up @@ -140,7 +142,8 @@ def test_get_document(client):
"confidence": 0.2010
}
],
"texts": ["some test text"]
"text": "some test text",
"links": []
}
}
factory.post_document(document.copy(), prod_col)
Expand All @@ -166,7 +169,7 @@ def test_delete_content(client):
"confidence": 0.2010
}
],
"texts": ["some test text"]
"text": "some test text"
}
}
factory.post_document(document.copy(), prod_col)
Expand Down
11 changes: 6 additions & 5 deletions chatbot/chat/scripts/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,19 @@ function query() {

populateChatWithMessage(input, "user")

http.open("POST", host+"v1/dialogflow/response", true);
http.setRequestHeader("Content-Type", "text/plain");
http.open("GET", host+"v2/response/"+input+"/?style=html", true);
http.setRequestHeader("Content-Type", "text/plain");
http.onreadystatechange = function() {
if(this.readyState == 4 && this.status == 200) {
var json = JSON.parse(http.responseText);
var response = json.fulfillmentText;
var response = json.response;
response = response.replace(/\n/g, '<br>');
populateChatWithMessage(response, "bot");
}
}
var data = JSON.stringify({"queryResult": { "queryText": input }});
http.send(data);
http.send();
//var data = JSON.stringify({"queryResult": { "queryText": input }});
//http.send(data);
}

/*
Expand Down
2 changes: 1 addition & 1 deletion chatbot/launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def insert_documents(data):
"title": title})

print("Conflicts: {}".format(conflicts))
factory.get_collection(conflict_col).create_index([("id", 1)],
factory.get_collection(conflict_col).create_index([("title", 1)],
unique=True)
for conflict in conflicts:
try:
Expand Down
20 changes: 7 additions & 13 deletions chatbot/model/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@

class KeyWord:
""" Keyword to fill keyword-list in model schema contents-list """

__word = None
__confidence = None

def __init__(self, word, confidence):
self.__word = word
self.__confidence = confidence
Expand All @@ -24,14 +20,10 @@ def get_keyword(self):

class Content:
""" Content to fill contents-list in model schema """

__title = ""
__keywords = []
__texts = []

def __init__(self, title, texts, keywords=[]):
def __init__(self, title, text, links=[], keywords=[]):
self.__title = title
self.__texts = texts
self.__text = text
self.__links = links

if keywords:
if not all(isinstance(keyword, KeyWord) for keyword in keywords):
Expand All @@ -43,7 +35,8 @@ def get_content(self):
return {
"title": self.__title,
"keywords": [keyword.get_keyword() for keyword in self.__keywords],
"texts": self.__texts,
"text": self.__text,
"links": self.__links
}

def __repr__(self):
Expand Down Expand Up @@ -130,7 +123,8 @@ def visit_node(self, data, model_template, models, title=None):
.format(title,
child["text"]))]

content = Content(title, [child["text"]], keywords)
content = Content(title, child["text"],
child["links"], keywords)
new_model = copy.deepcopy(model_template)
new_model["id"] = child["id"]
new_model["content"] = content.get_content()
Expand Down
2 changes: 1 addition & 1 deletion chatbot/model/test/test_data/test_data.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"url":"https://ntnu.no","tree":{"tag":"root","text":"root","id":"391","children":[{"tag":"meta","text":"revisjon,forvaltningsrevisjon","id":"123"},{"tag":"p","text":"emner2","id":"832"},{"tag":"h3","text":"mange bra kommune emner22","id":"542","children":[{"tag":"p","text":"Informatikk prosjektarbeid 2 er et kjempe bra emne!2","id":"654"}]}]}}]
[{"url":"https://ntnu.no","tree":{"tag":"root","text":"root","id":"391","children":[{"tag":"meta","text":"revisjon,forvaltningsrevisjon","id":"123","links":[]},{"tag":"p","text":"emner2","id":"832","links":[]},{"tag":"h3","text":"mange bra kommune emner22","id":"542","children":[{"tag":"p","text":"Informatikk prosjektarbeid 2 er et kjempe bra emne!2","id":"654","links":[["emne","https://emne.no"]]}]}]}}]
2 changes: 1 addition & 1 deletion chatbot/model/test/test_data/test_data_serialized.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"id":"832","title":"","description":"","url":"https://ntnu.no","last_modified":"","header_meta_keywords":["revisjon","forvaltningsrevisjon"],"keywords":[],"content":{"title":"","keywords":[],"texts":["emner2"]},"manually_changed":false},{"title":"","description":"","url":"https://ntnu.no","last_modified":"","header_meta_keywords":["revisjon","forvaltningsrevisjon"],"keywords":[],"content":{"title":"mange bra kommune emner22","keywords":[],"texts":["Informatikk prosjektarbeid 2 er et kjempe bra emne!2"]},"manually_changed":false}]
[{"id":"832","title":"","description":"","url":"https://ntnu.no","last_modified":"","header_meta_keywords":["revisjon","forvaltningsrevisjon"],"keywords":[],"content":{"title":"","keywords":[],"text":"emner2","links":[]},"manually_changed":false},{"title":"","description":"","url":"https://ntnu.no","last_modified":"","header_meta_keywords":["revisjon","forvaltningsrevisjon"],"keywords":[],"content":{"title":"mange bra kommune emner22","keywords":[],"text":"Informatikk prosjektarbeid 2 er et kjempe bra emne!2","links":[["emne","https://emne.com"]]},"manually_changed":false}]
31 changes: 27 additions & 4 deletions chatbot/model/test/test_model_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ def test_get_document():
fact.get_database().drop_collection("test")

try:
fact.post_document(data[0], "test")
test_col = fact.get_database().get_collection('test')

# Insert data
for d in data:
test_col.insert_one(d)

fact.post_document(data[1], "test")
fact.get_collection("test").create_index(
[("keywords", pymongo.TEXT),
Expand Down Expand Up @@ -46,17 +51,35 @@ def test_get_document():


def test_update_document():
data = '{"name": "testname", "manually_changed": false }'
data = {"name": "testname", "manually_changed": False}
try:
fact.post_document(data, "test")
fact.get_database().get_collection('test').insert_one(data)
fact.get_collection("test").create_index(
[("name", pymongo.TEXT)], default_language="norwegian")

newdata = '{"name": "nottestname"}'
newdata = {"name": "nottestname"}
fact.update_document({"name": "testname"}, newdata, "test")
doc = fact.get_document("nottestname", prod_col="test")[0]

assert doc["name"] == "nottestname"

finally:
fact.get_database().drop_collection("test")


def test_delete_document():
idx = '#321_test_delete_id'
doc = {'id': idx, 'data': 'some data to be deleted'}
try:
fact.get_database().get_collection('test').insert_one(doc)

query = {'id': idx}
fact.delete_document(query, 'test')

response_data = next(fact.get_database()
.get_collection('test')
.find(query), None)
assert response_data is None

finally:
fact.get_database().drop_collection('test')
10 changes: 5 additions & 5 deletions chatbot/model/test/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ def test_serialize_data():
assert test_data[0]["url"] == serialized_data[0]["url"]
assert test_data[0]["id"] == serialized_data[0]["id"]
assert (
test_data[0]["content"]["texts"]
== serialized_data[0]["content"]["texts"]
test_data[0]["content"]["text"]
== serialized_data[0]["content"]["text"]
)
assert (
test_data[1]["content"]["texts"]
== serialized_data[1]["content"]["texts"]
test_data[1]["content"]["text"]
== serialized_data[1]["content"]["text"]
)
assert (
test_data[0]["header_meta_keywords"][0]
Expand All @@ -43,7 +43,7 @@ def test_instance_of_KeyWord():
Content(
"Åpningstider",
"Svømmehallen er åpen alle dager 09:00-20:00",
[["svømme", 0.82], ["åpningstid", 0.87]],
keywords=[["svømme", 0.82], ["åpningstid", 0.87]],
)

# Test that no TypeError is raised when creating a Content object with
Expand Down
Loading