浏览代码

Merge pull request #18 from kalliope-project/wikipedia

Wikipedia
Nicolas Marcq 8 年之前
父节点
当前提交
ab4a52ca4d

+ 1 - 15
Docs/dev_env_install.md

@@ -21,21 +21,7 @@ sudo apt-get install python-pip python-dev libsmpeg0 libttspico-utils libsmpeg0
 
 Install libs
 ```
-pip install SpeechRecognition
-pip install pyaudio
-pip install ansible
-pip install pygame
-pip install python2-pythondialog
-pip install jinja
-pip install python-crontab
-pip install cffi
-pip install pygmail
-pip install pushetta
-pip install wakeonlan
-pip install ipaddress
-pip install pyowm
-pip install flask
-pip install python-twitter==3.1
+sudo pip install -r install/files/python_requirements.txt
 ```
 
 ### Test your env

+ 6 - 5
Docs/neuron_list.md

@@ -6,14 +6,15 @@ A neuron is a module that will perform some actions attached to an order. You ca
 |----------------------------------------------------|-----------------------------------------------------------------------------------------|-------------------|
 | [ansible_task](../neurons/ansible_task/)           | Run an ansible playbook                                                                 | ansible_task      |
 | [gmail_checker](../neurons/gmail_checker/)         | Get the number of unread email and their subjects from a gmail account                  | gmail_checker     |
-| [kill_switch](../neurons/kill_switch/)             | Stop Kalliope process                                                                   | kill_switch       |
-| [neurotransmitter](../neurons/neurotransmitter/)   | Link synapses together                                                                  | neurotransmitter  |
+| [kill_switch](../neurons/kill_switch/)             | Stop Kalliope process                                                                     | kill_switch       |
+| [neurotransmitter](../neurons/neurotransmitter/)   | Link synapse together                                                                   | neurotransmitter  |
 | [push_message](../neurons/push_message/)           | Send a push message to a remote device like Android/iOS/Windows Phone or Chrome browser | push_message      |
-| [say](../neurons/say/)                             | Make Kalliope talk by using TTS                                                         | say               |
+| [say](../neurons/say/)                             | Make Kalliope talk by using TTS                                                           | say               |
 | [script](../neurons/script/)                       | Run an executable script                                                                | script            |
 | [shell](../neurons/command/)                       | Run a shell command                                                                     | shell             |
-| [sleep](../neurons/sleep/)                         | Make Kalliope sleep for a while before continuing                                       | sleep             |
+| [sleep](../neurons/sleep/)                         | Make Kalliope sleep for a while before continuing                                         | sleep             |
 | [systemdate](../neurons/systemdate/)               | Give the local system date and time                                                     | systemdate        |
 | [tasker_autoremote](../neurons/tasker_autoremote/) | Send a message to Android tasker app                                                    | tasker_autoremote |
-| [twitter](../neurons/twitter/)                     | Send a Twit from Kalliope                                                               | twitter           |
+| [twitter](../neurons/twitter/)                     | Send a Twit from kalliope                                                               | twitter           |
+| [wikipedia](../neurons/wikipedia/)                 | Search for a page on Wikipedia                                                          | wikipedia         |
 

+ 5 - 5
Docs/stt.md

@@ -28,11 +28,11 @@ speech_to_text:
 
 ## Current Available STT
 
-[apiai](../stt/apiai/README.md)
-[bing](../stt/bing/README.md)
-[google](../stt/google/README.md)
-[houndify](../stt/houndify/README.md)
-[witai](../stt/wit/README.md)
+- [apiai](../stt/apiai/README.md)
+- [bing](../stt/bing/README.md)
+- [google](../stt/google/README.md)
+- [houndify](../stt/houndify/README.md)
+- [witai](../stt/wit/README.md)
 
 ## Full Example
 

+ 28 - 3
Docs/trigger.md

@@ -1,6 +1,31 @@
+# Triggers
 
+With Kalliope project, you can set whatever Hotword you want to wake it up. 
 
