Skip to content

Instantly share code, notes, and snippets.

@johntbush
Created July 22, 2016 19:30
Show Gist options
  • Save johntbush/9ae78b0f5659cf616808be27bc773e3a to your computer and use it in GitHub Desktop.
Save johntbush/9ae78b0f5659cf616808be27bc773e3a to your computer and use it in GitHub Desktop.
address_norm.diff
diff --git a/addressNorm/pom.xml b/addressNorm/pom.xml
index 6a7e46a..0aa8835 100644
--- a/addressNorm/pom.xml
+++ b/addressNorm/pom.xml
@@ -1,10 +1,12 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
- <artifactId>trax-platform</artifactId>
+ <artifactId>trax-platform-parent</artifactId>
<groupId>com.trax.platform</groupId>
- <version>1.1.40</version>
+ <version>1.2.2.9</version>
+ <relativePath>../parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
+ <version>1.2-SNAPSHOT</version>
<artifactId>trax-platform-addressNorm</artifactId>
<packaging>jar</packaging>
@@ -24,7 +26,12 @@
<dependency>
<groupId>com.trax.platform</groupId>
<artifactId>trax-platform-datastore</artifactId>
- <version>${project.version}</version>
+ <version>1.2.2</version>
+ </dependency>
+ <dependency>
+ <groupId>com.trax.platform</groupId>
+ <artifactId>trax-platform-utils</artifactId>
+ <version>1.3.14</version>
</dependency>
<dependency>
<groupId>net.iharder</groupId>
diff --git a/addressNorm/src/main/java/com/trax/platform/addressnorm/LocationFinder.java b/addressNorm/src/main/java/com/trax/platform/addressnorm/LocationFinder.java
index 3a1a69e..42ee43c 100644
--- a/addressNorm/src/main/java/com/trax/platform/addressnorm/LocationFinder.java
+++ b/addressNorm/src/main/java/com/trax/platform/addressnorm/LocationFinder.java
@@ -15,6 +15,8 @@ import java.util.HashSet;
import java.util.Set;
/**
+ * This is the base class for other *Finder classes.
+ *
* Created by toddwarwaruk on 9/18/14.
*/
abstract class LocationFinder {
@@ -52,6 +54,9 @@ abstract class LocationFinder {
for (int i = 0; i < NUM_RETRIES; i++) {
try {
URL url = query.getURL();
+ if (url == null)
+ continue;
+
THROTTLE.waitToGo();
Metrics.INSTANCE.geocodeQueried();
diff --git a/addressNorm/src/main/java/com/trax/platform/addressnorm/Query.java b/addressNorm/src/main/java/com/trax/platform/addressnorm/Query.java
index 6fce91c..f54f33f 100644
--- a/addressNorm/src/main/java/com/trax/platform/addressnorm/Query.java
+++ b/addressNorm/src/main/java/com/trax/platform/addressnorm/Query.java
@@ -210,29 +210,35 @@ class Query {
QP_LANGUAGE + '=' + "en" + '&' +
QP_CLIENT + '=' + CLIENT_ID;
- String components = "";
+ String remainingUri = "";
if (isNotBlank(filterCountry))
- components = "country:" + URLEncoder.encode(filterCountry, "UTF-8");
+ remainingUri = "country:" + URLEncoder.encode(filterCountry, "UTF-8");
if (isNotBlank(filterPostalCode))
- components = components + (components.isEmpty() ? "" : "|") + "postal_code:" + URLEncoder.encode(filterPostalCode, "UTF-8");
+ remainingUri = remainingUri + (remainingUri.isEmpty() ? "" : "|") + "postal_code:" + URLEncoder.encode(filterPostalCode, "UTF-8");
if (isNotBlank(filterAdministrativeArea))
- components = components + (components.isEmpty() ? "" : "|") + "administrative_area:" + URLEncoder.encode(filterAdministrativeArea, "UTF-8");
+ remainingUri = remainingUri + (remainingUri.isEmpty() ? "" : "|") + "administrative_area:" + URLEncoder.encode(filterAdministrativeArea, "UTF-8");
if (isNotBlank(filterLocality))
- components = components + (components.isEmpty() ? "" : "|") + "locality:" + URLEncoder.encode(filterLocality, "UTF-8");
+ remainingUri = remainingUri + (remainingUri.isEmpty() ? "" : "|") + "locality:" + URLEncoder.encode(filterLocality, "UTF-8");
if (isNotBlank(filterRoute))
- components = components + (components.isEmpty() ? "" : "|") + "route:" + URLEncoder.encode(filterRoute, "UTF-8");
- if (!components.isEmpty())
- partialUri = partialUri + '&' + QP_COMPONENTS + '=' + components;
+ remainingUri = remainingUri + (remainingUri.isEmpty() ? "" : "|") + "route:" + URLEncoder.encode(filterRoute, "UTF-8");
+
+ if (!remainingUri.isEmpty())
+ remainingUri = '&' + QP_COMPONENTS + '=' + remainingUri;
if (isNotBlank(address))
- partialUri = partialUri + '&' + QP_ADDRESS + '=' + URLEncoder.encode(address, "UTF-8");
+ remainingUri = remainingUri + '&' + QP_ADDRESS + '=' + URLEncoder.encode(address, "UTF-8");
if (isNotBlank(region))
- partialUri = partialUri + '&' + QP_REGION + '=' + URLEncoder.encode(region, "UTF-8");
-
- final String hash = getSignature(partialUri);
- final String url = DOMAIN + partialUri + '&' + QP_SIGNATURE + '=' + hash;
- log.debug(partialUri);
- return new URL(url);
+ remainingUri = remainingUri + '&' + QP_REGION + '=' + URLEncoder.encode(region, "UTF-8");
+
+ if (remainingUri.isEmpty())
+ return null;
+ else {
+ final String fullUri = partialUri + remainingUri;
+ final String hash = getSignature(fullUri);
+ final String url = DOMAIN + fullUri + '&' + QP_SIGNATURE + '=' + hash;
+ log.debug(fullUri);
+ return new URL(url);
+ }
}
}
diff --git a/addressNorm/src/main/scala/com/trax/platform/addressnorm/AddressNormalizationService.scala b/addressNorm/src/main/scala/com/trax/platform/addressnorm/AddressNormalizationService.scala
index 8d57b1a..62855bf 100644
--- a/addressNorm/src/main/scala/com/trax/platform/addressnorm/AddressNormalizationService.scala
+++ b/addressNorm/src/main/scala/com/trax/platform/addressnorm/AddressNormalizationService.scala
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.node.ObjectNode
import com.trax.platform.util.java.JsonUtils.asJsonNode
import com.trax.platform.util.scala.JsonUtils
+import org.apache.commons.lang3.StringUtils.isNotBlank
/**
* Given:
@@ -71,7 +72,9 @@ object AddressNormalizationService {
street1, street2, city, state, postalCode, country,
getAddress, getPostal, getCity, getCountry,
bypassCache)
- result.put("status", "OK")
+
+ if (!result.has("status"))
+ result.put("status", "OK")
result.set("original_request", request)
result
}
@@ -93,6 +96,20 @@ object AddressNormalizationService {
* This is the main entry point.
*/
def normalize(address: Address, options: Options): ObjectNode = {
+ if (!address.city.exists(isNotBlank(_)) &&
+ !address.country.exists(isNotBlank(_)) &&
+ !address.countryCode.exists(isNotBlank(_)) &&
+ !address.postal.exists(isNotBlank(_)) &&
+ !address.state.exists(isNotBlank(_)) &&
+ !address.street1.exists(isNotBlank(_)) &&
+ !address.street2.exists(isNotBlank(_))
+ ) {
+ val result: ObjectNode = JsonUtils.newObjectNode
+ result.put("status", "ERROR")
+ result.put("error_message", "Invalid request: at least one field must be non-empty")
+ return result
+ }
+
val countryComponent: CountryComponent = new CountryComponent(address)
val streetComponent: StreetComponent =
diff --git a/addressNorm/src/test/java/com/trax/platform/addressnorm/AddressDatabaseTest.java b/addressNorm/src/test/java/com/trax/platform/addressnorm/AddressDatabaseTest.java
index 387f3fa..619ab01 100644
--- a/addressNorm/src/test/java/com/trax/platform/addressnorm/AddressDatabaseTest.java
+++ b/addressNorm/src/test/java/com/trax/platform/addressnorm/AddressDatabaseTest.java
@@ -3,10 +3,12 @@ package com.trax.platform.addressnorm;
import org.junit.Test;
import java.io.IOException;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+import static org.junit.Assert.*;
-/**
- * Created by toddwarwaruk on 12/12/14.
- */
public class AddressDatabaseTest {
// @Test
// public void logCities() throws IOException {
@@ -14,27 +16,34 @@ public class AddressDatabaseTest {
// }
@Test
- public void logStates() throws IOException {
- AddressDatabase$.MODULE$.isKnownCity("foo");
-// Iterable<String> allStates = AddressDatabase$.MODULE$.getAllStates();
-// for(String state : allStates) {
-// Set<String> countries = AddressDatabase$.MODULE$.countriesForState(state);
-// if (countries.contains("US"))
-// System.out.println(state + " -> " + countries);
-// }
-//
-
+ public void testCities() throws IOException {
+ assertFalse("Found city named foobar", AddressDatabase$.MODULE$.isKnownCity("foobar"));
+ assertTrue("Could not find city named \"phoenix\"", AddressDatabase$.MODULE$.isKnownCity("phoenix"));
}
-
@Test
public void countryResolution() {
- System.out.println("countries with city named \"Zwolle\": " + AddressDatabase$.MODULE$.countriesForCity("Zwolle"));
- System.out.println("countries with postal code format matching \"8000 AJ\": " + AddressDatabase$.MODULE$.possibleCountries("8000 AJ"));
- }
-
- @Test
- public void possibleCountries() {
- System.out.println(AddressDatabase$.MODULE$.possibleCountries("g0x 3h0"));
+ // Countries with city named "Zwolle"
+ scala.collection.Set<String> result = AddressDatabase$.MODULE$.countriesForCity("Zwolle");
+ Set<String> expected = new HashSet<>(Arrays.asList(new String[]{"nl", "us"}));
+ assertEquals(expected.size(), result.size());
+ for (String expectedCountry : expected) {
+ assertTrue(result.contains(expectedCountry));
+ }
+
+ // Countries with postal code format matching "8000 AJ"
+ result = AddressDatabase$.MODULE$.possibleCountries("8000 AJ");
+ expected = new HashSet<>(Arrays.asList(new String[]{"tf", "cg", "ss", "st", "bs", "ae", "mw", "aq", "kp", "ug", "bf", "rw", "fj", "nr", "nu", "tt", "sx", "bw", "bq", "gn", "cd", "ye", "sr", "sc", "gy", "gh", "tz", "qa", "bj", "tg", "tl", "nl", "ck", "vu", "bi", "tv", "tk", "aw", "gq", "ki", "to", "sy", "um", "ao", "sb", "ml", "gm", "bz", "ag", "cm", "mo", "kn", "jm", "dj", "gd", "dm", "ci", "mr", "ie", "bv", "cf", "lc", "cw", "eh", "hk", "lt", "ws", "km", "sl", "er", "zw"}));
+ assertEquals(expected.size(), result.size());
+ for (String expectedCountry : expected) {
+ assertTrue(result.contains(expectedCountry));
+ }
+
+ result = AddressDatabase$.MODULE$.possibleCountries("g0x 3h0");
+ expected = new HashSet<>(Arrays.asList(new String[]{"tf", "cg", "ss", "st", "bs", "ae", "mw", "aq", "kp", "ug", "bf", "rw", "fj", "nr", "nu", "tt", "sx", "bw", "bq", "gn", "cd", "ye", "sr", "sc", "ca", "gy", "gh", "tz", "qa", "bj", "tg", "tl", "ck", "vu", "bi", "tv", "tk", "aw", "gq", "ki", "to", "sy", "um", "ao", "sb", "ml", "gm", "bz", "ag", "cm", "mo", "kn", "jm", "dj", "gd", "dm", "ci", "mr", "ie", "bv", "cf", "lc", "cw", "eh", "hk", "lt", "ws", "km", "sl", "er", "zw"}));
+ assertEquals(expected.size(), result.size());
+ for (String expectedCountry : expected) {
+ assertTrue(result.contains(expectedCountry));
+ }
}
}
diff --git a/addressNorm/src/test/java/com/trax/platform/addressnorm/AddressNormalizationTest.java b/addressNorm/src/test/java/com/trax/platform/addressnorm/AddressNormalizationTest.java
index d881b96..b4ac152 100644
--- a/addressNorm/src/test/java/com/trax/platform/addressnorm/AddressNormalizationTest.java
+++ b/addressNorm/src/test/java/com/trax/platform/addressnorm/AddressNormalizationTest.java
@@ -96,12 +96,33 @@ public class AddressNormalizationTest {
// TODO wrong address, correct city; maps gets it right // createTest(addresses.get(124441), logResult);
// TODO wrong address, correct city; maps gets it right // createTest(addresses.get(123360), logResult);
- JsonNode result = service.normalize(addresses.get().asJsonStringRequest());
+// JsonNode result = service.normalize(addresses.get().asJsonStringRequest());
+// NodeLogger.log(result);
+
+
+ JsonNode result = service.normalize("Foo", null, null, null, null, null, true, true, true, true, true);
+ System.out.println("\n\n1:");
+ NodeLogger.log(result);
+ result = service.normalize(null, "Foo", null, null, null, null, true, true, true, true, true);
+ System.out.println("\n\n2:");
+ NodeLogger.log(result);
+ result = service.normalize(null, null, "Foo", null, null, null, true, true, true, true, true);
+ System.out.println("\n\n3:");
+ NodeLogger.log(result);
+ result = service.normalize(null, null, null, "Foo", null, null, true, true, true, true, true);
+ System.out.println("\n\n4:");
+ NodeLogger.log(result);
+ result = service.normalize(null, null, null, null, "Foo", null, true, true, true, true, true);
+ System.out.println("\n\n5:");
+ NodeLogger.log(result);
+ result = service.normalize(null, null, null, null, null, "Foo", true, true, true, true, true);
+ System.out.println("\n\n6:");
+ NodeLogger.log(result);
+ result = service.normalize(null, null, null, null, null, null, true, true, true, true, true);
+ System.out.println("\n\n7:");
NodeLogger.log(result);
-// JsonNode result = service.normalize("Scottsdale Healthcare", "9003 Shea", "Phoenix", "AZ", null, "U.S.A.", true, true, true, true, false);
-// NodeLogger.log(result);
//
// result = service.normalize("980 Great West Road", "", "Brentford, Middlesex", "", "TW8 9GS", "GB", true, true, true, true, false);
// NodeLogger.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment