def ask_question(): question = request.form.get('user_input') answer = {} grandpy = GrandpyBot() parser = Parser(string=question) # Parse the input subject = parser.find_address() if subject: map_result, street = grandpy.find_place(subject) # Add the results of google maps to the answer answer.update(map_result) if street: wiki_result = grandpy.find_wiki_text(street) answer.update(wiki_result) else: # No subject parsed answer['error'] = { 'status': 'subject_not_found', 'message': "Excusez-moi mais je n'ai pas compris votre question." } # Create the response which will be sent to the client. # A unsuccessful response as always 'error' as the outermost key. body, http_code = (answer, 400) if 'error' in answer else ({ 'status': 'ok', 'message': grandpy.random_text(), **answer, }, 200) return body, http_code
def testParserStopWordsNotFound(): try: spanish_parser = Parser(language='es') assert len(spanish_parser._stopwords) == 0 except IOError as e: assert False, f"`Parser instanciation` raised an exception:\n{e}"
def place_request(): """Send request to Google place API and Media Wiki API. Return the response. """ user_input = request.form["data"] parsed_input = Parser.parse(user_input) google_response = request_google_place(parsed_input) wiki_response = {} if google_response["status"] == "OK": wiki_response = request_media_wiki(google_response["coords"], google_response["name"]) return jsonify({**google_response, **wiki_response})
def setUp(self) -> None: # TODO: sentence below fails # Salut GP, tu connais l'adresse de Openclassrooms, par hasard ? self.parser = Parser() self.question = "Salut GrandPy ! Est-ce que tu connais, " \ "par hasard, l'adresse d'OpenClassrooms ?"
class ParserTest(unittest.TestCase): def setUp(self) -> None: # TODO: sentence below fails # Salut GP, tu connais l'adresse de Openclassrooms, par hasard ? self.parser = Parser() self.question = "Salut GrandPy ! Est-ce que tu connais, " \ "par hasard, l'adresse d'OpenClassrooms ?" @staticmethod def testParserStopWordsNotFound(): try: spanish_parser = Parser(language='es') assert len(spanish_parser._stopwords) == 0 except IOError as e: assert False, f"`Parser instanciation` raised an exception:\n{e}" def testParsing(self): expected = ['!', ',', 'adresse', 'openclassrooms', '?'] parsed_data = self.parser.parse(self.question) self.assertEqual(expected, parsed_data) # Check if the original string haven't been mutated. self.assertEqual(self.question, self.parser.original_string) def testFirstExtractAddress(self): self.parser.parse(self.question) place = self.parser.find_address() self.assertEqual('openclassrooms', place) def testSecondExtractAddress(self): self.parser.parse("Tu connais l'adresse de la mairie de Paris") place = self.parser.find_address() self.assertEqual('mairie paris', place) def testThirdExtractAddress(self): self.parser.parse("Où se situe la Tour Eiffel ?") place = self.parser.find_address() self.assertEqual('tour eiffel', place) def testFourthExtractAddress(self): self.parser.parse("Où est la cathédrale Notre-Dame") place = self.parser.find_address() self.assertEqual('cathédrale notre-dame', place)
import os from flask import Flask, render_template, request, jsonify from grandpybot.wikipedia import Wiki from grandpybot.google_maps import GMaps from grandpybot.parser import Parser from grandpybot.stop_words import STOP_WORDS from grandpybot.messages import * app = Flask(__name__) google_api_key = "AIzaSyC71Qx-GXf4edGJ43FvsEw9e9Pr0qs5PFA" parser = Parser(STOP_WORDS) gmap = GMaps(google_api_key) wiki = Wiki() @app.route('/_get_json') def get_json(): user_input = request.args.get('userInput', type=str) parsed_input = parser.get_relevant_words(user_input) if parsed_input == "": failure_msg = return_failure() return jsonify(message1=failure_msg, error=True) gmap_place = gmap.get_position(parsed_input) if gmap_place != "no result": wiki_result = wiki.get_wiki_result(gmap_place["latitude"], gmap_place["longitude"], parsed_input) msg_adress = return_address(gmap_place["address"])
def test_parser_test_complete_sentences(sentence, excpected): """Situationals tests.""" assert Parser.parse(sentence) == excpected
def test_parser_catch_apostrophs_early(words): """Test if the parser catch the french links.""" assert Parser.parse(words) == ""
def test_parser_remove_signs(words): """Test if the parser remove specifics signs.""" assert Parser.parse(words) == ""
def test_parser_return_lower(words): """Test if the parser return the lowers words.""" assert Parser.parse(words) == words.lower()
def test_parser_catch_(words): """Test if the parser catch the givens words.""" assert Parser.parse(words) == ""