wiki:en/ProgrammingRobotsCourse/GettingWwwInfo

Version 18 (modified by xrambous, 4 years ago) (diff)

--

PV277 Programming Applications for Social Robots

How to get info from web?

Remember that Python usage in Pepper robots is limited to Python 2 and old version of packages. It is possible to install some (small) Python packages via pip --user to /home/nao but the space is limited.

If you want to test without the real robot, you can run the Python code using the virtual robot - see below.

Use API

If API exists, you can usually use JSON data after send the right parameters. Example to get weather data.

import requests
url = "http://api.openweathermap.org/data/2.5/weather"

params = {'q': 'Brno',
          'units': 'metric',
          'lang': 'cz'}

r = requests.get(url=url, params=params)
data = r.json()

print(data['main']['temp'])

(Full example forecast.py )

Parse webpage data

If no API is provided, you can download and parse the webpage.

from bs4 import BeautifulSoup
import requests
page = requests.get(url).text
soup = BeautifulSoup(page, features="lxml")
results = soup.findAll('h2', attrs={'class':'article-title'})

(Full example news.py)

How to integrate info with robot?

Dialog variables

In dialog file, set value of variable, use it in Python script in behavior.

concept:(team) [sparta spartě slavie slavii]
u:(["Můj [oblíbený nejoblíbenější] tým je" "Fandím {týmu}"] _~team) $team=$1

First word after sentence is available in $1 and passed to the script as team variable.

def onInput_onStart(self, team):
    self.team_usr = team

Use TTS in script

veta = self.team_name + " má " + str(act_team.points) + " bodů"
self.tts.say(veta)

Run script as service and parse service output

Specify service script in manifest.xml:

<services>
    <service autorun="true" execStart="/usr/bin/python2 scripts/rozvrh.py" name="rozvrh" />
</services>

In dialog, detect variables, pass them to service, and parse the call result to say answer:

u:(rozvrh) Pro jakou místnost bys chtěl znát rozvrh?
    u1:({pro} {místnost učebnu} _~letter _~number) Podívám se na rozvrh pro $1 $2 ^call(Rozvrh.get_current_lesson($1, $2)) \pau=500\
    c1:(In _* teaches _* course _*) V místnosti $1 je právě $3 s vyučujícím, který se jmenuje $2.
    c1:(In _* currently _*) V místnosti $1 je právě $2

Note that in c1: we can use _* since this regular expression is matched against text from the service, not against human speech. The text can be in English (or even in non-language), the answer to humans is then specified in the concrete dialog topic, e.g. in Czech. Unfortunately, the documentation is not clear in what can and what cannot be matched by _* here, tests are needed with real data.

u1 detects letter and number (concepts defined earlier in dialog) and calls get_current_lesson(). This function return strings, that are parsed in dialog and answer is translated to each locale.

def get_current_lesson(self, letter, number):
    room = letter+str(number)
    lesson = self.rozvrh.find_current_lesson(room)
    if lesson:
        if lesson.teacher:
            teacher = lesson.teacher.split(' ')[-1]
            return "In %s teaches %s course %s" % (lesson.room, teacher, lesson.name)
         else:
            return "In %s currently %s" % (lesson.room, lesson.name)

Run script as service and use TTS in script

Detect variables in dialog, pass them to script and let script use TTS to say answer.

u:("[řekni ukaž zobraz najdi] {mi} [odjezdy spoje] ze zastávky _~station_name")
    ^call(DialogKordisbot.say_answer1($1))

Dialog detects word with station name and calls say_answer1(). No answer is passed back, answer is said directly in script.

def say_answer1(self, station):
    #get data finalDepartures
    answer_msg = "First line {} goes to {} at {}, second line {} goes to {} at {}, third line {} goes to {} at {}".format(finalDepartures[0][0], finalDepartures[0][1], finalDepartures[0][2], finalDepartures[1][0], finalDepartures[1][1], finalDepartures[1][2], finalDepartures[2][0], finalDepartures[2][1], finalDepartures[2][2])

    self.s.ALAnimatedSpeech.say(answer_msg)

Test service with virtual robot

Examples

Explore example applications in /nlp/projekty/pepper/myapps:

  • kordisbot
  • sport_bot
  • rozvrh

Example app

  • Create new project in Choregraphe.
  • As usual, add Czech in project Properties.
  • Add Set Language box and select Czech.
  • Right click the free area -> Create a new box -> Dialog...
  • in the Dialog -> Add Topic - choose Czech and Add to the package content as collaborative dialog (allows to start the dialog just by talking to the robot)
  • Connect onStart -> Set Language -> Dialog
  • Right click dialog box -> Edit box. Find Outputs and click on + button. Name: country, Type: string, Nature: punctual

  • In Project files double click on dialog_czc.top and enter

Attachments (7)

Download all attachments as: .zip