def test_ShouldAddQuestionOnce_WhenSameQuestionAddedAgain(
            self, pickle_spy):

        q1 = q(question="question1")
        q2 = q(question="question2")

        x = TfidfSimilarity()
        x.add_question(q1)
        self.assertEqual(1, len(x.questions_to_add))
        x.add_question(q1)
        self.assertEqual(1, len(x.questions_to_add))
        x.add_question(q2)
        self.assertEqual(2, len(x.questions_to_add))

        print(x.questions_to_add)
    def test_ShouldUpdateTheModel_WhenNewQuestionsAreAvailable(
            self, generate_spy, pickle_spy):
        x = TfidfSimilarity()
        q1 = q(question="question1")
        x.add_question(q1)
        x.update_model()

        x._generate_similarity_index.assert_called_once()
def generate_dataset():
    with open(QA_DATASET_CSV, 'w', encoding='utf-8') as f:
        with urlopen(DATASET_URL) as url:
            j = json.loads(url.read().decode())
            for d in j['data']:
                for p in d['paragraphs']:
                    for qas in p['qas']:
                        f.write(q_to_line(
                            q(question=qas['question']))
                        )
Ejemplo n.º 4
0
    def test_ShouldReturn404AndAddQuestion_WhenSimilarQuestionCannotBeFound(
            self):
        mocked_tfidf_service = Mock()
        mocked_tfidf_service.query.return_value = []
        api_server.s = mocked_tfidf_service
        question = "notavailablequestion"

        request, response = api_server.app.test_client.get('/ask?q=' +
                                                           question)
        self.assertTrue(response.status == 404)
        self.assertEqual(mocked_tfidf_service.add_question.call_args_list[0],
                         call(q(question=question)))
async def ask(request):
    q_param = request.args['q'][0]
    question = q(question=q_param)
    results = s.query(question)
    if len(results) == 0:
        s.add_question(question)
        return response.json(
            {
                "message":
                "Question not found! Will be added to the database now.",
                "question": question.question
            },
            status=404)
    else:
        return json(results)