@Override public void execute(BstEntry context) { Stack<Object> stack = vm.getStack(); if (stack.size() < 3) { throw new VMException("Not enough operands on stack for operation format.name$"); } Object o1 = stack.pop(); Object o2 = stack.pop(); Object o3 = stack.pop(); if (!(o1 instanceof String) && !(o2 instanceof Integer) && !(o3 instanceof String)) { // warning("A string is needed for change.case$"); stack.push(""); return; } String format = (String) o1; Integer name = (Integer) o2; String names = (String) o3; if (names == null) { stack.push(""); } else { AuthorList a = AuthorList.parse(names); if (name > a.getNumberOfAuthors()) { throw new VMException("Author Out of Bounds. Number " + name + " invalid for " + names); } Author author = a.getAuthor(name - 1); stack.push(BibtexNameFormatter.formatName(author, format, vm)); } }
@Test public void testGetAuthorList() { // Test caching in authorCache. AuthorList al = AuthorList.parse("John Smith"); Assert.assertEquals(al, AuthorList.parse("John Smith")); Assert.assertFalse(al.equals(AuthorList.parse("Smith"))); }
@Test public void testFixAuthorForAlphabetization() { Assert.assertEquals("Smith, J.", AuthorList.fixAuthorForAlphabetization("John Smith")); Assert.assertEquals("Neumann, J.", AuthorList.fixAuthorForAlphabetization("John von Neumann")); Assert.assertEquals("Neumann, J.", AuthorList.fixAuthorForAlphabetization("J. von Neumann")); Assert.assertEquals( "Neumann, J. and Smith, J. and Black Brown, Jr., P.", AuthorList.fixAuthorForAlphabetization( "John von Neumann and John Smith and de Black Brown, Jr., Peter")); }
@Test public void testGetAuthorsLastOnly() { // No comma before and Assert.assertEquals("", AuthorList.parse("").getAsLastNames(false)); Assert.assertEquals("Smith", AuthorList.parse("John Smith").getAsLastNames(false)); Assert.assertEquals("Smith", AuthorList.parse("Smith, Jr, John").getAsLastNames(false)); Assert.assertEquals( "von Neumann, Smith and Black Brown", AuthorList.parse("John von Neumann and John Smith and Black Brown, Peter") .getAsLastNames(false)); // Oxford comma Assert.assertEquals("", AuthorList.parse("").getAsLastNames(true)); Assert.assertEquals("Smith", AuthorList.parse("John Smith").getAsLastNames(true)); Assert.assertEquals("Smith", AuthorList.parse("Smith, Jr, John").getAsLastNames(true)); Assert.assertEquals( "von Neumann, Smith, and Black Brown", AuthorList.parse("John von Neumann and John Smith and Black Brown, Peter") .getAsLastNames(true)); Assert.assertEquals( "von Neumann and Smith", AuthorList.parse("John von Neumann and John Smith").getAsLastNames(false)); }
private static int compareSingleField(String field, BibEntry one, BibEntry two) { String s1 = one.getField(field); String s2 = two.getField(field); if (s1 == null) { if (s2 == null) { return EMPTY_IN_BOTH; } return EMPTY_IN_ONE; } else if (s2 == null) { return EMPTY_IN_TWO; } if ("author".equals(field) || "editor".equals(field)) { // Specific for name fields. // Harmonise case: String auth1 = AuthorList.fixAuthor_lastNameOnlyCommas(s1, false).replace(" and ", " ").toLowerCase(); String auth2 = AuthorList.fixAuthor_lastNameOnlyCommas(s2, false).replace(" and ", " ").toLowerCase(); double similarity = DuplicateCheck.correlateByWords(auth1, auth2); if (similarity > 0.8) { return EQUAL; } return NOT_EQUAL; } else if ("pages".equals(field)) { // Pages can be given with a variety of delimiters, "-", "--", " - ", " -- ". // We do a replace to harmonize these to a simple "-": // After this, a simple test for equality should be enough: s1 = s1.replaceAll("[- ]+", "-"); s2 = s2.replaceAll("[- ]+", "-"); if (s1.equals(s2)) { return EQUAL; } return NOT_EQUAL; } else if ("journal".equals(field)) { // We do not attempt to harmonize abbreviation state of the journal names, // but we remove periods from the names in case they are abbreviated with // and without dots: s1 = s1.replace(".", "").toLowerCase(); s2 = s2.replace(".", "").toLowerCase(); double similarity = DuplicateCheck.correlateByWords(s1, s2); if (similarity > 0.8) { return EQUAL; } return NOT_EQUAL; } else { s1 = s1.toLowerCase(); s2 = s2.toLowerCase(); double similarity = DuplicateCheck.correlateByWords(s1, s2); if (similarity > 0.8) { return EQUAL; } return NOT_EQUAL; } }
@Test public void testGetAuthorsFirstFirstAnds() { Assert.assertEquals("John Smith", AuthorList.parse("John Smith").getAsFirstLastNamesWithAnd()); Assert.assertEquals( "John Smith and Peter Black Brown", AuthorList.parse("John Smith and Black Brown, Peter").getAsFirstLastNamesWithAnd()); Assert.assertEquals( "John von Neumann and John Smith and Peter Black Brown", AuthorList.parse("John von Neumann and John Smith and Black Brown, Peter") .getAsFirstLastNamesWithAnd()); Assert.assertEquals( "First von Last, Jr. III", AuthorList.parse("von Last, Jr. III, First").getAsFirstLastNamesWithAnd()); }
@Test public void testGetAuthorsLastFirstAnds() { Assert.assertEquals( "Smith, John", AuthorList.parse("John Smith").getAsLastFirstNamesWithAnd(false)); Assert.assertEquals( "Smith, John and Black Brown, Peter", AuthorList.parse("John Smith and Black Brown, Peter").getAsLastFirstNamesWithAnd(false)); Assert.assertEquals( "von Neumann, John and Smith, John and Black Brown, Peter", AuthorList.parse("John von Neumann and John Smith and Black Brown, Peter") .getAsLastFirstNamesWithAnd(false)); Assert.assertEquals( "von Last, Jr, First", AuthorList.parse("von Last, Jr ,First").getAsLastFirstNamesWithAnd(false)); Assert.assertEquals( "Smith, J.", AuthorList.parse("John Smith").getAsLastFirstNamesWithAnd(true)); Assert.assertEquals( "Smith, J. and Black Brown, P.", AuthorList.parse("John Smith and Black Brown, Peter").getAsLastFirstNamesWithAnd(true)); Assert.assertEquals( "von Neumann, J. and Smith, J. and Black Brown, P.", AuthorList.parse("John von Neumann and John Smith and Black Brown, Peter") .getAsLastFirstNamesWithAnd(true)); Assert.assertEquals( "von Last, Jr, F.", AuthorList.parse("von Last, Jr ,First").getAsLastFirstNamesWithAnd(true)); }
@SuppressWarnings("unused") @Test public void testFixAuthorFirstNameFirst() { Assert.assertEquals("John Smith", AuthorList.fixAuthorFirstNameFirst("John Smith")); Assert.assertEquals( "John Smith and Peter Black Brown", AuthorList.fixAuthorFirstNameFirst("John Smith and Black Brown, Peter")); Assert.assertEquals( "John von Neumann and John Smith and Peter Black Brown", AuthorList.fixAuthorFirstNameFirst( "John von Neumann and John Smith and Black Brown, Peter")); Assert.assertEquals( "First von Last, Jr. III", AuthorList.fixAuthorFirstNameFirst("von Last, Jr. III, First")); // Check caching Assert.assertTrue( AuthorList.fixAuthorFirstNameFirst("John von Neumann and John Smith and Black Brown, Peter") .equals( AuthorList.fixAuthorFirstNameFirst( "John von Neumann" + (0 == 1 ? "" : " and ") + "John Smith and Black Brown, Peter"))); }
@Test public void testGetAuthor() { Author author = AuthorList.parse("John Smith and von Neumann, Jr, John").getAuthor(0); Assert.assertEquals(Optional.of("John"), author.getFirst()); Assert.assertEquals(Optional.of("J."), author.getFirstAbbr()); Assert.assertEquals("John Smith", author.getFirstLast(false)); Assert.assertEquals("J. Smith", author.getFirstLast(true)); Assert.assertEquals(Optional.empty(), author.getJr()); Assert.assertEquals(Optional.of("Smith"), author.getLast()); Assert.assertEquals("Smith, John", author.getLastFirst(false)); Assert.assertEquals("Smith, J.", author.getLastFirst(true)); Assert.assertEquals("Smith", author.getLastOnly()); Assert.assertEquals("Smith, J.", author.getNameForAlphabetization()); Assert.assertEquals(Optional.empty(), author.getVon()); author = AuthorList.parse("Peter Black Brown").getAuthor(0); Assert.assertEquals(Optional.of("Peter Black"), author.getFirst()); Assert.assertEquals(Optional.of("P. B."), author.getFirstAbbr()); Assert.assertEquals("Peter Black Brown", author.getFirstLast(false)); Assert.assertEquals("P. B. Brown", author.getFirstLast(true)); Assert.assertEquals(Optional.empty(), author.getJr()); Assert.assertEquals(Optional.empty(), author.getVon()); author = AuthorList.parse("John Smith and von Neumann, Jr, John").getAuthor(1); Assert.assertEquals(Optional.of("John"), author.getFirst()); Assert.assertEquals(Optional.of("J."), author.getFirstAbbr()); Assert.assertEquals("John von Neumann, Jr", author.getFirstLast(false)); Assert.assertEquals("J. von Neumann, Jr", author.getFirstLast(true)); Assert.assertEquals(Optional.of("Jr"), author.getJr()); Assert.assertEquals(Optional.of("Neumann"), author.getLast()); Assert.assertEquals("von Neumann, Jr, John", author.getLastFirst(false)); Assert.assertEquals("von Neumann, Jr, J.", author.getLastFirst(true)); Assert.assertEquals("von Neumann", author.getLastOnly()); Assert.assertEquals("Neumann, Jr, J.", author.getNameForAlphabetization()); Assert.assertEquals(Optional.of("von"), author.getVon()); }
@Test public void testRemoveStartAndEndBraces() { Assert.assertEquals("{A}bbb{c}", AuthorList.parse("{A}bbb{c}").getAsLastNames(false)); Assert.assertEquals( "Vall{\\'e}e Poussin", AuthorList.parse("{Vall{\\'e}e Poussin}").getAsLastNames(false)); Assert.assertEquals( "Poussin", AuthorList.parse("{Vall{\\'e}e} {Poussin}").getAsLastNames(false)); Assert.assertEquals("Poussin", AuthorList.parse("Vall{\\'e}e Poussin").getAsLastNames(false)); Assert.assertEquals("Lastname", AuthorList.parse("Firstname {Lastname}").getAsLastNames(false)); Assert.assertEquals( "Firstname Lastname", AuthorList.parse("{Firstname Lastname}").getAsLastNames(false)); }
@Test public void testGetAuthorsNatbib() { Assert.assertEquals("", AuthorList.parse("").getAsNatbib()); Assert.assertEquals("Smith", AuthorList.parse("John Smith").getAsNatbib()); Assert.assertEquals( "Smith and Black Brown", AuthorList.parse("John Smith and Black Brown, Peter").getAsNatbib()); Assert.assertEquals( "von Neumann et al.", AuthorList.parse("John von Neumann and John Smith and Black Brown, Peter").getAsNatbib()); /* * [ 1465610 ] (Double-)Names containing hyphen (-) not handled correctly */ Assert.assertEquals( "Last-Name et al.", AuthorList.parse("First Second Last-Name" + " and John Smith and Black Brown, Peter") .getAsNatbib()); // Test caching AuthorList al = AuthorList.parse("John von Neumann and John Smith and Black Brown, Peter"); Assert.assertTrue(al.getAsNatbib().equals(al.getAsNatbib())); }
@SuppressWarnings("unused") @Test public void testFixAuthorNatbib() { Assert.assertEquals("", AuthorList.fixAuthorNatbib("")); Assert.assertEquals("Smith", AuthorList.fixAuthorNatbib("John Smith")); Assert.assertEquals( "Smith and Black Brown", AuthorList.fixAuthorNatbib("John Smith and Black Brown, Peter")); Assert.assertEquals( "von Neumann et al.", AuthorList.fixAuthorNatbib("John von Neumann and John Smith and Black Brown, Peter")); // Is not cached! Assert.assertTrue( AuthorList.fixAuthorNatbib("John von Neumann and John Smith and Black Brown, Peter") .equals( AuthorList.fixAuthorNatbib( "John von Neumann" + (0 == 1 ? "" : " and ") + "John Smith and Black Brown, Peter"))); }
/** * This is a convenience method for getAuthorsLastFirst() * * @see AuthorList#getAuthorsLastFirst */ public static String fixAuthor_lastNameFirstCommas( String authors, boolean abbr, boolean oxfordComma) { return AuthorList.getAuthorList(authors).getAuthorsLastFirst(abbr, oxfordComma); }
/** * This is a convenience method for getAuthorsFirstFirstAnds() * * @see AuthorList#getAuthorsFirstFirstAnds */ public static String fixAuthor_firstNameFirst(String authors) { return AuthorList.getAuthorList(authors).getAuthorsFirstFirstAnds(); }
@Override public String format(String fieldText) { return AuthorList.fixAuthorLastNameFirstCommas(fieldText, false, false); }
@Test public void testGetAuthorsLastFirstNoComma() { // No commas before and AuthorList al; al = AuthorList.parse(""); Assert.assertEquals("", al.getAsLastFirstNames(true, false)); Assert.assertEquals("", al.getAsLastFirstNames(false, false)); al = AuthorList.parse("John Smith"); Assert.assertEquals("Smith, John", al.getAsLastFirstNames(false, false)); Assert.assertEquals("Smith, J.", al.getAsLastFirstNames(true, false)); al = AuthorList.parse("John Smith and Black Brown, Peter"); Assert.assertEquals("Smith, John and Black Brown, Peter", al.getAsLastFirstNames(false, false)); Assert.assertEquals("Smith, J. and Black Brown, P.", al.getAsLastFirstNames(true, false)); al = AuthorList.parse("John von Neumann and John Smith and Black Brown, Peter"); // Method description is different than code -> additional comma // there Assert.assertEquals( "von Neumann, John, Smith, John and Black Brown, Peter", al.getAsLastFirstNames(false, false)); Assert.assertEquals( "von Neumann, J., Smith, J. and Black Brown, P.", al.getAsLastFirstNames(true, false)); al = AuthorList.parse("John Peter von Neumann"); Assert.assertEquals("von Neumann, J. P.", al.getAsLastFirstNames(true, false)); }
/** * This is a convenience method for getAuthorsNatbib() * * @see AuthorList#getAuthorsNatbib */ public static String fixAuthor_Natbib(String authors) { return AuthorList.getAuthorList(authors).getAuthorsNatbib(); }
/** * This is a convenience method for getAuthorsLastOnly() * * @see AuthorList#getAuthorsLastOnly */ public static String fixAuthor_lastNameOnlyCommas(String authors, boolean oxfordComma) { return AuthorList.getAuthorList(authors).getAuthorsLastOnly(oxfordComma); }
@Test public void parseNameWithHyphenInFirstName() throws Exception { Author expected = new Author("Tse-tung", "T.-t.", null, "Mao", null); Assert.assertEquals(new AuthorList(expected), AuthorList.parse("Tse-tung Mao")); }
@Test public void parseNameWithBracesAroundFirstName() throws Exception { // TODO: Be more intelligent and abbreviate the first name correctly Author expected = new Author("Tse-tung", "{Tse-tung}.", null, "Mao", null); Assert.assertEquals(new AuthorList(expected), AuthorList.parse("{Tse-tung} Mao")); }
@Test public void createCorrectInitials() { Assert.assertEquals( Optional.of("J. G."), AuthorList.parse("Hornberg, Johann Gottfried").getAuthor(0).getFirstAbbr()); }
@Test public void testGetAuthorsFirstFirst() { AuthorList al; al = AuthorList.parse(""); Assert.assertEquals("", al.getAsFirstLastNames(true, false)); Assert.assertEquals("", al.getAsFirstLastNames(false, false)); Assert.assertEquals("", al.getAsFirstLastNames(true, true)); Assert.assertEquals("", al.getAsFirstLastNames(false, true)); al = AuthorList.parse("John Smith"); Assert.assertEquals("John Smith", al.getAsFirstLastNames(false, false)); Assert.assertEquals("J. Smith", al.getAsFirstLastNames(true, false)); Assert.assertEquals("John Smith", al.getAsFirstLastNames(false, true)); Assert.assertEquals("J. Smith", al.getAsFirstLastNames(true, true)); al = AuthorList.parse("John Smith and Black Brown, Peter"); Assert.assertEquals("John Smith and Peter Black Brown", al.getAsFirstLastNames(false, false)); Assert.assertEquals("J. Smith and P. Black Brown", al.getAsFirstLastNames(true, false)); Assert.assertEquals("John Smith and Peter Black Brown", al.getAsFirstLastNames(false, true)); Assert.assertEquals("J. Smith and P. Black Brown", al.getAsFirstLastNames(true, true)); al = AuthorList.parse("John von Neumann and John Smith and Black Brown, Peter"); Assert.assertEquals( "John von Neumann, John Smith and Peter Black Brown", al.getAsFirstLastNames(false, false)); Assert.assertEquals( "J. von Neumann, J. Smith and P. Black Brown", al.getAsFirstLastNames(true, false)); Assert.assertEquals( "John von Neumann, John Smith, and Peter Black Brown", al.getAsFirstLastNames(false, true)); Assert.assertEquals( "J. von Neumann, J. Smith, and P. Black Brown", al.getAsFirstLastNames(true, true)); al = AuthorList.parse("John Peter von Neumann"); Assert.assertEquals("John Peter von Neumann", al.getAsFirstLastNames(false, false)); Assert.assertEquals("John Peter von Neumann", al.getAsFirstLastNames(false, true)); Assert.assertEquals("J. P. von Neumann", al.getAsFirstLastNames(true, false)); Assert.assertEquals("J. P. von Neumann", al.getAsFirstLastNames(true, true)); }
/** * This is a convenience method for getAuthorsLastFirstAnds(true) * * @see AuthorList#getAuthorsLastFirstAnds */ public static String fixAuthor_lastNameFirst(String authors) { return AuthorList.getAuthorList(authors).getAuthorsLastFirstAnds(false); }
@Test public void parseNameWithHyphenInLastName() throws Exception { Author expected = new Author("Firstname", "F.", null, "Bailey-Jones", null); Assert.assertEquals(new AuthorList(expected), AuthorList.parse("Firstname Bailey-Jones")); }
/** * This is a convenience method for getAuthorsLastFirstAnds() * * @see AuthorList#getAuthorsLastFirstAnds */ public static String fixAuthor_lastNameFirst(String authors, boolean abbreviate) { return AuthorList.getAuthorList(authors).getAuthorsLastFirstAnds(abbreviate); }
@Test 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")); }
/** * This is a convenience method for getAuthorsForAlphabetization() * * @see AuthorList#getAuthorsForAlphabetization */ public static String fixAuthorForAlphabetization(String authors) { return AuthorList.getAuthorList(authors).getAuthorsForAlphabetization(); }
/** Parse the entries in the source, and return a List of BibEntry objects. */ @Override public List<BibEntry> importEntries(InputStream stream, OutputPrinter status) throws IOException { ArrayList<BibEntry> bibitems = new ArrayList<>(); StringBuilder sb = new StringBuilder(); String str; try (BufferedReader in = new BufferedReader(ImportFormatReader.getReaderDefaultEncoding(stream))) { while ((str = in.readLine()) != null) { if (str.length() < 2) { continue; } if (str.indexOf("Record") == 0) { sb.append("__::__").append(str); } else { sb.append("__NEWFIELD__").append(str); } } } String[] entries = sb.toString().split("__::__"); String type = ""; HashMap<String, String> h = new HashMap<>(); for (String entry : entries) { if (entry.indexOf("Record") != 0) { continue; } h.clear(); String[] fields = entry.split("__NEWFIELD__"); for (String s : fields) { // System.out.println(fields[j]); String f3 = s.substring(0, 2); String frest = s.substring(5); if ("TI".equals(f3)) { h.put("title", frest); } else if ("PY".equals(f3)) { h.put("year", frest); } else if ("AU".equals(f3)) { h.put( "author", AuthorList.fixAuthor_lastNameFirst(frest.replace(",-", ", ").replace(";", " and "))); } else if ("AB".equals(f3)) { h.put("abstract", frest); } else if ("ID".equals(f3)) { h.put("keywords", frest); } else if ("SO".equals(f3)) { int m = frest.indexOf('.'); if (m >= 0) { String jr = frest.substring(0, m); h.put("journal", jr.replace("-", " ")); frest = frest.substring(m); m = frest.indexOf(';'); if (m >= 5) { String yr = frest.substring(m - 5, m); h.put("year", yr); frest = frest.substring(m); m = frest.indexOf(':'); if (m >= 0) { String pg = frest.substring(m + 1).trim(); h.put("pages", pg); h.put("volume", frest.substring(1, m)); } } } } else if ("RT".equals(f3)) { frest = frest.trim(); if ("Journal-Paper".equals(frest)) { type = "article"; } else if ("Conference-Paper".equals(frest) || "Conference-Paper; Journal-Paper".equals(frest)) { type = "inproceedings"; } else { type = frest.replace(" ", ""); } } } BibEntry b = new BibEntry( DEFAULT_BIBTEXENTRY_ID, EntryTypes.getTypeOrDefault(type)); // id assumes an existing database so don't // create one here b.setField(h); bibitems.add(b); } return bibitems; }
@Test public void parseNameWithBracesAroundLastName() throws Exception { Author expected = new Author("Hans", "H.", null, "van den Bergen", null); Assert.assertEquals(new AuthorList(expected), AuthorList.parse("{van den Bergen}, Hans")); }
@Test public void testGetAuthorsLastFirstOxfordComma() { // Oxford comma AuthorList al; al = AuthorList.parse(""); Assert.assertEquals("", al.getAsLastFirstNames(true, true)); Assert.assertEquals("", al.getAsLastFirstNames(false, true)); al = AuthorList.parse("John Smith"); Assert.assertEquals("Smith, John", al.getAsLastFirstNames(false, true)); Assert.assertEquals("Smith, J.", al.getAsLastFirstNames(true, true)); al = AuthorList.parse("John Smith and Black Brown, Peter"); Assert.assertEquals("Smith, John and Black Brown, Peter", al.getAsLastFirstNames(false, true)); Assert.assertEquals("Smith, J. and Black Brown, P.", al.getAsLastFirstNames(true, true)); al = AuthorList.parse("John von Neumann and John Smith and Black Brown, Peter"); Assert.assertEquals( "von Neumann, John, Smith, John, and Black Brown, Peter", al.getAsLastFirstNames(false, true)); Assert.assertEquals( "von Neumann, J., Smith, J., and Black Brown, P.", al.getAsLastFirstNames(true, true)); al = AuthorList.parse("John Peter von Neumann"); Assert.assertEquals("von Neumann, J. P.", al.getAsLastFirstNames(true, true)); }