source: tags/svn.1.5.4/NTREE/NT_validNameParser.cxx

Last change on this file was 8261, checked in by westram, 14 years ago
  • removed unused(-but-set) variables/params
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.0 KB
Line 
1/*
2 * Definition of all objects belonging to this version of
3 * the valid names text file
4 *
5 * 29. November 2002
6 *
7 * coded by Lothar Richter
8 *
9 * Copyright (C) 2002 Department of Microbiology (Technical University Munich)
10 */
11
12#if defined(DEVEL_LOTHAR)
13#define DUMP
14#endif // DEVEL_LOTHAR
15
16#include "nt_validNameParser.hxx"
17
18#include <arb_assert.h>
19
20#include <cstdlib>
21#include <cstdlib>
22#include <iostream>
23#include <fstream>
24
25using namespace std;
26
27#define nt_assert(bed) arb_assert(bed)
28
29namespace validNames {
30
31
32    TokLPtr tokenize(const string& description, TokLPtr tokenLP)
33    {
34        size_t tokenEnd = 0;
35        size_t tokenBegin = 0;
36
37        while (tokenEnd != description.size()) { // CC : warning: comparison between signed and unsigned (tokenEnd sollte nicht 'int' sondern 'unsigned' sein)
38            tokenEnd = description.find_first_of(' ', tokenBegin);
39            if (tokenEnd == string::npos) tokenEnd = description.size();
40            int tokLength = tokenEnd - tokenBegin;
41            if (tokLength != 0) {
42                tokenLP->push_back(description.substr(tokenBegin, tokenEnd - tokenBegin));
43            }
44            tokenBegin = tokenEnd + 1;
45
46        }
47        return tokenLP;
48    }
49
50
51
52
53
54    Desco determineType(const string& descriptionString)
55    { // begin determineType
56
57        DESCT actType = NOTYPE;
58        TokLPtr tokenLP =  new TokL;
59        tokenLP = tokenize(descriptionString, tokenLP);
60        // remove all tokens in parentheses
61        {
62            TokL::iterator it = tokenLP->begin();
63            while (it != tokenLP->end()) {
64                if (((*it).at(0) == '(') && *it != string("(corrig.)")) it = tokenLP->erase(it);
65                else ++it;
66            }
67        }
68
69        // check first word for upper case letters
70        string descNames[6]; // first the valid genus, species, subsp. then the other names
71        // stores occurrence of subsp. which is needed to retrieve the right tokens later on and status flags
72        int sspPos[2] = { 0, 0 }; // token subsp. occurs maximum twice
73        int ssp = 0;
74        bool isValid = true;
75        bool isRenamed = false;
76        bool isHetero = false;
77        bool isHomo = false;
78        bool isGenus = false;
79        // bool isSee = false;
80        bool isCorr = false;
81
82
83
84        for (TokL::iterator it = tokenLP->begin(); it != tokenLP->end(); it ++, ssp++)
85        {
86            if (isUpperCase(*it)) {
87                isGenus = true;
88#if defined(DUMP)
89                std::cout << "genus detected" << std::endl;
90#endif // DUMP
91            }
92
93
94            else { // begin operators
95                if (*it == string("->")) {
96                    nt_assert(!isHetero);
97                    nt_assert(!isHomo);
98                    nt_assert(isValid); // only one operator per line allowed
99                    isRenamed = true;
100                    isValid   = false;
101#if defined(DUMP)
102                    std::cout << "renaming detected" << std::endl;
103#endif // DUMP
104                }
105                else {
106                    if (*it == string("=>")) {
107                        nt_assert(!isRenamed);
108                        nt_assert(!isHomo);
109                        nt_assert(isValid);
110                        isHetero = true;
111                        isValid = false;
112#if defined(DUMP)
113                        std::cout << "heteronym detected" << std::endl;
114#endif // DUMP
115                    }
116                    else {
117                        if (*it == string("=")) {
118                            nt_assert(!isRenamed);
119                            nt_assert(!isHetero);
120                            nt_assert(isValid);
121                            isHomo = true;
122                            isValid = false;
123#if defined(DUMP)
124                            std::cout << "homonym detected" << std::endl;
125#endif // DUMP
126                        }
127                        else {
128                            if (*it == string("(corrig.)")) {
129                                isCorr = true;
130#if defined(DUMP)
131                                std::cout << "correction" << std::endl;
132#endif // DUMP
133                            }
134                            else {
135                                if (*it == string("see:")) {
136                                    // isSee = true;
137                                    isValid = false;
138#if defined(DUMP)
139                                    std::cout << "reference" << std::endl;
140#endif // DUMP
141                                }
142                                else {
143                                    if (*it == string("subsp.")) {
144#if defined(DUMP)
145                                        std::cout << "subspecies detected at position: >>>" << ssp << "<<<" << std::endl;
146#endif // DUMP
147                                        ssp == 2 ? sspPos[0] = ssp : sspPos[1] = ssp;
148                                        // max. one subsp. on each operator side
149#if defined(DUMP)
150                                        std::cout << "position of subsp.: " << sspPos[0] << "\tand: " << sspPos[1] << std::endl;
151#endif // DUMP
152                                    }
153                                }
154                            }
155                        }
156                    }
157                }
158            }
159        }
160
161
162
163        if (isGenus) {
164#if defined(DUMP)
165            std::cout << " GENUS description found " << std::endl;
166#endif // DUMP
167            if (isValid) {
168                descNames[0] = (*tokenLP)[0];
169                actType      = VALGEN;
170#if defined(DUMP)
171                std::cout << "VALIDGEN type set to: " << actType << std::endl;
172#endif// DUMP
173            }
174            else {
175                if (isHetero) {
176                    descNames[0] = (*tokenLP)[2];
177                    descNames[3] = (*tokenLP)[0];
178                    actType = HETGEN;
179#if defined(DUMP)
180                    std::cout << "HETERONYMGEN type set to: " << actType << std::endl;
181#endif // DUMP
182                }
183                else {
184                    if (isHomo) {
185                        descNames[0] = (*tokenLP)[2];
186                        descNames[3] = (*tokenLP)[0];
187                        actType = HOMGEN;
188#if defined(DUMP)
189                        std::cout << "HOMONYMGEN type set to: " << actType << std::endl;
190#endif // DUMP
191
192                    }
193                    else {
194
195                        if (isRenamed) {
196                            descNames[0] = (*tokenLP)[2];
197                            descNames[3] = (*tokenLP)[0];
198                            actType = RENGEN;
199#if defined(DUMP)
200                            std::cout << "RENAMEDGEN type set to: " << actType << std::endl;
201#endif // DUMP
202                        }
203                        else {
204#if defined(DUMP)
205                            std::cout << "no meaningful combination of conditions reached" << std::endl
206                                      << "for line: " << descriptionString << std::endl;
207                            std::cout << "description type is set to NOTYPE: " << NOTYPE << std::endl;
208#endif // DUMP
209                            isValid = false;
210#if defined(DUMP)
211                            std::cout << "isValid set to false "  << std::endl;
212#endif // DUMP
213                            actType = NOTYPE;
214                        }
215                    }
216                }
217            }
218        }
219        else {
220
221            //       just fancy experimental , maybe not 100% correct but looks good
222            if (!(((sspPos[0] == 0) || (sspPos[0] == 2)) && (((sspPos[1] > 4)&&(sspPos[1]< 9))||(sspPos[1]==0))))
223            {
224#if defined(DUMP)
225                std::cout << "subsp. at strange position found in line:" << std::endl << descriptionString << endl;
226                std::cout << "description type is set to NOTYPE: " << NOTYPE << std::endl;
227#endif // DUMP
228                isValid = false;
229#if defined(DUMP)
230                std::cout << "isValid set to false "  << std::endl;
231#endif // DUMP
232                actType = NOTYPE;
233            }
234
235            if (isValid) {
236                descNames[0] = (*tokenLP)[0];
237                descNames[1] = (*tokenLP)[1];
238                if (sspPos[0] != 0) { descNames[2] = (*tokenLP)[sspPos[0]+1]; } // only if subsp. exists
239                actType = VALSPEC;
240            }
241            else { // begin else isHetero
242                if (isHetero) {
243                    descNames[0] = (*tokenLP)[3 + sspPos[0]];
244                    descNames[1] = (*tokenLP)[4 + sspPos[0]];
245                    if (sspPos[1]!=0) { descNames[2]=(*tokenLP)[6 + sspPos[0]]; } // only if subsp. exists
246
247                    descNames[3] = (*tokenLP)[0];
248                    descNames[4] = (*tokenLP)[1];
249                    if (sspPos[0]!=0) { descNames[5]=(*tokenLP)[sspPos[0]+1]; } // only if subsp. exists
250
251                    actType = HETSPEC;
252                }
253                else {
254                    if (isHomo) {
255                        descNames[0] = (*tokenLP)[3 + sspPos[0]];
256                        descNames[1] = (*tokenLP)[4 + sspPos[0]];
257                        if (sspPos[1]!=0) { descNames[2]=(*tokenLP)[6 + sspPos[0]]; } // only if subsp. exists
258
259                        descNames[3] = (*tokenLP)[0];
260                        descNames[4] = (*tokenLP)[1];
261                        if (sspPos[0]!=0) { descNames[5]=(*tokenLP)[sspPos[0]+1]; } // only if subsp. exists
262
263                        actType = HOMSPEC;
264
265                    }
266                    else { // else branch isHomo
267                        if (isRenamed) {
268                            descNames[0] = (*tokenLP)[3 + sspPos[0]];
269                            descNames[1] = (*tokenLP)[4 + sspPos[0]];
270                            if (sspPos[1]!=0) { descNames[2]=(*tokenLP)[6 + sspPos[0]]; } // only if subsp. exists
271
272                            descNames[3] = (*tokenLP)[0];
273                            descNames[4] = (*tokenLP)[1];
274                            if (sspPos[0]!=0) { descNames[5]=(*tokenLP)[sspPos[0]+1]; } // only if subsp. exists
275
276                            actType = RENSPEC;
277
278                        }
279                        else { // species remaining cases
280#if defined(DUMP)
281                            std::cout << "not a valid description line detected" << std::endl;
282                            std::cout << "isValid: " << isValid << std::endl;
283                            std::cout << "isRenamed: " << isRenamed << std::endl;
284                            std::cout << "isHetero: " << isHetero << std::endl;
285                            std::cout << "isHomo: " << isHomo << std::endl;
286                            std::cout << "isGenus: " << isGenus << std::endl;
287                            std::cout << "isSee: " << isSee << std::endl;
288                            std::cout << "isCorr: " << isCorr << std::endl;
289                            std::cout << "sspPos: " << sspPos[0] << " and " << sspPos[1] << std::endl;
290                            std::cout << descriptionString << std::endl;
291#endif // DUMP
292                            actType = NOTYPE;
293                        }
294
295                    }
296                }
297            }
298        }
299
300
301#if defined(DUMP)
302        std::cout << descriptionString << std::endl;
303        std::cout << "classified as " << actType << std::endl;
304#endif // DUMP
305
306        Desco actDesc(actType, isCorr, descNames[0], descNames[1], descNames[2], descNames[3], descNames[4], descNames[5]);
307        delete tokenLP;
308        return actDesc;
309    }
310
311
312    string Desco::getFirstName() {
313        string tmp = firstgen;
314        if (!firstspec.empty()) {
315            tmp = tmp + " " + firstspec;
316            if (!firstsub.empty()) {
317                tmp = tmp + " " + "subsp." + " " + firstsub;
318            }
319        }
320
321
322        return tmp;
323    }
324
325    string Desco::getSecondName() {
326        string tmp = secondgen;
327        if (!secondspec.empty()) {
328            tmp = tmp + " " + firstspec;
329            if (!secondsub.empty()) {
330                tmp = tmp + " " + "subsp." + " " + secondsub;
331            }
332        }
333        return tmp;
334    }
335
336
337    bool isUpperCase(const string& input)
338    {
339        for (size_t i=0; i<input.length(); ++i)
340        {
341            if (input[i]<'A' || input[i]>'Z') { return false; }
342        }
343        return true;
344    }
345}
Note: See TracBrowser for help on using the repository browser.