DIC LAB 1 Problem 3 : Summarizing trending topics about a location

Define the limit of numbers to be searched


In [149]:
LIMIT = 10

Define the location search string


In [150]:
locationSearchString = "Los Angeles"

Define the location search string as latitude and longitude.

NOTE : 1 Latitude first Longitude Second, that should be the order and only one space separated.

2. Make sure one string is empty i.e locationSearchString or latlngSearchString


In [151]:
latlngSearchString = ""#"34.05223 -118.2437"

Define all the libraries which needs to be set for operations here


In [152]:
library("twitteR")
library("DBI")
library("RSQLite")
library("gtools")
library("bitops")
library("ggplot2")
library("RCurl")
library("RJSONIO")
library("ggmap")
library("sp")
library("mapdata")
library("maptools")
library("scales")
library("maps")
Sys.setlocale(category = "LC_ALL", locale = "C")


'LC_CTYPE=C;LC_NUMERIC=C;LC_TIME=C;LC_COLLATE=C;LC_MONETARY=C;LC_MESSAGES=en_US.UTF-8;LC_PAPER=en_US.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=C'

Setup the twitter app key for authentication


In [153]:
setup_twitter_oauth('YOUR_KEY')


[1] "Using direct authentication"

Following function fetches the state and country from the address_components field in json response .Following is the response

"address_components" : [ { "long_name" : "Buffalo", "short_name" : "Buffalo", "types" : [ "locality", "political" ] }, { "long_name" : "Erie County", "short_name" : "Erie County", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "New York", "short_name" : "NY", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "United States", "short_name" : "US", "types" : [ "country", "political" ] } ],

It will generate the state as New York and country as United States.


In [154]:
parseAddressComponents <- function(addr_comp){
    result = tryCatch(
        {
            addrSize = length(addr_comp)
            stateName = ""
            countryName = ""
            for(i in 1:addrSize){
                if(addr_comp[[i]]$types[[1]] == "administrative_area_level_1"){
                    stateName = addr_comp[[i]]$long_name
                }
                if(addr_comp[[i]]$types[[1]] == "country"){
                    countryName = addr_comp[[i]]$long_name
                }
            }
            if(stateName == ""){
                stateName = NA
            }
            if(countryName == ""){
                countryName = NA
            }
            return (c(stateName,countryName))
        },
        error=function(cond) {
            return (c(NA,NA))
        },
        warning=function(cond) {
        },
        finally={
        }
    )
    return (result);
}

This function generates the url which will be used to query the Google server. You have to pass the address for this function


In [155]:
getGeoCodeUrl <- function(address, return.call = "json", sensor = "false") {
    root <- "https://maps.google.com/maps/api/geocode/"
    url <- paste(root, return.call, "?address=", address,"&key=YOUR_KEY","&sensor=", sensor, sep = "")
    return(URLencode(url))
}

This function generates the url which will be used to query the Google server. You have to pass the latitude and longitude for this function


In [156]:
getLatLngURL <- function(lat, lng, return.call = "json", sensor = "false") {
    root <- "https://maps.google.com/maps/api/geocode/"
    latlng = paste(lat, lng, sep = ",")
    url <- paste(root, return.call, "?address=", latlng,"&key=AIzaSyCskJdqaLuNJBJ58IHDqfsjJAm9jkZbcKM","&sensor=", sensor, sep = "")
    return(URLencode(url))
}

The below code decomposes the json response from the Google Map API code

The output response is as follows for location with latitude and longitude : 42.88644679999999, -78.8783689

