Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zbmath fetcher #7440

Merged
merged 20 commits into from
Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve

### Added

- We added zbmath to the public databases from which the bibliographic information of an existing entry can be updated. [#7437](https://github.com/JabRef/jabref/issues/7437)
- We added the possibility to add a new entry via its zbMath ID (zbMATH can be chosen as ID type in the "Select entry type" window). [#7202](https://github.com/JabRef/jabref/issues/7202)
- We added the extension support and the external application support (For Texshow, Texmaker and LyX) to the flatpak [#7248](https://github.com/JabRef/jabref/pull/7248)
- We added some symbols and keybindings to the context menu in the entry editor. [#7268](https://github.com/JabRef/jabref/pull/7268)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -54,15 +55,22 @@ default void doPostCleanup(BibEntry entry) {
default List<BibEntry> performSearch(BibEntry entry) throws FetcherException {
Objects.requireNonNull(entry);

try (InputStream stream = new BufferedInputStream(getURLForEntry(entry).openStream())) {
URL UrlForEntry;
try {
if ((UrlForEntry = getURLForEntry(entry)) == null) {
return Collections.emptyList();
}
} catch (MalformedURLException | URISyntaxException e) {
throw new FetcherException("Search URI is malformed", e);
}

try (InputStream stream = new BufferedInputStream(UrlForEntry.openStream())) {
List<BibEntry> fetchedEntries = getParser().parseEntries(stream);

// Post-cleanup
fetchedEntries.forEach(this::doPostCleanup);

return fetchedEntries;
} catch (URISyntaxException e) {
throw new FetcherException("Search URI is malformed", e);
} catch (IOException e) {
// TODO: Catch HTTP Response 401 errors and report that user has no rights to access resource
throw new FetcherException("A network error occurred", e);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/jabref/logic/importer/WebFetchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public static SortedSet<EntryBasedFetcher> getEntryBasedFetchers(ImportFormatPre
set.add(new IsbnFetcher(importFormatPreferences));
set.add(new MathSciNet(importFormatPreferences));
set.add(new CrossRef());
set.add(new ZbMATH(importFormatPreferences));
return set;
}

Expand Down
71 changes: 62 additions & 9 deletions src/main/java/org/jabref/logic/importer/fetcher/ZbMATH.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import org.jabref.logic.cleanup.FieldFormatterCleanup;
import org.jabref.logic.cleanup.MoveFieldCleanup;
import org.jabref.logic.formatter.bibtexfields.RemoveBracesFormatter;
import org.jabref.logic.importer.EntryBasedParserFetcher;
import org.jabref.logic.importer.FetcherException;
import org.jabref.logic.importer.IdBasedParserFetcher;
import org.jabref.logic.importer.ImportFormatPreferences;
Expand All @@ -16,18 +19,23 @@
import org.jabref.logic.importer.fetcher.transformators.ZbMathQueryTransformer;
import org.jabref.logic.importer.fileformat.BibtexParser;
import org.jabref.logic.net.URLDownload;
import org.jabref.model.entry.AuthorList;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.field.UnknownField;
import org.jabref.model.util.DummyFileUpdateMonitor;

import kong.unirest.HttpResponse;
import kong.unirest.JsonNode;
import kong.unirest.Unirest;
import kong.unirest.json.JSONArray;
import org.apache.http.client.utils.URIBuilder;
import org.apache.lucene.queryparser.flexible.core.nodes.QueryNode;

/**
* Fetches data from the Zentralblatt Math (https://www.zbmath.org/)
*/
public class ZbMATH implements SearchBasedParserFetcher, IdBasedParserFetcher {
public class ZbMATH implements SearchBasedParserFetcher, IdBasedParserFetcher, EntryBasedParserFetcher {

private final ImportFormatPreferences preferences;

Expand All @@ -40,17 +48,62 @@ public String getName() {
return "zbMATH";
}

/**
* TODO: Implement EntryBasedParserFetcher
* We use the zbMATH Citation matcher (https://www.zbmath.org/citationmatching/)
* instead of the usual search since this tool is optimized for finding a publication based on partial information.
*/
/*
@Override
public URL getURLForEntry(BibEntry entry) throws URISyntaxException, MalformedURLException, FetcherException {
// Example: https://zbmath.org/citationmatching/match?q=Ratiu
Optional<String> zblidInEntry = entry.getField(StandardField.ZBL_NUMBER);
if (zblidInEntry.isPresent()) {
// zbmath id is already present
return getUrlForIdentifier(zblidInEntry.get());
}

URIBuilder uriBuilder = new URIBuilder("https://zbmath.org/citationmatching/match");
uriBuilder.addParameter("n", "1"); // return only the best matching entry
uriBuilder.addParameter("m", "5"); // return only entries with a score of at least 5

entry.getFieldOrAlias(StandardField.TITLE).ifPresent(title -> uriBuilder.addParameter("t", title));
entry.getFieldOrAlias(StandardField.JOURNAL).ifPresent(journal -> uriBuilder.addParameter("j", journal));
entry.getFieldOrAlias(StandardField.YEAR).ifPresent(year -> uriBuilder.addParameter("y", year));
entry.getFieldOrAlias(StandardField.PAGINATION)
.ifPresent(pagination -> uriBuilder.addParameter("p", pagination));
entry.getFieldOrAlias(StandardField.VOLUME).ifPresent(volume -> uriBuilder.addParameter("v", volume));
entry.getFieldOrAlias(StandardField.ISSUE).ifPresent(issue -> uriBuilder.addParameter("i", issue));

if (entry.getFieldOrAlias(StandardField.AUTHOR).isPresent()) {
// replace "and" by ";" as citation matching API uses ";" for separation
AuthorList authors = AuthorList.parse(entry.getFieldOrAlias(StandardField.AUTHOR).get());
String authorsWithSemicolon = authors.getAuthors().stream()
.map(author -> author.getLastFirst(false))
.collect(Collectors.joining(";"));
uriBuilder.addParameter("a", authorsWithSemicolon);
}

/*
zbmath citation matching API does only return json, thus we use the
citation matching API to extract the zbl_id and then use getUrlForIdentifier
to get the bibtex data.
*/
String urlString = uriBuilder.build().toString();
HttpResponse<JsonNode> response = Unirest.get(urlString)
ibe-314 marked this conversation as resolved.
Show resolved Hide resolved
.asJson();
String zblid = null;
if (response.getStatus() == 200) {
JSONArray result = response.getBody()
.getObject()
.getJSONArray("results");
if (result.length() > 0) {
zblid = result.getJSONObject(0)
.get("zbl_id")
.toString();
}
}
if (zblid == null) {
// citation matching API found no matching entry
return null;
} else {
return getUrlForIdentifier(zblid);
}
}
*/

@Override
public URL getURLForQuery(QueryNode luceneQuery) throws URISyntaxException, MalformedURLException, FetcherException {
URIBuilder uriBuilder = new URIBuilder("https://zbmath.org/bibtexoutput/");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public enum StandardField implements Field {
YEAR("year", FieldProperty.NUMERIC),
YEARFILED("yearfiled"),
MR_NUMBER("mrnumber"),
ZBL_NUMBER("zbl"),
XDATA("xdata", FieldProperty.MULTIPLE_ENTRY_LINK),
XREF("xref", FieldProperty.SINGLE_ENTRY_LINK),

Expand Down
32 changes: 30 additions & 2 deletions src/test/java/org/jabref/logic/importer/fetcher/ZbMATHTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.jabref.logic.importer.ImportFormatPreferences;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.field.UnknownField;
import org.jabref.model.entry.types.StandardEntryType;
import org.jabref.testutils.category.FetcherTest;

Expand Down Expand Up @@ -44,7 +43,7 @@ void setUp() throws Exception {
donaldsonEntry.setField(StandardField.TITLE, "An application of gauge theory to four dimensional topology");
donaldsonEntry.setField(StandardField.VOLUME, "18");
donaldsonEntry.setField(StandardField.YEAR, "1983");
donaldsonEntry.setField(new UnknownField("zbl"), "0507.57010");
donaldsonEntry.setField(StandardField.ZBL_NUMBER, "0507.57010");
}

@Test
Expand All @@ -58,4 +57,33 @@ void searchByIdFindsEntry() throws Exception {
Optional<BibEntry> fetchedEntry = fetcher.performSearchById("0507.57010");
assertEquals(Optional.of(donaldsonEntry), fetchedEntry);
}

@Test
void searchByEntryFindsEntry() throws Exception {
BibEntry searchEntry = new BibEntry();
searchEntry.setField(StandardField.TITLE, "An application of gauge theory to four dimensional topology");
searchEntry.setField(StandardField.AUTHOR, "S. K. {Donaldson}");

List<BibEntry> fetchedEntries = fetcher.performSearch(searchEntry);
assertEquals(Collections.singletonList(donaldsonEntry), fetchedEntries);
}

@Test
void searchByNoneEntryFindsNothing() throws Exception {
BibEntry searchEntry = new BibEntry();
searchEntry.setField(StandardField.TITLE, "t");
ibe-314 marked this conversation as resolved.
Show resolved Hide resolved
searchEntry.setField(StandardField.AUTHOR, "a");

List<BibEntry> fetchedEntries = fetcher.performSearch(searchEntry);
assertEquals(Collections.emptyList(), fetchedEntries);
}

@Test
void searchByIdInEntryFindsEntry() throws Exception {
BibEntry searchEntry = new BibEntry();
searchEntry.setField(StandardField.ZBL_NUMBER, "0507.57010");

List<BibEntry> fetchedEntries = fetcher.performSearch(searchEntry);
assertEquals(Collections.singletonList(donaldsonEntry), fetchedEntries);
}
}