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

Improve pdf content parser for DOIs #11782

Merged
merged 8 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -48,6 +48,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- The browse button for a Custom exporter now opens in the directory of the current used exporter file. [#11717](https://github.com/JabRef/jabref/pull/11717)
- We improved the display of long messages in the integrity check dialog. [#11619](https://github.com/JabRef/jabref/pull/11619)
- We improved the undo/redo buttons in the main toolbar and main menu to be disabled when there is nothing to undo/redo. [#8807](https://github.com/JabRef/jabref/issues/8807)
- We improved the DOI detection in PDF imports [#11782](https://github.com/JabRef/jabref/pull/11782)

### Fixed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.LinkedFile;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.identifier.DOI;
import org.jabref.model.entry.types.EntryType;
import org.jabref.model.entry.types.StandardEntryType;
import org.jabref.model.strings.StringUtil;
Expand Down Expand Up @@ -241,7 +242,7 @@ Optional<BibEntry> getEntryFromPDFContent(String firstpageContents, String lineS
String keywords = null;
String title;
String conference = null;
String DOI = null;
String doi = null;
String series = null;
String volume = null;
String number = null;
Expand All @@ -253,6 +254,7 @@ Optional<BibEntry> getEntryFromPDFContent(String firstpageContents, String lineS
if (curString.length() > 4) {
// special case: possibly conference as first line on the page
extractYear();
doi = getDoi(null);
if (curString.contains("Conference")) {
fillCurStringWithNonEmptyLines();
conference = curString;
Expand Down Expand Up @@ -384,27 +386,7 @@ Optional<BibEntry> getEntryFromPDFContent(String firstpageContents, String lineS
}
}
} else {
if (DOI == null) {
pos = curString.indexOf("DOI");
if (pos < 0) {
pos = curString.indexOf(StandardField.DOI.getName());
}
if (pos >= 0) {
pos += 3;
if (curString.length() > pos) {
char delimiter = curString.charAt(pos);
if ((delimiter == ':') || (delimiter == ' ')) {
pos++;
}
int nextSpace = curString.indexOf(' ', pos);
if (nextSpace > 0) {
DOI = curString.substring(pos, nextSpace);
} else {
DOI = curString.substring(pos);
}
}
}
}
doi = getDoi(doi);

if ((publisher == null) && curString.contains("IEEE")) {
// IEEE has the conference things at the end
Expand Down Expand Up @@ -459,8 +441,8 @@ Optional<BibEntry> getEntryFromPDFContent(String firstpageContents, String lineS
if (conference != null) {
entry.setField(StandardField.BOOKTITLE, conference);
}
if (DOI != null) {
entry.setField(StandardField.DOI, DOI);
if (doi != null) {
entry.setField(StandardField.DOI, doi);
}
if (series != null) {
entry.setField(StandardField.SERIES, series);
Expand All @@ -483,6 +465,20 @@ Optional<BibEntry> getEntryFromPDFContent(String firstpageContents, String lineS
return Optional.of(entry);
}

private String getDoi(String doi) {
int pos;
if (doi == null) {
pos = curString.indexOf("DOI");
if (pos < 0) {
pos = curString.indexOf(StandardField.DOI.getName());
}
if (pos >= 0) {
return DOI.findInText(curString).map(DOI::getDOI).orElse(null);
}
}
return doi;
}

private String getFirstPageContents(PDDocument document) throws IOException {
PDFTextStripper stripper = new PDFTextStripper();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,20 @@ void parsingEditorWithoutPagesorSeriesInformation() {
entry.setField(StandardField.TITLE, "Corpus Linguistics – An International Handbook – Lüdeling, Anke, Kytö, Merja (Eds.)");

String firstPageContents = "Corpus Linguistics – An International Handbook – Lüdeling, Anke,\n" +
"Kytö, Merja (Eds.)\n" +
"\n" +
"Anke Lüdeling, Merja Kytö (Eds.)\n" +
"\n" +
"VOLUME 2\n" +
"\n" +
"This handbook provides an up-to-date survey of the field of corpus linguistics, a Handbücher zur Sprach- und\n" +
"field whose methodology has revolutionized much of the empirical work done in Kommunikationswissenschaft / Handbooks\n" +
"\n" +
"of Linguistics and Communication Science\n" +
"most fields of linguistic study over the past decade. (HSK) 29/2\n" +
"\n" +
"vii, 578 pages\n" +
"Corpus linguistics investigates human language by starting out from large\n";
"Kytö, Merja (Eds.)\n" +
"\n" +
"Anke Lüdeling, Merja Kytö (Eds.)\n" +
"\n" +
"VOLUME 2\n" +
"\n" +
"This handbook provides an up-to-date survey of the field of corpus linguistics, a Handbücher zur Sprach- und\n" +
"field whose methodology has revolutionized much of the empirical work done in Kommunikationswissenschaft / Handbooks\n" +
"\n" +
"of Linguistics and Communication Science\n" +
"most fields of linguistic study over the past decade. (HSK) 29/2\n" +
"\n" +
"vii, 578 pages\n" +
"Corpus linguistics investigates human language by starting out from large\n";

assertEquals(Optional.of(entry), importer.getEntryFromPDFContent(firstPageContents, "\n"));
}
Expand All @@ -80,17 +80,54 @@ void parsingWithoutActualDOINumber() {
.withField(StandardField.TITLE, "Kent Academic Repository Full text document (pdf) Citation for published version Smith, Lucy Anna (2014) Mortality in the Ornamental Fish Retail Sector: an Analysis of Stock Losses and Stakeholder Opinions. DOI")
.withField(StandardField.YEAR, "5104");

String firstPageContents = "Kent Academic Repository Full text document (pdf)\n"
+ "Citation for published version\n"
+ "Smith, Lucy Anna (2014) Mortality in the Ornamental Fish Retail Sector: an Analysis of Stock\n"
+ "Losses and Stakeholder Opinions.\n"
+ "DOI\n\n\n"
+ "Link to record in KAR\n"
+ "http://kar.kent.ac.uk/51043/\n"
+ "Document Version\n"
+ "UNSPECIFIED\n"
+ "Master of Research (MRes) thesis, University of Kent,.";
String firstPageContents = """
Kent Academic Repository Full text document (pdf)
Citation for published version
Smith, Lucy Anna (2014) Mortality in the Ornamental Fish Retail Sector: an Analysis of Stock
Losses and Stakeholder Opinions.
DOI


Link to record in KAR
http://kar.kent.ac.uk/51043/
Document Version
UNSPECIFIED
Master of Research (MRes) thesis, University of Kent,.""";

assertEquals(Optional.of(entry), importer.getEntryFromPDFContent(firstPageContents, "\n"));
}

@Test
void extractDOIFromPage1() {
String firstPageContent = """
British Journal of Nutrition (2008), 99, 1–11 doi: 10.1017/S0007114507795296
q The Authors 2008

Review Article

Cocoa and health: a decade of research

Karen A. Cooper1, Jennifer L. Donovan2, Andrew L. Waterhouse3 and Gary Williamson1*
1Nestlé Research Center, Vers-Chez-les-Blanc, PO Box 44, CH-1000 Lausanne 26, Switzerland
2Department of Psychiatry and Behavioural Sciences, Medical University of South Carolina, Charleston, SC 29425, USA
3Department of Viticulture & Enology, University of California, Davis, CA 95616, USA

(Received 5 December 2006 – Revised 29 May 2007 – Accepted 31 May 2007)

Abbreviations: FMD, flow-mediated dilation; NO, nitirc oxide.

*Corresponding author: Dr Gary Williamson, fax þ41 21 785 8544, email [email protected]

British Journal of Nutrition
https://doi.org/10.1017/S0007114507795296 Published online by Cambridge University Press
""";

BibEntry entry = new BibEntry(StandardEntryType.InProceedings);
entry.setField(StandardField.DOI, "10.1017/S0007114507795296");
entry.setField(StandardField.AUTHOR, "Review Article");
entry.setField(StandardField.TITLE, "British Journal of Nutrition (2008), 99, 1–11 doi: 10.1017/S0007114507795296 q The Authors");
entry.setField(StandardField.YEAR, "2008");

assertEquals(Optional.of(entry), importer.getEntryFromPDFContent(firstPageContent, "\n"));
}
}
Loading