Browse Source

Merged dev into master

nico 8 years ago
parent
commit
aae0842053

+ 1 - 4
brain.yml

@@ -39,16 +39,13 @@
       - systemdate:
           say_template:
             - "il est {{ hours }} heure et {{ minutes }} minute"
-          cache: False
     signals:
       - order: "what time is it"
 
   - name: "Say local date from template"
     neurons:
       - systemdate:
-          file_template: fr_systemdate_template_example.j2
-          tts: "voxygen"
-          cache: False
+          file_template: en_systemdate_template_example.j2
     signals:
       - order: "test heure"
 

+ 4 - 0
core/NeuronModule.py

@@ -3,6 +3,7 @@ import logging
 import os
 import random
 
+import sys
 from jinja2 import Template
 
 from core.Utils import Utils
@@ -132,6 +133,9 @@ class NeuronModule(object):
                 t = Template(self.say_template)
                 returned_message = t.render(**message_dict)
 
+            # trick to remobe unicode problem when loading jinja template with non ascii char
+            reload(sys)
+            sys.setdefaultencoding('utf-8')
             # the user choose a file_template option
             if self.file_template is not None:  # the user choose a file_template option
                 real_file_template_path = "templates/%s" % self.file_template

+ 117 - 0
neurons/systemdate/README.md

@@ -0,0 +1,117 @@
+# Systemdate
+
+## Synopsis
+
+Give the current time from the system where Kalliope is installed. Return a dict of parameters that can be used in a template.
+
+## Options
+
+| parameter     | required | default | choices     | comment                                                               |
+|---------------|-----------|---------|-------------|-----------------------------------------------------------------------|
+| say_template  | no        |         |             | Say template used to make Kalliope speak out loud returned parameters |
+| file_template | no        |         |             | Like a say_template but from a file for complex usage                 |
+| cache         | no        | FALSE   | True, False | Should be set to False as audio output will changes at every minute   |
+
+
+## Return Values
+
+| name      | description                                       | type   | sample |
+|-----------|---------------------------------------------------|--------|--------|
+| hours     | Hour (24-hour clock) as a decimal number [00,23]. | string | 22     |
+| minutes   | Minute as a decimal number [00,59].               | string | 54     |
+| weekday   | Weekday as a decimal number [0(Sunday),6].        | string | 4      |
+| month     | Month as a decimal number [01,12].                | string | 4      |
+| day_month | Day of the month as a decimal number [01,31].     | string | 12     |
+| year      | Year with century as a decimal number             | string | 2016   |
+
+
+## Synapses example
+
+Simple synapse that give the current time with only hours and minutes
+```
+ - name: "time"
+    neurons:
+      - systemdate:
+          say_template:
+            - "It' {{ hours }} hours and {{ minutes }} minutes"
+    signals:
+      - order: "what time is it"
+```
+
+Synapse that give complete date and time with a template file.
+```
+ - name: "time"
+    neurons:
+      - systemdate:
+          file_template: en_systemdate_template_example.j2            
+    signals:
+      - order: "what time is it"
+```
+
+
+## Templates example 
+Following examples are available in the [neuron directory](template_examples/).
+
+This template will transcribe received numbers from the neuron into natural language
+```
+"It's {{ hours }} hours and {{ minutes }} minutes
+```
+
+This template, which it must be placed in a file_template, will give the complete date and time.
+```
+{% set day_of_week = {
+    "0": "sunday",
+    "1": "monday",
+    "2": "tuesday",
+    "3": "wednesday",
+    "4": "thursday",
+    "5": "friday",
+    "6": "saturday"
+    }[weekday] | default("")
+-%}
+
+{% set month_word = {"1": "january", "2": "february", "3": "march", "4": "april", "5": "may", "6": "june", "7": "july", "8": "august", "9": "september", "10": "october", "11": "november", "12": "december"}[month] | default("") -%}
+
+{% set day_month_formated = {
+    "1": "first",
+    "2": "second",
+    "3": "third",
+    "4": "fourth",
+    "5": "fifth",
+    "6": "sixth",
+    "7": "seventh",
+    "8": "eighth",
+    "9": "ninth",
+    "10": "tenth",
+    "11": "eleventh",
+    "12": "twelfth",
+    "13": "thirteenth",
+    "14": "fourteenth",
+    "15": "fifteenth",
+    "16": "sixteenth",
+    "17": "seventeenth",
+    "18": "eighteenth",
+    "19": "nineteenth",
+    "20": "twentieth",
+    "21": "twenty-first",
+    "22": "twenty-second",
+    "23": "twenty-third",
+    "24": "twenty-fourth",
+    "25": "twenty-fifth",
+    "26": "twenty-sixth",
+    "27": "twenty-seventh",
+    "28": "twenty-eighth",
+    "29": "twenty-ninth",
+    "30": "thirtieth",
+    "31": "thirty-first",
+
+}[day_month] | default("") -%}
+
+It' {{ hours }} hours and {{ minutes }} minutes.
+We are the {{ day_of_week }} {{ month_word }} the {{ day_month_formated }} {{ year }}
+```
+
+## Notes
+
+> **Note:** As the neuron is based on the local system date, this last must be well configured. A good practice is the installation and configuration of a NTP client
+ to synchronize the time on your Linux system with a centralized NTP server.