{ "results" : [ { "address_components" : [ { "long_name" : "Erie County Savings Bank", "short_name" : "Erie County Savings Bank", "types" : [ "premise" ] }, { "long_name" : "Downtown", "short_name" : "Downtown", "types" : [ "neighborhood", "political" ] }, { "long_name" : "Buffalo", "short_name" : "Buffalo", "types" : [ "locality", "political" ] }, { "long_name" : "Erie County", "short_name" : "Erie County", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "New York", "short_name" : "NY", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "United States", "short_name" : "US", "types" : [ "country", "political" ] }, { "long_name" : "14202", "short_name" : "14202", "types" : [ "postal_code" ] } ], "formatted_address" : "Erie County Savings Bank, Buffalo, NY 14202, USA", "geometry" : { "location" : { "lat" : 42.88639, "lng" : -78.878609 }, "location_type" : "ROOFTOP", "viewport" : { "northeast" : { "lat" : 42.88773898029149, "lng" : -78.8772600197085 }, "southwest" : { "lat" : 42.8850410197085, "lng" : -78.87995798029151 } } }, "place_id" : "ChIJtaBVzUsS04kRGnVU6uwosf8", "types" : [ "premise" ] }, { "address_components" : [ { "long_name" : "Cargill Pool Elevator", "short_name" : "Cargill Pool Elevator", "types" : [ "premise" ] }, { "long_name" : "Downtown", "short_name" : "Downtown", "types" : [ "neighborhood", "political" ] }, { "long_name" : "Buffalo", "short_name" : "Buffalo", "types" : [ "locality", "political" ] }, { "long_name" : "Erie County", "short_name" : "Erie County", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "New York", "short_name" : "NY", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "United States", "short_name" : "US", "types" : [ "country", "political" ] }, { "long_name" : "14202", "short_name" : "14202", "types" : [ "postal_code" ] } ], "formatted_address" : "Cargill Pool Elevator, Buffalo, NY 14202, USA", "geometry" : { "location" : { "lat" : 42.88639, "lng" : -78.87860999999999 }, "location_type" : "ROOFTOP", "viewport" : { "northeast" : { "lat" : 42.88773898029149, "lng" : -78.87726101970848 }, "southwest" : { "lat" : 42.8850410197085, "lng" : -78.87995898029149 } } }, "place_id" : "ChIJkadVzUsS04kRDBu5kuqtuiU", "types" : [ "premise" ] }, { "address_components" : [ { "long_name" : "5", "short_name" : "5", "types" : [ "street_number" ] }, { "long_name" : "Niagara Square", "short_name" : "Niagara Square", "types" : [ "route" ] }, { "long_name" : "Downtown", "short_name" : "Downtown", "types" : [ "neighborhood", "political" ] }, { "long_name" : "Buffalo", "short_name" : "Buffalo", "types" : [ "locality", "political" ] }, { "long_name" : "Erie County", "short_name" : "Erie County", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "New York", "short_name" : "NY", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "United States", "short_name" : "US", "types" : [ "country", "political" ] }, { "long_name" : "14202", "short_name" : "14202", "types" : [ "postal_code" ] } ], "formatted_address" : "5 Niagara Square, Buffalo, NY 14202, USA", "geometry" : { "location" : { "lat" : 42.886413, "lng" : -78.878152 }, "location_type" : "ROOFTOP", "viewport" : { "northeast" : { "lat" : 42.8877619802915, "lng" : -78.87680301970849 }, "southwest" : { "lat" : 42.8850640197085, "lng" : -78.87950098029151 } } }, "place_id" : "ChIJo5rq1EsS04kRo7vCCleRzR8", "types" : [ "street_address" ] }, { "address_components" : [ { "long_name" : "Niagara Square & City Hall", "short_name" : "Niagara Square & City Hall", "types" : [ "bus_station", "establishment", "point_of_interest", "transit_station" ] }, { "long_name" : "Downtown", "short_name" : "Downtown", "types" : [ "neighborhood", "political" ] }, { "long_name" : "Buffalo", "short_name" : "Buffalo", "types" : [ "locality", "political" ] }, { "long_name" : "Erie County", "short_name" : "Erie County", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "New York", "short_name" : "NY", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "United States", "short_name" : "US", "types" : [ "country", "political" ] }, { "long_name" : "14202", "short_name" : "14202", "types" : [ "postal_code" ] } ], "formatted_address" : "Niagara Square & City Hall, Buffalo, NY 14202, USA", "geometry" : { "location" : { "lat" : 42.886529, "lng" : -78.87892300000001 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 42.88787798029149, "lng" : -78.87757401970852 }, "southwest" : { "lat" : 42.88518001970849, "lng" : -78.88027198029152 } } }, "place_id" : "ChIJO-CEzEsS04kRRaYxWZtF__U", "types" : [ "bus_station", "establishment", "point_of_interest", "transit_station" ] }, { "address_components" : [ { "long_name" : "Downtown", "short_name" : "Downtown", "types" : [ "neighborhood", "political" ] }, { "long_name" : "Ridgeway", "short_name" : "Ridgeway", "types" : [ "political", "sublocality", "sublocality_level_1" ] }, { "long_name" : "Buffalo", "short_name" : "Buffalo", "types" : [ "locality", "political" ] }, { "long_name" : "Erie County", "short_name" : "Erie County", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "New York", "short_name" : "NY", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "United States", "short_name" : "US", "types" : [ "country", "political" ] } ], "formatted_address" : "Downtown, Buffalo, NY, USA", "geometry" : { "bounds" : { "northeast" : { "lat" : 42.8940569, "lng" : -78.86545699999999 }, "southwest" : { "lat" : 42.871821, "lng" : -78.87995699999999 } }, "location" : { "lat" : 42.8860328, "lng" : -78.8750644 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 42.8940569, "lng" : -78.86545699999999 }, "southwest" : { "lat" : 42.871821, "lng" : -78.87995699999999 } } }, "place_id" : "ChIJ8-dM-jcS04kRBN6L5FmCRXM", "types" : [ "neighborhood", "political" ] }, { "address_components" : [ { "long_name" : "Buffalo", "short_name" : "Buffalo", "types" : [ "locality", "political" ] }, { "long_name" : "Erie County", "short_name" : "Erie County", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "New York", "short_name" : "NY", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "United States", "short_name" : "US", "types" : [ "country", "political" ] } ], "formatted_address" : "Buffalo, NY, USA", "geometry" : { "bounds" : { "northeast" : { "lat" : 42.9664549, "lng" : -78.795157 }, "southwest" : { "lat" : 42.826023, "lng" : -78.9337276 } }, "location" : { "lat" : 42.88644679999999, "lng" : -78.8783689 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 42.9664549, "lng" : -78.795157 }, "southwest" : { "lat" : 42.826023, "lng" : -78.9142665 } } }, "place_id" : "ChIJoeXfUmES04kRcYEfGKUEI5g", "types" : [ "locality", "political" ] }, { "address_components" : [ { "long_name" : "14202", "short_name" : "14202", "types" : [ "postal_code" ] }, { "long_name" : "Buffalo", "short_name" : "Buffalo", "types" : [ "locality", "political" ] }, { "long_name" : "New York", "short_name" : "NY", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "United States", "short_name" : "US", "types" : [ "country", "political" ] } ], "formatted_address" : "Buffalo, NY 14202, USA", "geometry" : { "bounds" : { "northeast" : { "lat" : 43.05196490000001, "lng" : -78.82469999999999 }, "southwest" : { "lat" : 42.874781, "lng" : -78.90534 } }, "location" : { "lat" : 42.8943161, "lng" : -78.8736493 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 42.9022399, "lng" : -78.86897089999999 }, "southwest" : { "lat" : 42.874781, "lng" : -78.90534 } } }, "place_id" : "ChIJI5zC0cAS04kRgJcEeHfGtXE", "types" : [ "postal_code" ] }, { "address_components" : [ { "long_name" : "Erie County", "short_name" : "Erie County", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "New York", "short_name" : "NY", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "United States", "short_name" : "US", "types" : [ "country", "political" ] } ], "formatted_address" : "Erie County, NY, USA", "geometry" : { "bounds" : { "northeast" : { "lat" : 43.098676, "lng" : -78.46204609999999 }, "southwest" : { "lat" : 42.437997, "lng" : -79.31213609999999 } }, "location" : { "lat" : 42.9023958, "lng" : -78.8661589 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 43.098676, "lng" : -78.46204609999999 }, "southwest" : { "lat" : 42.437997, "lng" : -79.1371803 } } }, "place_id" : "ChIJRRlv_BcQ04kRP2qzyO_pHxA", "types" : [ "administrative_area_level_2", "political" ] }, { "address_components" : [ { "long_name" : "Buffalo-Niagara Falls, NY", "short_name" : "Buffalo-Niagara Falls, NY", "types" : [ "political" ] }, { "long_name" : "New York", "short_name" : "NY", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "United States", "short_name" : "US", "types" : [ "country", "political" ] } ], "formatted_address" : "Buffalo-Niagara Falls, NY, NY, USA", "geometry" : { "bounds" : { "northeast" : { "lat" : 43.374763, "lng" : -78.460416 }, "southwest" : { "lat" : 42.438068, "lng" : -79.1367245 } }, "location" : { "lat" : 42.8518007, "lng" : -78.74762079999999 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 43.374763, "lng" : -78.460416 }, "southwest" : { "lat" : 42.438068, "lng" : -79.1367245 } } }, "place_id" : "ChIJXc-dMF0L04kRLmL4rCidGIw", "types" : [ "political" ] }, { "address_components" : [ { "long_name" : "New York", "short_name" : "NY", "types" : [ "administrative_area_level_1", "establishment", "point_of_interest", "political" ] }, { "long_name" : "United States", "short_name" : "US", "types" : [ "country", "political" ] } ], "formatted_address" : "New York, USA", "geometry" : { "bounds" : { "northeast" : { "lat" : 45.015865, "lng" : -71.777491 }, "southwest" : { "lat" : 40.4773991, "lng" : -79.7625901 } }, "location" : { "lat" : 43.2994285, "lng" : -74.21793260000001 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 45.0125923, "lng" : -71.8562029 }, "southwest" : { "lat" : 40.4961036, "lng" : -79.761996 } } }, "place_id" : "ChIJqaUj8fBLzEwRZ5UY3sHGz90", "types" : [ "administrative_area_level_1", "establishment", "point_of_interest", "political" ] }, { "address_components" : [ { "long_name" : "United States", "short_name" : "US", "types" : [ "country", "political" ] } ], "formatted_address" : "United States", "geometry" : { "bounds" : { "northeast" : { "lat" : 71.5388001, "lng" : -66.885417 }, "southwest" : { "lat" : 18.7763, "lng" : 170.5957 } }, "location" : { "lat" : 37.09024, "lng" : -95.712891 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 49.38, "lng" : -66.94 }, "southwest" : { "lat" : 25.82, "lng" : -124.39 } } }, "place_id" : "ChIJCzYy5IS16lQRQrfeQ5K5Oxw", "types" : [ "country", "political" ] } ], "status" : "OK" }

