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

Refactor processCitation in GrobidService to match processPdf #8003

Merged
merged 6 commits into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
import org.jabref.logic.importer.ImportFormatPreferences;
import org.jabref.logic.importer.ParseException;
import org.jabref.logic.importer.SearchBasedFetcher;
import org.jabref.logic.importer.fileformat.BibtexParser;
import org.jabref.logic.importer.util.GrobidService;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.util.DummyFileUpdateMonitor;

import org.apache.lucene.queryparser.flexible.core.nodes.QueryNode;
import org.slf4j.Logger;
Expand Down Expand Up @@ -45,29 +43,20 @@ public GrobidCitationFetcher(ImportFormatPreferences importFormatPreferences) {
*
* @return A BibTeX string if extraction is successful
*/
private Optional<String> parseUsingGrobid(String plainText) throws RuntimeException {
private Optional<BibEntry> parseUsingGrobid(String plainText) throws RuntimeException {
try {
return Optional.of(grobidService.processCitation(plainText, GrobidService.ConsolidateCitations.WITH_METADATA));
return grobidService.processCitation(plainText, importFormatPreferences, GrobidService.ConsolidateCitations.WITH_METADATA);
} catch (SocketTimeoutException e) {
String msg = "Connection timed out.";
LOGGER.debug(msg, e);
throw new RuntimeException(msg, e);
} catch (IOException e) {
} catch (IOException | ParseException e) {
String msg = "Could not process citation. " + e.getMessage();
LOGGER.debug(msg, e);
return Optional.empty();
}
}

private Optional<BibEntry> parseBibToBibEntry(String bibtexString) {
try {
return BibtexParser.singleFromString(bibtexString,
importFormatPreferences, new DummyFileUpdateMonitor());
} catch (ParseException e) {
return Optional.empty();
}
}

@Override
public String getName() {
return "GROBID";
Expand All @@ -82,8 +71,6 @@ public List<BibEntry> performSearch(String searchQuery) throws FetcherException
.filter(str -> !str.isBlank())
.map(this::parseUsingGrobid)
.flatMap(Optional::stream)
.map(this::parseBibToBibEntry)
.flatMap(Optional::stream)
.collect(Collectors.toList());
} catch (RuntimeException e) {
throw new FetcherException(e.getMessage(), e.getCause());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;

import org.jabref.logic.importer.ImportFormatPreferences;
import org.jabref.logic.importer.ParseException;
Expand Down Expand Up @@ -46,12 +47,12 @@ public GrobidService(String grobidServerURL) {
}

/**
* Calls the Grobid server for converting the citation into BibTeX
* Calls the Grobid server for converting the citation into a BibEntry
*
* @return A plain BibTeX string (generated by the Grobid server)
* @return A BibEntry for the String
* @throws IOException if an I/O excecption during the call ocurred or no BibTeX entry could be determiend
*/
public String processCitation(String rawCitation, ConsolidateCitations consolidateCitations) throws IOException {
public Optional<BibEntry> processCitation(String rawCitation, ImportFormatPreferences importFormatPreferences, ConsolidateCitations consolidateCitations) throws IOException, ParseException {
Connection.Response response = Jsoup.connect(grobidServerURL + "/api/processCitation")
.header("Accept", MediaTypes.APPLICATION_BIBTEX)
.data("citations", rawCitation)
Expand All @@ -66,7 +67,7 @@ public String processCitation(String rawCitation, ConsolidateCitations consolida
throw new IOException("The GROBID server response does not contain anything.");
}

return httpResponse;
return BibtexParser.singleFromString(httpResponse, importFormatPreferences, new DummyFileUpdateMonitor());
}

public List<BibEntry> processPDF(Path filePath, ImportFormatPreferences importFormatPreferences) throws IOException, ParseException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@

import org.jabref.logic.importer.FetcherException;
import org.jabref.logic.importer.ImportFormatPreferences;
import org.jabref.logic.importer.ParseException;
import org.jabref.logic.importer.util.GrobidService;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.types.StandardEntryType;
import org.jabref.testutils.category.FetcherTest;

import org.antlr.v4.runtime.misc.ParseCancellationException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand Down Expand Up @@ -116,9 +118,9 @@ public void grobidPerformSearchWithInvalidDataTest(String invalidInput) throws F
}

@Test
public void performSearchThrowsExceptionInCaseOfConnectionIssues() throws IOException {
public void performSearchThrowsExceptionInCaseOfConnectionIssues() throws IOException, ParseException {
GrobidService grobidServiceMock = mock(GrobidService.class);
when(grobidServiceMock.processCitation(anyString(), any())).thenThrow(new SocketTimeoutException("Timeout"));
when(grobidServiceMock.processCitation(anyString(), any(), any())).thenThrow(new SocketTimeoutException("Timeout"));
grobidCitationFetcher = new GrobidCitationFetcher(importFormatPreferences, grobidServiceMock);

assertThrows(FetcherException.class, () -> {
Expand Down
45 changes: 23 additions & 22 deletions src/test/java/org/jabref/logic/importer/util/GrobidServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
import org.jabref.logic.importer.fileformat.PdfGrobidImporterTest;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.types.StandardEntryType;
import org.jabref.testutils.category.FetcherTest;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.Answers;

import static org.junit.Assert.assertTrue;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have the wrong junit, it. must be import static org.junit.jupiter.api.Assertions.assertTrue;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand All @@ -38,37 +40,36 @@ public static void setup() {
}

@Test
public void processValidCitationTest() throws IOException {
String expectedResponse =
"@article{-1,\n" +
" author = {Derwing, Tracey and Rossiter, Marian and Munro, Murray},\n" +
" title = {Teaching Native Speakers to Listen to Foreign-accented Speech},\n" +
" journal = {Journal of Multilingual and Multicultural Development},\n" +
" publisher = {Informa UK Limited},\n" +
" date = {2002-09},\n" +
" year = {2002},\n" +
" month = {9},\n" +
" pages = {245-259},\n" +
" volume = {23},\n" +
" number = {4},\n" +
" doi = {10.1080/01434630208666468}\n" +
"}\n";
String response = grobidService.processCitation("Derwing, T. M., Rossiter, M. J., & Munro, " +
public void processValidCitationTest() throws IOException, ParseException {
BibEntry exampleBibEntry = new BibEntry(StandardEntryType.Article).withCitationKey("-1")
.withField(StandardField.AUTHOR, "Derwing, Tracey and Rossiter, Marian and Munro, Murray")
.withField(StandardField.TITLE, "Teaching Native Speakers to Listen to Foreign-accented Speech")
.withField(StandardField.JOURNAL, "Journal of Multilingual and Multicultural Development")
.withField(StandardField.DOI, "10.1080/01434630208666468")
.withField(StandardField.DATE, "2002-09")
.withField(StandardField.YEAR, "2002")
.withField(StandardField.MONTH, "9")
.withField(StandardField.PAGES, "245-259")
.withField(StandardField.VOLUME, "23")
.withField(StandardField.PUBLISHER, "Informa UK Limited")
.withField(StandardField.NUMBER, "4");
Optional<BibEntry> response = grobidService.processCitation("Derwing, T. M., Rossiter, M. J., & Munro, " +
"M. J. (2002). Teaching native speakers to listen to foreign-accented speech. " +
"Journal of Multilingual and Multicultural Development, 23(4), 245-259.", GrobidService.ConsolidateCitations.WITH_METADATA);
assertEquals(expectedResponse, response);
"Journal of Multilingual and Multicultural Development, 23(4), 245-259.", importFormatPreferences, GrobidService.ConsolidateCitations.WITH_METADATA);
assertTrue(response.isPresent());
assertEquals(exampleBibEntry, response.get());
}

@Test
public void processEmptyStringTest() throws IOException {
String response = grobidService.processCitation(" ", GrobidService.ConsolidateCitations.WITH_METADATA);
public void processEmptyStringTest() throws IOException, ParseException {
Optional<BibEntry> response = grobidService.processCitation(" ", importFormatPreferences, GrobidService.ConsolidateCitations.WITH_METADATA);
assertNotNull(response);
assertEquals("", response);
assertTrue(response.isEmpty());
}

@Test
public void processInvalidCitationTest() {
assertThrows(IOException.class, () -> grobidService.processCitation("iiiiiiiiiiiiiiiiiiiiiiii", GrobidService.ConsolidateCitations.WITH_METADATA));
assertThrows(IOException.class, () -> grobidService.processCitation("iiiiiiiiiiiiiiiiiiiiiiii", importFormatPreferences, GrobidService.ConsolidateCitations.WITH_METADATA));
}

@Test
Expand Down