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

Fix medline importer keywword #11414

Merged
merged 10 commits into from
Jun 24, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- We fixed an issue where the entry editor context menu was not shown correctly when JabRef is opened on a second, extended screen [#11323](https://github.com/JabRef/jabref/issues/11323), [#11174](https://github.com/JabRef/jabref/issues/11174)
- We fixed an issue where the value of "Override default font settings" was not applied on startup [#11344](https://github.com/JabRef/jabref/issues/11344)
- We fixed an issue when "Library changed on disk" appeared after a save by JabRef. [#4877](https://github.com/JabRef/jabref/issues/4877)
- We fixed an issue where the Pubmed/Medline Plain importer would not respect the user defined keyword separator [#11413](https://github.com/JabRef/jabref/issues/11413)
- We fixed an issue where the value of "Override default font settings" was not applied on startup [#11344](https://github.com/JabRef/jabref/issues/11344)
- We fixed an issue where DatabaseChangeDetailsView was not scrollable when reviewing external metadata changes [#11220](https://github.com/JabRef/jabref/issues/11220)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void reset() {
formats.add(new InspecImporter());
formats.add(new IsiImporter());
formats.add(new MedlineImporter());
formats.add(new MedlinePlainImporter());
formats.add(new MedlinePlainImporter(importFormatPreferences));
formats.add(new ModsImporter(importFormatPreferences));
formats.add(new MsBibImporter());
formats.add(new OvidImporter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Map;
import java.util.regex.Pattern;

import org.jabref.logic.importer.ImportFormatPreferences;
import org.jabref.logic.importer.Importer;
import org.jabref.logic.importer.ParserResult;
import org.jabref.logic.util.StandardFileType;
Expand All @@ -32,6 +33,11 @@ public class MedlinePlainImporter extends Importer {
private static final Pattern PMCR_PATTERN = Pattern.compile("PMCR.*-.*");
private static final Pattern CREATE_DATE_PATTERN = Pattern.compile("\\d{4}/[0123]?\\d/\\s?[012]\\d:[0-5]\\d");
private static final Pattern COMPLETE_DATE_PATTERN = Pattern.compile("\\d{8}");
private final ImportFormatPreferences importFormatPreferences;

public MedlinePlainImporter(ImportFormatPreferences importFormatPreferences) {
this.importFormatPreferences = importFormatPreferences;
}

@Override
public String getName() {
Expand Down Expand Up @@ -84,9 +90,9 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException {
}

EntryType type = BibEntry.DEFAULT_TYPE;
String author = "";
String editor = "";
String comment = "";
StringBuilder author = new StringBuilder();
StringBuilder editor = new StringBuilder();
StringBuilder comment = new StringBuilder();
Map<Field, String> fieldConversionMap = new HashMap<>();

String[] lines = entry1.split("\n");
Expand All @@ -101,7 +107,7 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException {
continue;
}
if (lines[j + 1].charAt(4) != '-') {
if ((current.length() > 0) && !Character.isWhitespace(current.charAt(current.length() - 1))) {
if ((!current.isEmpty()) && !Character.isWhitespace(current.charAt(current.length() - 1))) {
current.append(' ');
}
current.append(lines[j + 1].trim());
Expand All @@ -128,16 +134,16 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException {
addStandardNumber(fieldConversionMap, label, value);

if ("FAU".equals(label)) {
if ("".equals(author)) {
author = value;
if (author.isEmpty()) {
author = new StringBuilder(value);
} else {
author += " and " + value;
author.append(" and ").append(value);
}
} else if ("FED".equals(label)) {
if ("".equals(editor)) {
editor = value;
if (editor.isEmpty()) {
editor = new StringBuilder(value);
} else {
editor += " and " + value;
editor.append(" and ").append(value);
}
}

Expand Down Expand Up @@ -176,34 +182,47 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException {
}
}

if ("IRAD".equals(label) || "IR".equals(label) || "FIR".equals(label)) {
String oldInvestigator = fieldConversionMap.get(new UnknownField("investigator"));
if (oldInvestigator == null) {
fieldConversionMap.put(new UnknownField("investigator"), value);
} else {
fieldConversionMap.put(new UnknownField("investigator"), oldInvestigator + ", " + value);
switch (label) {
case "IRAD",
"IR",
"FIR" -> {
fieldConversionMap.merge(new UnknownField("investigator"), value, (a, b) -> a + ", " + b);
}
} else if ("MH".equals(label) || "OT".equals(label)) {
if (!fieldConversionMap.containsKey(StandardField.KEYWORDS)) {
fieldConversionMap.put(StandardField.KEYWORDS, value);
} else {
String kw = fieldConversionMap.get(StandardField.KEYWORDS);
fieldConversionMap.put(StandardField.KEYWORDS, kw + ", " + value);
case "MH",
"OT" -> {
if (!fieldConversionMap.containsKey(StandardField.KEYWORDS)) {
fieldConversionMap.put(StandardField.KEYWORDS, value);
} else {
fieldConversionMap.compute(StandardField.KEYWORDS, (k, kw) -> kw + importFormatPreferences.bibEntryPreferences().getKeywordSeparator() + " " + value);
}
}
} else if ("CON".equals(label) || "CIN".equals(label) || "EIN".equals(label) || "EFR".equals(label)
|| "CRI".equals(label) || "CRF".equals(label) || "PRIN".equals(label) || "PROF".equals(label)
|| "RPI".equals(label) || "RPF".equals(label) || "RIN".equals(label) || "ROF".equals(label)
|| "UIN".equals(label) || "UOF".equals(label) || "SPIN".equals(label) || "ORI".equals(label)) {
if (!comment.isEmpty()) {
comment = comment + "\n";
case "CON",
"CIN",
"EIN",
"EFR",
"CRI",
"CRF",
"PRIN",
"PROF",
"RPI",
"RPF",
"RIN",
"ROF",
"UIN",
"UOF",
"SPIN",
"ORI" -> {
if (!comment.isEmpty()) {
comment.append("\n");
}
comment.append(value);
}
comment = comment + value;
}
}
fixAuthors(fieldConversionMap, author, StandardField.AUTHOR);
fixAuthors(fieldConversionMap, editor, StandardField.EDITOR);
fixAuthors(fieldConversionMap, author.toString(), StandardField.AUTHOR);
fixAuthors(fieldConversionMap, editor.toString(), StandardField.EDITOR);
if (!comment.isEmpty()) {
fieldConversionMap.put(StandardField.COMMENT, comment);
fieldConversionMap.put(StandardField.COMMENT, comment.toString());
}

BibEntry b = new BibEntry(type);
Expand All @@ -222,29 +241,29 @@ private boolean checkLineValidity(String line) {

private EntryType addSourceType(String value, EntryType type) {
String val = value.toLowerCase(Locale.ENGLISH);
switch (val) {
case "book":
return StandardEntryType.Book;
case "journal article":
case "classical article":
case "corrected and republished article":
case "historical article":
case "introductory journal article":
case "newspaper article":
return StandardEntryType.Article;
case "clinical conference":
case "consensus development conference":
case "consensus development conference, nih":
return StandardEntryType.Conference;
case "technical report":
return StandardEntryType.TechReport;
case "editorial":
return StandardEntryType.InProceedings;
case "overall":
return StandardEntryType.Proceedings;
default:
return type;
}
return switch (val) {
case "book" ->
StandardEntryType.Book;
case "journal article",
"classical article",
"corrected and republished article",
"historical article",
"introductory journal article",
"newspaper article" ->
StandardEntryType.Article;
case "clinical conference",
"consensus development conference",
"consensus development conference, nih" ->
StandardEntryType.Conference;
case "technical report" ->
StandardEntryType.TechReport;
case "editorial" ->
StandardEntryType.InProceedings;
case "overall" ->
StandardEntryType.Proceedings;
default ->
type;
};
}

private void addStandardNumber(Map<Field, String> hm, String lab, String value) {
Expand Down Expand Up @@ -333,7 +352,7 @@ private void addTitles(Map<Field, String> hm, String lab, String val, EntryType
}

private void addAbstract(Map<Field, String> hm, String lab, String value) {
String abstractValue = "";
String abstractValue;
if ("AB".equals(lab)) {
// adds copyright information that comes at the end of an abstract
if (value.contains("Copyright")) {
Expand All @@ -345,12 +364,7 @@ private void addAbstract(Map<Field, String> hm, String lab, String value) {
} else {
abstractValue = value;
}
String oldAb = hm.get(StandardField.ABSTRACT);
if (oldAb == null) {
hm.put(StandardField.ABSTRACT, abstractValue);
} else {
hm.put(StandardField.ABSTRACT, oldAb + '\n' + abstractValue);
}
hm.merge(StandardField.ABSTRACT, abstractValue, (a, b) -> a + '\n' + b);
} else if ("OAB".equals(lab) || "OABL".equals(lab)) {
hm.put(new UnknownField("other-abstract"), value);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/model/entry/field/StandardField.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

/**
* Standard BibTeX and BibLaTeX fields, as well as "normal" JabRef specific fields.
*
* <p>
* See {@link FieldNameLabel#getDescription(org.jabref.model.entry.field.Field)} for a description of each field.
*/
public enum StandardField implements Field {
Expand Down Expand Up @@ -141,7 +141,7 @@ public enum StandardField implements Field {
CREATIONDATE("creationdate", FieldProperty.DATE),
MODIFICATIONDATE("modificationdate", FieldProperty.DATE);

public static Set<Field> AUTOMATIC_FIELDS = Set.of(OWNER, TIMESTAMP, CREATIONDATE, MODIFICATIONDATE);
public static final Set<Field> AUTOMATIC_FIELDS = Set.of(OWNER, TIMESTAMP, CREATIONDATE, MODIFICATIONDATE);

private static final Map<String, StandardField> NAME_TO_STANDARD_FIELD = new HashMap<>();

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/jabref/logic/importer/ImporterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public static Stream<Importer> instancesToTest() {
new InspecImporter(),
new IsiImporter(),
new MedlineImporter(),
new MedlinePlainImporter(),
new MedlinePlainImporter(importFormatPreferences),
new ModsImporter(importFormatPreferences),
new MsBibImporter(),
new OvidImporter(),
Expand Down
Loading
Loading