From the above json repsonse the data is extracted in a data frame shown in following codes


In [157]:
getGeoCodeFromLatLng <- function(lat,lng){
    latlongUrl = getLatLngURL(lat,lng)
    latlngDoc <- getURL(latlongUrl)
    latlngDocData <- fromJSON(latlngDoc,simplify = FALSE)

    if(latlngDocData$status=="OK") {
        lat <- latlngDocData$results[[1]]$geometry$location$lat
        lng <- latlngDocData$results[[1]]$geometry$location$lng
        
        address_component_latlng <- parseAddressComponents(latlngDocData$results[[1]]$address_components)
        
        latlngState = address_component_latlng[1]
        latlngCountry = address_component_latlng[2]
        
        return(data.frame(Latitude = lat,Longitude = lng, State = latlngState, Country = latlngCountry))
    }else{
        print("Wrong location")
    }
}

The below code decomposes the json response from the Google Map API code

The output response is as follows for location with address Buffalo

{ "results" : [ { "address_components" : [ { "long_name" : "Buffalo", "short_name" : "Buffalo", "types" : [ "locality", "political" ] }, { "long_name" : "Erie County", "short_name" : "Erie County", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "New York", "short_name" : "NY", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "United States", "short_name" : "US", "types" : [ "country", "political" ] } ], "formatted_address" : "Buffalo, NY, USA", "geometry" : { "bounds" : { "northeast" : { "lat" : 42.9664549, "lng" : -78.795157 }, "southwest" : { "lat" : 42.826023, "lng" : -78.9337276 } }, "location" : { "lat" : 42.88644679999999, "lng" : -78.8783689 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 42.9664549, "lng" : -78.795157 }, "southwest" : { "lat" : 42.826023, "lng" : -78.9142665 } } }, "place_id" : "ChIJoeXfUmES04kRcYEfGKUEI5g", "types" : [ "locality", "political" ] } ], "status" : "OK" }

