static const SRelation *UttGetRelation(const SUtterance *self, const char *name, s_erc *error) { const SRelation *rel; const SObject *object; S_CLR_ERR(error); object = SMapGetObject(self->relations, name, error); if (S_CHK_ERR(error, S_CONTERR, "UttGetRelation", "Call to \"SMapObjectGet\" failed")) return NULL; if (object == NULL) return NULL; rel = S_CAST(object, SRelation, error); if (S_CHK_ERR(error, S_CONTERR, "UttGetRelation", "Cast from SObject to SRelation failed")) return NULL; return rel; }
S_API const SObject *SUtteranceGetFeature(const SUtterance *self, const char *name, s_erc *error) { const SObject *object; S_CLR_ERR(error); if (self == NULL) { S_CTX_ERR(error, S_ARGERROR, "SUtteranceGetFeature", "Argument \"self\" is NULL"); return NULL; } if (name == NULL) { S_CTX_ERR(error, S_ARGERROR, "SUtteranceGetFeature", "Argument \"name\" is NULL"); return NULL; } object = SMapGetObject(self->features, name, error); if (S_CHK_ERR(error, S_CONTERR, "SUtteranceGetFeature", "Call to \"SMapGetObject\" failed")) return NULL; return object; }
static void Initialize(SFeatProcessor *self, const SMap* features, s_erc *error) { S_CLR_ERR(error); SPhrasePuncFeatProc *castSelf = S_CAST(self, SPhrasePuncFeatProc, error); if (S_CHK_ERR(error, S_CONTERR, "Initialize", "Call to S_CAST failed")) goto quit_error; castSelf->symbols = S_CAST( SMapGetObject(features , "list punctuation", error), SMap, error); if (S_CHK_ERR(error, S_CONTERR, "Initialize", "Call to \"SMapGetObject\" failed")) goto quit_error; castSelf->symbols = SMapCopy ( NULL , castSelf->symbols , error); if (S_CHK_ERR(error, S_CONTERR, "Initialize", "Call to \"SMapCopy\" failed")) goto quit_error; /* error cleanup */ quit_error: return; }
static void s_get_phrasing_symbols(const SUttProcessor *uttProc, const char **end_punc, s_erc *error) { s_bool have_symbols; const SMap *phrasingSymbols; const char *symbols; S_CLR_ERR(error); /* set to default for now */ *end_punc = s_default_phrasing_end_punc_chars; /* check if symbols are defined */ have_symbols = SUttProcessorFeatureIsPresent(uttProc, "phrasing symbols", error); if (S_CHK_ERR(error, S_CONTERR, "s_get_phrasing_symbols", "Call to \"SUttProcessorFeatureIsPresent\" failed")) return; if (!have_symbols) return; /* no tokenizer symbols defined */ phrasingSymbols = S_CAST(SUttProcessorGetFeature(uttProc, "phrasing symbols", error), SMap, error); if (S_CHK_ERR(error, S_CONTERR, "s_get_phrasing_symbols", "Failed to get 'phrasing symbols' SMap feature")) return; /* check if we have 'end-punctuation' symbols */ have_symbols = SMapObjectPresent(phrasingSymbols, "end-punctuation", error); if (S_CHK_ERR(error, S_CONTERR, "s_get_phrasing_symbols", "Call to \"SMapObjectPresent\" failed")) return; if (have_symbols) { /* get them and set it in end_punc */ symbols = SObjectGetString(SMapGetObject(phrasingSymbols, "end-punctuation", error), error); if (S_CHK_ERR(error, S_CONTERR, "s_get_phrasing_symbols", "Failed to get 'end-punctuation' symbols")) return; *end_punc = symbols; } }
/* setSentenceType should be made out of two parts: * 1) the first section searchs for the last punctuation element of the sentence * -> if it is a '.' --> set "decl" type (where should I set this feature value?) * -> if it is a '!' --> set "excl" type (where should I set this feature value?) * -> if it is a '?' --> set "interrog" type (where should I set this feature value?) * 2) if the first part decides for "interrog" type, there should be other controls * to establish the sentence's complete type * */ static char* setSentenceType(const SItem *phrase, SMap *prosSymbols, s_erc *error) { S_CLR_ERR(error); char* result = "decl"; /* types: "decl, "excl", "interrog" */ /* stop at sentence's last token */ const SItem *wordFromCurrentPhrase = SItemPathToItem(phrase, "daughter", error); if (S_CHK_ERR(error, S_CONTERR, "setSentenceType", "Call to \"SItemPathToItem\" failed")) return NULL; SItem *wordAsToken = SItemAs(wordFromCurrentPhrase, "Token", error); if (S_CHK_ERR(error, S_CONTERR, "setSentenceType", "Call to \"SItemAs\" failed")) return NULL; SItem *tokenItem = SItemParent(wordAsToken, error); SItem *firstTokenItem = tokenItem; s_bool isPunct = SItemFeatureIsPresent(tokenItem, "IsPunctuation", error); if (S_CHK_ERR(error, S_CONTERR, "setSentenceType", "Call to \"SItemFeatureIsPresent\" failed")) return NULL; s_bool isFinalPunct = FALSE; while (isFinalPunct == FALSE) { isPunct = SItemFeatureIsPresent(tokenItem, "IsPunctuation", error); if (S_CHK_ERR(error, S_CONTERR, "setSentenceType", "Call to \"SItemFeatureIsPresent\" failed")) return NULL; if (isPunct) { const char *punctStr = SItemGetName(tokenItem, error); if (S_CHK_ERR(error, S_CONTERR, "setSentenceType", "Call to \"SItemGetName\" failed")) return NULL; if (s_strcmp(punctStr, ".", error) == 0) { isFinalPunct = TRUE; result = "decl"; } else if (s_strcmp(punctStr, "!", error) == 0) { isFinalPunct = TRUE; result = "excl"; } else if (s_strcmp(punctStr, "?", error) == 0) { isFinalPunct = TRUE; const char *posValueStr = NULL; char *posValueStr_filtered = NULL; s_bool currPosInCurrList; s_bool have_symbols = FALSE; SMap* valueMap = NULL; have_symbols = SMapObjectPresent(prosSymbols, "firstPosInQuestionW", error); if (S_CHK_ERR(error, S_CONTERR, "SetSentenceType", "Call to \"SMapObjectPresent\" failed")) goto quit_error; if (have_symbols) { valueMap = S_CAST(SMapGetObject(prosSymbols, "firstPosInQuestionW", error), SMap, error); if (S_CHK_ERR(error, S_CONTERR, "SetSentenceType", "Call to \"SMapGetObject\" failed")) goto quit_error; } else goto quit_error; posValueStr = SItemGetString(firstTokenItem, "POS", error); if (S_CHK_ERR(error, S_CONTERR, "SetSentenceType", "Call to \"SItemGetString\" failed")) goto quit_error; /* filter the current POS tag, remember to free the memory * pointed to by 'posValueStr_filtered' pointer */ posValueStr_filtered = filterPosTag(posValueStr, error); if (S_CHK_ERR(error, S_CONTERR, "SetSentenceType", "Call to \"filterPosTag\" failed")) goto quit_error; currPosInCurrList = searchStringMap(valueMap, posValueStr_filtered, error); if (currPosInCurrList == TRUE) { result = "interrogW"; } else { result = "interrog"; } quit_error: if (posValueStr_filtered) { S_FREE(posValueStr_filtered); } break; } } tokenItem = SItemNext(tokenItem, error); if (S_CHK_ERR(error, S_CONTERR, "setSentenceType", "Call to \"SItemNext\" failed")) return NULL; if(tokenItem == NULL) { isFinalPunct = TRUE; } } return result; }
static void Initialize(SFeatProcessor *self, const SMap* features, s_erc *error) { S_CLR_ERR(error); SPhraseTypeFeatProc *castSelf = S_CAST(self, SPhraseTypeFeatProc, error); if (S_CHK_ERR(error, S_CONTERR, "Initialize", "Call to S_CAST failed")) goto quit_error; castSelf->symbols = S_CAST( SMapGetObject(features , "list definitions", error), SMap, error); if (S_CHK_ERR(error, S_CONTERR, "Initialize", "Call to \"SMapGetObject\" failed")) goto quit_error; castSelf->symbols = SMapCopy ( NULL , castSelf->symbols , error); if (S_CHK_ERR(error, S_CONTERR, "Initialize", "Call to \"SMapCopy\" failed")) goto quit_error; /* Get the iterator for the SMap */ SIterator *itrList = S_ITERATOR_GET(castSelf->symbols, error); if (S_CHK_ERR(error, S_CONTERR, "Initialize", "Call to \"S_ITERATOR_GET\" failed")) goto quit_error; while( itrList != NULL ) { const char *curStr = SIteratorKey(itrList, error); if (S_CHK_ERR(error, S_CONTERR, "Initialize", "Call to \"SIteratorKey\" failed")) goto quit_error; SList *valueList; valueList = S_CAST(SMapGetObject(castSelf->symbols, curStr, error), SList, error); if (S_CHK_ERR(error, S_CONTERR, "Initialize", "Call to \"SMapGetObject\" failed")) goto quit_error; /* Initializing a new SMap */ SMap *newCastMap= S_MAP(S_NEW(SMapHashTable, error)); SIterator *itrValueList = S_ITERATOR_GET(valueList, error); if (S_CHK_ERR(error, S_CONTERR, "Initialize", "Call to \"S_ITERATOR_GET\" failed")) goto quit_error; while(itrValueList != NULL) { const SObject *curValueObj = SIteratorObject(itrValueList, error); if (S_CHK_ERR(error, S_CONTERR, "Initialize", "Call to \"SIteratorObject\" failed")) goto quit_error; const char *curValueStr = SObjectGetString(curValueObj, error); if (S_CHK_ERR(error, S_CONTERR, "Initialize", "Call to \"SObjectGetString\" failed")) goto quit_error; /* Insert the string inside the map */ SMapSetString(newCastMap, curValueStr, curValueStr, error); if (S_CHK_ERR(error, S_CONTERR, "Initialize", "Call to \"SMapSetObject\" failed")) goto quit_error; itrValueList = SIteratorNext(itrValueList); } /* Insertion of the SMap inside the SMap */ SMapSetObject(castSelf->symbols, curStr, (SObject *) newCastMap, error); if (S_CHK_ERR(error, S_CONTERR, "Initialize", "Call to \"SMapSetObject\" failed")) goto quit_error; itrList = SIteratorNext(itrList); } /* error cleanup */ quit_error: return; }
static SObject *Run(const SFeatProcessor *self, const SItem *item, s_erc *error) { SObject *extractedFeat = NULL; const SItem *itrItem; sint32 count; SWordsToNextPuncFeatProc *castSelf = S_CAST(self, SWordsToNextPuncFeatProc, error); if (S_CHK_ERR(error, S_CONTERR, "Initialize", "Call to S_CAST failed")) goto quit_error; S_CLR_ERR(error); if (item == NULL) return NULL; itrItem = SItemPathToItem (item, "R:Token.parent", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemPathToItem\" failed")) goto quit_error; SMap* posPunctuation = S_CAST( SMapGetObject ( castSelf->symbols, "pos punctuation", error ), SMap, error ); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SMapGetObject\" failed")) goto quit_error; if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"S_CAST\" failed")) goto quit_error; count = -1; s_bool found = FALSE; while (found == FALSE && itrItem != NULL) { count++; s_bool hasPos = SItemFeatureIsPresent ( itrItem, "POS", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemFeatureIsPresent\" failed")) goto quit_error; if (hasPos) { const char* keyPos = SItemGetString (itrItem, "POS", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemGetString\" failed")) goto quit_error; found= SMapObjectPresent(posPunctuation, keyPos, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SMapObjectPresent\" failed")) goto quit_error; } itrItem = SItemNext(itrItem, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemNext\" failed")) goto quit_error; } extractedFeat = SObjectSetInt(count, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SObjectSetInt\" failed")) goto quit_error; /* all OK here */ return extractedFeat; S_UNUSED(self); /* error cleanup */ quit_error: if (extractedFeat != NULL) S_DELETE(extractedFeat, "Run", error); return NULL; }
S_LOCAL void _s_load_voice_utterance_processors(const SMap *voiceConfig, SVoice *voice, s_erc *error) { SMap *uttProcessors; const SObject *tmp; const SMap *voiceConfigUttProcessors; s_bool key_present; SIterator *itr; const char *uttproc_name; const char *uttproc_class; const char *uttproc_plugin; const SMap *uttProcInfo; const SMap *uttProcFeats; SUttProcessor *uProcessor; SPlugin *plugin; SList *uttProcPlugins; S_CLR_ERR(error); /* create a map container for utterance-processors */ uttProcessors = S_MAP(S_NEW(SMapList, error)); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Failed to create new map for voice utterance-processors")) return; /* look for "utterance-processors" key in voiceConfig map */ key_present = SMapObjectPresent(voiceConfig, "utterance-processors", error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"SMapObjectPresent\" failed for \'utterance-processors\' key")) { S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); return; } if (!key_present) { /* no defined utterance-processors */ voice->uttProcessors = uttProcessors; return; } /* get utterance-processors from voiceConfig */ tmp = SMapGetObject(voiceConfig, "utterance-processors", error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"SMapGetObject\" failed")) { S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); return; } voiceConfigUttProcessors = S_CAST(tmp, SMap, error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"S_CAST (SMap)\" failed for \'utterance-processors\' object")) { S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); return; } /* * iterate through the voiceConfigUttProcessors, create the * utterance-processors and add their features (if any), and add * them to the uttProcessors map */ itr = S_ITERATOR_GET(voiceConfigUttProcessors, error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"S_ITERATOR_GET\" failed")) { S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); return; } if (itr) { /* create a plug-in list */ uttProcPlugins = S_LIST(S_NEW(SListList, error)); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Failed to create new list for voice utterance-processor plug-ins")) { S_DELETE(itr, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); return; } } else uttProcPlugins = NULL; while (itr) { /* the utterance-processor name */ uttproc_name = SIteratorKey(itr, error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"SIteratorKey\" failed")) { S_DELETE(itr, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); return; } /* the utterance-processor info (SMap) */ tmp = SIteratorObject(itr, error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"SIteratorObject\" failed")) { S_DELETE(itr, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); return; } uttProcInfo = S_CAST(tmp, SMap, error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"S_CAST (SMap)\" failed for utterance-processor \'%s\'", uttproc_name)) { S_DELETE(itr, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); return; } /* * uttProcInfo must have "class" and "plug-in" and * optionally "features" */ uttproc_class = SMapGetString(uttProcInfo, "class", error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"SMapGetString\" failed for \'class\' name of utterance-processor \'%s\'", uttproc_name)) { S_DELETE(itr, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); return; } uttproc_plugin = SMapGetString(uttProcInfo, "plug-in", error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"SMapGetString\" failed for \'plug-in\' name of utterance-processor \'%s\'", uttproc_name)) { S_DELETE(itr, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); return; } /* * load the plug-in, add it to the list of utterance-processor plug-ins and create * the utterance-processor object */ plugin = s_pm_load_plugin(uttproc_plugin, error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"s_pm_load_plugin\" failed for utterance-processor \'%s\'", uttproc_name)) { S_DELETE(itr, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); return; } SListPush(uttProcPlugins, S_OBJECT(plugin), error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"SListPush\" failed for utterance-processor plug-ins")) { S_DELETE(plugin, "_s_load_voice_utterance_processors", error); S_DELETE(itr, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); return; } uProcessor = S_UTTPROCESSOR(S_NEW_FROM_NAME(uttproc_class, error)); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Failed to create new utterance-processor of class \'%s\'", uttproc_class)) { S_DELETE(itr, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); return; } /* add the utterance-processor to the map of utterance-processors */ SMapSetObject(uttProcessors, uttproc_name, S_OBJECT(uProcessor), error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"SMapSetObject\" failed")) { S_DELETE(uProcessor, "_s_load_voice_utterance_processors", error); S_DELETE(itr, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); return; } /* Check if this utterance-processor has any defined features */ key_present = SMapObjectPresent(uttProcInfo, "features", error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"SMapObjectPresent\" failed for \'features\' key of utterance-processor \'%s\'", uttproc_name)) { S_DELETE(itr, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); return; } if (key_present) { /* get utterance-processors "features" */ tmp = SMapGetObject(uttProcInfo, "features", error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"SMapGetObject\" failed for \'features\' of utterance-processor \'%s\'", uttproc_name)) { S_DELETE(itr, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); return; } uttProcFeats = S_CAST(tmp, SMap, error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"S_CAST (SMap)\" failed for \'features\' of utterance-processor \'%s\'", uttproc_name)) { S_DELETE(itr, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); return; } /* copy them to the utterance-processor */ SMapCopy(uProcessor->features, uttProcFeats, error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"SMapCopy\" failed for \'features\' of utterance-processor \'%s\'", uttproc_name)) { S_DELETE(itr, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); return; } } /* initialize the utterance processor */ SUttProcessorInit(&uProcessor, voice, error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"SUttProcessorInit\" failed for utterance-processor of class \'%s\'", uttproc_class)) { S_DELETE(itr, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); return; } itr = SIteratorNext(itr); } if (uttProcPlugins != NULL) { SListMerge(voice->plugins, uttProcPlugins, error); if (S_CHK_ERR(error, S_CONTERR, "_s_load_voice_utterance_processors", "Call to \"SListMerge\" failed")) { S_DELETE(uttProcessors, "_s_load_voice_utterance_processors", error); S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); return; } S_DELETE(uttProcPlugins, "_s_load_voice_utterance_processors", error); } voice->uttProcessors = uttProcessors; }