Coverage for src/botadi/mokadi/mokadi_message.py: 83%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

35 statements  

1# -*- coding: utf-8 -*- 

2""" 

3@file 

4@brief Defines a message for mokadi. 

5""" 

6from .mokadi_grammar import interpret 

7from .mokadi_exceptions import MokadiException 

8 

9 

10class MokadiMessage: 

11 """ 

12 Defines a message for mokadi. 

13 """ 

14 

15 def __init__(self, message: str, confidence: float): 

16 """ 

17 Constructor 

18 

19 @param message message (string) 

20 @param confidence 

21 """ 

22 self._message = message 

23 self._confidence = confidence 

24 

25 def __str__(self): 

26 """ 

27 message to string 

28 """ 

29 return self._message 

30 

31 def __repr__(self): 

32 """ 

33 message to string 

34 """ 

35 return "MokadiMessage('%s', %f)" % (self._message.replace("'", "\\'"), self._confidence) 

36 

37 def _repr_html_(self): 

38 """ 

39 for notebooks 

40 """ 

41 return "<b>%1.2f<b> <i>%s</i>" % (self._confidence, self._message) 

42 

43 @property 

44 def message(self): 

45 """ 

46 property 

47 """ 

48 return self._message 

49 

50 @property 

51 def confidence(self): 

52 """ 

53 property 

54 """ 

55 return self._confidence 

56 

57 def interpret(self, exc=True, MokadiGrammarParser=None, MokadiGrammarLexer=None, 

58 MokadiGrammarListener=None): 

59 """ 

60 Interpret the message with the grammar. 

61 

62 @param exc raises an exception if error 

63 @param MokadiGrammarParser parser for a specific language (cannot be None despite the default value) 

64 @param MokadiGrammarLexer lexer for a specific language (cannot be None despite the default value) 

65 @param MokadiGrammarListener listener for a specific language (cannot be None despite the default value) 

66 @return interpretation 

67 """ 

68 if MokadiGrammarParser is None: 

69 raise MokadiException("MokadiGrammarParser cannot be None") 

70 if MokadiGrammarLexer is None: 

71 raise MokadiException("MokadiGrammarLexer cannot be None") 

72 if MokadiGrammarListener is None: 

73 raise MokadiException("MokadiGrammarListener cannot be None") 

74 if exc: 

75 return interpret(self._message, MokadiGrammarParser, MokadiGrammarLexer, MokadiGrammarListener) 

76 else: 

77 try: 

78 return interpret(self._message, MokadiGrammarParser, MokadiGrammarLexer, MokadiGrammarListener) 

79 except Exception as e: 

80 if not hasattr(self, "_errors"): 

81 self._errors = [] 

82 self._errors.append(e) 

83 return e