From the above json repsonse the data is extracted in a data frame shown in following codes

We remove all those locations which are not in United State or which are in United State but no information about the state.


In [158]:
getGeoCode <- function(address,verbose=FALSE) {
    if(verbose) cat(address,"\n")
    url <- getGeoCodeUrl(address)
    doc <- getURL(url)
    docData <- fromJSON(doc,simplify = FALSE)
    
    if(docData$status=="OK") {
        
        lat <- docData$results[[1]]$geometry$location$lat
        lng <- docData$results[[1]]$geometry$location$lng        
        
        address_component <- parseAddressComponents(docData$results[[1]]$address_components)
        state = address_component[1]
        country = address_component[2]
        
        if(!invalid(country) & country == "United States"){
            
            if(invalid(state)){
                return (getGeoCodeFromLatLng(lat,lng))
                
            }else{
                return(data.frame(Address = address, Latitude =  lat, Longitude = lng, State = state, Country = country))
            }
        } 
        Sys.sleep(0.5)
    }else{
        print("Wrong Place")
    }
}

Check which string is enabled latlngSearchString or locationSearchString


In [159]:
isGeoCode = FALSE
if(latlngSearchString != "" & locationSearchString == ""){
    isGeoCode = TRUE
}

Storing data frame for the location information searched from the getGeoCode function


