Message Boards Message Boards

GrammarApply -> GrammarToken -> Location = $failed (NLP City Extraction)

I'm trying to create a simple function that extracts all geolocation information from a given string.

Code:

In[42]:= deployFindCity = GrammarRules[
   {
    location : GrammarToken["findCity"] :>  location
    },
   {
    "findCity" -> GrammarToken["City"],
    "findCity" -> GrammarToken["Location"],
    "findCity" -> GrammarToken["StreetAddress"], 
    "findCity" -> GrammarToken["GeoCoordinates"]
    }
   ];
findCity = CloudDeploy[deployFindCity]

Out[43]= CloudObject["https://www.wolframcloud.com/objects/xxxxxxx"]

In[47]:= GrammarApply[findCity, "1023 S Salish ct, Spokane, WA, USA"]
Out[47]= $Failed

I thought it was my function so I tried this:

   Interpreter["StreetAddress"]["1023 S Salish ct, Spokane, WA, USA"]
   Out= Failure
    Message: No street address interpretation found. Try again.
    Tag: InterpretationFailure
    Type: StreetAddress
    Input: 1023 S Salish ct, Spokane, WA, USA

So, what is the best way to extract the Geolocation? I want to return the City and the GeoPosition.

I get this error in Mathematica as well as the editor in the Cloud.

POSTED BY: David Johnston
3 Replies
  1. You can use "Alternatives" with Interpreter. So the following is valid syntax. It first tries to interpret the string as a "City", then as a "Location" ... and so forth

    Interpreter["City"|"Location"|"StreetAddress"|"Geocoordintes"]

  2. The "Location" Interpreter subsumes the other types. So you really just need:

    Interpreter["Location"]

  3. If you look inside the documentation of Mathematica (in product, not online), you'll see that it's labeled as "Experimental". The "StreetAddress" interpreter relies on services to work. It looks like these are working again, but it's not recognizing your address. Open Street Maps, for example, doesn't seem to understand that address.

There are other Geocoding APIs that you might considering using. Which to use probably depends on how much data you want to process, what countries you need it to work in, etc etc.

POSTED BY: Sean Clarke

Thanks for your reply. Very much appreciated!

Okay, so, I need to get the latitude and longitude for the given address. I would assume Google Maps Geoencoder API would work best. However, I can't find any good tutorials, and my other thread started last year on integrating Google API's, has never been answered.

The Mathematica wolfram language library for accessing Google search is broken. That and the Google plus integration, were all I could find. Neither showed enough for me to figure out Google's stupid Oauth token refresh stuff.

There are several Google API's I would like to access also. A great general Oauth 2.0 tutorial would be most beneficial for the WL community but adding to that a couple harder Google API'S as examples would be fantastic.

POSTED BY: David Johnston

Wolfram Support send me a satisfactory solution. Basically, you can use Google Maps to deal with ambiguity of inputs and still find a good address. Keep in mind you can not put functions inside your CloudDeploy for Grammar functions.

cleanString[string_] := Module[{l1,l2},
    l1 = Characters@"ŠŽšžŸÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðñòóôõöùúûüýÿ";
    l2 = Characters@"SZszYAAAAAACEEEEIIIIDNOOOOOUUUUYaaaaaaceeeeiiiidnooooouuuuyy";
    StringReplace[string, Thread[l1 -> l2]~Join~{" " -> "%20", "," -> "%2C"}]
]

(* Works with Google map API version 3 *)
geoCoder[address_] := Module[{getGeo, geo, cod = 0, i = 0, link}, 
    link = cleanString@address;
    link = "https://maps.googleapis.com/maps/api/geocode/json?address=" <>link;
    Import[link, {"JSON", "Data", "results", 1, "geometry", "location"}]];   

Then you can just call this function after getting your data back from the Grammar function.

geoCoder["Av Paulista, 510, Sao Paulo, Brasil"]

You can find the original thread here: http://mathematica.stackexchange.com/questions/19024/geocoding-addresses-to-geographic-coordinates

POSTED BY: David Johnston
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract