1 /*
2   Copyright (c) 2006 - 2021
3   CLST  - Radboud University
4   ILK   - Tilburg University
5 
6   This file is part of libfolia
7 
8   libfolia is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 3 of the License, or
11   (at your option) any later version.
12 
13   libfolia is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17 
18   You should have received a copy of the GNU General Public License
19   along with this program; if not, see <http://www.gnu.org/licenses/>.
20 
21   For questions and suggestions, see:
22       https://github.com/LanguageMachines/ticcutils/issues
23   or send mail to:
24       lamasoftware (at ) science.ru.nl
25 */
26 
27 #include <iostream>
28 #include <string>
29 #include <vector>
30 #include <map>
31 #include <set>
32 #include <list>
33 #include "ticcutils/StringOps.h"
34 #include "libfolia/folia_properties.h"
35 #include "libfolia/folia.h"
36 
37 using namespace std;
38 
39 namespace folia {
40 
toString(const AnnotationType & at)41   string toString( const AnnotationType& at ){
42     /// return the string representation of AnnotationType at
43     auto result = ant_s_map.find(at);
44     if ( result == ant_s_map.end() ){
45       throw logic_error( "Unknown Annotationtype: index= "
46 			 + TiCC::toString(int(at)) );
47     };
48     return result->second;
49   }
50 
stringToAnnotationType(const string & st)51   AnnotationType stringToAnnotationType( const string& st ){
52     // convert a string into an AnnotationType
53     /*!
54      * \param st a string representing an AnnotationType
55      * \return an AnnotationType. Throws when not found.
56      *
57      * Also handles 'old' pre v1.5 names.
58      */
59     string s = st;
60     auto const tr = oldtags.find(st);
61     if ( tr != oldtags.end() ){
62       s = tr->second;
63     }
64     auto result = s_ant_map.find( s );
65     if ( result == s_ant_map.end() ){
66       throw logic_error( "Unknown translation for annotationtype: '"
67 			 + s + "'" );
68     }
69     return result->second;
70   }
71 
stringToAnnotatorType(const string & str)72   AnnotatorType stringToAnnotatorType( const string& str ){
73     // convert a string into an AnnotatorType
74     /*!
75      * \param str a (case insensitive) string representation
76      * \return an AnnotatorType
77      */
78     string at = TiCC::uppercase( str );
79     if ( at == "AUTO" ){
80       return folia::AUTO;
81     }
82     else if ( at == "MANUAL" ){
83       return folia::MANUAL;
84     }
85     else if ( at == "GENERATOR" ){
86       return folia::GENERATOR;
87     }
88     else if ( at == "DATASOURCE" ){
89       return folia::DATASOURCE;
90     }
91     else {
92       return folia::UNDEFINED;
93     }
94   }
95 
toString(const AnnotatorType & at)96   string toString( const AnnotatorType& at ){
97     /// return the string representation of AnnotatorType at
98     switch ( at ){
99     case AUTO:
100       return "auto";
101       break;
102     case MANUAL:
103       return "manual";
104       break;
105     case GENERATOR:
106       return "generator";
107       break;
108     case DATASOURCE:
109       return "datasource";
110       break;
111     default:
112       return "UNDEFINED";
113     }
114   }
115 
toString(const ElementType & et)116   string toString( const ElementType& et ) {
117     /// return the string representation of ElementType et
118     /*!
119      * \param et an ElementType
120      * \return a string representation.
121      */
122     auto result = et_s_map.find(et);
123     if ( result == et_s_map.end() ){
124       throw logic_error( "toString: Unknown Elementtype "
125 			 + TiCC::toString( int(et) ) );
126     }
127     return result->second;
128   }
129 
stringToElementType(const string & intag)130   ElementType stringToElementType( const string& intag ){
131     // convert a string into an ElementType
132     /*!
133      * \param intag a string representing an ElementType
134      * \return an ElementType. Throws when not found.
135      *
136      * Also handles 'old' pre v1.5 names.
137      */
138     string tag = intag;
139     auto const tr = oldtags.find(intag);
140     if ( tr != oldtags.end() ){
141       tag = tr->second;
142     }
143     auto result = s_et_map.find(tag);
144     if ( result == s_et_map.end() ){
145       throw ValueError( "unknown tag <" + tag + ">" );
146     }
147     return result->second;
148   }
149 
toString(const Attrib at)150   string toString( const Attrib at ){
151     /// return the string representation of Attrib at
152     switch ( at ){
153     case ID:
154       return "ID";
155       break;
156     case CLASS:
157       return "CLASS";
158       break;
159     case ANNOTATOR:
160       return "ANNOTATOR";
161       break;
162     case CONFIDENCE:
163       return "CONFIDENCE";
164       break;
165     case N:
166       return "N";
167       break;
168     case DATETIME:
169       return "DATETIME";
170       break;
171     case BEGINTIME:
172       return "BEGINTIME";
173       break;
174     case ENDTIME:
175       return "ENDTIME";
176       break;
177     case SRC:
178       return "SRC";
179       break;
180     case SPEAKER:
181       return "SPEAKER";
182       break;
183     case TEXTCLASS:
184       return "TEXTCLASS";
185       break;
186     case METADATA:
187       return "METADATA";
188       break;
189     default:
190       return "";
191     }
192   }
193 
operator <<(ostream & os,const Attrib & attribs)194   ostream& operator<<( ostream& os, const Attrib& attribs ){
195     /// Output operator for an Attrib element
196     /*!
197      * \param os the output stream
198      * \param attribs an Attrib.
199      * An Attrib contains a logical OR of serveral values.
200      * This function outputs a string value for every value set.
201      * separated by '|'
202      */
203     enum Attrib at = NO_ATT;
204     while ( at != ALL ){
205       if ( attribs & at ){
206 	os << toString( at ) << "|";
207       }
208       ++at;
209     }
210     return os;
211   }
212 
layertypeof(ElementType et)213   ElementType layertypeof( ElementType et ) {
214     /// return the Layer ElemenType of the given ElementType
215     /*!
216      * \param et the ElementType to check
217      * \return The Layer ElementType er belongs to. Or BASE if it has no Layer.
218      */
219     switch( et ) {
220     case Entity_t:
221     case EntitiesLayer_t:
222       return EntitiesLayer_t;
223     case Chunk_t:
224     case ChunkingLayer_t:
225       return ChunkingLayer_t;
226     case SyntacticUnit_t:
227     case SyntaxLayer_t:
228       return SyntaxLayer_t;
229     case TimeSegment_t:
230     case TimingLayer_t:
231       return TimingLayer_t;
232     case Morpheme_t:
233     case MorphologyLayer_t:
234       return MorphologyLayer_t;
235     case Phoneme_t:
236     case PhonologyLayer_t:
237       return PhonologyLayer_t;
238     case CoreferenceChain_t:
239     case CoreferenceLayer_t:
240       return CoreferenceLayer_t;
241     case Observation_t:
242     case ObservationLayer_t:
243       return ObservationLayer_t;
244     // case Predicate_t:
245     // case PredicateLayer_t:
246     //   return PredicateLayer_t;
247     case SentimentLayer_t:
248     case Sentiment_t:
249       return SentimentLayer_t;
250     case StatementLayer_t:
251     case Statement_t:
252       return SentimentLayer_t;
253     case SemanticRolesLayer_t:
254     case SemanticRole_t:
255       return SemanticRolesLayer_t;
256     case DependenciesLayer_t:
257     case Dependency_t:
258       return DependenciesLayer_t;
259     default:
260       return BASE;
261     }
262   }
263 
264 } // namespace folia
265