In [160]:
if(isGeoCode){
    latlngStr = strsplit(latlngSearchString, " ")
    locationDataDf = getGeoCodeFromLatLng(latlngStr[[1]][1],latlngStr[[1]][2])
}else{
    locationDataDf = getGeoCode(locationSearchString)
}

Print the data frame


In [161]:
head(locationDataDf)


LatitudeLongitudeStateCountry
34.05186 -118.2443 California United States

The closestTrendLocations function is passed a latitude and longitude and will return the same style data.frame as of availableTrendLocations.


In [162]:
closeTrendsDf = closestTrendLocations(locationDataDf$Latitude,locationDataDf$Longitude)

Print the data frame generated above


In [163]:
head(closeTrendsDf)


namecountrywoeid
Los Angeles United States2442047

The getTrends function is used to pull current trend information from a given location, which is specified using a WOEID


In [164]:
trendingDataDf = getTrends(closeTrendsDf$woeid)

Print the data frame generated above


In [165]:
head(trendingDataDf)


nameurlquerywoeid
#LARain http://twitter.com/search?q=%23LARain %23LARain 2442047
#Logan http://twitter.com/search?q=%23Logan %23Logan 2442047
Matt Reeves http://twitter.com/search?q=%22Matt+Reeves%22%22Matt+Reeves%22 2442047
#Lucifer http://twitter.com/search?q=%23Lucifer %23Lucifer 2442047
#FridayFeeling http://twitter.com/search?q=%23FridayFeeling %23FridayFeeling 2442047
Brad Grey http://twitter.com/search?q=%22Brad+Grey%22 %22Brad+Grey%22 2442047

Form a comma seprated string of lenth LIMIT, described above.


In [166]:
trendingTopics = print(paste(trendingDataDf$name[1:LIMIT],sep = " ",collapse = ", "))


[1] "#LARain, #Logan, Matt Reeves, #Lucifer, #FridayFeeling, Brad Grey, #StormWatch, Goose Gossage, Trent Richardson, Colie Bowers"

Print the trending topics near the user inputed location


In [167]:
if(isGeoCode){
    print(paste("The trending topics near",closeTrendsDf$name,"(",latlngSearchString,")","are :",trendingTopics,sep = " "))
}else{
    print(paste("The trending topics near",locationSearchString,"are :",trendingTopics,sep = " "))
}


[1] "The trending topics near Los Angeles ( 34.05223 -118.2437 ) are : #LARain, #Logan, Matt Reeves, #Lucifer, #FridayFeeling, Brad Grey, #StormWatch, Goose Gossage, Trent Richardson, Colie Bowers"

REFERENCES


In [ ]: