Skip to content

Commit

Permalink
Fix author parsing of ACM
Browse files Browse the repository at this point in the history
Fix cli debug log option

Fixes #10107
Follow up from #10137
  • Loading branch information
Siedlerchr committed Aug 4, 2023
1 parent 7d02ee9 commit bacd82d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We fixed a bug where the editor for strings in a bibliography file did not sort the entries by their keys [#10083](https://github.com/JabRef/jabref/pull/10083)
- We fixed an issues where clicking on the empty space of specific context menu entries would not trigger the associated action. [#8388](https://github.com/JabRef/jabref/issues/8388)
- We fixed an issue where JabRef would not remember if the window was in fullscreen or not [#4939](https://github.com/JabRef/jabref/issues/4939)
- We fixed an issue where the ACM Portal search sometimes would not return entries for some search queries when the article author had no given name [#10107](https://github.com/JabRef/jabref/issues/10107)

### Removed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import java.util.stream.Collectors;

import org.jabref.logic.importer.FetcherException;
import org.jabref.logic.importer.ParseException;
import org.jabref.logic.importer.Parser;
import org.jabref.logic.net.URLDownload;
import org.jabref.model.entry.Author;
import org.jabref.model.entry.AuthorList;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.types.StandardEntryType;
Expand Down Expand Up @@ -150,14 +151,8 @@ public BibEntry parseBibEntry(String jsonStr) {

if (jsonObject.has("author")) {
JsonArray authors = jsonObject.getAsJsonArray("author");
StringJoiner authorsJoiner = new StringJoiner(" and ");
for (JsonElement author : authors) {
JsonObject authorJsonObject = author.getAsJsonObject();
authorsJoiner.add(
authorJsonObject.get("given").getAsString() + " " + authorJsonObject.get("family").getAsString()
);
}
bibEntry.setField(StandardField.AUTHOR, authorsJoiner.toString());
String finalAuthors = AuthorList.of(getAuthors(authors)).getAsLastFirstNamesWithAnd(false);
bibEntry.setField(StandardField.AUTHOR, finalAuthors);
}

if (jsonObject.has("issued")) {
Expand Down Expand Up @@ -227,4 +222,24 @@ public BibEntry parseBibEntry(String jsonStr) {

return bibEntry;
}

private List<Author> getAuthors(JsonArray authors) {
List<Author> jabrefAuthors = new ArrayList<>();

for (JsonElement author : authors) {
JsonObject authorJsonObject = author.getAsJsonObject();

String given = null;
String family = null;
if (authorJsonObject.has("given")) {
given = authorJsonObject.get("given").getAsString();
}
if (authorJsonObject.has("family")) {
family = authorJsonObject.get("family").getAsString();
}
Author jabrefAuthor = new Author(given, null, null, family, null);
jabrefAuthors.add(jabrefAuthor);
}
return jabrefAuthors;
}
}
11 changes: 5 additions & 6 deletions src/main/java/org/jabref/logic/l10n/Localization.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.Set;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
Expand All @@ -35,7 +34,6 @@
public class Localization {
static final String RESOURCE_PREFIX = "l10n/JabRef";

private static final Logger LOGGER = LoggerFactory.getLogger(Localization.class);

private static Locale locale;
private static LocalizationBundle localizedMessages;
Expand All @@ -52,8 +50,9 @@ private Localization() {
*/
public static String lang(String key, Object... params) {
if (localizedMessages == null) {

// I'm logging this because it should never happen
LOGGER.error("Messages are not initialized before accessing key: {}", key);
System.err.println("Messages are not initialized before accessing key: " + key);
setLanguage(Language.ENGLISH);
}
var stringParams = Arrays.stream(params).map(Object::toString).toArray(String[]::new);
Expand All @@ -71,7 +70,7 @@ public static void setLanguage(Language language) {
Optional<Locale> knownLanguage = Language.convertToSupportedLocale(language);
final Locale defaultLocale = Locale.getDefault();
if (knownLanguage.isEmpty()) {
LOGGER.warn("Language {} is not supported by JabRef (Default: {})", language, defaultLocale);
LoggerFactory.getLogger(Localization.class).warn("Language {} is not supported by JabRef (Default: {})", language, defaultLocale);
setLanguage(Language.ENGLISH);
return;
}
Expand All @@ -87,7 +86,7 @@ public static void setLanguage(Language language) {
createResourceBundles(locale);
} catch (MissingResourceException ex) {
// should not happen as we have scripts to enforce this
LOGGER.warn("Could not find bundles for language " + locale + ", switching to full english language", ex);
LoggerFactory.getLogger(Localization.class).warn("Could not find bundles for language " + locale + ", switching to full english language", ex);
setLanguage(Language.ENGLISH);
}
}
Expand Down Expand Up @@ -148,7 +147,7 @@ private static String lookup(LocalizationBundle bundle, String key, String... pa

String translation = bundle.containsKey(key) ? bundle.getString(key) : "";
if (translation.isEmpty()) {
LOGGER.warn("Warning: could not get translation for \"{}\" for locale {}", key, Locale.getDefault());
LoggerFactory.getLogger(Localization.class).warn("Warning: could not get translation for \"{}\" for locale {}", key, Locale.getDefault());
translation = key;
}
return new LocalizationKeyParams(translation, params).replacePlaceholders();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void setUp() throws URISyntaxException, MalformedURLException {
.addParameter("AllField", searchQuery).build().toURL();
searchEntryList = List.of(
new BibEntry(StandardEntryType.Conference)
.withField(StandardField.AUTHOR, "Tobias Olsson and Morgan Ericsson and Anna Wingkvist")
.withField(StandardField.AUTHOR, "Olsson, Tobias and Ericsson, Morgan and Wingkvist, Anna")
.withField(StandardField.YEAR, "2017")
.withField(StandardField.MONTH, "9")
.withField(StandardField.DAY, "11")
Expand Down Expand Up @@ -112,6 +112,31 @@ void testParseBibEntry() {
assertEquals(searchEntryList.get(0), bibEntry);
}

@Test
void testParseBibEntryWithFamilyAuthorOnly() {
String json = "{\"id\":\"10.1145/3011077.3011113\",\"type\":\"PAPER_CONFERENCE\",\"author\":[{\"family\":\"Ngo-Thi-Thu-Trang\"},{\"family\":\"Bui\",\"given\":\"Hieu T.\"},{\"family\":\"Nguyen\",\"given\":\"Nhan D.\"}],\"accessed\":{\"date-parts\":[[2023,8,4]]},\"issued\":{\"date-parts\":[[2016,12,8]]},\"original-date\":{\"date-parts\":[[2016,12,8]]},\"abstract\":\"\",\"call-number\":\"10.1145/3011077.3011113\",\"collection-title\":\"SoICT '16\",\"container-title\":\"Proceedings of the 7th Symposium on Information and Communication Technology\",\"DOI\":\"10.1145/3011077.3011113\",\"event-place\":\"Ho Chi Minh City, Vietnam\",\"ISBN\":\"9781450348157\",\"keyword\":\"orthogonal frequency division multiplexing (OFDM), long-range passive optical network (LR PON), four-wave mixing (FWM), wavelength division multiplexing (WDM)\",\"number-of-pages\":\"6\",\"page\":\"216–221\",\"publisher\":\"Association for Computing Machinery\",\"publisher-place\":\"New York, NY, USA\",\"title\":\"A simple performance analysis of IM-DD OFDM WDM systems in long range PON application\",\"URL\":\"https://doi.org/10.1145/3011077.3011113\"}";
BibEntry expectedEntry = new BibEntry(StandardEntryType.Conference)
.withField(StandardField.AUTHOR, "Ngo-Thi-Thu-Trang and Bui, Hieu T. and Nguyen, Nhan D.")
.withField(StandardField.TITLE, "A simple performance analysis of IM-DD OFDM WDM systems in long range PON application")
.withField(StandardField.BOOKTITLE, "Proceedings of the 7th Symposium on Information and Communication Technology")
.withField(StandardField.YEAR, "2016")
.withField(StandardField.SERIES, "SoICT '16")
.withField(StandardField.PAGES, "216–221")
.withField(StandardField.ADDRESS, "New York, NY, USA")
.withField(StandardField.MONTH, "12")
.withField(StandardField.PUBLISHER, "Association for Computing Machinery")
.withField(StandardField.LOCATION, "Ho Chi Minh City, Vietnam")
.withField(StandardField.ISBN, "9781450348157")
.withField(StandardField.DAY, "8")
.withField(StandardField.PAGETOTAL, "6")
.withField(StandardField.DOI, "10.1145/3011077.3011113")
.withField(StandardField.KEYWORDS, "four-wave mixing (FWM), long-range passive optical network (LR PON), orthogonal frequency division multiplexing (OFDM), wavelength division multiplexing (WDM)")
.withField(StandardField.URL, "https://doi.org/10.1145/3011077.3011113");

BibEntry parsedEntry = parser.parseBibEntry(json);
assertEquals(expectedEntry, parsedEntry);
}

@Test
void testNoEntryFound() throws URISyntaxException, IOException, ParseException {
CookieHandler.setDefault(new CookieManager());
Expand Down

0 comments on commit bacd82d

Please sign in to comment.