+ 16 - 4
neurons/systemdate/systemdate.py

@@ -6,17 +6,29 @@ from core.NeuronModule import NeuronModule
 
 class Systemdate(NeuronModule):
     def __init__(self, **kwargs):
-        super(Systemdate, self).__init__(**kwargs)
+        # get the cache if set by the user, if not, set it to false as it is not necessary
+        cache = kwargs.get('cache', None)
+        if cache is None:
+            cache = False
+        super(Systemdate, self).__init__(cache=cache, **kwargs)
 
-        # get hours and minutes
 
-        hour = time.strftime("%H")
-        minute = time.strftime("%M")
 
+        # local time and date
+        hour = time.strftime("%H")          # Hour (24-hour clock) as a decimal number [00,23].
+        minute = time.strftime("%M")        # Minute as a decimal number [00,59].
+        weekday = time.strftime("%w")       # Weekday as a decimal number [0(Sunday),6].
+        day_month = time.strftime("%d")     # Day of the month as a decimal number [01,31].
+        month = time.strftime("%m")         # Month as a decimal number [01,12].
+        year = time.strftime("%Y")          # Year with century as a decimal number. E.g: 2016
 
         message = {
             "hours": hour,
             "minutes": minute,
+            "weekday": weekday,
+            "month": month,
+            "day_month": day_month,
+            "year": year
         }
         
         self.say(message)

+ 0 - 1
neurons/systemdate/template/fr_template2.j2

@@ -1 +0,0 @@
-{{ hours }} heures et {{ minutes }} minutes précisément

+ 1 - 0
neurons/systemdate/template_examples/en_template1.j2

@@ -0,0 +1 @@
+It's {{ hours }} hours and {{ minutes }} minutes.

+ 12 - 0
neurons/systemdate/template_examples/en_template2.j2

@@ -0,0 +1,12 @@
+{% set day_of_week = {"0": "sunday", "1": "monday", "2": "tuesday", "3": "wednesday", "4": "thursday", "5": "friday", "6": "saturday"}[weekday] | default("") -%}
+{% set month_word = {"1": "january", "2": "february", "3": "march", "4": "april", "5": "may", "6": "june", "7": "july", "8": "august", "9": "september", "10": "october", "11": "november", "12": "december"}[month] | default("") -%}
+
+{% set day_month_formated = {
+{
+"15": "fifteenth"
+}
+}
+[day_month] | default("") -%}
+
+It' {{ hours }} hours and {{ minutes }} minutes.
+We are the {{ day_of_week }} {{ month_word }} the {{ day_month_formated }} {{ year }}

+ 0 - 0
neurons/systemdate/template/fr_template1.j2 → neurons/systemdate/template_examples/fr_template1.j2


+ 5 - 0
neurons/systemdate/template_examples/fr_template2.j2