-With Kalliope project, you can set whatever Hotword you want to wake it up. You can create your magic word by connecting to [Snowboy](https://snowboy.kitt.ai/) 
-and then download the trained model file.
 
-Once downloaded, place the file in **trigger/snowboy/resources**.
+# Snowboy
+
+You can create your magic word by connecting to [Snowboy](https://snowboy.kitt.ai/) and then download the trained model file.
+
+Once downloaded:
+- place the file in **trigger/snowboy/resources**.
+- update the path in [your settings](settings.md).
+
+If you want to keep "Kalliope" as the name of your bot, we recommend you to enhance the existing Snowboy model for your language.
+
+We will update the following list with all Kalliope model created by the community. If the model doesn't exist, please create one with the following syntax:
+```
+kalliope-<language_code>
+```
+
+E.g
+```
+kalliope-en
+```
+Then, open an issue or create a pull request to add the model to the list bellow.
+
+## List of available Snowboy Kalliope model
+
+| Name                                                | language |
+|-----------------------------------------------------|----------|
+| [kalliope-fr](https://snowboy.kitt.ai/hotword/1363) | French   |

+ 1 - 1
README.md

@@ -42,7 +42,7 @@ Kalliope is easy-peasy to use, see the hello world
 | [Signals](Docs/signals.md)         | Signals are input event that can wake up kalliope (spoken order, scheduled event, REST API) |
 | [STT](Docs/stt.md)                 | Speech to text configuration                                                                |
 | [TTS](Docs/tts.md)                 | Text to speech configuration                                                                |
-| [Triggers](Docs/tts.md)            | Magic hotword engine used to make Kalliope listtening for an order                          |
+| [Triggers](Docs/trigger.md)        | Magic hotword engine used to make Kalliope listtening for an order                          |
 
 
 ## Contributing

+ 1 - 0
brain.yml

@@ -13,3 +13,4 @@
   - brains/wake_on_lan.yml
   - brains/twitter.yml
   - brains/neurotransmitter.yml
+  - brains/wikipedia.yml

+ 11 - 0
brains/wikipedia.yml

@@ -0,0 +1,11 @@
+---
+
+  - name: "wikipedia-search"
+    neurons:
+      - wikipedia:
+          language: "fr"
+          args:
+            - query
+          file_template: "wikipedia_returned_value.j2"
+    signals:
+      - order: "cherche sur Wikipédia {{ query }}"

+ 2 - 1
install/files/python_requirements.txt

@@ -12,4 +12,5 @@ wakeonlan==0.2.2
 ipaddress==1.0.16
 pyowm==2.5.0
 python-twitter==3.1
-flask==0.11.1
+flask==0.11.1
+wikipedia==1.4.0

+ 2 - 0
neurons/__init__.py

@@ -12,3 +12,5 @@ from tasker_autoremote import Tasker_autoremote
 from wake_on_lan import Wake_on_lan
 from twitter import Twitter
 from neurotransmitter import Neurotransmitter
+from wikipedia import Wikipedia
+

+ 68 - 0
neurons/wikipedia/README.md

@@ -0,0 +1,68 @@
+# wikipedia
+
+## Synopsis
+
+Get the summary of a Wikipedia page.
+
+## Options
+
+| parameter | required | default | choices                     | comment                                                                                                                           |
+|-----------|----------|---------|-----------------------------|-----------------------------------------------------------------------------------------------------------------------------------|
+| language  | yes      |         | E.g: "fr", "en", "it", "es" | See the list of available language in the "Note" section                                                                          |
+| query     | yes      |         |                             | The wikipedia page you are looking for.  This parameter can be passed as an argument in the neuron from the order with {{ query}} |
+| sentences | no       | 10      | Integer in range 1-10       | if set, return the first number of sentences(can be no greater than 10) specified in this parameter.                              |
+
+
+## Return Values
+
+| Name       | Description                             | Type   | sample                                                                                                                              |
+|------------|-----------------------------------------|--------|-------------------------------------------------------------------------------------------------------------------------------------|
+| summary    | Plain text summary of the searched page | string |  Wikipedia is a collaboratively edited, multilingual, free Internet encyclopedia supported by the non-profit Wikimedia Foundation.. |
+| returncode | Error code. See bellow                  | string | SummaryFound                                                                                                                        |
+| may_refer  | List of pages that can refer the query  | list   | ['Marc Le Bot', 'Bot', 'Jean-Marc Bot', 'bot', 'pied bot', 'robot', 'Sam Bot', 'Famille Both', 'Yves Bot', 'Ben Bot', 'Botswana']   |
+
+
+| returncode          | Description                             |
+|---------------------|-----------------------------------------|
+| SummaryFound        | A summary hs been found from the querry |
+| DisambiguationError | The query match more than ony one page. |
+| PageError           | No Wikipedia matched a query            |
+
+## Synapses example
+
+This synapse will look for the {{ query }} spelt by the user on Wikipedia
+```
+- name: "wikipedia-search"
+    neurons:
+      - wikipedia:
+          language: "en"
+          args:
+            - query
+          file_template: "wikipedia_returned_value.j2"
+    signals:
+      - order: "look on wikipedia {{ query }}"
+```
+
+## Templates example 
+
+This template will simply make Kalliope speak out loud the summary section of the Wikipédia page of the query.
+If the query match more than one page, Kaliope will give the user all matched pages.
+If the query doesn't match any page on Wikipedia, kalliope will notify the user.
+```
+{% if returncode == "DisambiguationError" %}
+    The query match following pages    
+    {% if may_refer is not none %}
+        {% for page in may_refer %}
+            {{ page }}
+        {% endfor %}
+    {% endif %}
+{% elif returncode == "PageError" %}
+    I haven't  found anything on this
+{% else %}
+    {{ summary }}
+{% endif %}
+```
+
+## Notes
+
+Available languages in [the detailed list of the offical Wikipedia page](https://en.wikipedia.org/wiki/List_of_Wikipedias#Detailed_list). The column is called "Wiki". E.g: "en"

+ 72 - 0
neurons/wikipedia/Wikipedia.py

@@ -0,0 +1,72 @@
+import logging
+
+from core.NeuronModule import NeuronModule, InvalidParameterException
+import wikipedia
+
+logging.basicConfig()
+logger = logging.getLogger("kalliope")
+
+
+class Wikipedia(NeuronModule):
+    def __init__(self, **kwargs):
+        # we don't need the TTS cache for this neuron
+        cache = kwargs.get('cache', None)
+        if cache is None:
+            cache = False
+            kwargs["cache"] = cache
+        super(Wikipedia, self).__init__(**kwargs)
+
+        # get parameters form the neuron
+        self.query = kwargs.get('query', None)
+        self.language = kwargs.get('language', None)
+        self.sentences = kwargs.get('sentences', None)
+
+        self.may_refer = None
+        self.returncode = None
+
+        # check parameters
+        if self._is_parameters_ok():
+            # set the language
+            wikipedia.set_lang(self.language)
+            # do the summary search
+            try:
+                summary = wikipedia.summary(self.query, auto_suggest=True, sentences=self.sentences)
+                # if we are here, no exception raised, we got a summary
+                self.returncode = "SummaryFound"
+            except wikipedia.exceptions.DisambiguationError, e:
+                # Exception raised when a page resolves to a Disambiguation page.
+                # The options property contains a list of titles of Wikipedia pages that the query may refer to.
+                self.may_refer = e.options
+                # Removing duplicates in lists.
+                self.may_refer = list(set(self.may_refer))
+                self.returncode = "DisambiguationError"
+                summary = ""
+            except wikipedia.exceptions.PageError, e:
+                # Exception raised when no Wikipedia matched a query.
+                self.returncode = "PageError"
+                summary = ""
+
+            message = {
+                "summary": summary,
+                "may_refer": self.may_refer,
+                "returncode": self.returncode
+            }
+            logger.debug("Wikipedia returned message: %s" % str(message))
+
+            self.say(message)
+
+    def _is_parameters_ok(self):
+        """
+        Check if received parameters are ok to perform operations in the neuron
+        :return:
+        """
+        if self.query is None:
+            raise InvalidParameterException("Wikipedia needs a query")
+        if self.language is None:
+            raise InvalidParameterException("Wikipedia needs a language")
+
+        valid_language = wikipedia.languages().keys()
+        if self.language not in valid_language:
+            raise InvalidParameterException("Wikipedia needs a valid language: %s" % valid_language)
+
+        return True

+ 1 - 0
neurons/wikipedia/__init__.py

@@ -0,0 +1 @@
+from Wikipedia import Wikipedia

+ 12 - 0
templates/wikipedia_returned_value.j2

@@ -0,0 +1,12 @@
+{% if returncode == "DisambiguationError" %}
+    Veuillez etre plus précis. La recherche que vous venez d'éffectuez peut correspondre au page Wikipédia suivante:
+    {% if may_refer is not none %}
+        {% for el in may_refer %}
+            {{ el }}
+        {% endfor %}
+    {% endif %}
+{% elif returncode == "PageError" %}
+    Je n'ai rien trouvé la dessus
+{% else %}
+    {{ summary }}
+{% endif %}

+ 15 - 4
test.py

@@ -21,17 +21,28 @@ logger.setLevel(logging.DEBUG)
 # oa = OrderAnalyser(order=order)
 # oa.start()
 
-SettingLoader.get_settings()
-
+# SettingLoader.get_settings()
+#
 brain = BrainLoader.get_brain()
 
-order = "pose moi une question"
-# order = "synapse2"
+order = "cherche sur Wikipédia bot"
+
 oa = OrderAnalyser(order=order, brain=brain)
 
 oa.start()
 
 
+# import wikipedia
+#
+# languages = wikipedia.languages().keys()
+# languages = sorted(languages)
+# for el in languages:
+#     print "- " + el
+
+
+
+
+