...
 
Commits (3)
......@@ -258,17 +258,13 @@ public final class NDC
/**
* Produce a normalized (5-4-2) representation of this NDC. If the NDC
* is not complete, will simply return the NDC as-is.
* is not complete, the labeler and product codes will be normalized.
*
* @return normalized NDC in 5-4-2 format (if possible)
*/
public NDC
normalize()
{
if (! this.complete())
{
return this;
}
if (this.normalized())
{
......@@ -288,7 +284,7 @@ public final class NDC
this.packageCode);
}
if (this.packageCode.length == 1)
if (this.packageCode != null && this.packageCode.length == 1)
{
return new NDC(this.labelerCode,
this.productCode,
......@@ -311,7 +307,7 @@ public final class NDC
{
return this.labelerCode.length == 5
&& this.productCode.length == 4
&& this.packageCode.length == 2;
&& (this.packageCode.length == 0 || this.packageCode.length == 2);
}
@Override
......
......@@ -89,6 +89,11 @@ public class NDCTest
test.accept("50242-040-62", "50242-0040-62");
/* 5-4-1 -> 5-4-2 */
test.accept("60575-4112-1", "60575-4112-01");
/* partial NDC normalization */
test.accept("50242-040", "50242-0040");
test.accept("0002-7597", "00002-7597");
test.accept("60575-4112", "60575-4112");
}
@Test
......
......@@ -42,7 +42,6 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
......@@ -191,7 +190,7 @@ implements DictionaryService
/* fuzzy hash search */
final List<List<String>> hashes =
partitionByLength(hashFuzz(query, HASH_DISTANCE, MIN_FUZZY_HASH_LEN));
partitionHashesByLength(hashFuzz(query, HASH_DISTANCE, MIN_FUZZY_HASH_LEN));
final Field<String> hashField =
field(name("phonetic_hash"), String.class);
......@@ -255,7 +254,7 @@ implements DictionaryService
/* fuzzy hash search over proprietary names */
final List<List<String>> hashes =
partitionByLength(hashFuzz(query, HASH_DISTANCE, MIN_FUZZY_HASH_LEN));
partitionHashesByLength(hashFuzz(query, HASH_DISTANCE, MIN_FUZZY_HASH_LEN));
for (final List<String> hashGroup : hashes)
{
......@@ -482,11 +481,12 @@ implements DictionaryService
}
private static final List<List<String>>
partitionByLength(Stream<String> stream)
partitionHashesByLength(Stream<String> stream)
{
return stream
.collect(Collectors.toMap(s -> s.length(), Stream :: of, Stream :: concat))
.collect(Collectors.toMap(s -> s.length() - StringUtils.countMatches(s, '_'),
Stream :: of, Stream :: concat))
.entrySet().stream()
.sorted((a, b) -> b.getKey() - a.getKey())
.map(e -> e.getValue().collect(Collectors.toList()))
......