Skip to content

Commit a9a9e88

Browse files
committed
Series import: extract country name from from "Minerals-Maldives" string.
1 parent 6826b24 commit a9a9e88

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/main/java/ru/mystamps/web/service/SeriesInfoExtractorServiceImpl.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.regex.Matcher;
2525
import java.util.regex.Pattern;
2626
import java.util.stream.Collectors;
27+
import java.util.stream.Stream;
2728

2829
import org.apache.commons.lang3.StringUtils;
2930

@@ -145,8 +146,18 @@ protected List<Integer> extractCountry(String fragment) {
145146

146147
log.debug("Determining country from a fragment: '{}'", fragment);
147148

148-
String[] names = StringUtils.split(fragment, "\n\t ,.");
149-
List<String> candidates = Arrays.stream(names)
149+
String[] words = StringUtils.split(fragment, "\n\t ,.");
150+
151+
Stream<String> names = Arrays.stream(words);
152+
153+
// Generate more candidates by split their names by a hyphen.
154+
// For example: "Minerals-Maldives" becomes [ "Minerals", "Maldives" ]
155+
Stream<String> additionalNames = Arrays.stream(words)
156+
.filter(el -> el.contains("-"))
157+
.map(el -> StringUtils.split(el, '-'))
158+
.flatMap(Arrays::stream);
159+
160+
List<String> candidates = Stream.concat(names, additionalNames)
150161
.filter(SeriesInfoExtractorServiceImpl::validCountryName)
151162
.distinct()
152163
.limit(MAX_CANDIDATES_FOR_LOOKUP)

src/test/groovy/ru/mystamps/web/service/SeriesInfoExtractorServiceImplTest.groovy

+10
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,16 @@ class SeriesInfoExtractorServiceImplTest extends Specification {
149149
1 * countryService.findIdsByNames(expectedCandidates) >> Random.listOfIntegers()
150150
}
151151

152+
def 'extractCountry() should generate additional candidates by split words by a hyphen'() {
153+
given:
154+
String fragment = 'foo-bar'
155+
List<String> expectedCandidates = [ 'foo-bar', 'foo', 'bar' ]
156+
when:
157+
service.extractCountry(fragment)
158+
then:
159+
1 * countryService.findIdsByNames(expectedCandidates) >> Random.listOfIntegers()
160+
}
161+
152162
def 'extractCountry() should try to search country names with candidate as a prefix'() {
153163
given:
154164
List<Integer> expectedResult = Random.listOfIntegers()

0 commit comments

Comments
 (0)