Something went wrong. Try again.
A lightweight geocoding server
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167package main
import ( "regexp" "strings"
"tangled.org/guid.foo/gazetteer/normalize"
"tangled.org/guid.foo/gazetteer/regions")
// Interpretation is one reading of a free-form query. When a split is// unclear the parser produces several and the score ranks them.type Interpretation struct { Number string Street string City string Region string // Place holds the middle text read as a bare place name. Place string Country string}
// PostalQuery is a postal code recognized inside a query.type PostalQuery struct { Country string Code string // Bare is true when the postal code was the entire query. Bare bool}
var ( zipPattern = regexp.MustCompile(`^\d{5}(\d{4})?$`) caFullPattern = regexp.MustCompile(`^[a-z]\d[a-z]\d[a-z]\d$`) caFSAPattern = regexp.MustCompile(`^[a-z]\d[a-z]$`) numberPattern = regexp.MustCompile(`^\d+[a-z]?$`) caUnitPattern = regexp.MustCompile(`^\d[a-z]\d$`) countrySuffix = map[string]string{"us": "US", "usa": "US", "canada": "CA"} maxSplitTokens = 8)
// parseQuery turns a query into candidate interpretations and an// optional postal code. The text is normalized but not yet// abbreviation expanded, so state codes like "me" survive to region// detection.func parseQuery(q string) ([]Interpretation, *PostalQuery) { tokens := strings.Fields(normalize.Text(q)) if len(tokens) == 0 { return nil, nil }
postal, tokens := extractPostal(tokens) if len(tokens) == 0 { return nil, postal }
country := "" if c, ok := countrySuffix[tokens[len(tokens)-1]]; ok { country = c tokens = tokens[:len(tokens)-1] }
region, regionCountry, tokens := extractRegion(tokens) if country == "" { country = regionCountry }
number := "" if len(tokens) > 1 && numberPattern.MatchString(tokens[0]) { number = tokens[0] tokens = tokens[1:] }
middle := tokens if len(middle) > maxSplitTokens { middle = middle[:maxSplitTokens] }
var out []Interpretation add := func(i Interpretation) { i.Region = region i.Country = country i.Number = number out = append(out, i) }
if len(middle) == 0 { if region != "" { // A bare region like "maine" still deserves place results. add(Interpretation{Place: regions.NameForCode(region)}) } return out, postal }
// Every split point of the middle: street then city. The last // split (whole middle as street, no city) and the first (whole // middle as city) are both included. for i := len(middle); i >= 1; i-- { street := expandTokens(middle[:i]) city := expandTokens(middle[i:]) add(Interpretation{Street: street, City: city}) }
if number == "" { add(Interpretation{Place: expandTokens(middle)}) } return out, postal}
func expandTokens(tokens []string) string { return normalize.Expand(strings.Join(tokens, " "))}
// extractPostal recognizes a US ZIP or a Canadian postal code among// the tokens and removes it. Canadian full codes may arrive as one// token ("e5e1a1") or two ("e5e 1a1").func extractPostal(tokens []string) (*PostalQuery, []string) { for i, t := range tokens { if zipPattern.MatchString(t) { code := t[:5] return &PostalQuery{Country: "US", Code: code, Bare: len(tokens) == 1}, remove(tokens, i, 1) } if caFullPattern.MatchString(t) { return &PostalQuery{Country: "CA", Code: strings.ToUpper(t), Bare: len(tokens) == 1}, remove(tokens, i, 1) } if caFSAPattern.MatchString(t) { if i+1 < len(tokens) && caUnitPattern.MatchString(tokens[i+1]) { code := strings.ToUpper(t + tokens[i+1]) return &PostalQuery{Country: "CA", Code: code, Bare: len(tokens) == 2}, remove(tokens, i, 2) } // A bare FSA is only unambiguous on its own: inside a // longer query, three-letter tokens are usually words. if len(tokens) == 1 { return &PostalQuery{Country: "CA", Code: strings.ToUpper(t), Bare: true}, nil } } } return nil, tokens}
func remove(tokens []string, i, n int) []string { out := make([]string, 0, len(tokens)-n) out = append(out, tokens[:i]...) return append(out, tokens[i+n:]...)}
// extractRegion recognizes a trailing state or province, as a postal// code ("me") or a full name ("new brunswick"), and removes it.func extractRegion(tokens []string) (code, country string, rest []string) { if len(tokens) == 0 { return "", "", tokens } last := tokens[len(tokens)-1] if len(last) == 2 && len(tokens) > 1 { if code, country, ok := regions.AdminByCode(last); ok { return code, country, tokens[:len(tokens)-1] } } for n := min(regions.MaxAdminNameWords, len(tokens)); n >= 1; n-- { name := strings.Join(tokens[len(tokens)-n:], " ") if code, country, ok := regions.AdminByName(name); ok { return code, country, tokens[:len(tokens)-n] } } return "", "", tokens}