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

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
POSTED BY: David Johnston
POSTED BY: Sean Clarke
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