@@ -0,0 +1,5 @@
+{% set day_of_week = {"0": "dimanche", "1": "lundi", "2": "mardi", "3": "mercredi", "4": "jeudi", "5": "vendredi", "6": "samedi"}[weekday] | default("") -%}
+{% set month_word = {"1": "janvier", "2": "février", "3": "mars", "4": "avril", "5": "mai", "6": "juin", "7": "juillet", "8": "août", "9": "septembre", "10": "octobre", "11": "novembre", "12": "décembre"}[month] | default("") -%}
+
+Il est {{ hours }} heures et {{ minutes }} minutes.
+Nous sommes le {{ day_of_week }} {{ day_month }} {{ month_word }} {{ year }}

+ 50 - 0
templates/en_systemdate_template_example.j2

@@ -0,0 +1,50 @@
+{% set day_of_week = {
+    "0": "sunday",
+    "1": "monday",
+    "2": "tuesday",
+    "3": "wednesday",
+    "4": "thursday",
+    "5": "friday",
+    "6": "saturday"
+    }[weekday] | default("")
+-%}
+
+{% set month_word = {"1": "january", "2": "february", "3": "march", "4": "april", "5": "may", "6": "june", "7": "july", "8": "august", "9": "september", "10": "october", "11": "november", "12": "december"}[month] | default("") -%}
+
+{% set day_month_formated = {
+    "1": "first",
+    "2": "second",
+    "3": "third",
+    "4": "fourth",
+    "5": "fifth",
+    "6": "sixth",
+    "7": "seventh",
+    "8": "eighth",
+    "9": "ninth",
+    "10": "tenth",
+    "11": "eleventh",
+    "12": "twelfth",
+    "13": "thirteenth",
+    "14": "fourteenth",
+    "15": "fifteenth",
+    "16": "sixteenth",
+    "17": "seventeenth",
+    "18": "eighteenth",
+    "19": "nineteenth",
+    "20": "twentieth",
+    "21": "twenty-first",
+    "22": "twenty-second",
+    "23": "twenty-third",
+    "24": "twenty-fourth",
+    "25": "twenty-fifth",
+    "26": "twenty-sixth",
+    "27": "twenty-seventh",
+    "28": "twenty-eighth",
+    "29": "twenty-ninth",
+    "30": "thirtieth",
+    "31": "thirty-first",
+
+}[day_month] | default("") -%}
+
+It' {{ hours }} hours and {{ minutes }} minutes.
+We are the {{ day_of_week }} {{ month_word }} the {{ day_month_formated }} {{ year }}

+ 5 - 1
templates/fr_systemdate_template_example.j2

@@ -1 +1,5 @@
-ma montre indique {{ hours }} heures et {{ minutes }} minutes
+{% set day_of_week = {"0": "dimanche", "1": "lundi", "2": "mardi", "3": "mercredi", "4": "jeudi", "5": "vendredi", "6": "samedi"}[weekday] | default("") -%}
+{% set month_word = {"1": "janvier", "2": "février", "3": "mars", "4": "avril", "5": "mai", "6": "juin", "7": "juillet", "8": "août", "9": "septembre", "10": "octobre", "11": "novembre", "12": "décembre"}[month] | default("") -%}
+
+Il est {{ hours }} heures et {{ minutes }} minutes.
+Nous sommes le {{ day_of_week }} {{ day_month }} {{ month_word }} {{ year }}

+ 7 - 15
test.py

@@ -4,6 +4,7 @@ import re
 from collections import Counter
 
 from core import OrderAnalyser
+from neurons import Systemdate
 from neurons.tasker_autoremote.tasker_autoremote import Tasker_autoremote
 
 logging.basicConfig()
@@ -11,21 +12,12 @@ logger = logging.getLogger("kalliope")
 logger.setLevel(logging.DEBUG)
 
 
-# # This does not work because of different encoding when using accent
-# from core import OrderAnalyser
-# # order = "kalliope régle le réveil pour sept heures et vingt minutes"
-# # order = "mais nous de la musique"
-#
-# order = "arrête la musique"
-# # order = order.decode('utf-8')
-# # print type(order)
-# oa = OrderAnalyser(order)
-#
-# oa.start()
-key = "mykey"
-message = "lost my phone"
-
-tk = Tasker_autoremote(key=key, message=message)
+
+order = "test heure"
+oa = OrderAnalyser(order)
+oa.start()
+
+