Tweet ENG: erinevus redaktsioonide vahel

Allikas: Lambda
Mine navigeerimisribale Mine otsikasti
Anier (arutelu | kaastöö)
Resümee puudub
Anier (arutelu | kaastöö)
 
(ei näidata sama kasutaja 10 vahepealset redaktsiooni)
1. rida: 1. rida:
<!-- Kõigepealt loe läbi praktikumiülesande spec, siis loe lisaks soovitusi ülesande programmeerimiseks ja näitekoode. -->
<!-- Kõigepealt loe läbi praktikumiülesande spec, siis loe lisaks soovitusi ülesande programmeerimiseks ja näitekoode. -->


= Ülesande spec: mida on tarvis teha =
= Specification =


On vaja kirjutada käsurea rakendus, mis võtab kasutajalt geograafilise asukoha nime
Write a command line application that takes a geographical location as a parameter (e.g. Tallinn, Estonia, Rapla, Harjumaa, etc) - that finds recent tweets from Twitter that are posted from this location and prints them out.
(näiteks Tallinn, Eesti, Rapla, Harjumaa, Nüpli vms), otsib seejärel twitteri blogisüsteemist
 
[http://www.twitter.com twitteri]
For example:
api-t kasutades välja viimased kirjed, mis on postitatud enam-vähem sellest piirkonnast ja trükib
need kirjed välja.
   
Näiteks:


     06.09.2008 altexor: Klaxons -a1 disco punk in tallinn. fun   
     06.09.2008 altexor: Klaxons -a1 disco punk in tallinn. fun   
16. rida: 12. rida:
     05.09.2008 nolecore: Too much Vana Tallinn....argggghhh     
     05.09.2008 nolecore: Too much Vana Tallinn....argggghhh     


Ülesanne annab kokku maksimaalselt 10 punkti. Põhiosa annab 5 punkti, täiendavad lisaks. Ilma
Assignment gives maximum 10 points. The main part 5pts and other from extra features. Assigment without the main part is not accepted.
töötava põhiosata lahendust ei aktsepteerita.


Sul võib olla huvitav ennast ise ka Twitter kasutajaks registreerida, kuid ülesande jaoks see kohustuslik ei ole.
It might be reasonable for you to register as Twitter user, although this is not mandatory for the assigment.


== Ülesande põhiosa detailid: kuidas otsing funktsioneerima peab ==
== Details of the main part: how it works ==


Sinu programm peab kasutama [http://www.twitter.com twitteri] API-t, vt
The program must use the [http://www.twitter.com Twitter] API - look for [http://apiwiki.twitter.com/Twitter-API-Documentation]
[http://apiwiki.twitter.com/Twitter-API-Documentation api seletusi ja juttu].


Selle API abil saad lihtsalt otsida twitteri kasutajate poolt twitteri profiili
You can easily look for tweets posted at a geographical location using this API. Notice that a Twitter user might not define his/her location correctly, but this is not your conecern. Twitter system tries to figure out geographical coordinates based on the location defined.
sisestatud asukohta geograafiliste koordinaatide järgi. Twitteri kasutaja ei
pruugi oma asukohta õigesti sisestada, aga see pole meie mure. Twitteri süstem püüab
kasutaja sisestatud asukoha nime järgi ise tema koordinaate välja rehkendada.


Api näited:
API examples:


* Kohapõhine otsing atomi formaadis: http://search.twitter.com/search.atom?geocode=59.438862,24.754472,10km
* Location search in ATOM format: http://search.twitter.com/search.atom?geocode=59.438862,24.754472,10km
* Kohapõhine otsing jsoni formaadis: http://search.twitter.com/search.json?geocode=59.438862,24.754472,10km
* Location search in JSON format: http://search.twitter.com/search.json?geocode=59.438862,24.754472,10km


Need URL-d annavad tagasi mingi mõistliku pikkusega listi viimastest sissekannetest
These URL-s give a list of tweets of reasonable size given the coordinates and radius around it.
antud koordinaatidel, antud raadiuse sees. Sina kasuta oma programmi väljundis sedasama listi,
nii palju, kui twitteri API vastuseid annab.


Kui sul ei ole spetsiaalset soovi kasutada jsoni formaati, siis tungiv soovitus on kasutada
If you don't have a special reason for using JSON format, then it is highly recommended to use the ATOM format (JSON is excellent for use within JavaScript)
siin ülesandes atomi formaati (json sobib väga hästi javascripti rakenduste jaoks).


Nagu näitest näed, peab sinu programm avama urli, mille lõpus on komadega eraldatud:
Your program has to open the URL which has fields separated by commas in the end:


* laiuskraad (59.438862)
* latitude (59.438862)
* pikkuskraad (24.754472)
* longitude (24.754472)
* raadius (10km)
* radius (10km)


== Google Maps API ==
== Google Maps API ==


Kuidas saada sisestatud asukoha (Eesti, Tallinn, Rapla jne) koordinaadid? Ülesandes pead siinkohas
How to get coordinates of a location given by user (Estonia, Tallinn, etc.)? You would have to use the Google Maps API:
kasutama Google mapsi API-t:


* [http://code.google.com/apis/maps/documentation/services.html#Geocoding_Direct Väike API kasutamisjuhend]
* [http://code.google.com/apis/maps/documentation/services.html#Geocoding_Direct A little manual]
* Näide XML formaadis: http://maps.google.com/maps/geo?output=xml&q=tallinn  
* Example in XML format: http://maps.google.com/maps/geo?output=xml&q=tallinn  
* Näide CSV formaadis: http://maps.google.com/maps/geo?output=csv&q=tallinn
* Example CSV format: http://maps.google.com/maps/geo?output=csv&q=tallinn
* Viimase (CSV) näite oodatav vastus:  200,4,59.438862,24.754472
* An expected result of a CSV query:  200,4,59.438862,24.754472


{| style="background-color:#FBEFEF;"
{| style="background-color:#FBEFEF;"
|-
|-
|'''NB!''' Kui vajutad ülaltoodud kahele näitelingile, saad tõenäoliselt vale tulemuse, kuhu on sisse kodeeritud hoopis veateade.<br/>Pead näite-urli kopeerima brauseri urli reale ja vajutama enter (või  kasutama hoopis mõnda käsurea-urlilugejat nagu curl), siis töötab ta OK.
'''NB!''' If you open the example links - you will probably get a wrong result with encoded error code.<br/>
You would have to copy-paste the url to your web browser and hit enter. (Or opening it from command line using CURL) - then it works OK.
|}
|}


Kuna Google CSV-formaat on oluliselt lihtsam, kui XML formaat, siis on siin ülesandes soovitav
The Google CSV format is far more simpler than XML. So it is recommended to use that.
kasutada seda.
 
CSV formaadi vastuse väljad on:


* Asukoha jaoks sobiv zoomi-aste (selle järgi saad pärast valida mõistliku raadiuse!)
The fields of a CSV response:
* OK/viga kood (200 on ok)
* laiuskraad
* pikkuskraad


NB! laiuskraad ja pikkuskraad tulevad teises järjekorras, kui twitter API neid sööb.
* Reasonable zoom level (you can use this for defining the radius later)
* Result code (200 is OK)
* Latitude
* Longitude


Siin kirjeldatud põhiosa annab niisiis 5 punkti ja tema korrektse töö korral saad praktikumi
NB! latitude and longitude com in inverted order in respect to Twitter API.
arvestatud.


== Ülesande esimene lisaosa: kohad.csv fail ==
Up to this it is the main part and it will give 5 points and you will get the assignment done.


== First extra assignment: kohad.csv file ==


See osa on täiendav jupp programmist, mille eesmärgiks on võimaldada
Additional feature of the program that is inteded for
* ise kohti (kodu, Tehnikaülikool, vanaema_juures jne) kirjeldada
* defining your own locations (home, University, etc)
* salvestada Google API antud vastuseid oma faili, et järgmine kord ei peaks uuesti küsima (kohalik cache).
* save Google API responses to it so the next time you dont have to query the API (local cache)


Selleks kasuta faili kohad.csv, mis peab olema sinu programmile kättesaadav (soovitavalt
The CSV file has to be available for you program and recommended to be located at the same folder where you program is. Plus it has to contain rows with the following field definitions:
samas kataloogis) ja peab koosnema CSV kujul ridadest


     ametlik_nimi,xkoord,ykoord,raadius_km,alternatiivnimi_1,...,alternatiivnimi_N
     official_name, x-coord, y-coord, radius_km, alternative_name_1, ..., alternative_name_N


või ridadest, kus koordinaadid ja raadius puudu:
or rows, where coordinates and radius are missing:


     ametlik_nimi,,,,alternatiivnimi_1,...,alternatiivnimi_N
     ametlik_nimi,,,,alternatiivnimi_1,...,alternatiivnimi_N
    official_name,,,,alternative_name_1, ..., alternative_name_N


kus
where
 
* ametliknimi on kohanimi, mida tuleks otsida (kodu asemel näiteks Tallinn, vanaema asemel näiteks Rapla jne)
* alternatiivnimi1,...,alternatiivnimi_N on nimed, mida kasutaja saab sisestada selle koha jaoks
* koordinaadid ja raadius on standardse tähendusega, kuid võivad olla esialgu puudu!


Sinu süsteemi algoritm kohad.csv kasutamiseks:
* official name is what you query for (Tallin instead of Home, Mustamäe instead of University)
* alternative names are the ones a user can query for, for that location
* coordinates are coordinates and can be undefined at first!


* Enne Google API kasutamist peab süsteem vaatama kõigepealt, kas antud kohanimi on selles failis.  
Algorithm for kohad.csv use:
* Kui jah, ja koordinaadid antud, siis kasutab neid koordinaate ja Google APIt ei kasuta.
* Kui jah, ja koordinaadid ei ole antud, siis:
** teeb Google API päringu ametliknimi kohta, leiab koordinaadid
** kirjutab need koordinaadid faili vastavale reale juurde: järgmine kord ei ole vaja enam Google API päringut teha.


* Check the local cache before query Google API
* If found use it, skip Google API
* If found but coordinates are missing:
** Query Google API with official_name and find the coordinates
** Save the coordinates to local cache so you don't have to hit Google API the next time


See moodul annab täielikult realiseerituna 2 punkti.
This module will give 2 points.


== Ülesande teine lisaosa: dataen.txt fail ==
== Second additional feature: dataen.txt file==




118. rida: 102. rida:
suures failis dataen.txt kodeeritud hinnangut elanikkonna suurusele selles kohas.
suures failis dataen.txt kodeeritud hinnangut elanikkonna suurusele selles kohas.


Fail on [http://www.world-gazetteer.com/dataen.zip siit downloaditav zip fail] 6.3M, lahti pakkides 23M.
An additional part of your program that uses dataen.txt file to calculate a reasonable radius. The file contains population info in a given location.


[http://www.world-gazetteer.com/wg.php?x=1129163518&men=stdl&lng=en&gln=xx&dat=32&srt=npan&col=aohdq  Siin on toodud formaadi seletus].
The file is a [http://www.world-gazetteer.com/dataen.zip downloadable ZIP] 6.3M, after extraction 23M.


See moodul annab täielikult realiseerituna 2 punkti.
[http://www.world-gazetteer.com/wg.php?x=1129163518&men=stdl&lng=en&gln=xx&dat=32&srt=npan&col=aohdq  Format explanation].


== Ülesande kolmas lisaosa: sorteerimine ==
This module will give 2 points.


See osa annab lisaks veel ühe punkti.
== Third extra feature: sorting ==


Sorteerimisega variandis käivitatakse progamm ühel järgmistest viisidest:
This part will give 1 point.
 
Sorting program will accept the following command line arguments:


* java findtweets Tallinn
* java findtweets Tallinn
138. rida: 124. rida:
nime järgi, kolmas sorteerib vastuslisti kirjutamiseaja järgi, neljas sisu järgi.
nime järgi, kolmas sorteerib vastuslisti kirjutamiseaja järgi, neljas sisu järgi.


Kõik neli varianti tuleb realiseerida.
The first example is the "normal" execution.
The second sorts and prints the results sorted based on the author name.
Third - based on submission date
Fourth - based on tweet content


== Ülesande punktid ==
You would have to implement all of these.


Kokkuvõtlikult:
== Scoring ==


* Põhiosa: 5
Scoring in conclusion:
* kohad.csv fail: 2
* dataen.txt fail: 2
* sorteerimine: 1


= Soovitusi ja ideid programmeerimiseks =
* Main part: 5
* kohad.csv file: 2
* dataen.txt file: 2
* sorting: 1


== Võrgust lugemine ==
= Recommendations and ideas =


== Reading from web ==


Kõigepealt soovitan teha eraldi katse selle
 
http://java.sun.com/docs/books/tutorial/networking/urls/readingURL.html
Learn the example program:
näiteprogrammiga.
http://java.sun.com/docs/books/tutorial/networking/urls/readingURL.


Kui ta ei suuda võrgust lugeda, siis on tõenäoline
Kui ta ei suuda võrgust lugeda, siis on tõenäoline
162. rida: 152. rida:
ühendust, vaid peab töötama läbi nn proxy.
ühendust, vaid peab töötama läbi nn proxy.


NB! On võimalik (kuid mitte kindel: proovi järgi!)
If you are unable to read from the web it is highly likely that your computer is not connected directly to the Internet has to do it through a proxy.
et kõik AK arvutiklasside arvutid peavad töötama
läbi proxy. Siis viidatud näiteprogramm arvutiklassides
võrgust lugeda otse ei suuda.  Loe [[TTÜ AK proxy]] kohta:
mida TTÜ AK arvutiklassides töötades vaja teha,
KUI proxy osutub vajalikuks.


Tõenäoliselt aga Sinu koduarvutis või kusagil mujal
NB! It is possible (but no for sure - try it!), that all computers in the class have to use the proxy. The given example program cannot read from the Web. read the [[TTÜ AK proxy]] tutorial: what you would have to do if it is necessary to use the proxy.
arvutis sellist piirangut ei ole ja näiteprogramm
suudab otse võrgust lugeda.


Kui sa ülaltoodud näiteprogrammi järgi url-i avamise ja sealt
You would probably not have this issue at home or some other location and stuff works.
lugemise oma programmi sisse paned, siis tuleb nende operatsioonide
ümber tingimata panna try { ... } catch (..) { ... }, täpselt
nagu failioperatsioonidegi ümber.


Lisaks võib sul olla huvitav ja kasulik lugeda [http://java.sun.com/docs/books/tutorial/networking/index.html  Sun-i tutoriali võrgu kasutamiseks]
Use try-catch block for I/O - this includes web access and local file access
 
This might be good reading [http://java.sun.com/docs/books/tutorial/networking/index.html  Sun tutorial for netowork I/O]


== Twitteri xml-data kasutamine ==
== Twitteri xml-data kasutamine ==
191. rida: 173. rida:
põhiosa täitmiseks pole seda massiivi isegi tingimata vaja.
põhiosa täitmiseks pole seda massiivi isegi tingimata vaja.


== dataen.txt faili kasutamine ==
== Using dataen.txt ==


Tudeng Sander saatis sellise kogemuse/soovituse, mida tasub järgida:
Student Sander sent additional hints:


Raadiuse arvutamisel populatsiooni järgi kasutasin algselt lihtsalt populatsiooni jagamist arvuga. Siis aga oli nii, et kui Tallinnale andis mõistliku raadiuse, siis New Yorgile andis suurema kui terve manner. Ent siis sain aru, et inimeste on linnades võrdeliselt pindalaga (pikkimõõde ruudus), mitte pikkimõõtmega. See tähendab, et raadiusega on võrdeline ruutjuur populatsioonist. Praegu kasutangi raadiuse hinnangu saamiseks  
Raadiuse arvutamisel populatsiooni järgi kasutasin algselt lihtsalt populatsiooni jagamist arvuga. Siis aga oli nii, et kui Tallinnale andis mõistliku raadiuse, siis New Yorgile andis suurema kui terve manner. Ent siis sain aru, et inimeste on linnades võrdeliselt pindalaga (pikkimõõde ruudus), mitte pikkimõõtmega. See tähendab, et raadiusega on võrdeline ruutjuur populatsioonist. Praegu kasutangi raadiuse hinnangu saamiseks  
Math.sqrt(populatsioon) / 40 <- leiutatud arv, esindab ruutjuurt keskmisest inimtihedusest asulates - selle järgi elab asulates keskmiselt 1600 inimest ühel ruutkilomeetril.


Muidugi Lapimaa ja Hiina asulate elanike tihedusel on vahe sees, aga enam-vähem annab mõistlikke numbreid. Tegin veel nii, et miinimum-raadiusest (5km) väiksemat ta ei anna, sest väikestest kohtadest on niikuinii vähe twiterdajaid, pole mõtet võimalusi vähendada.
I used a division by constant from population for computing the radius. This gave a reasonable result for Tallinn but not New York. So I figured the population is proportional with the area (longitutudal dimension squared) not the longitudal alone. Which means the radius is proportianal the square root of the population which I used for calculating the radius.
 
Math.sqrt(population) / 40 <- a constant figured out by trial-and-error


Although Lapland and China populations are different, this still gives a more-or-less reasonable results. Additionally I disallowed a radius smaller than 5km - plus it doesn't make sense to query tweets from poorly populated areas anyway.


== Näiteprogrammid ja muud ==
== Snippets and stuff ==


Sul võib olla kasu järgmistest näiteprogammidest:
These example programs can come in handy:


* [[splitproov.java | splitproov.java]] näiteprogramm sisendteksti tükeldamiseks
* [[splitproov.java | splitproov.java]] splitting an input text
* [[massiivproov.java | massiivproov.java]] näiteprogramm massiivide kasutamiseks
* [[massiivproov.java | massiivproov.java]] using arrays
* [[ioproov.java | ioproov.java]] näiteprogramm failide kasutamiseks
* [[ioproov.java | ioproov.java]] using files

Viimane redaktsioon: 8. oktoober 2010, kell 13:19


Specification

Write a command line application that takes a geographical location as a parameter (e.g. Tallinn, Estonia, Rapla, Harjumaa, etc) - that finds recent tweets from Twitter that are posted from this location and prints them out.

For example:

    06.09.2008 altexor: Klaxons -a1 disco punk in tallinn. fun   
    06.09.2008 Nath_Milliet: Just booked our plane tickets to go & 
      see Glenn in Tallinn on the 1st of november. I just can't wait :)
    05.09.2008 nolecore: Too much Vana Tallinn....argggghhh    

Assignment gives maximum 10 points. The main part 5pts and other from extra features. Assigment without the main part is not accepted.

It might be reasonable for you to register as Twitter user, although this is not mandatory for the assigment.

Details of the main part: how it works

The program must use the Twitter API - look for [1]

You can easily look for tweets posted at a geographical location using this API. Notice that a Twitter user might not define his/her location correctly, but this is not your conecern. Twitter system tries to figure out geographical coordinates based on the location defined.

API examples:

These URL-s give a list of tweets of reasonable size given the coordinates and radius around it.

If you don't have a special reason for using JSON format, then it is highly recommended to use the ATOM format (JSON is excellent for use within JavaScript)

Your program has to open the URL which has fields separated by commas in the end:

  • latitude (59.438862)
  • longitude (24.754472)
  • radius (10km)

Google Maps API

How to get coordinates of a location given by user (Estonia, Tallinn, etc.)? You would have to use the Google Maps API:

NB! If you open the example links - you will probably get a wrong result with encoded error code.
You would have to copy-paste the url to your web browser and hit enter. (Or opening it from command line using CURL) - then it works OK.

The Google CSV format is far more simpler than XML. So it is recommended to use that.

The fields of a CSV response:

  • Reasonable zoom level (you can use this for defining the radius later)
  • Result code (200 is OK)
  • Latitude
  • Longitude

NB! latitude and longitude com in inverted order in respect to Twitter API.

Up to this it is the main part and it will give 5 points and you will get the assignment done.

First extra assignment: kohad.csv file

Additional feature of the program that is inteded for

  • defining your own locations (home, University, etc)
  • save Google API responses to it so the next time you dont have to query the API (local cache)

The CSV file has to be available for you program and recommended to be located at the same folder where you program is. Plus it has to contain rows with the following field definitions:

   official_name, x-coord, y-coord, radius_km, alternative_name_1, ..., alternative_name_N

or rows, where coordinates and radius are missing:

   ametlik_nimi,,,,alternatiivnimi_1,...,alternatiivnimi_N
   official_name,,,,alternative_name_1, ..., alternative_name_N

where

  • official name is what you query for (Tallin instead of Home, Mustamäe instead of University)
  • alternative names are the ones a user can query for, for that location
  • coordinates are coordinates and can be undefined at first!

Algorithm for kohad.csv use:

  • Check the local cache before query Google API
  • If found use it, skip Google API
  • If found but coordinates are missing:
    • Query Google API with official_name and find the coordinates
    • Save the coordinates to local cache so you don't have to hit Google API the next time

This module will give 2 points.

Second additional feature: dataen.txt file

See osa on jälle täiendav jupp programmist, mis koha suuruse (ja sobiva raadiuse) hindamisel kasutab suures failis dataen.txt kodeeritud hinnangut elanikkonna suurusele selles kohas.

An additional part of your program that uses dataen.txt file to calculate a reasonable radius. The file contains population info in a given location.

The file is a downloadable ZIP 6.3M, after extraction 23M.

Format explanation.

This module will give 2 points.

Third extra feature: sorting

This part will give 1 point.

Sorting program will accept the following command line arguments:

  • java findtweets Tallinn
  • java findtweets Tallinn name
  • java findtweets Tallinn date
  • java findtweets Tallinn content

kus esimene variant on varemkirjeldatud ja harilik, teine aga sorteerib ja trükib vastuslisti autori nime järgi, kolmas sorteerib vastuslisti kirjutamiseaja järgi, neljas sisu järgi.

The first example is the "normal" execution. The second sorts and prints the results sorted based on the author name. Third - based on submission date Fourth - based on tweet content

You would have to implement all of these.

Scoring

Scoring in conclusion:

  • Main part: 5
  • kohad.csv file: 2
  • dataen.txt file: 2
  • sorting: 1

Recommendations and ideas

Reading from web

Learn the example program: http://java.sun.com/docs/books/tutorial/networking/urls/readingURL.

Kui ta ei suuda võrgust lugeda, siis on tõenäoline põhjus selles, et Sinu arvuti ei saa otse internetti ühendust, vaid peab töötama läbi nn proxy.

If you are unable to read from the web it is highly likely that your computer is not connected directly to the Internet has to do it through a proxy.

NB! It is possible (but no for sure - try it!), that all computers in the class have to use the proxy. The given example program cannot read from the Web. read the TTÜ AK proxy tutorial: what you would have to do if it is necessary to use the proxy.

You would probably not have this issue at home or some other location and stuff works.

Use try-catch block for I/O - this includes web access and local file access

This might be good reading Sun tutorial for netowork I/O

Twitteri xml-data kasutamine

Kuidas saada twitteri xml-vastusest vajalik info (kuupäev, nimi, sõnum)? Vaata twitteri xml-vastuse source! Otsi sealt oma programmiga järjest stringe:

  • <published>...</published> vahel olev string on kuupäev/kell
  • <title>...</title> vahel on string on sõnum
  • <name>... </name> vahel enne sulge olev string on autori nimi

Seejärel otsi jälle järgmist jne jne. Soovitan salvestada leitud stringid kahemõõtmelisse stringimassiivi, siis saab neid hiljem sorteerida. Ülesande põhiosa täitmiseks pole seda massiivi isegi tingimata vaja.

Using dataen.txt

Student Sander sent additional hints:

Raadiuse arvutamisel populatsiooni järgi kasutasin algselt lihtsalt populatsiooni jagamist arvuga. Siis aga oli nii, et kui Tallinnale andis mõistliku raadiuse, siis New Yorgile andis suurema kui terve manner. Ent siis sain aru, et inimeste on linnades võrdeliselt pindalaga (pikkimõõde ruudus), mitte pikkimõõtmega. See tähendab, et raadiusega on võrdeline ruutjuur populatsioonist. Praegu kasutangi raadiuse hinnangu saamiseks

I used a division by constant from population for computing the radius. This gave a reasonable result for Tallinn but not New York. So I figured the population is proportional with the area (longitutudal dimension squared) not the longitudal alone. Which means the radius is proportianal the square root of the population which I used for calculating the radius.

Math.sqrt(population) / 40 <- a constant figured out by trial-and-error

Although Lapland and China populations are different, this still gives a more-or-less reasonable results. Additionally I disallowed a radius smaller than 5km - plus it doesn't make sense to query tweets from poorly populated areas anyway.

Snippets and stuff

These example programs can come in handy: