Skip to content

Commit

Permalink
manage different identities
Browse files Browse the repository at this point in the history
debug option
  • Loading branch information
pasqLisena committed Jun 27, 2018
1 parent aec2158 commit 166363d
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group 'org.doremus'
version '1.3.1'
version '1.3.2'

sourceCompatibility = 1.8

Expand Down
39 changes: 36 additions & 3 deletions src/main/java/org/doremus/isnimatcher/ISNI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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, "");
Expand Down Expand Up @@ -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<ISNIRecord> 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) {
Expand All @@ -86,7 +112,7 @@ private static List<ISNIRecord> 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<String> response = request.asString();

Expand Down Expand Up @@ -131,4 +157,11 @@ static List<String> splitBody(String body) {
}


public static void setDebug(boolean debug) {
ISNI.debug = debug;
}

public static void setBestViafBehavior(boolean bestViafBehavior) {
ISNI.bestViafBehavior = bestViafBehavior;
}
}
13 changes: 10 additions & 3 deletions src/main/java/org/doremus/isnimatcher/ISNIRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public class ISNIRecord {

public List<Source> 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;
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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();
}
}
7 changes: 6 additions & 1 deletion src/main/java/org/doremus/isnimatcher/Source.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
}
21 changes: 21 additions & 0 deletions src/test/java/ISNITest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 166363d

Please sign in to comment.