Skip to content
This repository has been archived by the owner on Jun 13, 2022. It is now read-only.

Commit

Permalink
add one notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
sdpython committed Feb 17, 2019
1 parent 39b74a6 commit e2cd399
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 2 deletions.
94 changes: 94 additions & 0 deletions _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",
" ('<EOF>', ':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",
" ('<EOF>', ':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
}
49 changes: 49 additions & 0 deletions _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()
12 changes: 10 additions & 2 deletions src/botadi/mokadi/mokadi_grammar.py
Expand Up @@ -4,18 +4,26 @@
"""
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
@param MokadiGrammarListener listener for a specific language
@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:
Expand Down

0 comments on commit e2cd399

Please sign in to comment.