1 /*---------------------------------------------------------------------------* 2 * voc_basi.c * 3 * * 4 * Copyright 2007, 2008 Nuance Communciations, Inc. * 5 * * 6 * Licensed under the Apache License, Version 2.0 (the 'License'); * 7 * you may not use this file except in compliance with the License. * 8 * * 9 * You may obtain a copy of the License at * 10 * http://www.apache.org/licenses/LICENSE-2.0 * 11 * * 12 * Unless required by applicable law or agreed to in writing, software * 13 * distributed under the License is distributed on an 'AS IS' BASIS, * 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 15 * See the License for the specific language governing permissions and * 16 * limitations under the License. * 17 * * 18 *---------------------------------------------------------------------------*/ 19 20 #include <stdlib.h> 21 #include <string.h> 22 #ifndef _RTT 23 #include <stdio.h> 24 #endif 25 26 #ifdef unix 27 #include <unistd.h> 28 #endif 29 #include <assert.h> 30 31 32 #include "simapi.h" 33 #include "portable.h" 34 35 static const char voc_basi[] = "$Id: voc_basi.c,v 1.11.6.14 2008/01/21 20:30:05 dahan Exp $"; 36 37 #define ADD_SUNDRY_LABELS 0 38 #define ALLOW_UNDERSCORES 1 39 #define MAX_WORD_LEN 128 40 41 CA_Vocab *CA_AllocateVocabulary(void) 42 { 43 CA_Vocab *hVocab = NULL; 44 45 TRY_CA_EXCEPT 46 47 hVocab = (CA_Vocab *) CALLOC_CLR(1, sizeof(CA_Vocab), "ca.hVocab"); 48 hVocab->is_loaded = False; 49 hVocab->ca_rtti = CA_VOCABULARY_SIGNATURE; 50 return (hVocab); 51 52 BEG_CATCH_CA_EXCEPT 53 END_CATCH_CA_EXCEPT(hVocab) 54 } 55 56 57 void CA_FreeVocabulary(CA_Vocab *hVocab) 58 { 59 TRY_CA_EXCEPT 60 ASSERT(hVocab); 61 FREE((char *) hVocab); 62 return; 63 64 BEG_CATCH_CA_EXCEPT 65 END_CATCH_CA_EXCEPT(hVocab) 66 } 67 68 void CA_LoadDictionary(CA_Vocab *hVocab, const LCHAR *vocname, char *phtname, ESR_Locale* locale) 69 { 70 TRY_CA_EXCEPT 71 #ifndef _RTT 72 73 ASSERT(hVocab); 74 // if (phtname != NULL && strlen(phtname) > 0) 75 // hVocab->voc.pht_table= read_phoneme_table (phtname); 76 77 if (0 <= read_word_transcription(vocname, &hVocab->voc, locale)) { 78 hVocab->is_loaded = True; 79 } else { 80 hVocab->is_loaded = False; 81 } 82 return; 83 #else 84 log_report("RTT not in module\n"); 85 SERVICE_ERROR(FEATURE_NOT_SUPPORTED); 86 return; 87 #endif 88 89 BEG_CATCH_CA_EXCEPT 90 END_CATCH_CA_EXCEPT(hVocab) 91 } 92 93 void CA_UnloadDictionary(CA_Vocab *hVocab) 94 { 95 TRY_CA_EXCEPT 96 ASSERT(hVocab); 97 if (hVocab->is_loaded == False) 98 SERVICE_ERROR(VOCAB_NOT_LOADED); 99 100 delete_word_transcription(&hVocab->voc); 101 102 hVocab->is_loaded = False; 103 return; 104 105 BEG_CATCH_CA_EXCEPT 106 END_CATCH_CA_EXCEPT(hVocab) 107 } 108 109 110 int CA_CheckEntryInDictionary(CA_Vocab *hVocab, const char *label) 111 { 112 int pronCount; 113 char prons[256]; 114 TRY_CA_EXCEPT 115 ASSERT(hVocab); 116 117 pronCount = get_prons(&hVocab->voc, label, prons, sizeof(prons)); 118 119 if (pronCount <= 0) { 120 /* try lower case, the general convention for dictionaries */ 121 unsigned i; 122 char lower[128]; 123 for (i = 0; label[i]; i++) { 124 if (i >= sizeof(lower) - 1) return -1; 125 lower[i] = tolower(label[i]); 126 } 127 lower[i] = 0; 128 pronCount = get_prons(&hVocab->voc, lower, prons, sizeof(prons)); 129 if (pronCount <= 0) return False; 130 } 131 132 return (True); 133 134 BEG_CATCH_CA_EXCEPT 135 END_CATCH_CA_EXCEPT(hVocab) 136 } 137 138 int CA_GetFullEntryInDictionary(CA_Vocab *hVocab, const char *label, char *pron, int *pronSize, int pronMaxSize); 139 140 int CA_GetEntryInDictionary(CA_Vocab *hVocab, const char *label, char *pron, int *pronSize, int pronMaxSize) 141 { 142 int rc; 143 TRY_CA_EXCEPT 144 ASSERT(hVocab); 145 rc = CA_GetFullEntryInDictionary(hVocab, label, pron, pronSize, pronMaxSize); 146 return rc; 147 BEG_CATCH_CA_EXCEPT 148 END_CATCH_CA_EXCEPT(hVocab) 149 } 150 151 /* this looks up the entry entire, underscores and all, eg "good_bye" */ 152 153 int CA_GetFullEntryInDictionary(CA_Vocab *hVocab, const char *label, char *pron, int *pronSize, int pronMaxSize) 154 { 155 TRY_CA_EXCEPT 156 int pronCount; 157 158 ASSERT(hVocab); 159 160 pronCount = get_prons(&hVocab->voc, label, pron, pronMaxSize); 161 if (pronCount <= 0) 162 { 163 /* try lower case, the general convention for dictionaries */ 164 unsigned i; 165 char lower[128]; 166 for (i = 0; label[i]; i++) { 167 if (i >= sizeof(lower) - 1) return -1; 168 lower[i] = tolower((unsigned char)label[i]); 169 } 170 lower[i] = 0; 171 pronCount = get_prons(&hVocab->voc, lower, pron, pronMaxSize); 172 if (pronCount <= 0) return False; 173 } 174 //*pronSize = pronCount; 175 176 return (True); 177 178 BEG_CATCH_CA_EXCEPT 179 END_CATCH_CA_EXCEPT(hVocab) 180 } 181