| 1 | = [[https://is.muni.cz/auth/predmet/fi/pv277|PV277 Programming Applications for Social Robots]] (autumn 2019) = |
| 2 | |
| 3 | == How to get info from web? |
| 4 | |
| 5 | === Use API |
| 6 | If API exists, you can usually use JSON data after send the right parameters. Example to get weather data. |
| 7 | |
| 8 | {{{#!python |
| 9 | import requests |
| 10 | url = "http://api.openweathermap.org/data/2.5/weather" |
| 11 | |
| 12 | params = {'q': 'Brno', |
| 13 | 'units': 'metric', |
| 14 | 'lang': 'cz'} |
| 15 | |
| 16 | r = requests.get(url=url, params=params) |
| 17 | data = r.json() |
| 18 | |
| 19 | print(data['main']['temp']) |
| 20 | }}} |
| 21 | |
| 22 | (Full example [raw-attachment:forecast.py:wiki:en/ProgrammingRobotsCourse/GettingWwwInfo forecast.py] |
| 23 | ) |
| 24 | |
| 25 | === Parse webpage data |
| 26 | |
| 27 | If no API is provided, you can download and parse the webpage. |
| 28 | |
| 29 | {{{#!python |
| 30 | from bs4 import BeautifulSoup |
| 31 | import requests |
| 32 | page = requests.get(url).text |
| 33 | soup = BeautifulSoup(page, features="lxml") |
| 34 | results = soup.findAll('h2', attrs={'class':'article-title'}) |
| 35 | }}} |
| 36 | |
| 37 | (Full example [raw-attachment:news.py:wiki:en/ProgrammingRobotsCourse/GettingWwwInfo news.py]) |
| 38 | |
| 39 | == How to integrate info with robot? |
| 40 | |
| 41 | === Dialog variables |
| 42 | |
| 43 | In dialog file, set value of variable, use it in Python script in behavior. |
| 44 | |
| 45 | {{{#!python |
| 46 | u:(["Můj [oblíbený nejoblíbenější] tým je" "Fandím {týmu}"] _*) $team=$1 |
| 47 | }}} |
| 48 | |
| 49 | First word after sentence is available in {{{$1}}} and passed to the script as {{{team}}} variable. |
| 50 | |
| 51 | {{{#!python |
| 52 | def onInput_onStart(self, team): |
| 53 | self.team_usr = team |
| 54 | }}} |
| 55 | |
| 56 | === Use TTS in script |
| 57 | {{{#!python |
| 58 | veta = self.team_name + " má " + str(act_team.points) + " bodů" |
| 59 | self.tts.say(veta) |
| 60 | }}} |
| 61 | |
| 62 | === Run script as service and parse service output |
| 63 | |
| 64 | Specify service script in manifest.xml: |
| 65 | {{{#!xml |
| 66 | <services> |
| 67 | <service autorun="true" execStart="/usr/bin/python2 scripts/rozvrh.py" name="rozvrh" /> |
| 68 | </services> |
| 69 | }}} |
| 70 | |
| 71 | In dialog, detect variables, pass them to service, and parse call result to say answer: |
| 72 | |
| 73 | {{{ |
| 74 | u:(rozvrh) Pro jakou místnost bys chtěl znát rozvrh? |
| 75 | 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\ |
| 76 | c1:(In _* teaches _* course _*) V místnosti $1 je právě $3 s vyučujícím, který se jmenuje $2. |
| 77 | c1:(In _* currently _*) V místnosti $1 je právě $2 |
| 78 | }}} |
| 79 | |
| 80 | {{{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. |
| 81 | |
| 82 | {{{#!python |
| 83 | def get_current_lesson(self, letter, number): |
| 84 | room = letter+str(number) |
| 85 | lesson = self.rozvrh.find_current_lesson(room) |
| 86 | if lesson: |
| 87 | if lesson.teacher: |
| 88 | teacher = lesson.teacher.split(' ')[-1] |
| 89 | return "In %s teaches %s course %s" % (lesson.room, teacher, lesson.name) |
| 90 | else: |
| 91 | return "In %s currently %s" % (lesson.room, lesson.name) |
| 92 | }}} |
| 93 | |
| 94 | |
| 95 | === Run script as service and use TTS in script |
| 96 | |
| 97 | Detect variables in dialog, pass them to script and let script use TTS to say answer. |
| 98 | |
| 99 | {{{ |
| 100 | u:("[řekni ukaž zobraz najdi] {mi} [odjezdy spoje] ze zastávky _~station_name") |
| 101 | ^call(DialogKordisbot.say_answer1($1)) |
| 102 | }}} |
| 103 | |
| 104 | Dialog detect word with station name and calls {{{say_answer1()}}}. No answer is passed back, answer is said directly in script. |
| 105 | |
| 106 | {{{#!python |
| 107 | def say_answer1(self, station): |
| 108 | #get data finalDepartures |
| 109 | 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]) |
| 110 | |
| 111 | self.s.ALAnimatedSpeech.say(answer_msg) |
| 112 | }}} |
| 113 | |
| 114 | |
| 115 | == Examples |
| 116 | |
| 117 | Explore example applications in {{{/nlp/projekty/pepper/myapps}}}: |
| 118 | * kordisbot |
| 119 | * sport_bot |
| 120 | * rozvrh |