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

Commit

Permalink
update actions
Browse files Browse the repository at this point in the history
  • Loading branch information
sdpython committed Apr 2, 2017
1 parent a58a9f8 commit 82e65d1
Show file tree
Hide file tree
Showing 15 changed files with 273 additions and 56 deletions.
Binary file modified _unittests/ut_mokadi/data/simple.pptx
Binary file not shown.
83 changes: 83 additions & 0 deletions _unittests/ut_mokadi/test_engine_ex.py
@@ -0,0 +1,83 @@
#-*- coding: utf-8 -*-
"""
@brief test log(time=10s)
"""

import sys
import os
import unittest


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

try:
import pyquickhelper as skip_
except ImportError:
path = os.path.normpath(
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..",
"..",
"pyquickhelper",
"src")))
if path not in sys.path:
sys.path.append(path)
import pyquickhelper as skip_

from pyquickhelper.loghelper import fLOG
from pyquickhelper.pycode import get_temp_folder
from src.jupytalk.mokadi import MokadiEngine, MokadiMessage
from src.jupytalk.mokadi.mokadi_action_slides import MokadiActionSlides
from src.jupytalk.mokadi.mokadi_action_conversation import MokadiActionConversation


class TestEngineExtended(unittest.TestCase):

def test_engine_ex(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")

messages = ["MOKADI liste presentation",
"MOKADI lire presentation 1 slide 2",
"MOKADI hello"]

temp = get_temp_folder(__file__, "temp_engine_ex")
clog = fLOG
folder = os.path.join(temp, "..", "data")

actions = [MokadiActionSlides(folder, fLOG=fLOG),
MokadiActionConversation(fLOG=fLOG),
]

engine = MokadiEngine(temp, clog, actions=actions)
verif = 0
for i, text in enumerate(messages):
fLOG("***", text)
mes = MokadiMessage(text, 1)
res = list(engine.process(mes, exc=True))
fLOG(res)
self.assertTrue(len(res) > 0)
if i == 2:
self.assertEqual(len(res), 1)
self.assertTrue(res[0].HasSound)
verif += 1
self.assertTrue(verif > 0)


if __name__ == "__main__":
unittest.main()
11 changes: 7 additions & 4 deletions _unittests/ut_mokadi/test_grammar_mokadi_ex.py
Expand Up @@ -54,15 +54,18 @@ def test_mokadi_interpret_long_list(self):
"MOKADI liste presentation",
"MOKADI lire présentation 1 slide 2",
"MOKADI Comment vas-tu ?",
"MOKADI hello",
]
expec = [[('MOKADI', ':MOKADI:'), ('fetch', ':word:'), ('mail', ':word:'), ('<EOF>', ':P:')],
expec = [[('MOKADI', ':MOKADI:'), ('fetch', ':word:'), ('mail', ':word:')],
[('MOKADI', ':MOKADI:'), ('liste', ':verb:'),
('presentation', ':presentation:'), ('<EOF>', ':P:')],
('presentation', ':presentation:')],
[('MOKADI', ':MOKADI:'), ('lire', ':verb:'), ('présentation', ':presentation:'),
('1', ':int:'), ('slide', ':slide:'), ('2', ':int:'), ('<EOF>', ':P:')],
('1', ':int:'), ('slide', ':slide:'), ('2', ':int:')],
[('MOKADI', ':MOKADI:'), ('Comment', ':word:'), ('vas', ':word:'),
('-', ':op:'), ('tu', ':word:'), ('?', ':question:'), ('<EOF>', ':P:')]
('-', ':op:'), ('tu', ':word:'), ('?', ':question:')],
[('MOKADI', ':MOKADI:'), ('hello', ':word:')],
]
expec = [_ + [('<EOF>', ':P:')] for _ in expec]

for i, code in enumerate(codes):
fLOG("{0}/{1}: {2}".format(i + 1, len(codes), code))
Expand Down
8 changes: 5 additions & 3 deletions _unittests/ut_mokadi/test_record.py
Expand Up @@ -38,7 +38,7 @@
import pyquickhelper as skip_

from pyquickhelper.loghelper import fLOG
from pyquickhelper.pycode import is_travis_or_appveyor
from pyquickhelper.pycode import is_travis_or_appveyor, get_temp_folder
from src.jupytalk.mokadi import record_speech, play_speech


