Jelajahi Sumber

add wikipedia neuron

nico 8 tahun lalu
induk
melakukan
07507266b3

+ 1 - 15
Docs/dev_env_install.md

@@ -19,21 +19,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

+ 2 - 1
Docs/neuron_list.md

@@ -7,7 +7,7 @@ A neuron is a module you can use in your synapses. See the [complete neuron docu
 | [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 Jarvis process                                                                     | kill_switch       |
-| [neurotransmitter](../neurons/neurotransmitter/)   | Link synapses together                                                                  | neurotransmitter  |
+| [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 Jarvis talk by using TTS                                                           | say               |
 | [script](../neurons/script/)                       | Run an executable script                                                                | script            |
@@ -16,4 +16,5 @@ A neuron is a module you can use in your synapses. See the [complete neuron docu
 | [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           |
+| [wikipedia](../neurons/wikipedia/)                 | Search for a page on Wikipedia                                                          | wikipedia         |
 

+ 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
+

+ 488 - 0
neurons/wikipedia/README.md

@@ -0,0 +1,488 @@
+# 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
+
+Complete list of available languages:
+
+- aa
+- ab
+- ace
+- ady
+- ady-cyrl
+- aeb
+- aeb-arab
+- aeb-latn
+- af
+- ak
+- aln
+- als
+- am
+- an
+- ang
+- anp
+- ar
+- arc
+- arn
+- arq
+- ary
+- arz
+- as
+- ase
+- ast
+- av
+- avk
+- awa
+- ay
+- az
+- azb
+- ba
+- ban
+- bar
+- bat-smg
+- bbc
+- bbc-latn
+- bcc
+- bcl
+- be
+- be-tarask
+- be-x-old
+- bg
+- bgn
+- bh
+- bho
+- bi
+- bjn
+- bm
+- bn
+- bo
+- bpy
+- bqi
+- br
+- brh
+- bs
+- bto
+- bug
+- bxr
+- ca
+- cbk-zam
+- cdo
+- ce
+- ceb
+- ch
+- cho
+- chr
+- chy
+- ckb
+- co
+- cps
+- cr
+- crh
+- crh-cyrl
+- crh-latn
+- cs
+- csb
+- cu
+- cv
+- cy
+- da
+- de
+- de-at
+- de-ch
+- de-formal
+- diq
+- dsb
+- dtp
+- dty
+- dv
+- dz
+- ee
+- egl
+- el
+- eml
+- en
+- en-ca
+- en-gb
+- eo
+- es
+- et
+- eu
+- ext
+- fa
+- ff
+- fi
+- fit
+- fiu-vro
+- fj
+- fo
+- fr
+- frc
+- frp
+- frr
+- fur
+- fy
+- ga
+- gag
+- gan
+- gan-hans
+- gan-hant
+- gd
+- gl
+- glk
+- gn
+- gom
+- gom-deva
+- gom-latn
+- got
+- grc
+- gsw
+- gu
+- gv
+- ha
+- hak
+- haw
+- he
+- hi
+- hif
+- hif-latn
+- hil
+- ho
+- hr
+- hrx
+- hsb
+- ht
+- hu
+- hy
+- hz
+- ia
+- id
+- ie
+- ig
+- ii
+- ik
+- ike-cans
+- ike-latn
+- ilo
+- inh
+- io
+- is
+- it
+- iu
+- ja
+- jam
+- jbo
+- jut
+- jv
+- ka
+- kaa
+- kab
+- kbd
+- kbd-cyrl
+- kg
+- khw
+- ki
+- kiu
+- kj
+- kk
+- kk-arab
+- kk-cn
+- kk-cyrl
+- kk-kz
+- kk-latn
+- kk-tr
+- kl
+- km
+- kn
+- ko
+- ko-kp
+- koi
+- kr
+- krc
+- kri
+- krj
+- ks
+- ks-arab
+- ks-deva
+- ksh
+- ku
+- ku-arab
+- ku-latn
+- kv
+- kw
+- ky
+- la
+- lad
+- lb
+- lbe
+- lez
+- lfn
+- lg
+- li
+- lij
+- liv
+- lki
+- lmo
+- ln
+- lo
+- loz
+- lrc
+- lt
+- ltg
+- lus
+- luz
+- lv
+- lzh
+- lzz
+- mai
+- map-bms
+- mdf
+- mg
+- mh
+- mhr
+- mi
+- min
+- mk
+- ml
+- mn
+- mo
+- mr
+- mrj
+- ms
+- mt
+- mus
+- mwl
+- my
+- myv
+- mzn
+- na
+- nah
+- nan
+- nap
+- nb
+- nds
+- nds-nl
+- ne
+- new
+- ng
+- niu
+- nl
+- nl-informal
+- nn
+- no
+- nov
+- nrm
+- nso
+- nv
+- ny
+- oc
+- olo
+- om
+- or
+- os
+- pa
+- pag
+- pam
+- pap
+- pcd
+- pdc
+- pdt
+- pfl
+- pi
+- pih
+- pl
+- pms
+- pnb
+- pnt
+- prg
+- ps
+- pt
+- pt-br
+- qu
+- qug
+- rgn
+- rif
+- rm
+- rmy
+- rn
+- ro
+- roa-rup
+- roa-tara
+- ru
+- rue
+- rup
+- ruq
+- ruq-cyrl
+- ruq-latn
+- rw
+- sa
+- sah
+- sat
+- sc
+- scn
+- sco
+- sd
+- sdc
+- sdh
+- se
+- sei
+- ses
+- sg
+- sgs
+- sh
+- shi
+- shi-latn
+- shi-tfng
+- shn
+- si
+- simple
+- sk
+- sl
+- sli
+- sm
+- sma
+- sn
+- so
+- sq
+- sr
+- sr-ec
+- sr-el
+- srn
+- ss
+- st
+- stq
+- su
+- sv
+- sw
+- szl
+- ta
+- tcy
+- te
+- tet
+- tg
+- tg-cyrl
+- tg-latn
+- th
+- ti
+- tk
+- tl
+- tly
+- tn
+- to
+- tokipona
+- tpi
+- tr
+- tru
+- ts
+- tt
+- tt-cyrl
+- tt-latn
+- tum
+- tw
+- ty
+- tyv
+- tzm
+- udm
+- ug
+- ug-arab
+- ug-latn
+- uk
+- ur
+- uz
+- uz-cyrl
+- uz-latn
+- ve
+- vec
+- vep
+- vi
+- vls
+- vmf
+- vo
+- vot
+- vro
+- wa
+- war
+- wo
+- wuu
+- xal
+- xh
+- xmf
+- yi
+- yo
+- yue
+- za
+- zea
+- zh
+- zh-classical
+- zh-cn
+- zh-hans
+- zh-hant
+- zh-hk
+- zh-min-nan
+- zh-mo
+- zh-my
+- zh-sg
+- zh-tw
+- zh-yue
+- zu

+ 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
+
+
+
+
+