NCBI C++ ToolKit
Author.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: Author.cpp 95805 2021-12-28 18:41:52Z stakhovv $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: .......
27  *
28  * File Description:
29  * .......
30  *
31  * Remark:
32  * This code was originally generated by application DATATOOL
33  * using the following specifications:
34  * 'biblio.asn'.
35  */
36 
37 // standard includes
38 #include <ncbi_pch.hpp>
40 
41 // generated includes
43 
44 // generated classes
45 
47 
48 BEGIN_objects_SCOPE // namespace ncbi::objects::
49 
50 // destructor
52 {
53 }
54 
55 
57 {
58  // XXX - honor flags?
60  return true;
61 }
62 
63 
65 {
66  const CPerson_id& pid = GetName();
67  switch (pid.Which()) {
68  case CPerson_id::e_Name:
69  {
70  const CName_std& name = pid.GetName();
71  if (HasText(name.GetLast())) {
72  return x_GetLabelV2
73  (label, flags, name.GetLast(),
74  name.CanGetInitials() ? name.GetInitials() : kEmptyStr,
75  name.CanGetSuffix() ? name.GetSuffix() : kEmptyStr);
76  } else if (name.IsSetFull() && HasText(name.GetFull())) {
77  return x_GetLabelV2(label, flags, name.GetFull());
78  } else {
79  return false;
80  }
81  }
82  case CPerson_id::e_Ml:
83  return x_GetLabelV2(label, flags, pid.GetMl());
84  case CPerson_id::e_Str:
85  return x_GetLabelV2(label, flags, pid.GetStr());
87  return x_GetLabelV2(label, flags, pid.GetConsortium());
88  default:
89  return false;
90  }
91 }
92 
93 
95  CTempString initials, CTempString suffix)
96 {
97  if (name.empty()) {
98  return false;
99  }
100 
101  if (name.size() <= 6 && (SWNC(name, "et al") || SWNC(name, "et,al"))) {
102  name = "et al.";
103  if (NStr::EndsWith(*label, " and ")) {
104  label->replace(label->size() - 5, NPOS, ", ");
105  }
106  }
107 
108  SIZE_TYPE pos = label->size();
109  *label += name;
110  if (HasText(initials)) {
111  *label += ',';
112  *label += initials;
113  }
114  if (HasText(suffix)) {
115  *label += ' ';
116  *label += suffix;
117  }
118 
119  if ((flags & fLabel_FlatEMBL) != 0) {
120  NStr::ReplaceInPlace(*label, ",", " ", pos);
121  }
122 
123  return true;
124 }
125 
126 static string s_NormalizeInitials(const string& raw_initials)
127 {
128  //
129  // Note:
130  // Periods _only_ after CAPs to avoid decorating hyphens (which _are_
131  // legal in the "initials" part.
132  //
133  string normal_initials;
134  for (char ch : raw_initials) {
135  normal_initials += ch;
136  if (isupper(ch)) {
137  normal_initials += '.';
138  }
139  }
140  return normal_initials;
141 }
142 
143 static string s_NormalizeSuffix(const string& raw_suffix)
144 {
145  //
146  // Note: (2008-02-13) Suffixes I..VI no longer have trailing periods.
147  //
148  static const map<string, string> smap = {
149  {"1d", "I" },
150  {"1st", "I" },
151  {"2d", "II" },
152  {"2nd", "II" },
153  {"3d", "III"},
154  {"3rd", "III"},
155  {"4th", "IV" },
156  {"5th", "V" },
157  {"6th", "VI" },
158  {"Jr", "Jr."},
159  {"Sr", "Sr."} };
160 
161  auto search = smap.find(raw_suffix);
162  if (search != smap.end()) {
163  return search->second;
164  }
165  return raw_suffix;
166 }
167 
168 void s_SplitMLAuthorName(string name, string& last, string& initials, string& suffix, bool normalize_suffix)
169 {
171  if (name.empty()) {
172  return;
173  }
174 
175  vector<string> parts;
176  NStr::Split(name, " ", parts, NStr::fSplit_Tokenize);
177  if (parts.empty()) {
178  return;
179  }
180  if (parts.size() == 1) {
181  //
182  // Designate the only part we have as the last name.
183  //
184  last = parts[0];
185  return;
186  }
187 
188  const string& last_part = parts[parts.size() - 1];
189 
190  if (parts.size() == 2) {
191  //
192  // Designate the first part as the last name and the second part as the
193  // initials.
194  //
195  last = parts[0];
196  initials = s_NormalizeInitials(last_part);
197  return;
198  }
199 
200  //
201  // At least three parts.
202  //
203  // If the second to last part is all CAPs then those are the initials. The
204  // last part is the suffix, and everything up to the initials is the last
205  // name.
206  //
207  const string& second_to_last_part = parts[parts.size() - 2];
208 
209  if (NStr::IsUpper(second_to_last_part)) {
210  last = NStr::Join(vector<string>(parts.begin(), parts.end() - 2), " ");
211  initials = s_NormalizeInitials(second_to_last_part);
212 
213  suffix = normalize_suffix ? s_NormalizeSuffix(last_part) : last_part;
214  return;
215  }
216 
217  //
218  // Fall through:
219  // Guess that the last part is the initials and everything leading up to it
220  // is a (rather unusual) last name.
221  //
222  last = NStr::Join(vector<string>(parts.begin(), parts.end() - 1), " ");
223  initials = s_NormalizeInitials(last_part);
224  return;
225 
226  // ------------------------------------------------------------------------
227  // CASE NOT HANDLED:
228  //
229  // (1) Initials with a blank in them. UNFIXABLE!
230  // (2) Initials with non CAPs in them. Probably fixable through a
231  // white list of allowable exceptions. Tedious, better let the indexers
232  // fix it.
233  // ------------------------------------------------------------------------
234 }
235 
236 CRef<CPerson_id> CAuthor::x_ConvertMlToStandard(const string& name, bool normalize_suffix)
237 {
238  string last, initials, suffix;
239  s_SplitMLAuthorName(name, last, initials, suffix, normalize_suffix);
240 
241  CRef<CPerson_id> person_id;
242  if (!last.empty()) {
243 
244  person_id.Reset(new CPerson_id());
245  person_id->SetName().SetLast(last);
246  if (!initials.empty()) {
247  person_id->SetName().SetInitials(initials);
248  }
249  if (!suffix.empty()) {
250  person_id->SetName().SetSuffix(suffix);
251  }
252  }
253  return person_id;
254 }
255 
256 
257 CRef<CAuthor> CAuthor::ConvertMlToStandard(const string& ml_name, bool normalize_suffix)
258 {
259  CRef<CAuthor> new_author;
260  if (!NStr::IsBlank(ml_name)) {
261  new_author.Reset(new CAuthor());
262  CRef<CPerson_id> std_name = x_ConvertMlToStandard(ml_name, normalize_suffix);
263  new_author->SetName(*std_name);
264  }
265  return new_author;
266 }
267 
268 
269 CRef<CAuthor> CAuthor::ConvertMlToStandard(const CAuthor& author, bool normalize_suffix)
270 {
271  CRef<CAuthor> new_author(new CAuthor());
272  new_author->Assign(author);
273 
274  if (new_author->IsSetName() &&
275  new_author->GetName().IsMl()) {
276  const string ml_name = new_author->GetName().GetMl();
277  CRef<CPerson_id> std_name = x_ConvertMlToStandard(ml_name, normalize_suffix);
278  new_author->ResetName();
279  new_author->SetName(*std_name);
280  }
281  return new_author;
282 }
283 
284 
285 
286 END_objects_SCOPE // namespace ncbi::objects::
287 
289 
290 /* Original file checksum: lines: 65, chars: 1880, CRC32: f9047a4f */
CAuthor –.
Definition: Author.hpp:59
static bool x_GetLabelV2(string *label, TLabelFlags flags, CTempString name, CTempString initials=kEmptyStr, CTempString suffix=kEmptyStr)
Definition: Author.cpp:94
bool GetLabelV1(string *label, TLabelFlags flags) const override
Definition: Author.cpp:56
static CRef< CAuthor > ConvertMlToStandard(const CAuthor &author, bool normalize_suffix=false)
Definition: Author.cpp:269
~CAuthor()
Definition: Author.cpp:51
static CRef< CPerson_id > x_ConvertMlToStandard(const string &name, bool normalize_suffix=false)
Definition: Author.cpp:236
bool GetLabelV2(string *label, TLabelFlags flags) const override
Definition: Author.cpp:64
CAuthor()
Definition: Author.hpp:63
@Name_std.hpp User-defined methods of the data storage class.
Definition: Name_std.hpp:56
void GetLabel(string *label, ETypeLabel type=eGenbank) const
Definition: Person_id.cpp:57
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
Definition: tempstr.hpp:65
int TLabelFlags
binary OR of ELabelFlags
@ fLabel_FlatEMBL
For EMBL or EMBLPept [V2].
static bool HasText(const string &s)
static bool SWNC(const string &str, const string &pfx)
const_iterator end() const
Definition: map.hpp:152
const_iterator find(const key_type &key) const
Definition: map.hpp:153
static uch flags
static DLIST_TYPE *DLIST_NAME() last(DLIST_LIST_TYPE *list)
Definition: dlist.tmpl.h:51
virtual void Assign(const CSerialObject &source, ESerialRecursionMode how=eRecursive)
Set object to copy of another one.
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
NCBI_NS_STD::string::size_type SIZE_TYPE
Definition: ncbistr.hpp:132
#define kEmptyStr
Definition: ncbistr.hpp:123
static list< string > & Split(const CTempString str, const CTempString delim, list< string > &arr, TSplitFlags flags=0, vector< SIZE_TYPE > *token_pos=NULL)
Split a string using specified delimiters.
Definition: ncbistr.cpp:3457
static bool EndsWith(const CTempString str, const CTempString end, ECase use_case=eCase)
Check if a string ends with a specified suffix value.
Definition: ncbistr.hpp:5430
static bool IsBlank(const CTempString str, SIZE_TYPE pos=0)
Check if a string is blank (has no text).
Definition: ncbistr.cpp:106
#define NPOS
Definition: ncbistr.hpp:133
static void TruncateSpacesInPlace(string &str, ETrunc where=eTrunc_Both)
Truncate spaces in a string (in-place)
Definition: ncbistr.cpp:3197
static string Join(const TContainer &arr, const CTempString &delim)
Join strings using the specified delimiter.
Definition: ncbistr.hpp:2697
bool empty(void) const
Return true if the represented string is empty (i.e., the length is zero)
Definition: tempstr.hpp:334
static bool IsUpper(const CTempString str)
Checks if all letters in the given string have a upper case.
Definition: ncbistr.cpp:445
static string & ReplaceInPlace(string &src, const string &search, const string &replace, SIZE_TYPE start_pos=0, SIZE_TYPE max_replace=0, SIZE_TYPE *num_replace=0)
Replace occurrences of a substring within a string.
Definition: ncbistr.cpp:3401
size_type size(void) const
Return the length of the represented array.
Definition: tempstr.hpp:327
@ fSplit_Tokenize
All delimiters are merged and trimmed, to get non-empty tokens only.
Definition: ncbistr.hpp:2508
static const char label[]
void SetName(TName &value)
Assign a value to Name data member.
Definition: Author_.cpp:81
void ResetName(void)
Reset Name data member.
Definition: Author_.cpp:72
const TName & GetName(void) const
Get the Name member data.
Definition: Author_.hpp:352
bool IsSetName(void) const
Author, Primary or Secondary Check if a value has been assigned to Name data member.
Definition: Author_.hpp:340
E_Choice Which(void) const
Which variant is currently selected.
Definition: Person_id_.hpp:324
const TStr & GetStr(void) const
Get the variant data.
Definition: Person_id_.hpp:391
bool IsMl(void) const
Check if variant Ml is selected.
Definition: Person_id_.hpp:365
bool CanGetInitials(void) const
Check if it is safe to call GetInitials method.
Definition: Name_std_.hpp:604
const TInitials & GetInitials(void) const
Get the Initials member data.
Definition: Name_std_.hpp:610
const TFull & GetFull(void) const
Get the Full member data.
Definition: Name_std_.hpp:563
bool IsSetFull(void) const
full name eg.
Definition: Name_std_.hpp:551
const TMl & GetMl(void) const
Get the variant data.
Definition: Person_id_.hpp:371
const TConsortium & GetConsortium(void) const
Get the variant data.
Definition: Person_id_.hpp:411
const TSuffix & GetSuffix(void) const
Get the Suffix member data.
Definition: Name_std_.hpp:657
TName & SetName(void)
Select the variant.
Definition: Person_id_.cpp:143
bool CanGetSuffix(void) const
Check if it is safe to call GetSuffix method.
Definition: Name_std_.hpp:651
const TLast & GetLast(void) const
Get the Last member data.
Definition: Name_std_.hpp:422
const TName & GetName(void) const
Get the variant data.
Definition: Person_id_.cpp:137
@ e_Ml
MEDLINE name (semi-structured) eg. "Jones RM".
Definition: Person_id_.hpp:97
@ e_Consortium
consortium name
Definition: Person_id_.hpp:99
@ e_Name
structured name
Definition: Person_id_.hpp:96
@ e_Str
unstructured name
Definition: Person_id_.hpp:98
int isupper(Uchar c)
Definition: ncbictype.hpp:70
void s_SplitMLAuthorName(string name, string &last, string &initials, string &suffix, bool normalize_suffix)
Definition: Author.cpp:168
static string s_NormalizeSuffix(const string &raw_suffix)
Definition: Author.cpp:143
static string s_NormalizeInitials(const string &raw_initials)
Definition: Author.cpp:126
static const char * suffix[]
Definition: pcregrep.c:408
Modified on Thu Mar 28 17:06:14 2024 by modify_doxy.py rev. 669887