Expand All @@ -55,8 +55,10 @@ def test_record(self):
return

fLOG("record")
temp = get_temp_folder(__file__, "temp_record")
output = os.path.join(temp, "output.wav")
try:
record = record_speech(3, fLOG=fLOG)
record = record_speech(3, fLOG=fLOG, WAVE_OUTPUT_FILENAME=output)
except Exception as e:
if os.environ["USERNAME"] == "ensaestudent" or \
os.environ["USERNAME"] == "vsxavierdupre" or \
Expand All @@ -66,7 +68,7 @@ def test_record(self):
"paris" in os.environ["COMPUTERNAME"].lower() or \
os.environ["USERNAME"].endswith("$"):
return
fLOG("play")
fLOG("play", len(record))
play_speech(record)
fLOG("end")

Expand Down
5 changes: 3 additions & 2 deletions setup.py
Expand Up @@ -36,7 +36,8 @@

packages = find_packages('src', exclude='src')
package_dir = {k: "src/" + k.replace(".", "/") for k in packages}
package_data = {project_var_name + ".mokadi": ["*.g4", "*.tokens"], }
package_data = {project_var_name + ".mokadi": ["*.g4", "*.tokens"],
project_var_name + ".mokadi.daa": ["*.wav"]}


############
Expand Down Expand Up @@ -207,7 +208,7 @@ def write_version():
sys.path.append(os.path.join(
os.path.dirname(__file__), "..", "pyensae", "src"))
from pyensae.languages import build_grammar
build_grammar(grammar)
build_grammar(grammar, fLOG=logging_function)
r = True
if not r and not ({"bdist_msi", "sdist",
"bdist_wheel", "publish", "publish_doc", "register",
Expand Down
18 changes: 10 additions & 8 deletions src/jupytalk/mokadi/MokadiGrammarLexer.py
@@ -1,13 +1,13 @@
# Generated from
# C:\xadupre\__home_\GitHub\jupytalk\src\jupytalk\mokadi\MokadiGrammar.g4
# by ANTLR 4.6
# Generated from \MokadiGrammar.g4 by ANTLR 4.7
from antlr4 import *
from io import StringIO
from typing.io import TextIO
import sys


def serializedATN():
with StringIO() as buf:
buf.write("\3\u0430\ud6d1\u8206\uad2d\u4417\uaef1\u8d80\uaadd\2*")
buf.write("\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2*")
buf.write("\u0145\b\1\4\2\t\2\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7")
buf.write("\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4\13\t\13\4\f\t\f\4\r\t\r")
buf.write("\4\16\t\16\4\17\t\17\4\20\t\20\4\21\t\21\4\22\t\22\4\23")
Expand Down Expand Up @@ -38,7 +38,7 @@ def serializedATN():
buf.write("\25\f\27\r\31\16\33\17\35\20\37\21!\22#\23%\24\'\25)\26")
buf.write("+\27-\30/\31\61\32\63\33\65\34\67\359\36;\37= ?!A\"C#")
buf.write("E$G%I\2K&M\'O(Q\2S\2U\2W\2Y\2[)]*\3\2\7\3\2))\3\2$$\3")
buf.write("\2\62;\6\2C\\aac|\u0082\u0101\5\2\13\f\17\17\"\"\u014a")
buf.write("\2\62;\6\2C\\aac|\u0082\u0101\5\2\13\f\17\17\"\"\2\u014a")
buf.write("\2\3\3\2\2\2\2\5\3\2\2\2\2\7\3\2\2\2\2\t\3\2\2\2\2\13")
buf.write("\3\2\2\2\2\r\3\2\2\2\2\17\3\2\2\2\2\21\3\2\2\2\2\23\3")
buf.write("\2\2\2\2\25\3\2\2\2\2\27\3\2\2\2\2\31\3\2\2\2\2\33\3\2")
Expand Down Expand Up @@ -190,6 +190,8 @@ class MokadiGrammarLexer(Lexer):
LINE_COMMENT = 39
WS = 40

channelNames = [u"DEFAULT_TOKEN_CHANNEL", u"HIDDEN"]

modeNames = ["DEFAULT_MODE"]

literalNames = ["<INVALID>",
Expand All @@ -214,9 +216,9 @@ class MokadiGrammarLexer(Lexer):

grammarFileName = "MokadiGrammar.g4"

def __init__(self, input=None):
super().__init__(input)
self.checkVersion("4.6")
def __init__(self, input=None, output: TextIO = sys.stdout):
super().__init__(input, output)
self.checkVersion("4.7")
self._interp = LexerATNSimulator(
self, self.atn, self.decisionsToDFA, PredictionContextCache())
self._actions = None
Expand Down
4 changes: 1 addition & 3 deletions src/jupytalk/mokadi/MokadiGrammarListener.py
@@ -1,6 +1,4 @@
# Generated from
# C:\xadupre\__home_\GitHub\jupytalk\src\jupytalk\mokadi\MokadiGrammar.g4
# by ANTLR 4.6
# Generated from \MokadiGrammar.g4 by ANTLR 4.7
from antlr4 import *
if __name__ is not None and "." in __name__:
from .MokadiGrammarParser import MokadiGrammarParser
Expand Down
62 changes: 32 additions & 30 deletions src/jupytalk/mokadi/MokadiGrammarParser.py
@@ -1,12 +1,14 @@
# Generated from C:\xadupre\__home_\GitHub\jupytalk\src\jupytalk\mokadi\MokadiGrammar.g4 by ANTLR 4.6
# Generated from \MokadiGrammar.g4 by ANTLR 4.7
# encoding: utf-8
from antlr4 import *
from io import StringIO
from typing.io import TextIO
import sys


def serializedATN():
with StringIO() as buf:
buf.write("\3\u0430\ud6d1\u8206\uad2d\u4417\uaef1\u8d80\uaadd\3+")
buf.write("\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\3+")
buf.write("y\4\2\t\2\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b")
buf.write("\t\b\4\t\t\t\4\n\t\n\4\13\t\13\4\f\t\f\4\r\t\r\4\16\t")
buf.write("\16\4\17\t\17\4\20\t\20\4\21\t\21\3\2\3\2\3\2\3\2\3\3")
Expand All @@ -19,31 +21,31 @@ def serializedATN():
buf.write("\21\3\21\5\21o\n\21\3\21\3\21\3\21\5\21t\n\21\3\21\5\21")
buf.write("w\n\21\3\21\2\2\22\2\4\6\b\n\f\16\20\22\24\26\30\32\34")
buf.write("\36 \2\7\3\2\5\b\4\2\t\t##\3\2\n\f\3\2\r\22\3\2\23\37")
buf.write("x\2\"\3\2\2\2\4+\3\2\2\2\6/\3\2\2\2\b\61\3\2\2\2\n9\3")
buf.write("\2\2\2\fC\3\2\2\2\16E\3\2\2\2\20G\3\2\2\2\22I\3\2\2\2")
buf.write("\24N\3\2\2\2\26P\3\2\2\2\30R\3\2\2\2\32W\3\2\2\2\34Y\3")
buf.write("\2\2\2\36\\\3\2\2\2 v\3\2\2\2\"#\5\f\7\2#$\5\4\3\2$%\7")
buf.write("\2\2\3%\3\3\2\2\2&,\5\6\4\2\'(\7\3\2\2()\5\6\4\2)*\7\4")
buf.write("\2\2*,\3\2\2\2+&\3\2\2\2+\'\3\2\2\2,\5\3\2\2\2-\60\5\b")
buf.write("\5\2.\60\5\n\6\2/-\3\2\2\2/.\3\2\2\2\60\7\3\2\2\2\61\62")
buf.write("\5\22\n\2\62\67\5\16\b\2\63\64\5\36\20\2\64\65\5\20\t")
buf.write("\2\65\66\5\36\20\2\668\3\2\2\2\67\63\3\2\2\2\678\3\2\2")
buf.write("\28\t\3\2\2\29=\5\24\13\2:<\5\24\13\2;:\3\2\2\2<?\3\2")
buf.write("\2\2=;\3\2\2\2=>\3\2\2\2>A\3\2\2\2?=\3\2\2\2@B\5\30\r")
buf.write("\2A@\3\2\2\2AB\3\2\2\2B\13\3\2\2\2CD\t\2\2\2D\r\3\2\2")
buf.write("\2EF\t\3\2\2F\17\3\2\2\2GH\t\4\2\2H\21\3\2\2\2IJ\t\5\2")
buf.write("\2J\23\3\2\2\2KO\7%\2\2LO\5\32\16\2MO\5\26\f\2NK\3\2\2")
buf.write("\2NL\3\2\2\2NM\3\2\2\2O\25\3\2\2\2PQ\t\6\2\2Q\27\3\2\2")
buf.write("\2RS\7 \2\2S\31\3\2\2\2TX\5\36\20\2UX\5 \21\2VX\5\34\17")
buf.write("\2WT\3\2\2\2WU\3\2\2\2WV\3\2\2\2X\33\3\2\2\2YZ\7&\2\2")
buf.write("Z\35\3\2\2\2[]\7+\2\2\\[\3\2\2\2\\]\3\2\2\2]^\3\2\2\2")
buf.write("^_\7$\2\2_\37\3\2\2\2`b\7+\2\2a`\3\2\2\2ab\3\2\2\2bc\3")
buf.write("\2\2\2cd\7$\2\2de\7!\2\2ek\7$\2\2fh\7\"\2\2gi\7+\2\2h")
buf.write("g\3\2\2\2hi\3\2\2\2ij\3\2\2\2jl\7$\2\2kf\3\2\2\2kl\3\2")
buf.write("\2\2lw\3\2\2\2mo\7+\2\2nm\3\2\2\2no\3\2\2\2op\3\2\2\2")
buf.write("pq\7$\2\2qs\7\"\2\2rt\7+\2\2sr\3\2\2\2st\3\2\2\2tu\3\2")
buf.write("\2\2uw\7$\2\2va\3\2\2\2vn\3\2\2\2w!\3\2\2\2\20+/\67=A")
buf.write("NW\\ahknsv")
buf.write("\2x\2\"\3\2\2\2\4+\3\2\2\2\6/\3\2\2\2\b\61\3\2\2\2\n9")
buf.write("\3\2\2\2\fC\3\2\2\2\16E\3\2\2\2\20G\3\2\2\2\22I\3\2\2")
buf.write("\2\24N\3\2\2\2\26P\3\2\2\2\30R\3\2\2\2\32W\3\2\2\2\34")
buf.write("Y\3\2\2\2\36\\\3\2\2\2 v\3\2\2\2\"#\5\f\7\2#$\5\4\3\2")
buf.write("$%\7\2\2\3%\3\3\2\2\2&,\5\6\4\2\'(\7\3\2\2()\5\6\4\2)")
buf.write("*\7\4\2\2*,\3\2\2\2+&\3\2\2\2+\'\3\2\2\2,\5\3\2\2\2-\60")
buf.write("\5\b\5\2.\60\5\n\6\2/-\3\2\2\2/.\3\2\2\2\60\7\3\2\2\2")
buf.write("\61\62\5\22\n\2\62\67\5\16\b\2\63\64\5\36\20\2\64\65\5")
buf.write("\20\t\2\65\66\5\36\20\2\668\3\2\2\2\67\63\3\2\2\2\678")
buf.write("\3\2\2\28\t\3\2\2\29=\5\24\13\2:<\5\24\13\2;:\3\2\2\2")
buf.write("<?\3\2\2\2=;\3\2\2\2=>\3\2\2\2>A\3\2\2\2?=\3\2\2\2@B\5")
buf.write("\30\r\2A@\3\2\2\2AB\3\2\2\2B\13\3\2\2\2CD\t\2\2\2D\r\3")
buf.write("\2\2\2EF\t\3\2\2F\17\3\2\2\2GH\t\4\2\2H\21\3\2\2\2IJ\t")
buf.write("\5\2\2J\23\3\2\2\2KO\7%\2\2LO\5\32\16\2MO\5\26\f\2NK\3")
buf.write("\2\2\2NL\3\2\2\2NM\3\2\2\2O\25\3\2\2\2PQ\t\6\2\2Q\27\3")
buf.write("\2\2\2RS\7 \2\2S\31\3\2\2\2TX\5\36\20\2UX\5 \21\2VX\5")
buf.write("\34\17\2WT\3\2\2\2WU\3\2\2\2WV\3\2\2\2X\33\3\2\2\2YZ\7")
buf.write("&\2\2Z\35\3\2\2\2[]\7+\2\2\\[\3\2\2\2\\]\3\2\2\2]^\3\2")
buf.write("\2\2^_\7$\2\2_\37\3\2\2\2`b\7+\2\2a`\3\2\2\2ab\3\2\2\2")
buf.write("bc\3\2\2\2cd\7$\2\2de\7!\2\2ek\7$\2\2fh\7\"\2\2gi\7+\2")
buf.write("\2hg\3\2\2\2hi\3\2\2\2ij\3\2\2\2jl\7$\2\2kf\3\2\2\2kl")
buf.write("\3\2\2\2lw\3\2\2\2mo\7+\2\2nm\3\2\2\2no\3\2\2\2op\3\2")
buf.write("\2\2pq\7$\2\2qs\7\"\2\2rt\7+\2\2sr\3\2\2\2st\3\2\2\2t")
buf.write("u\3\2\2\2uw\7$\2\2va\3\2\2\2vn\3\2\2\2w!\3\2\2\2\20+/")
buf.write("\67=ANW\\ahknsv")
return buf.getvalue()


Expand Down Expand Up @@ -141,9 +143,9 @@ class MokadiGrammarParser (Parser):
WS = 40
Sign = 41

def __init__(self, input: TokenStream):
super().__init__(input)
self.checkVersion("4.6")
def __init__(self, input: TokenStream, output: TextIO = sys.stdout):
super().__init__(input, output)
self.checkVersion("4.7")
self._interp = ParserATNSimulator(
self, self.atn, self.decisionsToDFA, self.sharedContextCache)
self._predicates = None
Expand Down
Empty file.
Binary file added src/jupytalk/mokadi/data/hello.wav
Binary file not shown.
24 changes: 22 additions & 2 deletions src/jupytalk/mokadi/mokadi_action.py
Expand Up @@ -9,11 +9,31 @@ class MokadiAction:
Action.
"""

def __init__(self):
def __init__(self, fLOG=None):
"""
Constructor.
"""
pass
if fLOG is not None:
self._fLOG = fLOG

def fLOG(self, *l, **p):
"""
logging function
"""
if hasattr(self, "_fLOG") and self._fLOG is not None:
self._fLOG(*l, **p)

def __str__(self):
"""
usual
"""
return self.__class__.__name__

def __repr__(self):
"""
usual
"""
return self.__class__.__name__

def can_do(self, interpreted, message):
"""
Expand Down
57 changes: 57 additions & 0 deletions src/jupytalk/mokadi/mokadi_action_conversation.py
@@ -0,0 +1,57 @@
#-*- coding: utf-8 -*-
"""
@file
@brief Defines an action for Mokadi.
"""
import os
from .mokadi_action import MokadiAction
from .mokadi_info import MokadiInfo
from .mokadi_exceptions import MokadiException


class MokadiActionConversation(MokadiAction):
"""
Action. Conversation.
"""

_sounds = {"hello": os.path.join(os.path.abspath(os.path.dirname(__file__)), "data", "hello.wav"),
}

def __init__(self, fLOG=None):
"""
Constructor.
@param folder folder where to look for presentation
@param fLOG logging function
"""
MokadiAction.__init__(self, fLOG=fLOG)

def can_do(self, interpreted, message):
"""
Tells if the class can process the message.
@param interpreted interpreted message
@param message message
@return true if the class can process the message
"""
if len(interpreted) <= 1:
return False
res = self.process_interpreted_message(interpreted, message)
for info in res:
return True
return False

def process_interpreted_message(self, interpretation, message):
"""
Process the interpreted message.
@param interpretation interpretation
@param message original message
@return iterator on Info
"""
sentance = [w[0].lower() for w in interpretation[1:3]]
if sentance[0] in {"hello", "bonjour"}:
yield MokadiInfo("ok", "hello", sound=MokadiActionConversation._sounds["hello"])
else:
raise MokadiException(
"Unable to answer to '{0}'.".format(sentance))

0 comments on commit 82e65d1

Please sign in to comment.