From aaa2031df1b8ed315a0f24707d54be5d9d30490b Mon Sep 17 00:00:00 2001 From: bruehldev Date: Mon, 27 Jun 2016 20:00:35 +0200 Subject: [PATCH 01/14] Fixes #1181 and #1504: Improved "Normalize to BibTeX name format" Added the jr, sr,... special cases for semicolon partition. Fixed to avoid the "and", "{", ";" cases. Added Test for every case. --- CHANGELOG.md | 3 +- .../bibtexfields/NormalizeNamesFormatter.java | 86 +++++++++++++++++++ .../NormalizeNamesFormatterTest.java | 45 +++++++++- .../sf/jabref/model/entry/AuthorListTest.java | 10 +++ 4 files changed, 142 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c33af578e6..8dc3f348ac2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,7 +30,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `# - Fixed field `location` containing only city is not exported correctly to MS-Office 2007 xml format ### Removed -- The non-supported feature of being able to define file directories for any extension is removed. Still, it should work for older databases using the legacy `ps` and `pdf` fields, although we strongly encourage using the `file` field. +- The non-supported feature of being able to define file directories for any extension is removed. Still, it should work for older databases using the legacy `ps` and `pdf` fields, although we strongly encourage using the `file` field. @@ -195,6 +195,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `# - Manage content selectors now saves edited existing lists again and only marks database as changed when the content selectors are changed - When inserting a duplicate the right entry will be selected - Preview panel height is now saved immediately, thus is shown correctly if the panel height is changed, closed and opened again +- Fixed [#1181](https://github.com/JabRef/jabref/issues/1181) and [#1504](https://github.com/JabRef/jabref/issues/1504): Improved "Normalize to BibTeX name format": Support separated names with commas and colons. Considered name affixes such as "Jr". ### Removed - [#1610](https://github.com/JabRef/jabref/issues/1610) Removed the possibility to auto show or hide the groups interface diff --git a/src/main/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatter.java b/src/main/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatter.java index 3937ac59cbe..242fba30cbf 100644 --- a/src/main/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatter.java +++ b/src/main/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatter.java @@ -1,5 +1,11 @@ package net.sf.jabref.logic.formatter.bibtexfields; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; +import java.util.Objects; +import java.util.StringJoiner; + import net.sf.jabref.logic.formatter.Formatter; import net.sf.jabref.logic.l10n.Localization; import net.sf.jabref.model.entry.AuthorList; @@ -9,6 +15,9 @@ */ public class NormalizeNamesFormatter implements Formatter { + // Avoid partition where these values are contained + private final Collection avoidTermsInLowerCase = Arrays.asList("jr", "sr", "jnr", "snr", "von", "zu", "van", "der"); + @Override public String getName() { return Localization.lang("Normalize names of persons"); @@ -21,6 +30,73 @@ public String getKey() { @Override public String format(String value) { + Objects.requireNonNull(value); + // Handle case names in order lastname, firstname and separated by "," + // E.g., Ali Babar, M., Dingsøyr, T., Lago, P., van der Vliet, H. + if (!value.contains(" and ") && !value.contains("{") && !value.contains(";")) { + String[] valueParts = value.split(","); + // Delete spaces for correct case identification + for(int i=0; i < valueParts.length; i++) { + valueParts[i] = valueParts[i].trim(); + } + // Looking for space between pre- and lastname + boolean spaceInAllParts = false; + for (int i=0; i avoidIndex = new HashSet<>(); + + for (int i = 0; i < valueParts.length; i++) { + if (avoidTermsInLowerCase.contains(valueParts[i].toLowerCase())) { + avoidIndex.add(i); + valuePartsCount--; + } + } + + if ((valuePartsCount % 2) == 0) { + // We hit the described special case with name affix like Jr + StringBuilder stringBuilder = new StringBuilder(); + // avoidedTimes need to increase the count of avoided terms for correct module calculation + int avoidedTimes = 0; + for (int i = 0; i < valueParts.length; i++) { + if (avoidIndex.contains(i)) { + // We hit a name affix + stringBuilder.append(valueParts[i]); + stringBuilder.append(','); + avoidedTimes++; + } else { + stringBuilder.append(valueParts[i]); + if (((i + avoidedTimes) % 2) == 0) { + // Hit separation between last name and firstname --> comma has to be kept + stringBuilder.append(','); + } else { + // Hit separation between full names (e.g., Ali Babar, M. and Dingsøyr, T.) --> semicolon has to be used + // Will be treated correctly by AuthorList.parse(value); + stringBuilder.append(';'); + } + } + } + value = stringBuilder.toString(); + } + } + } + AuthorList authorList = AuthorList.parse(value); return authorList.getAsLastFirstNamesWithAnd(false); } @@ -35,4 +111,14 @@ public String getExampleInput() { return "Albert Einstein and Alan Turing"; } + private static boolean contains(final String[] array, final String[] searchTerms) { + for (String currentTerm : array) { + for (String beCompared : searchTerms) { + if (beCompared.trim().toLowerCase().equals(currentTerm.trim().toLowerCase())) { + return true; + } + } + } + return false; + } } diff --git a/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java b/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java index 316578eef59..67375fb969a 100644 --- a/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java +++ b/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java @@ -98,6 +98,18 @@ public void lastThenJuniorThenFirst() { expectCorrect("Name, della, first", "Name, della, first"); } + @Test + public void testConcatenationOfAuthorsWithCommas() { + expectCorrect("Ali Babar, M., Dingsøyr, T., Lago, P., van der Vliet, H.", + "Ali Babar, M. and Dingsøyr, T. and Lago, P. and van der Vliet, H."); + expectCorrect("Ali Babar, M.", "Ali Babar, M."); + } + + @Test + public void testOddCountOfCommas() { + expectCorrect("Ali Babar, M., Dingsøyr, T., Lago P.", "Ali Babar, M., Dingsøyr T. Lago P."); + } + private void expectCorrect(String input, String expected) { Assert.assertEquals(expected, formatter.format(input)); } @@ -107,4 +119,35 @@ public void formatExample() { assertEquals("Einstein, Albert and Turing, Alan", formatter.format(formatter.getExampleInput())); } -} \ No newline at end of file + @Test + public void testNameAffixe() { + expectCorrect("Surname, jr, First, Surname2, First2", "Surname, jr, First and Surname2, First2"); + } + + @Test + public void testAvoidSpecialCharacter() { + expectCorrect("Surname, {, First; Surname2, First2", "Surname, {, First; Surname2, First2"); + } + + @Test + public void testAndInName() { + expectCorrect("Surname, and , First, Surname2, First2", "Surname and , First, Surname2 First2"); + } + + @Test + public void testMultipleNameAffixes() { + expectCorrect("Mair, Jr, Daniel, Brühl, Sr, Daniel", "Mair, Jr, Daniel and Brühl, Sr, Daniel"); + } + + @Test + public void testCommaSeperatedNames() { + expectCorrect("Cristina Bosoi, Mariana Oliveira, Rafael Ochoa Sanchez, Mélanie Tremblay, Gabrie TenHave, Nicoolas Deutz, Christopher F. Rose, Chantal Bemeur", + "Bosoi, Cristina and Oliveira, Mariana and Sanchez, Rafael Ochoa and Tremblay, Mélanie and TenHave, Gabrie and Deutz, Nicoolas and Rose, Christopher F. and Bemeur, Chantal"); + } + + @Test + public void testMultipleSpaces() { + expectCorrect("Cristina Bosoi, Mariana Oliveira, Rafael Ochoa Sanchez , Mélanie Tremblay , Gabrie TenHave, Nicoolas Deutz, Christopher F. Rose, Chantal Bemeur", + "Bosoi, Cristina and Oliveira, Mariana and Sanchez, Rafael Ochoa and Tremblay, Mélanie and TenHave, Gabrie and Deutz, Nicoolas and Rose, Christopher F. and Bemeur, Chantal"); + } +} diff --git a/src/test/java/net/sf/jabref/model/entry/AuthorListTest.java b/src/test/java/net/sf/jabref/model/entry/AuthorListTest.java index 16bef651b4d..b4703595a19 100644 --- a/src/test/java/net/sf/jabref/model/entry/AuthorListTest.java +++ b/src/test/java/net/sf/jabref/model/entry/AuthorListTest.java @@ -524,6 +524,15 @@ public void testGetAuthorsLastFirstAnds() { } + @Test + public void testGetAuthorsLastFirstAndsCaching() { + // getAsLastFirstNamesWithAnd caches its results, therefore we call the method twice using the same arguments + Assert.assertEquals("Smith, John", AuthorList.parse("John Smith").getAsLastFirstNamesWithAnd(false)); + Assert.assertEquals("Smith, John", AuthorList.parse("John Smith").getAsLastFirstNamesWithAnd(false)); + Assert.assertEquals("Smith, J.", AuthorList.parse("John Smith").getAsLastFirstNamesWithAnd(true)); + Assert.assertEquals("Smith, J.", AuthorList.parse("John Smith").getAsLastFirstNamesWithAnd(true)); + } + @Test public void testGetAuthorsFirstFirst() { @@ -636,4 +645,5 @@ public void parseNameWithBraces() throws Exception { Author expected = new Author("H{e}lene", "H.", null, "Fiaux", null); Assert.assertEquals(new AuthorList(expected), AuthorList.parse("H{e}lene Fiaux")); } + } From 5f86b7b7d7134c6bb02a900bd1b89c8263cc568e Mon Sep 17 00:00:00 2001 From: bruehldev Date: Tue, 26 Jul 2016 18:20:17 +0200 Subject: [PATCH 02/14] Added Tests for all avoiding Terms like der,zu,van,von,.. --- .../bibtexfields/NormalizeNamesFormatterTest.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java b/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java index 67375fb969a..1baff7add63 100644 --- a/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java +++ b/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java @@ -150,4 +150,19 @@ public void testMultipleSpaces() { expectCorrect("Cristina Bosoi, Mariana Oliveira, Rafael Ochoa Sanchez , Mélanie Tremblay , Gabrie TenHave, Nicoolas Deutz, Christopher F. Rose, Chantal Bemeur", "Bosoi, Cristina and Oliveira, Mariana and Sanchez, Rafael Ochoa and Tremblay, Mélanie and TenHave, Gabrie and Deutz, Nicoolas and Rose, Christopher F. and Bemeur, Chantal"); } + + @Test + public void testAvoidPreposition() { + expectCorrect("Hans von Zimmer, Michael van Oberbergern, Kevin zu Berger", "von Zimmer, Hans and van Oberbergern, Michael and zu Berger, Kevin"); + } + + @Test + public void testPreposition() { + expectCorrect("Hans von Zimmer, Michael van Oberbergern, Kevin zu Berger", "von Zimmer, Hans and van Oberbergern, Michael and zu Berger, Kevin"); + } + + @Test + public void testAvoidNameAffixes() { + expectCorrect("Canon der Barbar, Alexander der Große","der Barbar, Canon and der Große, Alexander"); + } } From 1f2dc659d3673b6acab0bbd4e058b024a3f4cc82 Mon Sep 17 00:00:00 2001 From: bruehldev Date: Mon, 15 Aug 2016 01:40:37 +0200 Subject: [PATCH 03/14] - Corrected several comments - Fix codacy issues --- .../bibtexfields/NormalizeNamesFormatter.java | 19 +++++-------------- .../NormalizeNamesFormatterTest.java | 8 ++++---- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/src/main/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatter.java b/src/main/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatter.java index 242fba30cbf..d1dbb81456a 100644 --- a/src/main/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatter.java +++ b/src/main/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatter.java @@ -4,7 +4,6 @@ import java.util.Collection; import java.util.HashSet; import java.util.Objects; -import java.util.StringJoiner; import net.sf.jabref.logic.formatter.Formatter; import net.sf.jabref.logic.l10n.Localization; @@ -36,7 +35,7 @@ public String format(String value) { if (!value.contains(" and ") && !value.contains("{") && !value.contains(";")) { String[] valueParts = value.split(","); // Delete spaces for correct case identification - for(int i=0; i < valueParts.length; i++) { + for (int i=0; i < valueParts.length; i++) { valueParts[i] = valueParts[i].trim(); } // Looking for space between pre- and lastname @@ -58,8 +57,10 @@ public String format(String value) { } else { // Looking for name affixes to avoid // partCount need to reduce by the count off avoiding terms + //valuePartsCount holds the count of name parts without the avoided terms + int valuePartsCount = valueParts.length; - // Holding the index of every term, which need to avoid + // Holds the index of each term which needs to be avoided Collection avoidIndex = new HashSet<>(); for (int i = 0; i < valueParts.length; i++) { @@ -72,7 +73,7 @@ public String format(String value) { if ((valuePartsCount % 2) == 0) { // We hit the described special case with name affix like Jr StringBuilder stringBuilder = new StringBuilder(); - // avoidedTimes need to increase the count of avoided terms for correct module calculation + // avoidedTimes needs to be increased b< the count of avoided terms for correct odd/even calculation int avoidedTimes = 0; for (int i = 0; i < valueParts.length; i++) { if (avoidIndex.contains(i)) { @@ -111,14 +112,4 @@ public String getExampleInput() { return "Albert Einstein and Alan Turing"; } - private static boolean contains(final String[] array, final String[] searchTerms) { - for (String currentTerm : array) { - for (String beCompared : searchTerms) { - if (beCompared.trim().toLowerCase().equals(currentTerm.trim().toLowerCase())) { - return true; - } - } - } - return false; - } } diff --git a/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java b/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java index 1baff7add63..55785043727 100644 --- a/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java +++ b/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java @@ -110,10 +110,6 @@ public void testOddCountOfCommas() { expectCorrect("Ali Babar, M., Dingsøyr, T., Lago P.", "Ali Babar, M., Dingsøyr T. Lago P."); } - private void expectCorrect(String input, String expected) { - Assert.assertEquals(expected, formatter.format(input)); - } - @Test public void formatExample() { assertEquals("Einstein, Albert and Turing, Alan", formatter.format(formatter.getExampleInput())); @@ -165,4 +161,8 @@ public void testPreposition() { public void testAvoidNameAffixes() { expectCorrect("Canon der Barbar, Alexander der Große","der Barbar, Canon and der Große, Alexander"); } + + private void expectCorrect(String input, String expected) { + Assert.assertEquals(expected, formatter.format(input)); + } } From 8257118a589d43bf9444625da470c72518ce8b0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Br=C3=BChl?= Date: Mon, 15 Aug 2016 13:22:14 +0200 Subject: [PATCH 04/14] Deleted method expectCorrect and insert assertEquals manually with formated inputs --- .../NormalizeNamesFormatterTest.java | 82 +++++++++---------- 1 file changed, 39 insertions(+), 43 deletions(-) diff --git a/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java b/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java index 55785043727..8b7a3e6f410 100644 --- a/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java +++ b/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java @@ -20,149 +20,145 @@ public void setUp() { @Test public void testNormalizeAuthorList() { - expectCorrect("Staci D Bilbo", "Bilbo, Staci D."); - expectCorrect("Staci D. Bilbo", "Bilbo, Staci D."); + Assert.assertEquals("Bilbo, Staci D.", formatter.format("Staci D Bilbo")); + Assert.assertEquals("Bilbo, Staci D.", formatter.format("Staci D. Bilbo")); - expectCorrect("Staci D Bilbo and Smith SH and Jaclyn M Schwarz", "Bilbo, Staci D. and Smith, S. H. and Schwarz, Jaclyn M."); + Assert.assertEquals("Bilbo, Staci D. and Smith, S. H. and Schwarz, Jaclyn M.", formatter.format("Staci D Bilbo and Smith SH and Jaclyn M Schwarz")); - expectCorrect("Ølver MA", "Ølver, M. A."); + Assert.assertEquals("Ølver, M. A.", formatter.format("Ølver MA")); - expectCorrect("Ølver MA; GG Øie; Øie GG; Alfredsen JÅÅ; Jo Alfredsen; Olsen Y.Y. and Olsen YY.", - "Ølver, M. A. and Øie, G. G. and Øie, G. G. and Alfredsen, J. Å. Å. and Alfredsen, Jo and Olsen, Y. Y. and Olsen, Y. Y."); + Assert.assertEquals("Ølver, M. A. and Øie, G. G. and Øie, G. G. and Alfredsen, J. Å. Å. and Alfredsen, Jo and Olsen, Y. Y. and Olsen, Y. Y.", + formatter.format("Ølver MA; GG Øie; Øie GG; Alfredsen JÅÅ; Jo Alfredsen; Olsen Y.Y. and Olsen YY.")); - expectCorrect("Ølver MA; GG Øie; Øie GG; Alfredsen JÅÅ; Jo Alfredsen; Olsen Y.Y.; Olsen YY.", - "Ølver, M. A. and Øie, G. G. and Øie, G. G. and Alfredsen, J. Å. Å. and Alfredsen, Jo and Olsen, Y. Y. and Olsen, Y. Y."); + Assert.assertEquals("Ølver, M. A. and Øie, G. G. and Øie, G. G. and Alfredsen, J. Å. Å. and Alfredsen, Jo and Olsen, Y. Y. and Olsen, Y. Y.", + formatter.format("Ølver MA; GG Øie; Øie GG; Alfredsen JÅÅ; Jo Alfredsen; Olsen Y.Y.; Olsen YY.")); - expectCorrect("Alver, Morten and Alver, Morten O and Alfredsen, JA and Olsen, Y.Y.", "Alver, Morten and Alver, Morten O. and Alfredsen, J. A. and Olsen, Y. Y."); + Assert.assertEquals("Alver, Morten and Alver, Morten O. and Alfredsen, J. A. and Olsen, Y. Y.", formatter.format("Alver, Morten and Alver, Morten O and Alfredsen, JA and Olsen, Y.Y.")); - expectCorrect("Alver, MA; Alfredsen, JA; Olsen Y.Y.", "Alver, M. A. and Alfredsen, J. A. and Olsen, Y. Y."); + Assert.assertEquals("Alver, M. A. and Alfredsen, J. A. and Olsen, Y. Y.", formatter.format("Alver, MA; Alfredsen, JA; Olsen Y.Y.")); - expectCorrect("Kolb, Stefan and J{\\\"o}rg Lenhard and Wirtz, Guido", "Kolb, Stefan and Lenhard, J{\\\"o}rg and Wirtz, Guido"); + Assert.assertEquals("Kolb, Stefan and Lenhard, J{\\\"o}rg and Wirtz, Guido", formatter.format("Kolb, Stefan and J{\\\"o}rg Lenhard and Wirtz, Guido")); } @Test public void twoAuthorsSeperatedByColon() { - expectCorrect("Staci Bilbo; Morten Alver", "Bilbo, Staci and Alver, Morten"); + Assert.assertEquals("Bilbo, Staci and Alver, Morten", formatter.format("Staci Bilbo; Morten Alver")); } @Test public void threeAuthorsSeperatedByColon() { - expectCorrect("Staci Bilbo; Morten Alver; Test Name", "Bilbo, Staci and Alver, Morten and Name, Test"); + Assert.assertEquals("Bilbo, Staci and Alver, Morten and Name, Test", formatter.format("Staci Bilbo; Morten Alver; Test Name")); } // Test for https://github.com/JabRef/jabref/issues/318 @Test public void threeAuthorsSeperatedByAnd() { - expectCorrect("Stefan Kolb and J{\\\"o}rg Lenhard and Guido Wirtz", "Kolb, Stefan and Lenhard, J{\\\"o}rg and Wirtz, Guido"); + Assert.assertEquals("Kolb, Stefan and Lenhard, J{\\\"o}rg and Wirtz, Guido", formatter.format("Stefan Kolb and J{\\\"o}rg Lenhard and Guido Wirtz")); } // Test for https://github.com/JabRef/jabref/issues/318 @Test public void threeAuthorsSeperatedByAndWithDash() { - expectCorrect("Heng-Yu Jian and Xu, Z. and Chang, M.-C.F.", "Jian, Heng-Yu and Xu, Z. and Chang, M.-C. F."); + Assert.assertEquals("Jian, Heng-Yu and Xu, Z. and Chang, M.-C. F.", formatter.format("Heng-Yu Jian and Xu, Z. and Chang, M.-C.F.")); } // Test for https://github.com/JabRef/jabref/issues/318 @Test public void threeAuthorsSeperatedByAndWithLatex() { - expectCorrect("Oscar Gustafsson and Linda S. DeBrunner and Victor DeBrunner and H{\\aa}kan Johansson", "Gustafsson, Oscar and DeBrunner, Linda S. and DeBrunner, Victor and Johansson, H{\\aa}kan"); + Assert.assertEquals("Gustafsson, Oscar and DeBrunner, Linda S. and DeBrunner, Victor and Johansson, H{\\aa}kan", + formatter.format("Oscar Gustafsson and Linda S. DeBrunner and Victor DeBrunner and H{\\aa}kan Johansson")); } @Test public void lastThenInitial() { - expectCorrect("Smith S", "Smith, S."); + Assert.assertEquals("Smith, S.", formatter.format("Smith S")); } @Test public void lastThenInitials() { - expectCorrect("Smith SH", "Smith, S. H."); + Assert.assertEquals("Smith, S. H.", formatter.format("Smith SH")); } @Test public void initialThenLast() { - expectCorrect("S Smith", "Smith, S."); + Assert.assertEquals("Smith, S.", formatter.format("S Smith")); } @Test public void initialDotThenLast() { - expectCorrect("S. Smith", "Smith, S."); + Assert.assertEquals("Smith, S.", formatter.format("S. Smith")); } @Test public void initialsThenLast() { - expectCorrect("SH Smith", "Smith, S. H."); + Assert.assertEquals("Smith, S. H.", formatter.format("SH Smith")); } @Test public void lastThenJuniorThenFirst() { - expectCorrect("Name, della, first", "Name, della, first"); + Assert.assertEquals("Name, della, first", formatter.format("Name, della, first")); } @Test public void testConcatenationOfAuthorsWithCommas() { - expectCorrect("Ali Babar, M., Dingsøyr, T., Lago, P., van der Vliet, H.", - "Ali Babar, M. and Dingsøyr, T. and Lago, P. and van der Vliet, H."); - expectCorrect("Ali Babar, M.", "Ali Babar, M."); + Assert.assertEquals("Ali Babar, M. and Dingsøyr, T. and Lago, P. and van der Vliet, H.", formatter.format("Ali Babar, M., Dingsøyr, T., Lago, P., van der Vliet, H.")); + Assert.assertEquals("Ali Babar, M.", formatter.format("Ali Babar, M.")); } @Test public void testOddCountOfCommas() { - expectCorrect("Ali Babar, M., Dingsøyr, T., Lago P.", "Ali Babar, M., Dingsøyr T. Lago P."); + Assert.assertEquals("Ali Babar, M., Dingsøyr T. Lago P.", formatter.format("Ali Babar, M., Dingsøyr, T., Lago P.")); } @Test public void formatExample() { - assertEquals("Einstein, Albert and Turing, Alan", formatter.format(formatter.getExampleInput())); + assertEquals(formatter.format(formatter.getExampleInput()), "Einstein, Albert and Turing, Alan"); } @Test public void testNameAffixe() { - expectCorrect("Surname, jr, First, Surname2, First2", "Surname, jr, First and Surname2, First2"); + Assert.assertEquals("Surname, jr, First and Surname2, First2", formatter.format("Surname, jr, First, Surname2, First2")); } @Test public void testAvoidSpecialCharacter() { - expectCorrect("Surname, {, First; Surname2, First2", "Surname, {, First; Surname2, First2"); + Assert.assertEquals("Surname, {, First; Surname2, First2", formatter.format("Surname, {, First; Surname2, First2")); } @Test public void testAndInName() { - expectCorrect("Surname, and , First, Surname2, First2", "Surname and , First, Surname2 First2"); + Assert.assertEquals("Surname and , First, Surname2 First2", formatter.format("Surname, and , First, Surname2, First2")); } @Test public void testMultipleNameAffixes() { - expectCorrect("Mair, Jr, Daniel, Brühl, Sr, Daniel", "Mair, Jr, Daniel and Brühl, Sr, Daniel"); + Assert.assertEquals("Mair, Jr, Daniel and Brühl, Sr, Daniel", formatter.format("Mair, Jr, Daniel, Brühl, Sr, Daniel")); } @Test public void testCommaSeperatedNames() { - expectCorrect("Cristina Bosoi, Mariana Oliveira, Rafael Ochoa Sanchez, Mélanie Tremblay, Gabrie TenHave, Nicoolas Deutz, Christopher F. Rose, Chantal Bemeur", - "Bosoi, Cristina and Oliveira, Mariana and Sanchez, Rafael Ochoa and Tremblay, Mélanie and TenHave, Gabrie and Deutz, Nicoolas and Rose, Christopher F. and Bemeur, Chantal"); + Assert.assertEquals("Bosoi, Cristina and Oliveira, Mariana and Sanchez, Rafael Ochoa and Tremblay, Mélanie and TenHave, Gabrie and Deutz, Nicoolas and Rose, Christopher F. and Bemeur, Chantal", + formatter.format("Cristina Bosoi, Mariana Oliveira, Rafael Ochoa Sanchez, Mélanie Tremblay, Gabrie TenHave, Nicoolas Deutz, Christopher F. Rose, Chantal Bemeur")); } @Test public void testMultipleSpaces() { - expectCorrect("Cristina Bosoi, Mariana Oliveira, Rafael Ochoa Sanchez , Mélanie Tremblay , Gabrie TenHave, Nicoolas Deutz, Christopher F. Rose, Chantal Bemeur", - "Bosoi, Cristina and Oliveira, Mariana and Sanchez, Rafael Ochoa and Tremblay, Mélanie and TenHave, Gabrie and Deutz, Nicoolas and Rose, Christopher F. and Bemeur, Chantal"); + Assert.assertEquals("Bosoi, Cristina and Oliveira, Mariana and Sanchez, Rafael Ochoa and Tremblay, Mélanie and TenHave, Gabrie and Deutz, Nicoolas and Rose, Christopher F. and Bemeur, Chantal", + formatter.format("Cristina Bosoi, Mariana Oliveira, Rafael Ochoa Sanchez , Mélanie Tremblay , Gabrie TenHave, Nicoolas Deutz, Christopher F. Rose, Chantal Bemeur")); } @Test public void testAvoidPreposition() { - expectCorrect("Hans von Zimmer, Michael van Oberbergern, Kevin zu Berger", "von Zimmer, Hans and van Oberbergern, Michael and zu Berger, Kevin"); + Assert.assertEquals("von Zimmer, Hans and van Oberbergern, Michael and zu Berger, Kevin", formatter.format("Hans von Zimmer, Michael van Oberbergern, Kevin zu Berger")); } @Test public void testPreposition() { - expectCorrect("Hans von Zimmer, Michael van Oberbergern, Kevin zu Berger", "von Zimmer, Hans and van Oberbergern, Michael and zu Berger, Kevin"); + Assert.assertEquals("von Zimmer, Hans and van Oberbergern, Michael and zu Berger, Kevin", formatter.format("Hans von Zimmer, Michael van Oberbergern, Kevin zu Berger")); } @Test public void testAvoidNameAffixes() { - expectCorrect("Canon der Barbar, Alexander der Große","der Barbar, Canon and der Große, Alexander"); + Assert.assertEquals("der Barbar, Canon and der Große, Alexander", formatter.format("Canon der Barbar, Alexander der Große")); } - - private void expectCorrect(String input, String expected) { - Assert.assertEquals(expected, formatter.format(input)); - } -} +} \ No newline at end of file From e7ea5b60b5ae60a22da4b1ad245d559c3362e51e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Br=C3=BChl?= Date: Mon, 15 Aug 2016 14:10:51 +0200 Subject: [PATCH 05/14] Unnecessary use of fully qualified name fixed --- .../NormalizeNamesFormatterTest.java | 64 +++++++++---------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java b/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java index 8b7a3e6f410..3bc0fb19c7a 100644 --- a/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java +++ b/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java @@ -20,94 +20,94 @@ public void setUp() { @Test public void testNormalizeAuthorList() { - Assert.assertEquals("Bilbo, Staci D.", formatter.format("Staci D Bilbo")); - Assert.assertEquals("Bilbo, Staci D.", formatter.format("Staci D. Bilbo")); + assertEquals("Bilbo, Staci D.", formatter.format("Staci D Bilbo")); + assertEquals("Bilbo, Staci D.", formatter.format("Staci D. Bilbo")); - Assert.assertEquals("Bilbo, Staci D. and Smith, S. H. and Schwarz, Jaclyn M.", formatter.format("Staci D Bilbo and Smith SH and Jaclyn M Schwarz")); + assertEquals("Bilbo, Staci D. and Smith, S. H. and Schwarz, Jaclyn M.", formatter.format("Staci D Bilbo and Smith SH and Jaclyn M Schwarz")); - Assert.assertEquals("Ølver, M. A.", formatter.format("Ølver MA")); + assertEquals("Ølver, M. A.", formatter.format("Ølver MA")); - Assert.assertEquals("Ølver, M. A. and Øie, G. G. and Øie, G. G. and Alfredsen, J. Å. Å. and Alfredsen, Jo and Olsen, Y. Y. and Olsen, Y. Y.", + assertEquals("Ølver, M. A. and Øie, G. G. and Øie, G. G. and Alfredsen, J. Å. Å. and Alfredsen, Jo and Olsen, Y. Y. and Olsen, Y. Y.", formatter.format("Ølver MA; GG Øie; Øie GG; Alfredsen JÅÅ; Jo Alfredsen; Olsen Y.Y. and Olsen YY.")); - Assert.assertEquals("Ølver, M. A. and Øie, G. G. and Øie, G. G. and Alfredsen, J. Å. Å. and Alfredsen, Jo and Olsen, Y. Y. and Olsen, Y. Y.", + assertEquals("Ølver, M. A. and Øie, G. G. and Øie, G. G. and Alfredsen, J. Å. Å. and Alfredsen, Jo and Olsen, Y. Y. and Olsen, Y. Y.", formatter.format("Ølver MA; GG Øie; Øie GG; Alfredsen JÅÅ; Jo Alfredsen; Olsen Y.Y.; Olsen YY.")); - Assert.assertEquals("Alver, Morten and Alver, Morten O. and Alfredsen, J. A. and Olsen, Y. Y.", formatter.format("Alver, Morten and Alver, Morten O and Alfredsen, JA and Olsen, Y.Y.")); + assertEquals("Alver, Morten and Alver, Morten O. and Alfredsen, J. A. and Olsen, Y. Y.", formatter.format("Alver, Morten and Alver, Morten O and Alfredsen, JA and Olsen, Y.Y.")); - Assert.assertEquals("Alver, M. A. and Alfredsen, J. A. and Olsen, Y. Y.", formatter.format("Alver, MA; Alfredsen, JA; Olsen Y.Y.")); + assertEquals("Alver, M. A. and Alfredsen, J. A. and Olsen, Y. Y.", formatter.format("Alver, MA; Alfredsen, JA; Olsen Y.Y.")); - Assert.assertEquals("Kolb, Stefan and Lenhard, J{\\\"o}rg and Wirtz, Guido", formatter.format("Kolb, Stefan and J{\\\"o}rg Lenhard and Wirtz, Guido")); + assertEquals("Kolb, Stefan and Lenhard, J{\\\"o}rg and Wirtz, Guido", formatter.format("Kolb, Stefan and J{\\\"o}rg Lenhard and Wirtz, Guido")); } @Test public void twoAuthorsSeperatedByColon() { - Assert.assertEquals("Bilbo, Staci and Alver, Morten", formatter.format("Staci Bilbo; Morten Alver")); + assertEquals("Bilbo, Staci and Alver, Morten", formatter.format("Staci Bilbo; Morten Alver")); } @Test public void threeAuthorsSeperatedByColon() { - Assert.assertEquals("Bilbo, Staci and Alver, Morten and Name, Test", formatter.format("Staci Bilbo; Morten Alver; Test Name")); + assertEquals("Bilbo, Staci and Alver, Morten and Name, Test", formatter.format("Staci Bilbo; Morten Alver; Test Name")); } // Test for https://github.com/JabRef/jabref/issues/318 @Test public void threeAuthorsSeperatedByAnd() { - Assert.assertEquals("Kolb, Stefan and Lenhard, J{\\\"o}rg and Wirtz, Guido", formatter.format("Stefan Kolb and J{\\\"o}rg Lenhard and Guido Wirtz")); + assertEquals("Kolb, Stefan and Lenhard, J{\\\"o}rg and Wirtz, Guido", formatter.format("Stefan Kolb and J{\\\"o}rg Lenhard and Guido Wirtz")); } // Test for https://github.com/JabRef/jabref/issues/318 @Test public void threeAuthorsSeperatedByAndWithDash() { - Assert.assertEquals("Jian, Heng-Yu and Xu, Z. and Chang, M.-C. F.", formatter.format("Heng-Yu Jian and Xu, Z. and Chang, M.-C.F.")); + assertEquals("Jian, Heng-Yu and Xu, Z. and Chang, M.-C. F.", formatter.format("Heng-Yu Jian and Xu, Z. and Chang, M.-C.F.")); } // Test for https://github.com/JabRef/jabref/issues/318 @Test public void threeAuthorsSeperatedByAndWithLatex() { - Assert.assertEquals("Gustafsson, Oscar and DeBrunner, Linda S. and DeBrunner, Victor and Johansson, H{\\aa}kan", + assertEquals("Gustafsson, Oscar and DeBrunner, Linda S. and DeBrunner, Victor and Johansson, H{\\aa}kan", formatter.format("Oscar Gustafsson and Linda S. DeBrunner and Victor DeBrunner and H{\\aa}kan Johansson")); } @Test public void lastThenInitial() { - Assert.assertEquals("Smith, S.", formatter.format("Smith S")); + assertEquals("Smith, S.", formatter.format("Smith S")); } @Test public void lastThenInitials() { - Assert.assertEquals("Smith, S. H.", formatter.format("Smith SH")); + assertEquals("Smith, S. H.", formatter.format("Smith SH")); } @Test public void initialThenLast() { - Assert.assertEquals("Smith, S.", formatter.format("S Smith")); + assertEquals("Smith, S.", formatter.format("S Smith")); } @Test public void initialDotThenLast() { - Assert.assertEquals("Smith, S.", formatter.format("S. Smith")); + assertEquals("Smith, S.", formatter.format("S. Smith")); } @Test public void initialsThenLast() { - Assert.assertEquals("Smith, S. H.", formatter.format("SH Smith")); + assertEquals("Smith, S. H.", formatter.format("SH Smith")); } @Test public void lastThenJuniorThenFirst() { - Assert.assertEquals("Name, della, first", formatter.format("Name, della, first")); + assertEquals("Name, della, first", formatter.format("Name, della, first")); } @Test public void testConcatenationOfAuthorsWithCommas() { - Assert.assertEquals("Ali Babar, M. and Dingsøyr, T. and Lago, P. and van der Vliet, H.", formatter.format("Ali Babar, M., Dingsøyr, T., Lago, P., van der Vliet, H.")); - Assert.assertEquals("Ali Babar, M.", formatter.format("Ali Babar, M.")); + assertEquals("Ali Babar, M. and Dingsøyr, T. and Lago, P. and van der Vliet, H.", formatter.format("Ali Babar, M., Dingsøyr, T., Lago, P., van der Vliet, H.")); + assertEquals("Ali Babar, M.", formatter.format("Ali Babar, M.")); } @Test public void testOddCountOfCommas() { - Assert.assertEquals("Ali Babar, M., Dingsøyr T. Lago P.", formatter.format("Ali Babar, M., Dingsøyr, T., Lago P.")); + assertEquals("Ali Babar, M., Dingsøyr T. Lago P.", formatter.format("Ali Babar, M., Dingsøyr, T., Lago P.")); } @Test @@ -117,48 +117,48 @@ public void formatExample() { @Test public void testNameAffixe() { - Assert.assertEquals("Surname, jr, First and Surname2, First2", formatter.format("Surname, jr, First, Surname2, First2")); + assertEquals("Surname, jr, First and Surname2, First2", formatter.format("Surname, jr, First, Surname2, First2")); } @Test public void testAvoidSpecialCharacter() { - Assert.assertEquals("Surname, {, First; Surname2, First2", formatter.format("Surname, {, First; Surname2, First2")); + assertEquals("Surname, {, First; Surname2, First2", formatter.format("Surname, {, First; Surname2, First2")); } @Test public void testAndInName() { - Assert.assertEquals("Surname and , First, Surname2 First2", formatter.format("Surname, and , First, Surname2, First2")); + assertEquals("Surname and , First, Surname2 First2", formatter.format("Surname, and , First, Surname2, First2")); } @Test public void testMultipleNameAffixes() { - Assert.assertEquals("Mair, Jr, Daniel and Brühl, Sr, Daniel", formatter.format("Mair, Jr, Daniel, Brühl, Sr, Daniel")); + assertEquals("Mair, Jr, Daniel and Brühl, Sr, Daniel", formatter.format("Mair, Jr, Daniel, Brühl, Sr, Daniel")); } @Test public void testCommaSeperatedNames() { - Assert.assertEquals("Bosoi, Cristina and Oliveira, Mariana and Sanchez, Rafael Ochoa and Tremblay, Mélanie and TenHave, Gabrie and Deutz, Nicoolas and Rose, Christopher F. and Bemeur, Chantal", + assertEquals("Bosoi, Cristina and Oliveira, Mariana and Sanchez, Rafael Ochoa and Tremblay, Mélanie and TenHave, Gabrie and Deutz, Nicoolas and Rose, Christopher F. and Bemeur, Chantal", formatter.format("Cristina Bosoi, Mariana Oliveira, Rafael Ochoa Sanchez, Mélanie Tremblay, Gabrie TenHave, Nicoolas Deutz, Christopher F. Rose, Chantal Bemeur")); } @Test public void testMultipleSpaces() { - Assert.assertEquals("Bosoi, Cristina and Oliveira, Mariana and Sanchez, Rafael Ochoa and Tremblay, Mélanie and TenHave, Gabrie and Deutz, Nicoolas and Rose, Christopher F. and Bemeur, Chantal", + assertEquals("Bosoi, Cristina and Oliveira, Mariana and Sanchez, Rafael Ochoa and Tremblay, Mélanie and TenHave, Gabrie and Deutz, Nicoolas and Rose, Christopher F. and Bemeur, Chantal", formatter.format("Cristina Bosoi, Mariana Oliveira, Rafael Ochoa Sanchez , Mélanie Tremblay , Gabrie TenHave, Nicoolas Deutz, Christopher F. Rose, Chantal Bemeur")); } @Test public void testAvoidPreposition() { - Assert.assertEquals("von Zimmer, Hans and van Oberbergern, Michael and zu Berger, Kevin", formatter.format("Hans von Zimmer, Michael van Oberbergern, Kevin zu Berger")); + assertEquals("von Zimmer, Hans and van Oberbergern, Michael and zu Berger, Kevin", formatter.format("Hans von Zimmer, Michael van Oberbergern, Kevin zu Berger")); } @Test public void testPreposition() { - Assert.assertEquals("von Zimmer, Hans and van Oberbergern, Michael and zu Berger, Kevin", formatter.format("Hans von Zimmer, Michael van Oberbergern, Kevin zu Berger")); + assertEquals("von Zimmer, Hans and van Oberbergern, Michael and zu Berger, Kevin", formatter.format("Hans von Zimmer, Michael van Oberbergern, Kevin zu Berger")); } @Test public void testAvoidNameAffixes() { - Assert.assertEquals("der Barbar, Canon and der Große, Alexander", formatter.format("Canon der Barbar, Alexander der Große")); + assertEquals("der Barbar, Canon and der Große, Alexander", formatter.format("Canon der Barbar, Alexander der Große")); } } \ No newline at end of file From 0bdba775f9a7571ee00d4bad6df921583276a6a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Br=C3=BChl?= Date: Mon, 15 Aug 2016 16:04:55 +0200 Subject: [PATCH 06/14] Deleted unused import --- .../formatter/bibtexfields/NormalizeNamesFormatterTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java b/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java index 3bc0fb19c7a..bf0c14b58d5 100644 --- a/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java +++ b/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java @@ -1,6 +1,5 @@ package net.sf.jabref.logic.formatter.bibtexfields; -import org.junit.Assert; import org.junit.Before; import org.junit.Test; From 0253d2cf5bac2dd1bcffdb4ff432da60842333cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Br=C3=BChl?= Date: Mon, 29 Aug 2016 13:44:22 +0200 Subject: [PATCH 07/14] QS processed - Renamed value and valuePart variable - Edit comment - Exchanged for loop --- CHANGELOG.md | 3 +- .../bibtexfields/NormalizeNamesFormatter.java | 43 ++++++++++--------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8dc3f348ac2..aa7854fe602 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `# - Fixed [#1663](https://github.com/JabRef/jabref/issues/1663): Better multi-monitor support - Fixed [#1882](https://github.com/JabRef/jabref/issues/1882): Crash after saving illegal bibtexkey in entry editor - Fixed field `location` containing only city is not exported correctly to MS-Office 2007 xml format +- Fixed [#1181](https://github.com/JabRef/jabref/issues/1181) and [#1504](https://github.com/JabRef/jabref/issues/1504): Improved "Normalize to BibTeX name format": Support separated names with commas and colons. Considered name affixes such as "Jr". ### Removed - The non-supported feature of being able to define file directories for any extension is removed. Still, it should work for older databases using the legacy `ps` and `pdf` fields, although we strongly encourage using the `file` field. @@ -195,7 +196,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `# - Manage content selectors now saves edited existing lists again and only marks database as changed when the content selectors are changed - When inserting a duplicate the right entry will be selected - Preview panel height is now saved immediately, thus is shown correctly if the panel height is changed, closed and opened again -- Fixed [#1181](https://github.com/JabRef/jabref/issues/1181) and [#1504](https://github.com/JabRef/jabref/issues/1504): Improved "Normalize to BibTeX name format": Support separated names with commas and colons. Considered name affixes such as "Jr". + ### Removed - [#1610](https://github.com/JabRef/jabref/issues/1610) Removed the possibility to auto show or hide the groups interface diff --git a/src/main/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatter.java b/src/main/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatter.java index d1dbb81456a..bb901862719 100644 --- a/src/main/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatter.java +++ b/src/main/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatter.java @@ -28,20 +28,23 @@ public String getKey() { } @Override - public String format(String value) { - Objects.requireNonNull(value); + public String format(String nameList) { + Objects.requireNonNull(nameList); // Handle case names in order lastname, firstname and separated by "," // E.g., Ali Babar, M., Dingsøyr, T., Lago, P., van der Vliet, H. - if (!value.contains(" and ") && !value.contains("{") && !value.contains(";")) { - String[] valueParts = value.split(","); + if (!nameList.contains(" and ") && !nameList.contains("{") && !nameList.contains(";")) { + String[] arrayNameList = nameList.split(","); // Delete spaces for correct case identification - for (int i=0; i < valueParts.length; i++) { - valueParts[i] = valueParts[i].trim(); + for (int i=0; i < arrayNameList.length; i++) { + arrayNameList[i] = arrayNameList[i].trim(); + } + for (String namePart : arrayNameList) { + namePart = namePart.trim(); } // Looking for space between pre- and lastname boolean spaceInAllParts = false; - for (int i=0; i avoidIndex = new HashSet<>(); - for (int i = 0; i < valueParts.length; i++) { - if (avoidTermsInLowerCase.contains(valueParts[i].toLowerCase())) { + for (int i = 0; i < arrayNameList.length; i++) { + if (avoidTermsInLowerCase.contains(arrayNameList[i].toLowerCase())) { avoidIndex.add(i); valuePartsCount--; } @@ -75,30 +78,30 @@ public String format(String value) { StringBuilder stringBuilder = new StringBuilder(); // avoidedTimes needs to be increased b< the count of avoided terms for correct odd/even calculation int avoidedTimes = 0; - for (int i = 0; i < valueParts.length; i++) { + for (int i = 0; i < arrayNameList.length; i++) { if (avoidIndex.contains(i)) { // We hit a name affix - stringBuilder.append(valueParts[i]); + stringBuilder.append(arrayNameList[i]); stringBuilder.append(','); avoidedTimes++; } else { - stringBuilder.append(valueParts[i]); + stringBuilder.append(arrayNameList[i]); if (((i + avoidedTimes) % 2) == 0) { // Hit separation between last name and firstname --> comma has to be kept stringBuilder.append(','); } else { // Hit separation between full names (e.g., Ali Babar, M. and Dingsøyr, T.) --> semicolon has to be used - // Will be treated correctly by AuthorList.parse(value); + // Will be treated correctly by AuthorList.parse(nameList); stringBuilder.append(';'); } } } - value = stringBuilder.toString(); + nameList = stringBuilder.toString(); } } } - AuthorList authorList = AuthorList.parse(value); + AuthorList authorList = AuthorList.parse(nameList); return authorList.getAsLastFirstNamesWithAnd(false); } From bc55e0f550519093044273c9e8a7ae6e18ea7e71 Mon Sep 17 00:00:00 2001 From: bruehldev Date: Thu, 1 Sep 2016 17:00:17 +0200 Subject: [PATCH 08/14] Using Arrays.asList instead of Array. Replaced for-loops with fancy util methods. --- .../bibtexfields/NormalizeNamesFormatter.java | 42 +++++++------------ 1 file changed, 15 insertions(+), 27 deletions(-) diff --git a/src/main/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatter.java b/src/main/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatter.java index bb901862719..422a605ffbe 100644 --- a/src/main/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatter.java +++ b/src/main/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatter.java @@ -1,9 +1,7 @@ package net.sf.jabref.logic.formatter.bibtexfields; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashSet; -import java.util.Objects; +import java.util.*; +import java.util.stream.Collectors; import net.sf.jabref.logic.formatter.Formatter; import net.sf.jabref.logic.l10n.Localization; @@ -32,25 +30,15 @@ public String format(String nameList) { Objects.requireNonNull(nameList); // Handle case names in order lastname, firstname and separated by "," // E.g., Ali Babar, M., Dingsøyr, T., Lago, P., van der Vliet, H. - if (!nameList.contains(" and ") && !nameList.contains("{") && !nameList.contains(";")) { - String[] arrayNameList = nameList.split(","); + if (!nameList.toUpperCase(Locale.ENGLISH).contains(" AND ") && !nameList.contains("{") && !nameList.contains(";")) { + List arrayNameList = Arrays.asList(nameList.split(",")); + // Delete spaces for correct case identification - for (int i=0; i < arrayNameList.length; i++) { - arrayNameList[i] = arrayNameList[i].trim(); - } - for (String namePart : arrayNameList) { - namePart = namePart.trim(); - } + arrayNameList.replaceAll(String::trim); + // Looking for space between pre- and lastname - boolean spaceInAllParts = false; - for (int i=0; i name.contains(" ")).collect(Collectors + .toList()).size() == arrayNameList.size(); // We hit the comma name separator case // Usually the getAsLastFirstNamesWithAnd method would separate them if pre- and lastname are separated with "and" @@ -62,12 +50,12 @@ public String format(String nameList) { // arrayNameList needs to reduce by the count off avoiding terms // valuePartsCount holds the count of name parts without the avoided terms - int valuePartsCount = arrayNameList.length; + int valuePartsCount = arrayNameList.size(); // Holds the index of each term which needs to be avoided Collection avoidIndex = new HashSet<>(); - for (int i = 0; i < arrayNameList.length; i++) { - if (avoidTermsInLowerCase.contains(arrayNameList[i].toLowerCase())) { + for (int i = 0; i < arrayNameList.size(); i++) { + if (avoidTermsInLowerCase.contains(arrayNameList.get(i).toLowerCase())) { avoidIndex.add(i); valuePartsCount--; } @@ -78,14 +66,14 @@ public String format(String nameList) { StringBuilder stringBuilder = new StringBuilder(); // avoidedTimes needs to be increased b< the count of avoided terms for correct odd/even calculation int avoidedTimes = 0; - for (int i = 0; i < arrayNameList.length; i++) { + for (int i = 0; i < arrayNameList.size(); i++) { if (avoidIndex.contains(i)) { // We hit a name affix - stringBuilder.append(arrayNameList[i]); + stringBuilder.append(arrayNameList.get(i)); stringBuilder.append(','); avoidedTimes++; } else { - stringBuilder.append(arrayNameList[i]); + stringBuilder.append(arrayNameList.get(i)); if (((i + avoidedTimes) % 2) == 0) { // Hit separation between last name and firstname --> comma has to be kept stringBuilder.append(','); From 10ba1da596f679df8f219f32ecae76940e2f64ca Mon Sep 17 00:00:00 2001 From: bruehldev Date: Thu, 1 Sep 2016 17:03:36 +0200 Subject: [PATCH 09/14] Added test for upper case sensitve AND tests. --- .../bibtexfields/NormalizeNamesFormatterTest.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java b/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java index bf0c14b58d5..bdb3e89235c 100644 --- a/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java +++ b/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java @@ -160,4 +160,11 @@ public void testPreposition() { public void testAvoidNameAffixes() { assertEquals("der Barbar, Canon and der Große, Alexander", formatter.format("Canon der Barbar, Alexander der Große")); } + + @Test + public void testUpperCaseSensitiveList() { + assertEquals("der Barbar, Canon and der Große, Alexander", formatter.format("Canon der Barbar AND Alexander der Große")); + assertEquals("der Barbar, Canon and der Große, Alexander", formatter.format("Canon der Barbar aNd Alexander der Große")); + assertEquals("der Barbar, Canon and der Große, Alexander", formatter.format("Canon der Barbar AnD Alexander der Große")); + } } \ No newline at end of file From 4d07e241b6e7b88c7e0b0a28d5b60c708f212ca4 Mon Sep 17 00:00:00 2001 From: bruehldev Date: Thu, 1 Sep 2016 18:04:43 +0200 Subject: [PATCH 10/14] Corrected imports --- .../bibtexfields/NormalizeNamesFormatter.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatter.java b/src/main/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatter.java index 422a605ffbe..0aeaf7999a4 100644 --- a/src/main/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatter.java +++ b/src/main/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatter.java @@ -1,12 +1,17 @@ package net.sf.jabref.logic.formatter.bibtexfields; -import java.util.*; -import java.util.stream.Collectors; - import net.sf.jabref.logic.formatter.Formatter; import net.sf.jabref.logic.l10n.Localization; import net.sf.jabref.model.entry.AuthorList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Locale; +import java.util.Objects; +import java.util.stream.Collectors; + /** * Formatter normalizing a list of person names to the BibTeX format. */ From d4b162f5f949afe6063da5b541ac556059acd221 Mon Sep 17 00:00:00 2001 From: bruehldev Date: Thu, 1 Sep 2016 18:32:16 +0200 Subject: [PATCH 11/14] Fixed wrong order of imports --- .../formatter/bibtexfields/NormalizeNamesFormatter.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatter.java b/src/main/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatter.java index 0aeaf7999a4..c05477e0e1b 100644 --- a/src/main/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatter.java +++ b/src/main/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatter.java @@ -1,9 +1,5 @@ package net.sf.jabref.logic.formatter.bibtexfields; -import net.sf.jabref.logic.formatter.Formatter; -import net.sf.jabref.logic.l10n.Localization; -import net.sf.jabref.model.entry.AuthorList; - import java.util.Arrays; import java.util.Collection; import java.util.HashSet; @@ -12,6 +8,10 @@ import java.util.Objects; import java.util.stream.Collectors; +import net.sf.jabref.logic.formatter.Formatter; +import net.sf.jabref.logic.l10n.Localization; +import net.sf.jabref.model.entry.AuthorList; + /** * Formatter normalizing a list of person names to the BibTeX format. */ From 56a8c89f8be9032561a6b45f0aceddbe2130e52d Mon Sep 17 00:00:00 2001 From: bruehldev Date: Thu, 8 Sep 2016 15:08:18 +0200 Subject: [PATCH 12/14] Moved code to authorlist --- CHANGELOG.md | 10 ++- .../bibtexfields/NormalizeNamesFormatter.java | 70 ----------------- .../net/sf/jabref/model/entry/AuthorList.java | 77 +++++++++++++++++++ 3 files changed, 84 insertions(+), 73 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa7854fe602..24f2947cca6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,10 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `# - [#1897](https://github.com/JabRef/jabref/issues/1897) Implemented integrity check for `year` field: Last four nonpunctuation characters should be numerals - Address in MS-Office 2007 xml format is now imported as `location` - [#1912](https://github.com/JabRef/jabref/issues/1912) Implemented integrity check for `edition` field: Should have the first letter capitalized (BibTeX), Should contain an integer or a literal (BibLaTeX) +- `number` field is now exported as `number` field in MS-Office 2007 xml format, if no `issue` field is present and the entry type is not `patent` +- `note` field is now exported as `comments` field in MS-Office 2007 xml format +- `comments` field in MS-Office 2007 xml format is now imported as `note` field +- [#463](https://github.com/JabRef/jabref/issues/463): Disable menu-item and toolbar-buttons while no database is open ### Fixed - Fixed selecting an entry out of multiple duplicates @@ -28,10 +32,11 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `# - Fixed [#1663](https://github.com/JabRef/jabref/issues/1663): Better multi-monitor support - Fixed [#1882](https://github.com/JabRef/jabref/issues/1882): Crash after saving illegal bibtexkey in entry editor - Fixed field `location` containing only city is not exported correctly to MS-Office 2007 xml format +- Fixed field `key` field is not exported to MS-Office 2008 xml format - Fixed [#1181](https://github.com/JabRef/jabref/issues/1181) and [#1504](https://github.com/JabRef/jabref/issues/1504): Improved "Normalize to BibTeX name format": Support separated names with commas and colons. Considered name affixes such as "Jr". ### Removed -- The non-supported feature of being able to define file directories for any extension is removed. Still, it should work for older databases using the legacy `ps` and `pdf` fields, although we strongly encourage using the `file` field. +- The non-supported feature of being able to define file directories for any extension is removed. Still, it should work for older databases using the legacy `ps` and `pdf` fields, although we strongly encourage using the `file` field. @@ -197,7 +202,6 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `# - When inserting a duplicate the right entry will be selected - Preview panel height is now saved immediately, thus is shown correctly if the panel height is changed, closed and opened again - ### Removed - [#1610](https://github.com/JabRef/jabref/issues/1610) Removed the possibility to auto show or hide the groups interface - It is not longer possible to choose to convert HTML sub- and superscripts to equations @@ -605,4 +609,4 @@ The changelog of 2.11 and versions before is maintained as [text file](https://g [3.1]: https://github.com/JabRef/jabref/compare/v3.0...v3.1 [3.0]: https://github.com/JabRef/jabref/compare/v2.11.1...v3.0 [dev_2.11]: https://github.com/JabRef/jabref/compare/v2.11.1...dev_2.11 -[2.11.1]: https://github.com/JabRef/jabref/compare/v2.11...v2.11.1 +[2.11.1]: https://github.com/JabRef/jabref/compare/v2.11...v2.11.1 \ No newline at end of file diff --git a/src/main/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatter.java b/src/main/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatter.java index c05477e0e1b..602e9c824c3 100644 --- a/src/main/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatter.java +++ b/src/main/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatter.java @@ -1,12 +1,6 @@ package net.sf.jabref.logic.formatter.bibtexfields; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashSet; -import java.util.List; -import java.util.Locale; import java.util.Objects; -import java.util.stream.Collectors; import net.sf.jabref.logic.formatter.Formatter; import net.sf.jabref.logic.l10n.Localization; @@ -17,9 +11,6 @@ */ public class NormalizeNamesFormatter implements Formatter { - // Avoid partition where these values are contained - private final Collection avoidTermsInLowerCase = Arrays.asList("jr", "sr", "jnr", "snr", "von", "zu", "van", "der"); - @Override public String getName() { return Localization.lang("Normalize names of persons"); @@ -33,67 +24,6 @@ public String getKey() { @Override public String format(String nameList) { Objects.requireNonNull(nameList); - // Handle case names in order lastname, firstname and separated by "," - // E.g., Ali Babar, M., Dingsøyr, T., Lago, P., van der Vliet, H. - if (!nameList.toUpperCase(Locale.ENGLISH).contains(" AND ") && !nameList.contains("{") && !nameList.contains(";")) { - List arrayNameList = Arrays.asList(nameList.split(",")); - - // Delete spaces for correct case identification - arrayNameList.replaceAll(String::trim); - - // Looking for space between pre- and lastname - boolean spaceInAllParts = arrayNameList.stream().filter(name -> name.contains(" ")).collect(Collectors - .toList()).size() == arrayNameList.size(); - - // We hit the comma name separator case - // Usually the getAsLastFirstNamesWithAnd method would separate them if pre- and lastname are separated with "and" - // If not, we check if spaces separate pre- and lastname - if (spaceInAllParts) { - nameList = nameList.replaceAll(",", " and"); - } else { - // Looking for name affixes to avoid - // arrayNameList needs to reduce by the count off avoiding terms - // valuePartsCount holds the count of name parts without the avoided terms - - int valuePartsCount = arrayNameList.size(); - // Holds the index of each term which needs to be avoided - Collection avoidIndex = new HashSet<>(); - - for (int i = 0; i < arrayNameList.size(); i++) { - if (avoidTermsInLowerCase.contains(arrayNameList.get(i).toLowerCase())) { - avoidIndex.add(i); - valuePartsCount--; - } - } - - if ((valuePartsCount % 2) == 0) { - // We hit the described special case with name affix like Jr - StringBuilder stringBuilder = new StringBuilder(); - // avoidedTimes needs to be increased b< the count of avoided terms for correct odd/even calculation - int avoidedTimes = 0; - for (int i = 0; i < arrayNameList.size(); i++) { - if (avoidIndex.contains(i)) { - // We hit a name affix - stringBuilder.append(arrayNameList.get(i)); - stringBuilder.append(','); - avoidedTimes++; - } else { - stringBuilder.append(arrayNameList.get(i)); - if (((i + avoidedTimes) % 2) == 0) { - // Hit separation between last name and firstname --> comma has to be kept - stringBuilder.append(','); - } else { - // Hit separation between full names (e.g., Ali Babar, M. and Dingsøyr, T.) --> semicolon has to be used - // Will be treated correctly by AuthorList.parse(nameList); - stringBuilder.append(';'); - } - } - } - nameList = stringBuilder.toString(); - } - } - } - AuthorList authorList = AuthorList.parse(nameList); return authorList.getAsLastFirstNamesWithAnd(false); } diff --git a/src/main/java/net/sf/jabref/model/entry/AuthorList.java b/src/main/java/net/sf/jabref/model/entry/AuthorList.java index 6282bd5bfc4..89d326e24ee 100644 --- a/src/main/java/net/sf/jabref/model/entry/AuthorList.java +++ b/src/main/java/net/sf/jabref/model/entry/AuthorList.java @@ -1,7 +1,11 @@ package net.sf.jabref.model.entry; +import java.util.Arrays; +import java.util.Collection; import java.util.Collections; +import java.util.HashSet; import java.util.List; +import java.util.Locale; import java.util.Objects; import java.util.WeakHashMap; import java.util.stream.Collectors; @@ -130,6 +134,9 @@ public class AuthorList { private static final WeakHashMap AUTHOR_CACHE = new WeakHashMap<>(); + // Avoid partition where these values are contained + private final static Collection avoidTermsInLowerCase = Arrays.asList("jr", "sr", "jnr", "snr", "von", "zu", "van", "der"); + /** * Creates a new list of authors. *

