//CPS012 Add first or middle names for ambiguous last names v5.0 //Version 5.0 Completely re-written for Citavi 6.4 (> Citavi 6.3.16) //Berücksichtigt die 3 neuen Werte: NameIdentity.LastNamePrefix, NameIdentity.LastNameFirstNameInitialMiddleNameInitial & NameIdentity.LastNameFirstNameInitialMiddleNameFull //Version 4.2 Added first name font style parameter //Version 4.1 Built-in option to insert non-breaking spaces and hyphens //Version 4.0 Completely re-written for Citavi 6 (6.2 or higher) //Disambiguation of identical person names by successively adding first name initials, full first names, middle name initals and full middle names (if available) public IEnumerable <ITextUnit> GetTextUnits(ComponentPart componentPart, Template template, Citation citation, out bool handled) { //Should only the primary authors be considered in checking last names for ambiguity? bool checkAmbiguityForPrimaryAuthorsOnly = false; //When a first and/or middle name is added for disambiguation, should that be before or after the last name? PersonNameOrder nameOrderForAmbiguityResolution = PersonNameOrder.FirstNameLastName; //Formatting of the first name; combine several styles with a pipe character //FontStyle firstNameFontStyle = FontStyle.Bold | FontStyle.Italic; FontStyle firstNameFontStyle = FontStyle.Neutral; //In case of ambiguous last names, should the first attempt to disambigutate be the addition of full first names or just the initials? NameFormat firstNameFormatForAmbiguityResolution = NameFormat.Abbreviated; //NameFormat.Full John Mike //NameFormat.Abbreviated J. M. //NameFormat.AbbreviatedNoPeriod J M //NameFormat.AbbreviatedCompact J.M. //NameFormat.Compact JM //Insert non-breaking spaces and hyphens - geschütztes Leerzeichen und geschützten Bindestrich einfügen var useNonBreakingSpacesInAndBetweenFirstAndMiddleNames = true; //if true, then e.g. Meyers, J.°R. var useNonBreakingSpaceBetweenLastAndFirstName = true; //if true, then e.g. Meyers,°John Richard var useNonBreakingSpaceBetweenPrefixAndName = true; //if true, then e.g. von°Bülow, V. var useNonBreakingHyphenInFirstAndMiddleName = true; //if true, then e.g. Ewing, J.-R. handled = false; if (citation == null || citation.Reference == null) { return(null); } if (componentPart == null || componentPart.Elements == null || !componentPart.Elements.Any()) { return(null); } CitationManager citationManager = citation.CitationManager; if (citationManager == null) { return(null); } PersonFieldElement personFieldElement = componentPart.Elements.OfType <PersonFieldElement>().FirstOrDefault(); if (personFieldElement == null) { return(null); } if (personFieldElement.SuppressOutput) { return(null); } #region Insert non-breaking spaces and hyphens IEnumerable <PersonFieldElement> personFieldElements = componentPart.Elements.OfType <PersonFieldElement>(); if (personFieldElements == null || personFieldElements.Count() == 0) { return(null); } foreach (PersonFieldElement element in personFieldElements) { if (useNonBreakingSpacesInAndBetweenFirstAndMiddleNames) { element.FirstGroupUseNonBreakingSpaceInAndBetweenFirstAndMiddleNames = true; element.SecondGroupUseNonBreakingSpaceInAndBetweenFirstAndMiddleNames = true; element.LastPersonUseNonBreakingSpaceInAndBetweenFirstAndMiddleNames = true; } if (useNonBreakingSpaceBetweenLastAndFirstName) { element.FirstGroupUseNonBreakingSpaceBetweenLastAndFirstName = true; element.SecondGroupUseNonBreakingSpaceBetweenLastAndFirstName = true; element.LastPersonUseNonBreakingSpaceBetweenLastAndFirstName = true; } if (useNonBreakingSpaceBetweenPrefixAndName) { element.FirstGroupUseNonBreakingSpaceBetweenPrefixAndName = true; element.SecondGroupUseNonBreakingSpaceBetweenPrefixAndName = true; element.LastPersonUseNonBreakingSpaceBetweenPrefixAndName = true; } if (useNonBreakingHyphenInFirstAndMiddleName) { element.FirstGroupUseNonBreakingHyphenInFirstAndMiddleNames = true; element.SecondGroupUseNonBreakingHyphenInFirstAndMiddleNames = true; element.LastPersonUseNonBreakingHyphenInFirstAndMiddleNames = true; } } #endregion #region BeforeFormatPerson: Resolve last name ambiguity BeforeFormatPersonEventArgs bfp; personFieldElement.PersonFormatter.BeforeFormatPerson += (sender, e) => { bfp = (BeforeFormatPersonEventArgs)e; if (bfp.Person == null) { return; } if (checkAmbiguityForPrimaryAuthorsOnly && bfp.Index > 1) { return; } bool isLastNameAmbiguous = checkAmbiguityForPrimaryAuthorsOnly ? citationManager.IsFirstCitedPersonLastnameAmbiguous(bfp.Person.LastName) : citationManager.IsCitedPersonLastNameAmbiguous(bfp.Person.LastName); if (!isLastNameAmbiguous) { return; } NameIdentity nameIdentity = checkAmbiguityForPrimaryAuthorsOnly ? citationManager.GetFirstPersonNameIdentity(bfp.Person) : citationManager.GetPersonNameIdentity(bfp.Person); /* PERSON A PERSON B * NameIdentity.None Müller, Gerd Jakob Meier, Konrad Martin * NameIdentity.LastName Müller, Gerd Jakob Müller, Konrad Martin * NameIdentity.LastNamePrefix Müller, Gerd Jakob von Müller, Konrad Martin von * NameIdentity.LastNameFirstNameInitial Müller, Gerd Jakob Müller, Gustav Martin * NameIdentity.LastNameFirstNameFull Müller, Gerd Jakob Müller, Gerd Martin * NameIdentity.LastNameFirstNameFullMiddleNameInitial Müller, Gerd Jakob Müller, Gerd Johann * NameIdentity.LastNameFirstNameFullMiddleNameFull Müller, Gerd Jakob Müller, Gerd Jakob [der Ältere] */ switch (nameIdentity) { case NameIdentity.LastName: { if (!string.IsNullOrEmpty(bfp.Person.Prefix)) { bfp.FirstNameFormat = firstNameFormatForAmbiguityResolution; bfp.MiddleNameUsage = MiddleNameUsage.None; break; } else { bfp.FirstNameFormat = firstNameFormatForAmbiguityResolution; bfp.MiddleNameFormat = firstNameFormatForAmbiguityResolution; bfp.MiddleNameUsage = MiddleNameUsage.All; break; } } case NameIdentity.LastNamePrefix: { bfp.FirstNameFormat = firstNameFormatForAmbiguityResolution; bfp.MiddleNameFormat = firstNameFormatForAmbiguityResolution; bfp.MiddleNameUsage = MiddleNameUsage.All; bfp.NamePrefixFontStyle = firstNameFontStyle; break; } case NameIdentity.LastNameFirstNameInitial: { if (!string.IsNullOrEmpty(bfp.Person.MiddleName)) { bfp.FirstNameFormat = firstNameFormatForAmbiguityResolution; bfp.MiddleNameFormat = firstNameFormatForAmbiguityResolution; bfp.MiddleNameUsage = MiddleNameUsage.All; break; } else { bfp.FirstNameFormat = NameFormat.Full; bfp.MiddleNameFormat = NameFormat.Full; bfp.MiddleNameUsage = MiddleNameUsage.All; break; } } case NameIdentity.LastNameFirstNameInitialMiddleNameInitial: { bfp.FirstNameFormat = NameFormat.Full; bfp.MiddleNameFormat = NameFormat.Full; bfp.MiddleNameUsage = MiddleNameUsage.All; break; } case NameIdentity.LastNameFirstNameInitialMiddleNameFull: { bfp.FirstNameFormat = NameFormat.Full; bfp.MiddleNameFormat = NameFormat.Full; bfp.MiddleNameUsage = MiddleNameUsage.All; break; } case NameIdentity.LastNameFirstNameFull: { bfp.FirstNameFormat = firstNameFormatForAmbiguityResolution; bfp.MiddleNameFormat = firstNameFormatForAmbiguityResolution; bfp.MiddleNameUsage = MiddleNameUsage.All; break; } case NameIdentity.LastNameFirstNameFullMiddleNameInitial: { bfp.FirstNameFormat = NameFormat.Full; bfp.MiddleNameFormat = NameFormat.Full; bfp.MiddleNameUsage = MiddleNameUsage.All; break; } case NameIdentity.LastNameFirstNameFullMiddleNameFull: { //we can safely assume, that they have a different title or name addendum, like Jr. Sr. etc. bfp.SuppressNameSuffix = false; bfp.MiddleNameUsage = MiddleNameUsage.None; break; } case NameIdentity.None: default: { return; } } bfp.NameOrder = nameOrderForAmbiguityResolution; }; #endregion return(null); }
//CPS023 Format certain persons differently, by adding an "f" to the beginning of the person's notes field public IEnumerable <ITextUnit> GetTextUnits(ComponentPart componentPart, Template template, Citation citation, out bool handled) { //When a first and/or middle name is added for disambiguation, should that be before or after the last name? PersonNameOrder nameOrder = PersonNameOrder.FirstNameLastName; NameFormat firstNameFormat = NameFormat.Abbreviated; FontStyle lastNameFontStyle = FontStyle.Neutral; FontStyle firstNameFontStyle = FontStyle.Italic; //NameFormat.Full John Mike //NameFormat.Abbreviated J. M. //NameFormat.AbbreviatedNoPeriod J M //NameFormat.AbbreviatedCompact J.M. //NameFormat.Compact JM handled = false; if (citation == null || citation.Reference == null) { return(null); } if (componentPart == null || componentPart.Elements == null || !componentPart.Elements.Any()) { return(null); } PersonFieldElement personFieldElement = componentPart.Elements.OfType <PersonFieldElement>().FirstOrDefault(); if (personFieldElement == null) { return(null); } if (personFieldElement.SuppressOutput) { return(null); } #region BeforeFormatPerson BeforeFormatPersonEventArgs bfp; personFieldElement.PersonFormatter.BeforeFormatPerson += (sender, e) => { bfp = (BeforeFormatPersonEventArgs)e; if (bfp.Person == null) { return; } if (string.IsNullOrWhiteSpace(bfp.Person.Notes)) { return; } //add ab "F" to the person's notes field to indicate: output with first name if (!bfp.Person.Notes.StartsWith("F", StringComparison.OrdinalIgnoreCase)) { return; } bfp.FirstNameFormat = firstNameFormat; bfp.NameOrder = nameOrder; bfp.FirstAndMiddleNameFontStyle = firstNameFontStyle; bfp.LastNameFontStyle = lastNameFontStyle; }; #endregion return(null); }
//Version 1.0a Add initials if there are other authors with the same last name (FIRST PERSON ONLY) public IEnumerable <ITextUnit> GetTextUnits(ComponentPart componentPart, Template template, Citation citation, out bool handled) { PersonNameOrder nameOrderForAmbiguityResolution = PersonNameOrder.LastNameFirstName; NameFormat firstNameFormatForAmbiguityResolution = NameFormat.Abbreviated; handled = false; if (componentPart == null) { return(null); } if (citation == null || citation.CitationManager == null) { return(null); } Reference reference = citation.Reference; if (reference == null) { return(null); } PersonFieldElement personFieldElement = componentPart.GetFieldElements().OfType <PersonFieldElement>().FirstOrDefault(); if (personFieldElement == null) { return(null); } IEnumerable <Person> persons = personFieldElement.GetPersons(reference); if (persons == null || !persons.Any()) { return(null); } Guid firstPersonId = persons.First <Person>().Id; #region BeforeFormatPerson BeforeFormatPersonEventArgs b; personFieldElement.PersonFormatter.BeforeFormatPerson += (sender, e) => { b = (BeforeFormatPersonEventArgs)e; if (b.Person == null) { return; } if (!b.Person.Id.Equals(firstPersonId)) { return; } if (citation.CitationManager.IsCitedPersonLastNameAmbiguous(b.Person.LastName)) { b.NameOrder = nameOrderForAmbiguityResolution; b.FirstNameFormat = firstNameFormatForAmbiguityResolution; } }; #endregion return(null); }
//CPS012 Add first or middle names for ambiguous last names v3.5 //IMPORTANT: in Citavi 6.1 or below, it is only possible to identify persons with the same last name, not first or middle names. //Also disambiguation can only take place in a single stp, e.g. by adding first name initials only, or by adding full first names, or by adding both first and middle names. public IEnumerable <ITextUnit> GetTextUnits(ComponentPart componentPart, Template template, Citation citation, out bool handled) { //IMPORTANT: It is not possible Citavi 6.1 or below, to confine the name ambiguity check to the primary authors only. //If this is a requirement, pls. update to Citavi 6.2 or higher and make use of the latest component macr CPS012 version 4.0 or higher. //It is possible though to confine the disambiguation to the primary authors. bool disambiguatePrimaryAuthorsOnly = true; //When a first and/or middle name is added for disambiguation, should that be before or after the last name? PersonNameOrder nameOrderForAmbiguityResolution = PersonNameOrder.FirstNameLastName; //In case of ambiguous last names, should the disambiguation happen by adding full first names or just the initials? NameFormat firstNameFormatForAmbiguityResolution = NameFormat.Abbreviated; //NameFormat.Full John Mike //NameFormat.Abbreviated J. M. //NameFormat.AbbreviatedNoPeriod J M //NameFormat.AbbreviatedCompact J.M. //NameFormat.Compact JM //Should the middle name(s) be added for disambiguation? MiddleNameUsage middleNameUsageForAmbiguityResolution = MiddleNameUsage.All; //MiddleNameUsage.FirstOnly //MiddleNameUsage.All //In case of ambiguous last names, should disambiguation happen by adding full middle names or just the initials? NameFormat middleNameFormatForAmbiguityResolution = NameFormat.Full; handled = false; if (citation == null || citation.Reference == null) { return(null); } if (componentPart == null || componentPart.Elements == null || !componentPart.Elements.Any()) { return(null); } CitationManager citationManager = citation.CitationManager; if (citationManager == null) { return(null); } PersonFieldElement personFieldElement = componentPart.Elements.OfType <PersonFieldElement>().FirstOrDefault(); if (personFieldElement == null) { return(null); } if (personFieldElement.SuppressOutput) { return(null); } #region BeforeFormatPerson: Resolve last name ambiguity BeforeFormatPersonEventArgs bfp; personFieldElement.PersonFormatter.BeforeFormatPerson += (sender, e) => { bfp = (BeforeFormatPersonEventArgs)e; if (bfp.Person == null) { return; } if (disambiguatePrimaryAuthorsOnly && bfp.Index > 1) { return; } bool isLastNameAmbiguous = citationManager.IsCitedPersonLastNameAmbiguous(bfp.Person.LastName); if (!isLastNameAmbiguous) { return; } bfp.FirstNameFormat = firstNameFormatForAmbiguityResolution; bfp.NameOrder = nameOrderForAmbiguityResolution; bfp.MiddleNameUsage = middleNameUsageForAmbiguityResolution; bfp.MiddleNameFormat = middleNameFormatForAmbiguityResolution; }; #endregion return(null); }