| 1 | /*************************************************************************/ |
| 2 | /* */ |
| 3 | /* Language Technologies Institute */ |
| 4 | /* Carnegie Mellon University */ |
| 5 | /* Copyright (c) 1999 */ |
| 6 | /* All Rights Reserved. */ |
| 7 | /* */ |
| 8 | /* Permission is hereby granted, free of charge, to use and distribute */ |
| 9 | /* this software and its documentation without restriction, including */ |
| 10 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 11 | /* distribute, sublicense, and/or sell copies of this work, and to */ |
| 12 | /* permit persons to whom this work is furnished to do so, subject to */ |
| 13 | /* the following conditions: */ |
| 14 | /* 1. The code must retain the above copyright notice, this list of */ |
| 15 | /* conditions and the following disclaimer. */ |
| 16 | /* 2. Any modifications must be clearly marked as such. */ |
| 17 | /* 3. Original authors' names are not deleted. */ |
| 18 | /* 4. The authors' names are not used to endorse or promote products */ |
| 19 | /* derived from this software without specific prior written */ |
| 20 | /* permission. */ |
| 21 | /* */ |
| 22 | /* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */ |
| 23 | /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ |
| 24 | /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ |
| 25 | /* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */ |
| 26 | /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ |
| 27 | /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ |
| 28 | /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ |
| 29 | /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ |
| 30 | /* THIS SOFTWARE. */ |
| 31 | /* */ |
| 32 | /*************************************************************************/ |
| 33 | /* Author: Alan W Black ([email protected]) */ |
| 34 | /* Date: January 2000 */ |
| 35 | /*************************************************************************/ |
| 36 | /* */ |
| 37 | /* Regexes, this is just a front end to Henry Spencer's regex code */ |
| 38 | /* Includes a mapping of fsf format regex's to hs format (escaping) */ |
| 39 | /* */ |
| 40 | /*************************************************************************/ |
| 41 | |
| 42 | /* ----------------------------------------------------------------- */ |
| 43 | /* The English TTS System "Flite+hts_engine" */ |
| 44 | /* developed by HTS Working Group */ |
| 45 | /* http://hts-engine.sourceforge.net/ */ |
| 46 | /* ----------------------------------------------------------------- */ |
| 47 | /* */ |
| 48 | /* Copyright (c) 2005-2013 Nagoya Institute of Technology */ |
| 49 | /* Department of Computer Science */ |
| 50 | /* */ |
| 51 | /* 2005-2008 Tokyo Institute of Technology */ |
| 52 | /* Interdisciplinary Graduate School of */ |
| 53 | /* Science and Engineering */ |
| 54 | /* */ |
| 55 | /* All rights reserved. */ |
| 56 | /* */ |
| 57 | /* Redistribution and use in source and binary forms, with or */ |
| 58 | /* without modification, are permitted provided that the following */ |
| 59 | /* conditions are met: */ |
| 60 | /* */ |
| 61 | /* - Redistributions of source code must retain the above copyright */ |
| 62 | /* notice, this list of conditions and the following disclaimer. */ |
| 63 | /* - Redistributions in binary form must reproduce the above */ |
| 64 | /* copyright notice, this list of conditions and the following */ |
| 65 | /* disclaimer in the documentation and/or other materials provided */ |
| 66 | /* with the distribution. */ |
| 67 | /* - Neither the name of the HTS working group nor the names of its */ |
| 68 | /* contributors may be used to endorse or promote products derived */ |
| 69 | /* from this software without specific prior written permission. */ |
| 70 | /* */ |
| 71 | /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND */ |
| 72 | /* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, */ |
| 73 | /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */ |
| 74 | /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */ |
| 75 | /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS */ |
| 76 | /* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, */ |
| 77 | /* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */ |
| 78 | /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, */ |
| 79 | /* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */ |
| 80 | /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, */ |
| 81 | /* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY */ |
| 82 | /* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */ |
| 83 | /* POSSIBILITY OF SUCH DAMAGE. */ |
| 84 | /* ----------------------------------------------------------------- */ |
| 85 | |
| 86 | #include "cst_alloc.h" |
| 87 | #include "cst_regex.h" |
| 88 | |
| 89 | #include "cst_regex_defs.h" |
| 90 | |
| 91 | /* For access by const models */ |
| 92 | const cst_regex *const cst_regex_table[] = { |
| 93 | &cst_rx_dotted_abbrev_rx |
| 94 | }; |
| 95 | |
| 96 | #ifndef FLITE_PLUS_HTS_ENGINE |
| 97 | static char *regularize(const char *unregex,int match); |
| 98 | |
| 99 | void cst_regex_init() |
| 100 | { |
| 101 | /* no need to initialize regexes anymore, they are pre-compiled */ |
| 102 | |
| 103 | return; |
| 104 | } |
| 105 | #endif /* !FLITE_PLUS_HTS_ENGINE */ |
| 106 | |
| 107 | int cst_regex_match(const cst_regex *r, const char *str) |
| 108 | { |
| 109 | cst_regstate *s; |
| 110 | |
| 111 | if (r == NULL) return 0; |
| 112 | s = hs_regexec(r, str); |
| 113 | if (s) { |
| 114 | cst_free(s); |
| 115 | return 1; |
| 116 | } else |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | #ifndef FLITE_PLUS_HTS_ENGINE |
| 121 | cst_regstate *cst_regex_match_return(const cst_regex *r, const char *str) |
| 122 | { |
| 123 | if (r == NULL) |
| 124 | return 0; |
| 125 | |
| 126 | return hs_regexec(r, str); |
| 127 | } |
| 128 | |
| 129 | cst_regex *new_cst_regex(const char *str) |
| 130 | { |
| 131 | cst_regex *r; |
| 132 | char *reg_str = regularize(str,1); |
| 133 | |
| 134 | r = hs_regcomp(reg_str); |
| 135 | cst_free(reg_str); |
| 136 | |
| 137 | return r; |
| 138 | } |
| 139 | |
| 140 | void delete_cst_regex(cst_regex *r) |
| 141 | { |
| 142 | if (r) |
| 143 | hs_regdelete(r); |
| 144 | |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | /* These define the different escape conventions for the FSF's */ |
| 149 | /* regexp code and Henry Spencer's */ |
| 150 | |
| 151 | static const char *fsf_magic="^$*+?[].\\"; |
| 152 | static const char *fsf_magic_backslashed="()|<>"; |
| 153 | static const char *spencer_magic="^$*+?[].()|\\\n"; |
| 154 | static const char *spencer_magic_backslashed="<>"; |
| 155 | |
| 156 | /* Adaptation of rjc's mapping of fsf format to henry spencer's format */ |
| 157 | /* of escape sequences, as taken from EST_Regex.cc in EST */ |
| 158 | static char *regularize(const char *unregex,int match) |
| 159 | { |
| 160 | char *reg = cst_alloc(char, cst_strlen(unregex)*2+3); |
| 161 | char *r=reg; |
| 162 | const char *e; |
| 163 | int magic=0,last_was_bs=0; |
| 164 | const char * in_brackets=NULL; |
| 165 | const char *ex = (unregex?unregex:""); |
| 166 | |
| 167 | if (match && *ex != '^') |
| 168 | *(r++) = '^'; |
| 169 | |
| 170 | for(e=ex; *e ; e++) |
| 171 | { |
| 172 | if (*e == '\\' && !last_was_bs) |
| 173 | { |
| 174 | last_was_bs=1; |
| 175 | continue; |
| 176 | } |
| 177 | |
| 178 | magic=strchr((last_was_bs?fsf_magic_backslashed:fsf_magic), *e)!=NULL; |
| 179 | |
| 180 | if (in_brackets) |
| 181 | { |
| 182 | *(r++) = *e; |
| 183 | if (*e == ']' && (e-in_brackets)>1) |
| 184 | in_brackets=0; |
| 185 | } |
| 186 | else if (magic) |
| 187 | { |
| 188 | if (strchr(spencer_magic_backslashed, *e)) |
| 189 | *(r++) = '\\'; |
| 190 | |
| 191 | *(r++) = *e; |
| 192 | if (*e == '[') |
| 193 | in_brackets=e; |
| 194 | } |
| 195 | else |
| 196 | { |
| 197 | if (strchr(spencer_magic, *e)) |
| 198 | *(r++) = '\\'; |
| 199 | |
| 200 | *(r++) = *e; |
| 201 | } |
| 202 | last_was_bs=0; |
| 203 | } |
| 204 | |
| 205 | if (match && (e==ex || *(e-1) != '$')) |
| 206 | { |
| 207 | if (last_was_bs) |
| 208 | *(r++) = '\\'; |
| 209 | *(r++) = '$'; |
| 210 | } |
| 211 | |
| 212 | *r='\0'; |
| 213 | |
| 214 | return reg; |
| 215 | } |
| 216 | #endif /* !FLITE_PLUS_HTS_ENGINE */ |