@@ -157,6 +164,46 @@ protected AuthorList(Author author) { public static AuthorList parse(String authors) { Objects.requireNonNull(authors); + // Handle case names in order lastname, firstname and separated by "," + // E.g., Ali Babar, M., Dingsøyr, T., Lago, P., van der Vliet, H. + if (!authors.toUpperCase(Locale.ENGLISH).contains(" AND ") && !authors.contains("{") && !authors.contains(";")) { + List arrayNameList = Arrays.asList(authors.split(",")); + + // Delete spaces for correct case identification + arrayNameList.replaceAll(String::trim); + + // Looking for space between pre- and lastname + boolean spaceInAllParts = arrayNameList.stream().filter(name -> name.contains(" ")).collect(Collectors + .toList()).size() == arrayNameList.size(); + + // We hit the comma name separator case + // Usually the getAsLastFirstNamesWithAnd method would separate them if pre- and lastname are separated with "and" + // If not, we check if spaces separate pre- and lastname + if (spaceInAllParts) { + authors = authors.replaceAll(",", " and"); + } else { + // Looking for name affixes to avoid + // arrayNameList needs to reduce by the count off avoiding terms + // valuePartsCount holds the count of name parts without the avoided terms + + int valuePartsCount = arrayNameList.size(); + // Holds the index of each term which needs to be avoided + Collection avoidIndex = new HashSet<>(); + + for (int i = 0; i < arrayNameList.size(); i++) { + if (avoidTermsInLowerCase.contains(arrayNameList.get(i).toLowerCase())) { + avoidIndex.add(i); + valuePartsCount--; + } + } + + if ((valuePartsCount % 2) == 0) { + // We hit the described special case with name affix like Jr + authors = buildWithAffix(avoidIndex, arrayNameList).toString(); + } + } + } + AuthorList authorList = AUTHOR_CACHE.get(authors); if (authorList == null) { AuthorListParser parser = new AuthorListParser(); @@ -577,4 +624,34 @@ public String getForAlphabetization() { return authorsAlph; } + /** + * Builds a new array of strings with stringbuilder. + * Regarding to the name affixes. + * @return New string with correct seperation + */ + private static StringBuilder buildWithAffix(Collection indexArray, List nameList) { + StringBuilder stringBuilder = new StringBuilder(); + // avoidedTimes needs to be increased by the count of avoided terms for correct odd/even calculation + int avoidedTimes = 0; + for (int i = 0; i < nameList.size(); i++) { + if (indexArray.contains(i)) { + // We hit a name affix + stringBuilder.append(nameList.get(i)); + stringBuilder.append(','); + avoidedTimes++; + } else { + stringBuilder.append(nameList.get(i)); + if (((i + avoidedTimes) % 2) == 0) { + // Hit separation between last name and firstname --> comma has to be kept + stringBuilder.append(','); + } else { + // Hit separation between full names (e.g., Ali Babar, M. and Dingsøyr, T.) --> semicolon has to be used + // Will be treated correctly by AuthorList.parse(authors); + stringBuilder.append(';'); + } + } + } + return stringBuilder; + } + } From 104a41e7c4e65fd6e6604c888fb776952993de3f Mon Sep 17 00:00:00 2001 From: bruehldev Date: Thu, 8 Sep 2016 15:42:59 +0200 Subject: [PATCH 13/14] Added tets for authors (semi-correct) seperated by semicolons. --- .../bibtexfields/NormalizeNamesFormatterTest.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java b/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java index bdb3e89235c..536a0d1b173 100644 --- a/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java +++ b/src/test/java/net/sf/jabref/logic/formatter/bibtexfields/NormalizeNamesFormatterTest.java @@ -167,4 +167,12 @@ public void testUpperCaseSensitiveList() { assertEquals("der Barbar, Canon and der Große, Alexander", formatter.format("Canon der Barbar aNd Alexander der Große")); assertEquals("der Barbar, Canon and der Große, Alexander", formatter.format("Canon der Barbar AnD Alexander der Große")); } + + @Test + public void testSemiCorrectNamesWithSemicolon() { + assertEquals("Last, First and Last2, First2 and Last3, First3", formatter.format("Last, First; Last2, First2; Last3, First3")); + assertEquals("Last, Jr, First and Last2, First2", formatter.format("Last, Jr, First; Last2, First2")); + assertEquals("Last, First and Last2, First2 and Last3, First3 and Last4, First4", formatter.format("Last, First; Last2, First2; Last3, First3; First4 Last4")); + assertEquals("Last and Last2, First2 and Last3, First3 and Last4, First4", formatter.format("Last; Last2, First2; Last3, First3; Last4, First4")); + } } \ No newline at end of file From d6e388217692ec33b18d87c8021add24ebc62c93 Mon Sep 17 00:00:00 2001 From: bruehldev Date: Mon, 12 Sep 2016 22:40:00 +0200 Subject: [PATCH 14/14] Add empty line --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b3ad96c05cc..57562154a8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -632,4 +632,4 @@ The changelog of 2.11 and versions before is maintained as [text file](https://g [3.1]: https://github.com/JabRef/jabref/compare/v3.0...v3.1 [3.0]: https://github.com/JabRef/jabref/compare/v2.11.1...v3.0 [dev_2.11]: https://github.com/JabRef/jabref/compare/v2.11.1...dev_2.11 -[2.11.1]: https://github.com/JabRef/jabref/compare/v2.11...v2.11.1 \ No newline at end of file +[2.11.1]: https://github.com/JabRef/jabref/compare/v2.11...v2.11.1