How to track an Event, trademark, company or subject to know not only how much people is talking about it, but also analizying sentimentally what is being said to know whether people is talking positive or negative.
An example mixing public APIs from Twitter, Google Maps and APICulture.
This is the slides set I used for my talk at Mediterranea APIDays held in Madrid in
13. www.autentia.com
Google Maps API
Well known interface
Gives different layers
- Map
- Terrain
- Satellite
Place Points Of Interest
Group POIs
POI metadata
domingo 2 de junio de 13
14. www.autentia.com
Google Maps API use
<script
src="http://maps.google.com/maps/api/js?sensor=false"></script>
var
map;
function
initialize()
{
var
center
=
new
google.maps.LatLng(50,
20);
map
=
new
google.maps.Map(document.getElementById('map'),
{
zoom:
3,
center:
center,
mapTypeId:
google.maps.MapTypeId.ROADMAP
});
}
google.maps.event.addDomListener(window,
'load',
initialize);
domingo 2 de junio de 13
15. www.autentia.com
Google Maps API use
<script
src="http://maps.google.com/maps/api/js?sensor=false"></script>
var
map;
function
initialize()
{
var
center
=
new
google.maps.LatLng(50,
20);
map
=
new
google.maps.Map(document.getElementById('map'),
{
zoom:
3,
center:
center,
mapTypeId:
google.maps.MapTypeId.ROADMAP
});
}
google.maps.event.addDomListener(window,
'load',
initialize);
<div id="map-container">
<div id="map"></div>
</div>
domingo 2 de junio de 13
16. www.autentia.com
Google Maps API use
function
callEvents(){
var
searchQuery
=
$('#searchQuery').value;
$.ajax({
url:
'/sentimentz/pois/'
+
encodeURIComponent(searchQuery),
type:
'GET',
contentType:
'application/json',
success:
function(data){
setTimeout(function(){
putEvents(data)},
500);
},
error:
function(data,
status,
er){
//....
}
});
}
domingo 2 de junio de 13
17. www.autentia.com
Google Maps API use
function
callEvents(){
var
searchQuery
=
$('#searchQuery').value;
$.ajax({
url:
'/sentimentz/pois/'
+
encodeURIComponent(searchQuery),
type:
'GET',
contentType:
'application/json',
success:
function(data){
setTimeout(function(){
putEvents(data)},
500);
},
error:
function(data,
status,
er){
//....
}
});
}
[
{
"rate":0,
"msg":"Muy
buena
exposición
sobre
API
business
model
#APIDays",
"location":{
"latitude":40.44614387,
"longitude":-‐3.67365923
},
"id":340414678016593920,
"createAt":1369996158000
}
]
domingo 2 de junio de 13
18. www.autentia.com
Google Maps API use
function
putEvents(events){
var
bounds
=
new
google.maps.LatLngBounds();
var
markers
=
[];
document.getElementById("totalGeoTweets").innerHTML
=
events.length;
for
(var
i
=
0;
i
<
events.length;
i++)
{
var
event
=
events[i];
var
latLng
=
new
google.maps.LatLng(event.location.latitude,
event.location.longitude);
bounds.extend(latLng);
var
marker
=
new
google.maps.Marker({
position:
latLng,
map:
map,
});
markers.push(marker);
}
map.fitBounds(bounds);
var
markerCluster
=
new
MarkerClusterer(map,
markers);
}
domingo 2 de junio de 13
19. www.autentia.com
The Source: twitter
An open window to shout your
thougths...
... that everyone can listen
#hashtags
@users
queryStrings
Geolocalized
domingo 2 de junio de 13
22. www.autentia.com
Twitter: The search API
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<version>3.0.3</version>
</dependency>
domingo 2 de junio de 13
23. www.autentia.com
Twitter: The search API
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<version>3.0.3</version>
</dependency>
public
List<Event>
retrieveAllTweetsByQuery(String
hashtag)
throws
TwitterException,
MalformedURLException
{
Twitter
twitter
=
TwitterFactory.getInstance();
Query
query
=
new
Query(hashtag);
QueryResult
result;
List<Status>
tweets
=
new
ArrayList<Status>();
do
{
result
=
twitter.search(query);
tweets.addAll(result.getTweets());
}
while
((query
=
result.nextQuery())
!=
null);
}
domingo 2 de junio de 13
24. www.autentia.com
Twitter: The search API
public
List<Event>
retrieveAllTweetsByQuery(String
hashtag)
throws
TwitterException,
MalformedURLException
{
Twitter
twitter
=
getTwitterAccessor();
Query
query
=
new
Query(hashtag);
QueryResult
result;
List<Status>
tweets
=
new
ArrayList<Status>();
do
{
result
=
twitter.search(query);
tweets.addAll(result.getTweets());
}
while
((query
=
result.nextQuery())
!=
null);
}
domingo 2 de junio de 13
31. www.autentia.com
The missing piece
public
Sentiment
rate(Tweet
tweet)
{
logger.debug("Rating
sentimentally
tweet
"
+
tweet.getText());
Sentiment
sentiment;
try
{
sentiment
=
restTemplate.postForObject(serviceUrl,
tweet,
Sentiment.class);
}
catch
(Exception
ex)
{
logger.error("Could
not
rate
"
+
tweet
+
"
Error:
"
+
ex.getMessage(),
ex);
return
Sentiment.UNRATED;
}
logger.debug("Tweet
rated
sentimentally
as
:
"
+
sentiment);
return
sentiment;
}
domingo 2 de junio de 13
32. www.autentia.com
The missing piece
public
class
Sentiment
{
private
static
final
int
LEVEL_UNKNOWN
=
0;
public
static
final
Sentiment
UNRATED
=
new
Sentiment(LEVEL_UNKNOWN,
LEVEL_UNKNOWN,
"NEUTRO");
@JsonProperty("intensidad")
private
int
intensity
=
0;
@JsonProperty("certidumbre")
private
int
certainty
=
0;
@JsonProperty("ponderacion")
private
String
sentiment;
}
domingo 2 de junio de 13
33. www.autentia.com
The missing piece
public
class
Sentiment
{
private
static
final
int
LEVEL_UNKNOWN
=
0;
public
static
final
Sentiment
UNRATED
=
new
Sentiment(LEVEL_UNKNOWN,
LEVEL_UNKNOWN,
"NEUTRO");
@JsonProperty("intensidad")
private
int
intensity
=
0;
@JsonProperty("certidumbre")
private
int
certainty
=
0;
@JsonProperty("ponderacion")
private
String
sentiment;
}
{
"intensidad":
4,
"certidumbre":
4,
"ponderacion":
"POSITIVA",
"texto":
"wso2
api
manager
interesante
gestionar
apis
uso
http//kcyme/lqie
apidays"
}
domingo 2 de junio de 13
36. www.autentia.com
Some final considerations
Be careful with API invocation pace
(Some APIs have limits/prices)
Use Queues/Schedulers to calm API
calls traffic
Some Sentiment could not be reliable
Intruder tweets
Not spanish
Slang, arbbr, typos
domingo 2 de junio de 13
37. www.autentia.com
“Somos pocos, somos buenos, estamos
motivados y nos gusta lo que hacemos”
Thanks for listening
@dgomezg
@autentia
domingo 2 de junio de 13