diff --git a/build.gradle b/build.gradle index d6a8c99..263515e 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ plugins { } group 'org.doremus' -version '1.3.1' +version '1.3.2' sourceCompatibility = 1.8 diff --git a/src/main/java/org/doremus/isnimatcher/ISNI.java b/src/main/java/org/doremus/isnimatcher/ISNI.java index 63d9aa0..3f58978 100644 --- a/src/main/java/org/doremus/isnimatcher/ISNI.java +++ b/src/main/java/org/doremus/isnimatcher/ISNI.java @@ -8,6 +8,7 @@ import javax.xml.bind.JAXBException; import java.io.IOException; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -20,6 +21,9 @@ public class ISNI { private final static Pattern FLEX_DATE_PATTERN = Pattern.compile("[\\d?.]{0,4}"); + private static boolean debug = false; + private static boolean bestViafBehavior = false; + public static ISNIRecord get(String id) throws IOException { if (id.startsWith(ISNI_BASE)) id = id.replace(ISNI_BASE, ""); @@ -55,12 +59,34 @@ public static ISNIRecord search(String forename, String surname, String date) th if (date == null) return records.get(0); String _date = cleanDate(date); - // for (ISNIRecord r : records) r.save("test/" + r.id + ".xml"); - return records.stream() + if (debug) { + System.out.println(records.size() + " records"); + for (ISNIRecord r : records) r.save("test/" + r.id + ".xml"); + } + + ISNIRecord match = records.stream() .filter(r -> r.hasName(forename, surname, true)) .filter(r -> dateMatch(_date, r.getBirthYear())) .findFirst() .orElse(null); + + if (bestViafBehavior) + return getBestViaf(match, records); + + return match; + } + + private static ISNIRecord getBestViaf(ISNIRecord base, List records) { + // Among the records with the same VIAF id, return the one with more links + if (base == null) return null; + String viaf = base.getViafURI(); + if (viaf == null) return base; + + + return records.stream() + .filter(r -> viaf.equals(r.getViafURI())) + .max(Comparator.comparingInt(ISNIRecord::getLinksNumber)) + .orElse(base); } private static boolean dateMatch(String expected, String actual) { @@ -86,7 +112,7 @@ private static List performQuery(String query, int n) throws IOExcep .queryString("recordSchema", "isni-b") .queryString("maximumRecords", n); - // System.out.println(request.getUrl()); + if (debug) System.out.println(request.getUrl()); HttpResponse response = request.asString(); @@ -131,4 +157,11 @@ static List splitBody(String body) { } + public static void setDebug(boolean debug) { + ISNI.debug = debug; + } + + public static void setBestViafBehavior(boolean bestViafBehavior) { + ISNI.bestViafBehavior = bestViafBehavior; + } } diff --git a/src/main/java/org/doremus/isnimatcher/ISNIRecord.java b/src/main/java/org/doremus/isnimatcher/ISNIRecord.java index d9b9990..c5701e4 100644 --- a/src/main/java/org/doremus/isnimatcher/ISNIRecord.java +++ b/src/main/java/org/doremus/isnimatcher/ISNIRecord.java @@ -47,9 +47,9 @@ public class ISNIRecord { public List getSources() { if (source == null) { - source = sources1; - if (sources1 == null) source = sources2; - else source.addAll(sources2); + source = new ArrayList<>(); + if (sources1 != null) source.addAll(sources1); + if (sources2 != null) source.addAll(sources2); } return source; } @@ -111,6 +111,10 @@ public String getViafURI() { for (Source s : getSources()) if ("VIAF".equals(s.codeOfSource)) return s.asViafURI(); + for (Source s : getSources()) + if (s.sourceIdentifier.matches("VIAF \\d+")) + return Source.makeViafUri(s.sourceIdentifier.replace("VIAF ", "")); + return null; } @@ -218,4 +222,7 @@ public boolean hasName(String forename, String surname, boolean _default) { return false; } + public int getLinksNumber() { + return this.getSources().size() + this.getExternalInformations().size(); + } } diff --git a/src/main/java/org/doremus/isnimatcher/Source.java b/src/main/java/org/doremus/isnimatcher/Source.java index b5aa542..99b1db0 100644 --- a/src/main/java/org/doremus/isnimatcher/Source.java +++ b/src/main/java/org/doremus/isnimatcher/Source.java @@ -27,6 +27,11 @@ public String asBNFUri() { } public String asViafURI() { - return VIAF_BASE + sourceIdentifier; + return makeViafUri(sourceIdentifier); + } + + public static String makeViafUri(String viaf) { + return VIAF_BASE + viaf; + } } diff --git a/src/test/java/ISNITest.java b/src/test/java/ISNITest.java index f61a268..9c256fe 100644 --- a/src/test/java/ISNITest.java +++ b/src/test/java/ISNITest.java @@ -1,5 +1,6 @@ import org.doremus.isnimatcher.ISNI; import org.doremus.isnimatcher.ISNIRecord; +import org.junit.Before; import org.junit.Test; import javax.xml.bind.JAXBException; @@ -13,6 +14,11 @@ public class ISNITest { private final static String MOZART_URI = "http://isni.org/isni/0000000121269154"; private final static String BEETHOVEN_ID = "0000000121268987"; + @Before + public void initialize() { + ISNI.setDebug(true); + } + @Test public void searchWithFullName() throws IOException { String str = "Mozart, Wolfgang Amadeus"; @@ -52,6 +58,21 @@ public void searchWithNameAndDate() throws IOException { @Test public void searchWithNameAndIncompleteDate() throws IOException { + ISNIRecord r = ISNI.search("Robert Markham", "19.."); + assertEquals("0000000107873128", r.id); + } + + @Test + public void differentIdentities() throws IOException { + ISNI.setBestViafBehavior(true); + ISNIRecord r = ISNI.search("Guillaume Apollinaire", "1880"); + assertEquals("0000000121371423", r.id); + ISNI.setBestViafBehavior(false); + } + + + @Test + public void searchWithNameSurname() throws IOException { ISNIRecord r1 = ISNI.search("Martin", "Walter", "19.."); ISNIRecord r2 = ISNI.search("Walter", "Martin", "19.."); assertNotEquals(r1.id, r2.id);