From e2cd399922931d0ea5ce1909c87984059b669ff3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?xavier=20dupr=C3=A9?= Date: Sun, 17 Feb 2019 01:00:49 +0100 Subject: [PATCH] add one notebook --- _doc/notebooks/grammars.ipynb | 94 +++++++++++++++++++ .../ut_documentation/test_nb_grammar.py | 49 ++++++++++ src/botadi/mokadi/mokadi_grammar.py | 12 ++- 3 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 _doc/notebooks/grammars.ipynb create mode 100644 _unittests/ut_documentation/test_nb_grammar.py diff --git a/_doc/notebooks/grammars.ipynb b/_doc/notebooks/grammars.ipynb new file mode 100644 index 0000000..384eebb --- /dev/null +++ b/_doc/notebooks/grammars.ipynb @@ -0,0 +1,94 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Grammar\n", + "\n", + "The chatbot relies on a grammar to interpret an order. By default, it interprets French." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[('MOKADI', ':MOKADI:'),\n", + " ('lire', ':verb_voir:'),\n", + " ('mail', ':mails:'),\n", + " ('', ':P:')]" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from botadi.mokadi import interpret\n", + "cmd = \"MOKADI lire mail\"\n", + "interpret(cmd)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[('MOKADI', ':MOKADI:'),\n", + " ('lire', ':verb_voir:'),\n", + " ('pr\u00e9sentation', ':presentation:'),\n", + " ('1', ':int:'),\n", + " ('transparent', ':slide:'),\n", + " ('num\u00e9ro', ':numero:'),\n", + " ('2', ':int:'),\n", + " ('', ':P:')]" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cmd = \"MOKADI lire pr\u00e9sentation 1 transparent num\u00e9ro 2\"\n", + "interpret(cmd)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} \ No newline at end of file diff --git a/_unittests/ut_documentation/test_nb_grammar.py b/_unittests/ut_documentation/test_nb_grammar.py new file mode 100644 index 0000000..7f07587 --- /dev/null +++ b/_unittests/ut_documentation/test_nb_grammar.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- +""" +@brief test log(time=5s) +""" + +import sys +import os +import unittest +from pyquickhelper.loghelper import fLOG +from pyquickhelper.pycode import add_missing_development_version +from pyquickhelper.ipythonhelper import test_notebook_execution_coverage + + +try: + import src +except ImportError: + path = os.path.normpath( + os.path.abspath( + os.path.join( + os.path.split(__file__)[0], + "..", + ".."))) + if path not in sys.path: + sys.path.append(path) + import src + +import src.botadi + + +class TestNotebookGrammar(unittest.TestCase): + + def setUp(self): + add_missing_development_version(["jyquickhelper"], __file__, hide=True) + + def test_notebook_grammar(self): + fLOG( + __file__, + self._testMethodName, + OutputPrint=__name__ == "__main__") + + self.assertTrue(src.botadi is not None) + folder = os.path.join(os.path.dirname(__file__), + "..", "..", "_doc", "notebooks") + test_notebook_execution_coverage( + __file__, "grammar", folder, 'botadi', copy_files=[], fLOG=fLOG) + + +if __name__ == "__main__": + unittest.main() diff --git a/src/botadi/mokadi/mokadi_grammar.py b/src/botadi/mokadi/mokadi_grammar.py index 3367469..fa557da 100644 --- a/src/botadi/mokadi/mokadi_grammar.py +++ b/src/botadi/mokadi/mokadi_grammar.py @@ -4,11 +4,13 @@ """ from .mokadi_parser import get_tree_string, parse_mokadi, run_parse from .mokadi_exceptions import MokadiException +from .grammars import MokadiGrammar_frParser, MokadiGrammar_frLexer, MokadiGrammar_frListener -def interpret(sentance, MokadiGrammarParser, MokadiGrammarLexer, MokadiGrammarListener): +def interpret(sentance, MokadiGrammarParser=None, MokadiGrammarLexer=None, + MokadiGrammarListener=None): """ - Interpret a sentance and returns a list of word. + Interprets a sentance and returns a list of words. @param MokadiGrammarParser parser for a specific language @param MokadiGrammarLexer lexer for a specific language @@ -16,6 +18,12 @@ def interpret(sentance, MokadiGrammarParser, MokadiGrammarLexer, MokadiGrammarLi @param sentance any string @return list of tuple (word, kind) """ + if MokadiGrammarParser is None: + MokadiGrammarParser = MokadiGrammar_frParser + if MokadiGrammarLexer is None: + MokadiGrammarLexer = MokadiGrammar_frLexer + if MokadiGrammarListener is None: + MokadiGrammarListener = MokadiGrammar_frListener parser = parse_mokadi(sentance, MokadiGrammarParser, MokadiGrammarLexer) stdout, stderr, tree = run_parse(parser) if stderr and len(stderr) > 0: