1 2 /* 3 Copyright (c) 2006 - 2021 4 CLST - Radboud University 5 ILK - Tilburg University 6 7 This file is part of libfolia 8 9 libfolia is free software; you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation; either version 3 of the License, or 12 (at your option) any later version. 13 14 libfolia is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with this program; if not, see <http://www.gnu.org/licenses/>. 21 22 For questions and suggestions, see: 23 https://github.com/LanguageMachines/ticcutils/issues 24 or send mail to: 25 lamasoftware (at ) science.ru.nl 26 */ 27 28 #include <string> 29 #include <map> 30 #include <iostream> 31 #include "libfolia/folia_textpolicy.h" 32 33 using namespace std; 34 35 namespace folia { 36 37 /// create a TextPolicy object 38 /*! 39 \param cls a string representing a text-class 40 \param flags the TEXT_FLAGS settings to use 41 */ TextPolicy(const string & cls,const TEXT_FLAGS flags)42 TextPolicy::TextPolicy( const string& cls, const TEXT_FLAGS flags ): 43 _class(cls), 44 _text_flags( flags ), 45 _correction_handling( CORRECTION_HANDLING::CURRENT ), 46 _debug(false) 47 { 48 } 49 50 /// create a TextPolicy object for the 'current' text-class 51 /*! 52 \param flags the TEXT_FLAGS settings to use 53 */ TextPolicy(const TEXT_FLAGS flags)54 TextPolicy::TextPolicy( const TEXT_FLAGS flags ): 55 TextPolicy( "current", flags ) { 56 } 57 toString(CORRECTION_HANDLING ch)58 string toString( CORRECTION_HANDLING ch ){ 59 /// give a text representation for a CORRECION_HANDLING 60 /*! 61 \param ch the CORRECTION_HANDLING 62 \return a string representing the CORRECTION_HANDLING 63 */ 64 switch( ch ){ 65 case CORRECTION_HANDLING::CURRENT: 66 return "current"; 67 break; 68 case CORRECTION_HANDLING::ORIGINAL: 69 return "original"; 70 break; 71 case CORRECTION_HANDLING::EITHER: 72 return "either"; 73 break; 74 default: 75 return "CORRECTION_HANDLING.toString() NOT implemented case"; 76 } 77 } 78 operator <<(ostream & os,const TextPolicy & tp)79 ostream& operator<<( ostream& os, const TextPolicy& tp ){ 80 /// Output a TextPolicy (for debugging purposes) 81 /*! 82 \param os the output stream 83 \param tp the TextPolicy 84 \return the outputstream after the \em tp is output. 85 */ 86 bool retain = tp.is_set( TEXT_FLAGS::RETAIN ); 87 bool strict = tp.is_set( TEXT_FLAGS::STRICT ); 88 bool hide = tp.is_set( TEXT_FLAGS::HIDDEN ); 89 bool trim = !tp.is_set( TEXT_FLAGS::NO_TRIM_SPACES ); 90 os << "class=" << tp.get_class() << "\t" 91 << (strict?"strict":"not strict") << ", " 92 << (retain?"retain":"untokenized") << ", " 93 << (hide?"show_hidden":"hide hidden") << ", " 94 << (trim?"trimming spaces":"not trimming spaces") << ", " 95 << "correction handling: " << toString( tp.get_correction_handling() ); 96 return os; 97 } 98 is_set(TEXT_FLAGS tf) const99 bool TextPolicy::is_set( TEXT_FLAGS tf ) const { 100 /// check is the flag is set 101 /*! 102 \param tf the TEXT_FLAGS flag to test 103 \return \em true when set, \em false otherwise 104 */ 105 return ( tf & _text_flags ) == tf; 106 } 107 set(TEXT_FLAGS tf)108 void TextPolicy::set( TEXT_FLAGS tf ) { 109 /// set a flag 110 /*! 111 \param tf the TEXT_FLAGS flag to set 112 */ 113 _text_flags |= tf; 114 } 115 clear(TEXT_FLAGS tf)116 void TextPolicy::clear( TEXT_FLAGS tf ) { 117 /// clear a flag 118 /*! 119 \param tf the TEXT_FLAGS flag to clear (unset) 120 */ 121 _text_flags &= ~tf; 122 } 123 add_handler(const string & label,const tag_handler & fp)124 void TextPolicy::add_handler( const string& label, 125 const tag_handler& fp ){ 126 /// add a tag_handler to the currect handlers 127 /*! 128 \param label a label to identify the handler 129 \param fp the function to register 130 131 may override an existing handler, if the label is already in use. 132 */ 133 _tag_handlers.insert( make_pair( label, fp ) ); 134 } 135 remove_handler(const string & label)136 const TextPolicy::tag_handler TextPolicy::remove_handler( const string& label ){ 137 /// remove a tag_handler 138 /*! 139 \param label the label to identify the handler 140 \return the function which is removed. Or 0 when the label didn't match 141 */ 142 auto pnt = _tag_handlers.find( label ); 143 if ( pnt != _tag_handlers.end() ){ 144 _tag_handlers.erase( pnt ); 145 return pnt->second; 146 } 147 else { 148 return 0; 149 } 150 } 151 get_handler(const string & label) const152 const TextPolicy::tag_handler TextPolicy::get_handler( const string& label ) const{ 153 /// return a tag_handler fir a give label 154 /*! 155 \param label the label to identify the handler 156 \return the function which is found. Or 0 when the label didn't match 157 */ 158 auto pnt = _tag_handlers.find( label ); 159 if ( pnt != _tag_handlers.end() ){ 160 return pnt->second; 161 } 162 else { 163 return 0; 164 } 165 } 166 167 } // namespace folia 168