static const char* setSentenceType(const SItem *phrase, SMap *puncMap, s_erc *error) { S_CLR_ERR(error); const char* result = NULL; /* types: "decl, "excl", "interrog" */ /* stop at sentence's last token */ const SItem *wordFromCurrentPhrase = SItemPathToItem(phrase, "daughtern", 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); tokenItem = SItemNext(tokenItem, error); if (S_CHK_ERR(error, S_CONTERR, "setSentenceType", "Call to \"SItemNext\" failed")) return NULL; const char *punctStr = SItemGetName(tokenItem, error); if (S_CHK_ERR(error, S_CONTERR, "setSentenceType", "Call to \"SItemGetName\" failed")) return NULL; s_bool found= SMapObjectPresent(puncMap, punctStr, error); if (S_CHK_ERR(error, S_CONTERR, "setSentenceType", "Call to \"SMapObjectPresent\" failed")) return NULL; result = punctStr; if( found == TRUE) { result = SMapGetString ( puncMap, punctStr, error); if (S_CHK_ERR(error, S_CONTERR, "setSentenceType", "Call to \"SMapGetString\" failed")) return NULL; } else { result = NULL; } return result; }
static void s_compute_stresses ( const SFeatProcessor* proc, SItem* word, s_erc *error ) { S_CLR_ERR(error); SItem *wordAsSylStructure = SItemAs(word, "SylStructure", error); if (S_CHK_ERR(error, S_CONTERR, "s_compute_stresses", "Call to \"SItemAs\" failed")) return; SItem *syllable = SItemDaughter(wordAsSylStructure, error); if (S_CHK_ERR(error, S_CONTERR, "s_compute_stresses", "Call to \"SItemDaughter\" failed")) return; SObject* result = NULL; while (syllable != NULL) { result = SFeatProcessorRun ( proc, syllable, error); if (S_CHK_ERR(error, S_CONTERR, "s_compute_stresses", "Call to \"SItemPathToFeatProc\" failed")) goto s_compute_stresses_cleanup; const char* resultString = SObjectGetString ( result, error ); if (S_CHK_ERR(error, S_CONTERR, "s_compute_stresses", "Call to \"SObjectGetInt\" failed")) goto s_compute_stresses_cleanup; SItemSetString ( syllable, "stress", resultString, error ); if (S_CHK_ERR(error, S_CONTERR, "s_compute_stresses", "Call to \"SItemSetInt\" failed")) goto s_compute_stresses_cleanup; syllable = SItemNext ( syllable, error ); if (S_CHK_ERR(error, S_CONTERR, "s_compute_stresses", "Call to \"SItemNext\" failed")) goto s_compute_stresses_cleanup; if(result != NULL) { S_DELETE(result, "s_compute_stresses", error); } } s_compute_stresses_cleanup: if(result != NULL) { S_DELETE(result, "s_compute_stresses", error); } }
static SObject *Run(const SFeatProcessor *self, const SItem *item, s_erc *error) { SObject *extractedFeat = NULL; const SItem *itrItem; sint32 count; S_CLR_ERR(error); if (item == NULL) return NULL; itrItem = item; count = -1; while (itrItem != NULL) { count++; 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; /* error cleanup */ quit_error: if (extractedFeat != NULL) S_DELETE(extractedFeat, "Run", error); return NULL; S_UNUSED(self); }
static uint32 get_num_frames(SRelp *self, const SRelation *unitRel, s_erc *error) { const SItem *unit; SItem *sourceUnit = NULL; STrackFloat *sourceTrack = NULL; uint32 num_frames = 0; S_CLR_ERR(error); unit = SRelationHead(unitRel, error); if (S_CHK_ERR(error, S_CONTERR, "get_num_frames", "Call to \"SRelationHead\" failed")) return 0; while (unit != NULL) { sourceUnit = (SItem*)SItemGetObject(unit, "source-unit", error); if (S_CHK_ERR(error, S_CONTERR, "get_num_frames", "Call to \"SItemGetObject\" failed")) return 0; sourceTrack = (STrackFloat*)SItemGetObject(sourceUnit, "lpc-coefs", error); if (S_CHK_ERR(error, S_CONTERR, "get_num_frames", "Call to \"SItemGetObject\" failed")) return 0; num_frames += sourceTrack->data->row_count; unit = SItemNext(unit, error); if (S_CHK_ERR(error, S_CONTERR, "get_num_frames", "Call to \"SItemNext\" failed")) return 0; } self->num_channels = sourceTrack->data->col_count; return num_frames; }
static sint32 word_num_syls(const SItem *item, s_erc *error) { const SItem *itemInSylStructRel; const SItem *itrItem; sint32 count; S_CLR_ERR(error); if (item == NULL) return 0; itemInSylStructRel = SItemAs(item, "SylStructure", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemAs\" failed")) return 0; if (itemInSylStructRel == NULL) return 0; itrItem = SItemDaughter(itemInSylStructRel, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemDaughter\" failed")) return 0; count = 0; while (itrItem != NULL) { count++; itrItem = SItemNext(itrItem, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemNext\" failed")) return 0; } /* all OK here */ return count; }
static void Run(const SUttProcessor *self, SUtterance *utt, s_erc *error) { SHTSEngineMESynthUttProc105 *HTSsynth = (SHTSEngineMESynthUttProc105*)self; SPlugin *audioPlugin; const SRelation *segmentRel; SAudio *audio = NULL; s_bool is_present; char **label_data = NULL; int label_size; const SItem *item; const SItem *itemItr; int counter; uint i; int frame; int state; const double rate = HTSsynth->engine.global.fperiod * 1e+7 / HTSsynth->engine.global.sampling_rate; int nstate; S_CLR_ERR(error); /* we require the segment relation */ is_present = SUtteranceRelationIsPresent(utt, "Segment", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SUtteranceRelationIsPresent\" failed")) goto quit_error; if (!is_present) { S_CTX_ERR(error, S_FAILURE, "Run", "Failed to find 'Segment' relation in utterance"); goto quit_error; } segmentRel = SUtteranceGetRelation(utt, "Segment", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SUtteranceGetRelation\" failed")) goto quit_error; item = SRelationHead(segmentRel, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SRelationHead\" failed")) goto quit_error; itemItr = item; label_size = 0; while (itemItr != NULL) { label_size++; itemItr = SItemNext(itemItr, error); } label_data = S_CALLOC(char*, label_size); itemItr = item; counter = 0; while (itemItr != NULL) { SObject *dFeat; const char *tmp; dFeat = SItemPathToFeatProc(itemItr, "hts_labels", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemPathToFeatProc\" failed")) goto quit_error; if (dFeat == NULL) { S_CTX_ERR(error, S_FAILURE, "Run", "Failed to generate hts labels for segment item"); goto quit_error; } tmp = SObjectGetString(dFeat, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SObjectGetString\" failed")) goto quit_error; label_data[counter++] = s_strdup(tmp, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"s_strdup\" failed")) goto quit_error; SItemSetObject((SItem*)itemItr, "hts_label", dFeat, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemSetObject\" failed")) goto quit_error; itemItr = SItemNext(itemItr, error); } /* speech synthesis part */ HTS_Engine_load_label_from_string_list(&(HTSsynth->engine), label_data, label_size); check_and_change_rate_volume(HTSsynth, utt, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"check_and_change_rate_volume\" failed")) goto quit_error; HTS_Engine_create_sstream(&(HTSsynth->engine)); check_and_change_tone(HTSsynth, utt, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"check_and_change_tone\" failed")) goto quit_error; HTS_Engine_create_pstream(&(HTSsynth->engine)); if (HTSsynth->me == TRUE) /* mixed excitation */ { HTS_Engine_create_gstream_me(&(HTSsynth->engine), HTSsynth->me_num_filters, HTSsynth->me_filter_order, HTSsynth->me_filter, HTSsynth->xp_sig, HTSsynth->xn_sig, HTSsynth->hp, HTSsynth->hn, HTSsynth->pd_filter, HTSsynth->pd_filter_order); } else { HTS_Engine_create_gstream(&(HTSsynth->engine)); } nstate = HTS_Speect_ModelSet_get_nstate(&(HTSsynth->engine)); itemItr = item; counter = 0; frame = 0; state = 0; while (itemItr != NULL) { int j; int duration = 0; float tmp; for (j = 0; j < nstate; j++) duration += HTS_Speect_SStreamSet_get_duration(&(HTSsynth->engine), state++); tmp = frame * rate; SItemSetFloat((SItem*)itemItr, "start", tmp/1e+7, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemSetFloat\" failed")) goto quit_error; tmp = (frame + duration) * rate; SItemSetFloat((SItem*)itemItr, "end", tmp/1e+7, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemSetFloat\" failed")) goto quit_error; frame += duration; itemItr = SItemNext(itemItr, error); counter++; } /* We need to give the utterance the audio plug-in. If we don't do * this and the voice is deleted before the utterance, then the * utterance can't do *anything* with the audio. Not even delete * it (segfault). This should be fast because it is already * loaded. * Note that this happens before the audio is set. This is because * utt features are a list implementation. */ audioPlugin = s_pm_load_plugin("audio.spi", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SUtteranceSetFeature\" failed")) goto quit_error; SUtteranceSetFeature(utt, "audio_plugin", S_OBJECT(audioPlugin), error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SUtteranceSetFeature\" failed")) { S_DELETE(audioPlugin, "Run", error); goto quit_error; } /* create an audio object */ audio = S_NEW(SAudio, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Failed to create new 'SAudio' object")) goto quit_error; /* set audio feature in utterance */ SUtteranceSetFeature(utt, "audio", S_OBJECT(audio), error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SUtteranceSetFeature\" failed")) { S_DELETE(audio, "Run", error); goto quit_error; } audio->sample_rate = HTSsynth->engine.global.sampling_rate; audio->num_samples = (uint32)HTS_Speect_GStreamSet_get_total_nsample(&(HTSsynth->engine)); audio->samples = S_MALLOC(float, audio->num_samples); if (audio->samples == NULL) { S_FTL_ERR(error, S_MEMERROR, "Run", "Failed to allocate memory for 'float' object"); goto quit_error; } /* write data */ for (i = 0; i < audio->num_samples; i++) audio->samples[i] = (float)(HTS_Speect_GStreamSet_get_speech(&(HTSsynth->engine), i) * 1.0); for (counter = 0; counter < label_size; counter++) S_FREE(label_data[counter]); S_FREE(label_data); HTS_Engine_refresh(&(HTSsynth->engine)); /* all OK here */ return; /* error clean-up code */ quit_error: if (label_data != NULL) { for (counter = 0; counter < label_size; counter++) { if (label_data[counter] != NULL) S_FREE(label_data[counter]); } S_FREE(label_data); } return; }
static SObject *Run(const SFeatProcessor *self, const SItem *item, s_erc *error) { SObject *extractedFeat = NULL; const SItem *phraseItem; const SItem *wordItem; const SItem *sylStructWordItem; const SItem *syllableItem; sint32 num_stressed = 0; s_bool is_current_syl = FALSE; S_CLR_ERR(error); if (item == NULL) return NULL; /* get current phrase */ phraseItem = SItemPathToItem(item, "R:SylStructure.parent.R:Phrase.parent", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemPathToItem\" failed")) goto quit_error; if (phraseItem == NULL) { S_CTX_ERR(error, S_FAILURE, "Run", "Failed to get phrase of given syllable"); goto quit_error; } wordItem = SItemDaughter(phraseItem, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemDaughter\" failed")) goto quit_error; while (wordItem != NULL) { sylStructWordItem = SItemAs(wordItem, "SylStructure", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemAs\" failed")) goto quit_error; /* get syllables */ syllableItem = SItemDaughter(sylStructWordItem, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemDaughter\" failed")) goto quit_error; while (syllableItem != NULL) { s_bool is_stressed; is_current_syl = SItemEqual(syllableItem, item, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemEqual\" failed")) goto quit_error; if (is_current_syl) break; is_stressed = syl_is_stressed(syllableItem, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"syl_is_stressed\" failed")) goto quit_error; if (is_stressed) num_stressed++; syllableItem = SItemNext(syllableItem, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemNext\" failed")) goto quit_error; } if (is_current_syl) break; wordItem = SItemNext(wordItem, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemNext\" failed")) goto quit_error; } extractedFeat = SObjectSetInt(num_stressed, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SObjectSetInt\" failed")) goto quit_error; /* all OK here */ return extractedFeat; /* error cleanup */ quit_error: if (extractedFeat != NULL) S_DELETE(extractedFeat, "Run", error); return NULL; S_UNUSED(self); }
static SObject *Run(const SFeatProcessor *self, const SItem *item, s_erc *error) { SObject *extractedFeat = NULL; s_bool found = FALSE; SItem *boundary = SItemLastDaughter ( item, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemLastDaughter\" failed")) goto quit_error; boundary = SItemAs (boundary, "Boundaries", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemAs\" failed")) goto quit_error; SItem * boundaryNext = SItemNext (boundary, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemNext\" failed")) goto quit_error; if (boundaryNext != NULL) boundary = boundaryNext; while (!found && boundary != NULL) { found = SItemFeatureIsPresent( boundary, "tobi_endtone", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemFeatureIsPresent\" failed")) goto quit_error; if (!found) { boundary = SItemPrev(boundary, error ); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemPrev\" failed")) goto quit_error; } } if ( found ) { const char* tone = SItemGetString( boundary, "tobi_endtone", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemGetFeature\" failed")) goto quit_error; extractedFeat = SObjectSetString( tone, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SObjectSetString\" failed")) goto quit_error; } return extractedFeat; /* error cleanup */ quit_error: if (extractedFeat != NULL) S_DELETE(extractedFeat, "Run", error); return NULL; S_UNUSED(self); S_UNUSED(item); }
static SObject *Run(const SFeatProcessor *self, const SItem *item, s_erc *error) { SObject *extractedFeat = NULL; const SItem *syllableItem; sint32 num_syls = 1; /* include current syllable */ S_CLR_ERR(error); if (item == NULL) return NULL; /* get the syllable as in syllable relation */ syllableItem = SItemAs(item, "Syllable", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemAs\" failed")) goto quit_error; if (syllableItem == NULL) { S_CTX_ERR(error, S_FAILURE, "Run", "Failed to get the syllable as it is in the 'Syllable' relation"); goto quit_error; } /* start with the next syllable w.r.t the given one */ syllableItem = SItemNext(syllableItem, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemNext\" failed")) goto quit_error; while (syllableItem != NULL) { s_bool is_stressed; is_stressed = syl_is_stressed(syllableItem, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"syl_is_stressed\" failed")) goto quit_error; if (is_stressed) break; else num_syls++; syllableItem = SItemNext(syllableItem, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemNext\" failed")) goto quit_error; } extractedFeat = SObjectSetInt(num_syls, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SObjectSetInt\" failed")) goto quit_error; /* all OK here */ return extractedFeat; /* error cleanup */ quit_error: if (extractedFeat != NULL) S_DELETE(extractedFeat, "Run", error); return NULL; S_UNUSED(self); }
static SObject *Run(const SFeatProcessor *self, const SItem *item, s_erc *error) { SObject *extractedFeat = NULL; const SItem *itemInSylStructRel; const SItem *segment; const SPhoneset *phoneset; s_bool multilingual = FALSE; S_CLR_ERR(error); if (item == NULL) return NULL; /* get the phoneset */ phoneset = _get_phoneset(item, &multilingual, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"_get_phoneset\" failed")) return NULL; itemInSylStructRel = SItemAs(item, "SylStructure", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemAs\" failed")) goto quit_error; segment = SItemDaughter(itemInSylStructRel, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemDaughter\" failed")) goto quit_error; while (segment != NULL) { s_bool is_vowel; const char *item_name; item_name = SItemGetName(segment, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemGetName\" failed")) goto quit_error; if (item_name == NULL) continue; is_vowel = S_PHONESET_CALL(phoneset, phone_has_feature)(phoneset, item_name, "vowel", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"phone_has_feature\" failed")) goto quit_error; if (is_vowel) { if (multilingual) { extractedFeat = SItemPathToFeatProc(segment, "segment_name_multilingual", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemPathToFeatProc\" failed")) goto quit_error; } else { extractedFeat = SObjectSetString(item_name, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SObjectSetString\" failed")) goto quit_error; } return extractedFeat; } segment = SItemNext(segment, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemNext\" failed")) goto quit_error; } /* did not find a vowel */ extractedFeat = SObjectSetString("novowel", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SObjectSetString\" failed")) goto quit_error; /* all OK here */ return extractedFeat; /* error cleanup */ quit_error: if (extractedFeat != NULL) S_DELETE(extractedFeat, "Run", error); return NULL; S_UNUSED(self); }
static SObject *Run(const SFeatProcessor *self, const SItem *item, s_erc *error) { SObject *extractedFeat = NULL; const SItem *wordItem; sint32 num_words = 1; /* include current word */ S_CLR_ERR(error); if (item == NULL) return NULL; /* get the word as in the word relation */ wordItem = SItemAs(item, "Word", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemAs\" failed")) goto quit_error; if (wordItem == NULL) { S_CTX_ERR(error, S_FAILURE, "Run", "Failed to get given word as in 'Word' relation"); goto quit_error; } /* start with next word */ wordItem = SItemNext(wordItem, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemNext\" failed")) goto quit_error; while (wordItem != NULL) { s_bool is_content; is_content = word_is_content(wordItem, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"word_is_content\" failed")) goto quit_error; if (is_content) break; else num_words++; wordItem= SItemNext(wordItem, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemNext\" failed")) goto quit_error; } extractedFeat = SObjectSetInt(num_words, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SObjectSetInt\" failed")) goto quit_error; /* all OK here */ return extractedFeat; /* error cleanup */ quit_error: if (extractedFeat != NULL) S_DELETE(extractedFeat, "Run", error); return NULL; S_UNUSED(self); }
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; }
static int write_relation(SDatasource *ds, const SRelation *rel, int num_tiers, float end_time, s_erc *error) { const char *relname; const SItem *head = NULL; const char *tier_header = "\titem [%d]:\n\t\tclass = \"IntervalTier\"\n\t\tname = \"%s\"\n"; const char *tier_start_end = "\t\txmin = 0\n\t\txmax = %f\n"; const char *tier_intervals = "\t\tintervals: size = %d\n"; const char *tier_item = " \t\tintervals [%d]:\n\t\t\txmin = %f\n\t\t\txmax = %f\n\t\t\ttext = \"%s\"\n"; char *buff; size_t str_size; float prev_end = 0.0; const SItem *itr; float item_start = 0.0; float item_end = 0.0; const char *item_name = NULL; SList *info; size_t l_size; int counter; SIterator *litr; S_CLR_ERR(error); if (rel == NULL) return num_tiers; num_tiers += 1; relname = SRelationName(rel, error); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"SRelationName\" failed")) return 0; head = SRelationHead(rel, error); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"SRelationHead\" failed")) return 0; /* tier header */ s_asprintf(&buff, error, tier_header, num_tiers, relname); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"s_asprintf\" failed")) return 0; str_size = s_strsize(buff, error); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"s_strsize\" failed")) { S_FREE(buff); return 0; } SDatasourceWrite(ds, buff, sizeof(char), str_size, error); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"SDatasourceWrite\" failed")) { S_FREE(buff); return 0; } S_FREE(buff); /* start time and end time (always 0 and utt end) */ s_asprintf(&buff, error, tier_start_end, end_time); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"s_asprintf\" failed")) return 0; str_size = s_strsize(buff, error); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"s_strsize\" failed")) { S_FREE(buff); return 0; } SDatasourceWrite(ds, buff, sizeof(char), str_size, error); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"SDatasourceWrite\" failed")) { S_FREE(buff); return 0; } S_FREE(buff); info = S_LIST(S_NEW(SListList, error)); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Failed to create new list for info cache")) return 0; prev_end = 0.0; itr = head; while (itr != NULL) { item_start = get_start(itr, relname, error); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"get_start\" failed")) { S_DELETE(info, "write_relation", error); return 0; } item_end = get_end(itr, relname, error); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"get_end\" failed")) { S_DELETE(info, "write_relation", error); return 0; } if (!s_float_equal(item_start, prev_end)) { /* prepend extra "SIL" */ add_info(info, prev_end, item_start, "SIL", error); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"add_info\" failed")) { S_DELETE(info, "write_relation", error); return 0; } } item_name = SItemGetName(itr, error); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"SItemGetName\" failed")) { S_DELETE(info, "write_relation", error); return 0; } add_info(info, item_start, item_end, item_name, error); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"add_info\" failed")) { S_DELETE(info, "write_relation", error); return 0; } prev_end = item_end; itr = SItemNext(itr, error); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"SItemNext\" failed")) { S_DELETE(info, "write_relation", error); return 0; } } if (!s_float_equal(item_end, end_time)) { add_info(info, item_end, end_time, "SIL", error); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"add_info\" failed")) { S_DELETE(info, "write_relation", error); return 0; } } l_size = SListSize(info, error); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"SListSize\" failed")) { S_DELETE(info, "write_relation", error); return 0; } /* tier intervals */ s_asprintf(&buff, error, tier_intervals, l_size); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"s_asprintf\" failed")) { S_DELETE(info, "write_relation", error); return 0; } str_size = s_strsize(buff, error); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"s_strsize\" failed")) { S_FREE(buff); S_DELETE(info, "write_relation", error); return 0; } SDatasourceWrite(ds, buff, sizeof(char), str_size, error); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"SDatasourceWrite\" failed")) { S_FREE(buff); S_DELETE(info, "write_relation", error); return 0; } S_FREE(buff); litr = S_ITERATOR_GET(info, error); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"S_ITERATOR_GET\" failed")) { S_DELETE(info, "write_relation", error); return 0; } for (counter = 1; litr != NULL; litr = SIteratorNext(litr), counter++) { SList *itemList; SObject *tmp; char *name; itemList = S_LIST(SIteratorObject(litr, error)); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"SIteratorObject\" failed")) { S_DELETE(info, "write_relation", error); S_DELETE(litr, "write_relation", error); return 0; } tmp = SListPop(itemList, error); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"SIteratorObject\" failed")) { S_DELETE(info, "write_relation", error); S_DELETE(litr, "write_relation", error); return 0; } item_name = SObjectGetString(tmp, error); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"SObjectGetString\" failed")) { S_DELETE(info, "write_relation", error); S_DELETE(litr, "write_relation", error); S_DELETE(tmp, "write_relation", error); return 0; } name = s_strdup(item_name, error); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"s_strdup\" failed")) { S_DELETE(info, "write_relation", error); S_DELETE(litr, "write_relation", error); S_DELETE(tmp, "write_relation", error); return 0; } S_DELETE(tmp, "write_relation", error); tmp = SListPop(itemList, error); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"SIteratorObject\" failed")) { S_DELETE(info, "write_relation", error); S_DELETE(litr, "write_relation", error); S_FREE(name); return 0; } item_end = SObjectGetFloat(tmp, error); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"SObjectGetFloat\" failed")) { S_DELETE(info, "write_relation", error); S_DELETE(litr, "write_relation", error); S_DELETE(tmp, "write_relation", error); S_FREE(name); return 0; } S_DELETE(tmp, "write_relation", error); tmp = SListPop(itemList, error); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"SIteratorObject\" failed")) { S_DELETE(info, "write_relation", error); S_DELETE(litr, "write_relation", error); S_FREE(name); return 0; } item_start = SObjectGetFloat(tmp, error); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"SObjectGetFloat\" failed")) { S_DELETE(info, "write_relation", error); S_DELETE(litr, "write_relation", error); S_DELETE(tmp, "write_relation", error); S_FREE(name); return 0; } S_DELETE(tmp, "write_relation", error); /* tier item */ s_asprintf(&buff, error, tier_item, counter, item_start, item_end, name); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"s_asprintf\" failed")) { S_DELETE(info, "write_relation", error); S_DELETE(litr, "write_relation", error); S_FREE(name); return 0; } str_size = s_strsize(buff, error); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"s_strsize\" failed")) { S_FREE(buff); S_DELETE(info, "write_relation", error); S_DELETE(litr, "write_relation", error); S_FREE(name); return 0; } SDatasourceWrite(ds, buff, sizeof(char), str_size, error); if (S_CHK_ERR(error, S_CONTERR, "write_relation", "Call to \"SDatasourceWrite\" failed")) { S_FREE(buff); S_DELETE(info, "write_relation", error); S_DELETE(litr, "write_relation", error); S_FREE(name); return 0; } S_FREE(buff); S_FREE(name); } S_DELETE(info, "write_relation", error); return num_tiers; }
static void Run(const SUttProcessor *self, SUtterance *utt, s_erc *error) { const SRelation *wordRel; SItem *wordItem; const SItem *tokenItem; s_bool is_present; const char *end_punc; SRelation *phraseRelation = NULL; SItem *phraseItem = NULL; const char *post_punc; SRelation *sentenceRelation = NULL; SItem *sentenceItem = NULL; S_CLR_ERR(error); /* we require the word relation */ is_present = SUtteranceRelationIsPresent(utt, "Word", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SUtteranceRelationIsPresent\" failed")) goto quit_error; if (!is_present) { S_CTX_ERR(error, S_FAILURE, "Run", "Failed to find 'Word' relation in utterance"); goto quit_error; } wordRel = SUtteranceGetRelation(utt, "Word", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SUtteranceGetRelation\" failed")) goto quit_error; /* get phrasing symbols */ s_get_phrasing_symbols(self, &end_punc, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"s_get_phrasing_symbols\" failed")) goto quit_error; /* create Phrase relation */ phraseRelation = SUtteranceNewRelation(utt, "Phrase", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SUtteranceNewRelation\" failed")) goto quit_error; /* create Sentence relation */ sentenceRelation = SUtteranceNewRelation(utt, "Sentence", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SUtteranceNewRelation\" failed")) goto quit_error; /* start at the first item in the word relation, cast away * const, we want to add daughter items */ wordItem = (SItem*)SRelationHead(wordRel, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SRelationHead\" failed")) goto quit_error; while (wordItem != NULL) { SItem *lastWordInToken; SItem *wordAsToken; if (phraseItem == NULL) { /* if phrase item is NULL, create a new phrase item (NULL * shared content) that is appended to phrase * relation. Will happen in first pass. */ sentenceItem = SRelationAppend(sentenceRelation, NULL, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SRelationAppend\" failed")) goto quit_error; /* Added on top a sentence item, for now is one on one with the phrase item * */ phraseItem = SRelationAppend(phraseRelation, NULL, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SRelationAppend\" failed")) goto quit_error; /* add an item name, NB, no break */ SItemSetString(phraseItem, "name", "NB", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemSetString\" failed")) goto quit_error; SItemAddDaughter(sentenceItem, phraseItem, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemAddDaughter\" failed")) goto quit_error; } /* Create a daughter item for the phrase item. Shared content * is the word item. */ SItemAddDaughter(phraseItem, wordItem, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemAddDaughter\" failed")) goto quit_error; /* get word as in Token relation */ wordAsToken = SItemAs(wordItem, "Token", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Failed to get word item's as in Token relation")) goto quit_error; /* * get word's token which is the parent of wordAsToken. */ tokenItem = SItemParent(wordAsToken, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Failed to get word item's token item")) goto quit_error; /* get last word in token */ lastWordInToken = SItemLastDaughter(tokenItem, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Failed to get last daughter of token item")) goto quit_error; /* check if the next token is punctuation */ is_present = FALSE; tokenItem = SItemNext(tokenItem, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemNext\" failed")) goto quit_error; if (tokenItem != NULL) { is_present = SItemFeatureIsPresent(tokenItem, "IsPunctuation", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemFeatureIsPresent\" failed")) goto quit_error; if (is_present) { sint32 value = SItemGetInt(tokenItem, "IsPunctuation", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemGetInt\" failed")) goto quit_error; is_present = (value > 0); } } if ((is_present) && (wordAsToken == lastWordInToken)) { char *ptr; post_punc = SItemGetName(tokenItem, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemGetName\" failed")) goto quit_error; /* check if it is in the end_punc list */ ptr = s_strpbrk(post_punc, end_punc, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"s_strpbrk\" failed")) goto quit_error; if (ptr != NULL) { /* add a phrase break */ SItemSetString(phraseItem, "name", "BB", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemSetString\" failed")) goto quit_error; /* set to NULL so that a new phrase item is created */ phraseItem = NULL; } } wordItem = SItemNext(wordItem, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemNext\" failed")) goto quit_error; } /* here all is OK */ return; /* error clean-up code */ quit_error: if (phraseRelation != NULL) { SUtteranceDelRelation(utt, "Phrase", error); S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SUtteranceDelRelation\" failed"); } }
static void Run(const SUttProcessor *self, SUtterance *utt, s_erc *error) { SCrfSuiteUttProc *crfsuiteProc = (SCrfSuiteUttProc*)self; crfsuite_model_t * ptr_model = malloc (sizeof(crfsuite_tagger_t)); crfsuite_tagger_t * ptr_tagger = malloc (sizeof(crfsuite_tagger_t)); crfsuite_dictionary_t * ptr_attrs = malloc (sizeof(crfsuite_dictionary_t)); crfsuite_dictionary_t * ptr_labels = malloc (sizeof(crfsuite_dictionary_t)); crfsuite_instance_t * instance = malloc (sizeof(crfsuite_instance_t)); /* Initialize model object */ if ( crfsuite_create_instance_from_file( crfsuiteProc->model_file, (void**)&ptr_model ) != 0 ) { goto exit_cleanup; } const SRelation* phrase = SUtteranceGetRelation(utt, "Phrase", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SUtteranceGetRelation\" failed")) return; SItem* itrPhrase = SRelationHead( phrase, error ); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SRelationHead\" failed")) return; while ( itrPhrase != NULL ) { /* Obtain the dictionary interface representing the labels in the model. */ if ( ptr_model->get_labels(ptr_model, &ptr_labels) != 0) { goto exit_cleanup; } /* Obtain the dictionary interface representing the attributes in the model. */ if ( ptr_model->get_attrs(ptr_model, &ptr_attrs) != 0) { goto exit_cleanup; } /* Obtain the tagger interface. */ if ( ptr_model->get_tagger(ptr_model, &ptr_tagger) != 0) { goto exit_cleanup; } instance = create_phrase_instance ( itrPhrase, ptr_attrs, ptr_labels, error ); int *output = calloc(sizeof(int), instance->num_items); floatval_t score = 0; /* Set the instance to the tagger. */ if ( ptr_tagger->set(ptr_tagger, instance) != 0) { goto exit_cleanup; } /* Obtain the viterbi label sequence. */ if (ptr_tagger->viterbi(ptr_tagger, output, &score) != 0) { goto exit_cleanup; } /* Extract the output and insert in the POS attribute */ const SItem* tokenTMP = SItemPathToItem ( itrPhrase, "daughter.R:Token", error ); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemPathToItem\" failed")) return; SItem* token = SItemParent (tokenTMP, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemParent\" failed")) return; const SItem* lastToken = SItemPathToItem ( itrPhrase, "n.daughter.R:Token.parent", error ); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemPathToItem\" failed")) return; int i = 0; while ( token != NULL && token != lastToken ) { const char * str = malloc (sizeof (char)*16); ptr_labels->to_string (ptr_labels, output[i], &str); i += 1; SItemSetString (token, "POS", str, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemSetString\" failed")) return; token = SItemNext(token, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemNext\" failed")) return; } free(output); crfsuite_instance_finish(instance); itrPhrase = SItemNext(itrPhrase, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemNext\" failed")) return; } /* here all is OK */ S_UNUSED(utt); exit_cleanup: if ( ptr_model != NULL ) free ( ptr_model ); if ( ptr_tagger != NULL ) free ( ptr_tagger ); if ( ptr_attrs != NULL ) free ( ptr_attrs ); if ( ptr_labels != NULL ) free ( ptr_labels ); if ( instance != NULL ) free ( instance ); }
static crfsuite_instance_t* create_phrase_instance ( SItem* phrase, crfsuite_dictionary_t* attrs, crfsuite_dictionary_t* labels, s_erc *error) { crfsuite_instance_t * result = malloc ( sizeof(crfsuite_instance_t) ); int i = 0; int L = labels->num(labels); const SItem* itrItem = NULL; const SItem* itrItemNext = NULL; const SItem* finishItem = NULL; const char* lbl[] = {"num", "sym", "cap", "p1", "p2", "p3", "s1", "s2", "s3", "P1", "P2", "P3", "P4", "S1", "S2", "S3", "S4", "S5", "S6", "w" }; const int words_length = 19; const char* words[19] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}; int position = 0; int lbl_counter = 0; char buffer[8192]; crfsuite_instance_init ( result ); itrItemNext = SItemPathToItem (phrase, "daughter.R:Token.parent", error); if (S_CHK_ERR(error, S_CONTERR, "create_phrase_instance", "Call to \"SItemPathToItem\" failed")) return NULL; finishItem = SItemPathToItem (phrase, "n.daughter.R:Token.parent", error); if (S_CHK_ERR(error, S_CONTERR, "create_phrase_instance", "Call to \"SItemPathToItem\" failed")) return NULL; int counter = 0; while ( itrItemNext != finishItem && itrItemNext != NULL && counter < 9) { words[counter] = SItemGetName(itrItemNext, error); if (S_CHK_ERR(error, S_CONTERR, "create_phrase_instance", "Call to \"SItemGetName\" failed")) return NULL; counter++; itrItemNext = SItemNext(itrItemNext, error); if (S_CHK_ERR(error, S_CONTERR, "create_phrase_instance", "Call to \"SItemNext\" failed")) return NULL; } itrItem = SItemPathToItem (phrase, "daughter.R:Token.parent", error); if (S_CHK_ERR(error, S_CONTERR, "create_phrase_instance", "Call to \"SItemPathToItem\" failed")) return NULL; while ( itrItem != finishItem && itrItem != NULL) { /* Extraction of the features for each token */ const char *tokenName = SItemGetName (itrItem, error); if (S_CHK_ERR(error, S_CONTERR, "create_phrase_instance", "Call to \"SItemGetName\" failed")) return NULL; lbl_counter = 0; /* Extraction of label's ID */ int tokenID = labels->to_id(labels, "UNK"); int attribute_id; /* If unknown the set the 0 labels (unknown) */ if (tokenID < 0) tokenID = L; crfsuite_item_t itemToken; crfsuite_attribute_t attribute; crfsuite_item_init(&itemToken); const char *feat = NULL; s_bool found = FALSE; /* if token contains numbers */ found = hasNumber (tokenName, error); if (S_CHK_ERR(error, S_CONTERR, "create_phrase_instance", "Call to \"hasNumber\" failed")) return NULL; sprintf(buffer, "%s[%d]=%s", lbl[lbl_counter], 0, found? "Y" : "N"); lbl_counter++; attribute_id = attrs->to_id (attrs,buffer); crfsuite_attribute_set (&attribute, attribute_id, 1.0); crfsuite_item_append_attribute(&itemToken, &attribute); /* if token contains symbols */ found = hasSymbol (tokenName, error); if (S_CHK_ERR(error, S_CONTERR, "create_phrase_instance", "Call to \"hasSymbol\" failed")) return NULL; sprintf(buffer, "%s[%d]=%s", lbl[lbl_counter], 0, found? "Y" : "N"); lbl_counter++; attribute_id = attrs->to_id (attrs,buffer); crfsuite_attribute_set (&attribute, attribute_id, 1.0); crfsuite_item_append_attribute(&itemToken, &attribute); /* if token contains Capitals */ found = hasCapital (tokenName, error); if (S_CHK_ERR(error, S_CONTERR, "create_phrase_instance", "Call to \"hasCapital\" failed")) return NULL; sprintf(buffer, "%s[%d]=%s", lbl[lbl_counter], 0, found? "Y" : "N"); lbl_counter++; attribute_id = attrs->to_id (attrs,buffer); crfsuite_attribute_set (&attribute, attribute_id, 1.0); crfsuite_item_append_attribute(&itemToken, &attribute); /* Prefixes of 1 to 3 chars of token */ i = 1; while ( i < 4 ) { feat = getFirstChars ( tokenName, i, error ); if (S_CHK_ERR(error, S_CONTERR, "create_phrase_instance", "Call to \"getFirstChars\" failed")) return NULL; if ( feat == NULL ) feat = "__nil__"; sprintf(buffer, "%s[%d]=%s", lbl[lbl_counter], 0, feat); lbl_counter++; attribute_id = attrs->to_id (attrs,buffer); crfsuite_attribute_set (&attribute, attribute_id, 1.0); crfsuite_item_append_attribute(&itemToken, &attribute); i += 1; } /* Suffixes of 1 to 3 chars of token */ i = 1; while ( i < 4 ) { feat = getLastChars ( tokenName, i, error ); if (S_CHK_ERR(error, S_CONTERR, "create_phrase_instance", "Call to \"getLastChars\" failed")) return NULL; if ( feat == NULL ) feat = "__nil__"; sprintf(buffer, "%s[%d]=%s", lbl[lbl_counter], 0, feat); lbl_counter++; attribute_id = attrs->to_id (attrs,buffer); crfsuite_attribute_set (&attribute, attribute_id, 1.0); crfsuite_item_append_attribute(&itemToken, &attribute); i += 1; } /* Prefixes of 1 to 4 chars of token without duplicates adjacent */ i = 1; while ( i < 5 ) { feat = getFirstChars ( removeDoubles(tokenName, error), i, error ); if (S_CHK_ERR(error, S_CONTERR, "create_phrase_instance", "Call to \"removeDoubles\" failed")) return NULL; if (S_CHK_ERR(error, S_CONTERR, "create_phrase_instance", "Call to \"getFirstChars\" failed")) return NULL; if ( feat == NULL ) feat = "__nil__"; sprintf(buffer, "%s[%d]=%s", lbl[lbl_counter], 0, feat); lbl_counter++; attribute_id = attrs->to_id (attrs,buffer); crfsuite_attribute_set (&attribute, attribute_id, 1.0); crfsuite_item_append_attribute(&itemToken, &attribute); i += 1; } /* Suffixes of 1 to 6 chars of token without duplicates adjacent */ i = 1; while ( i < 7 ) { feat = getLastChars ( removeDoubles(tokenName, error), i, error ); if (S_CHK_ERR(error, S_CONTERR, "create_phrase_instance", "Call to \"removeDoubles\" failed")) return NULL; if (S_CHK_ERR(error, S_CONTERR, "create_phrase_instance", "Call to \"getFirstChars\" failed")) return NULL; if ( feat == NULL ) feat = "__nil__"; sprintf(buffer, "%s[%d]=%s", lbl[lbl_counter], 0, feat); lbl_counter++; attribute_id = attrs->to_id (attrs,buffer); crfsuite_attribute_set (&attribute, attribute_id, 1.0); crfsuite_item_append_attribute(&itemToken, &attribute); i += 1; } /* Words features */ sprintf(buffer, "%s[%d]=%s", lbl[lbl_counter], 0, words[(position+0+words_length)%words_length]); attribute_id = attrs->to_id (attrs,buffer); crfsuite_attribute_set (&attribute, attribute_id, 1.0); crfsuite_item_append_attribute(&itemToken, &attribute); const char *tmp = words[(position-1+words_length)%words_length]; if(tmp!=NULL) { sprintf(buffer, "%s[%d]=%s", lbl[lbl_counter], -1, tmp); attribute_id = attrs->to_id (attrs,buffer); crfsuite_attribute_set (&attribute, attribute_id, 1.0); crfsuite_item_append_attribute(&itemToken, &attribute); } tmp = words[(position+1+words_length)%words_length]; if(tmp!=NULL) { sprintf(buffer, "%s[%d]=%s", lbl[lbl_counter], 1, tmp); attribute_id = attrs->to_id (attrs,buffer); crfsuite_attribute_set (&attribute, attribute_id, 1.0); crfsuite_item_append_attribute(&itemToken, &attribute); } tmp = words[(position-2+words_length)%words_length]; if(tmp!=NULL) { sprintf(buffer, "%s[%d]=%s", lbl[lbl_counter], -2, tmp); attribute_id = attrs->to_id (attrs,buffer); crfsuite_attribute_set (&attribute, attribute_id, 1.0); crfsuite_item_append_attribute(&itemToken, &attribute); } tmp = words[(position+2+words_length)%words_length]; if(tmp!=NULL) { sprintf(buffer, "%s[%d]=%s", lbl[lbl_counter], 2, tmp); attribute_id = attrs->to_id (attrs,buffer); crfsuite_attribute_set (&attribute, attribute_id, 1.0); crfsuite_item_append_attribute(&itemToken, &attribute); } const char * tmp1 = NULL; i = 0; while ( i < 4 ) { tmp = words[(position-2+i+words_length)%words_length]; tmp1 = words[(position-1+i+words_length)%words_length]; if( tmp!=NULL && tmp1 != NULL ) { sprintf(buffer, "%s[%d]|%s[%d]=%s|%s", lbl[lbl_counter], -2+i, lbl[lbl_counter], -1+i, tmp, tmp1 ); attribute_id = attrs->to_id (attrs,buffer); crfsuite_attribute_set (&attribute, attribute_id, 1.0); crfsuite_item_append_attribute(&itemToken, &attribute); } i++; } const char * tmp2 = NULL; i = 0; while ( i < 3 ) { tmp = words[(position-2+i+words_length)%words_length]; tmp1 = words[(position-1+i+words_length)%words_length]; tmp2 = words[(position+0+i+words_length)%words_length]; if( tmp!=NULL && tmp1 != NULL && tmp2 != NULL ) { sprintf(buffer, "%s[%d]|%s[%d]|%s[%d]=%s|%s|%s", lbl[lbl_counter], -2+i, lbl[lbl_counter], -1+i, lbl[lbl_counter], +0+i, tmp, tmp1, tmp2 ); attribute_id = attrs->to_id (attrs,buffer); crfsuite_attribute_set (&attribute, attribute_id, 1.0); crfsuite_item_append_attribute(&itemToken, &attribute); } i++; } const char * tmp3 = NULL; i = 0; while ( i < 2 ) { tmp = words[(position-2+i+words_length)%words_length]; tmp1 = words[(position-1+i+words_length)%words_length]; tmp2 = words[(position+0+i+words_length)%words_length]; tmp3 = words[(position+1+i+words_length)%words_length]; if( tmp!=NULL && tmp1 != NULL && tmp2 != NULL && tmp3 != NULL ) { sprintf(buffer, "%s[%d]|%s[%d]|%s[%d]|%s[%d]=%s|%s|%s|%s", lbl[lbl_counter], -2+i, lbl[lbl_counter], -1+i, lbl[lbl_counter], +0+i, lbl[lbl_counter], +1+i, tmp, tmp1, tmp2, tmp3 ); attribute_id = attrs->to_id (attrs,buffer); crfsuite_attribute_set (&attribute, attribute_id, 1.0); crfsuite_item_append_attribute(&itemToken, &attribute); } i++; } tmp = words[(position-2+words_length)%words_length]; tmp1 = words[(position-1+words_length)%words_length]; tmp2 = words[(position+0+words_length)%words_length]; tmp3 = words[(position+1+words_length)%words_length]; const char * tmp4 = words[(position+2+words_length)%words_length]; if( tmp!=NULL && tmp1 != NULL && tmp2 != NULL && tmp3 != NULL && tmp4 != NULL ) { sprintf(buffer, "%s[%d]|%s[%d]|%s[%d]|%s[%d]|%s[%d]=%s|%s|%s|%s|%s", lbl[lbl_counter], -2, lbl[lbl_counter], -1, lbl[lbl_counter], +0, lbl[lbl_counter], +1, lbl[lbl_counter], +2, tmp, tmp1, tmp2, tmp3, tmp4 ); attribute_id = attrs->to_id (attrs,buffer); crfsuite_attribute_set (&attribute, attribute_id, 1.0); crfsuite_item_append_attribute(&itemToken, &attribute); } tmp = words[(position+0+words_length)%words_length]; i = 1; while ( i < 10 ) { tmp1 = words[(position-i+words_length)%words_length]; if ( tmp1 != NULL ) { sprintf(buffer, "%s[%d]|%s[%d]=%s|%s", lbl[lbl_counter], 0, lbl[lbl_counter], 0-i, tmp, tmp1 ); attribute_id = attrs->to_id (attrs,buffer); crfsuite_attribute_set (&attribute, attribute_id, 1.0); crfsuite_item_append_attribute(&itemToken, &attribute); } i++; } i = 1; while ( i < 10 ) { tmp1 = words[(position+i+words_length)%words_length]; if ( tmp1 != NULL ) { sprintf(buffer, "%s[%d]|%s[%d]=%s|%s", lbl[lbl_counter], 0, lbl[lbl_counter], 0+i, tmp, tmp1 ); attribute_id = attrs->to_id (attrs,buffer); crfsuite_attribute_set (&attribute, attribute_id, 1.0); crfsuite_item_append_attribute(&itemToken, &attribute); } i++; } /* Update words array */ position++; if ( itrItemNext != NULL ) { words[position+9] = SItemGetName (itrItemNext, error); if (S_CHK_ERR(error, S_CONTERR, "create_phrase_instance", "Call to \"SItemNext\" failed")) return NULL; itrItemNext = SItemNext ( itrItemNext, error ); if (S_CHK_ERR(error, S_CONTERR, "create_phrase_instance", "Call to \"SItemNext\" failed")) return NULL; } else words[position+9] = NULL; itrItem = SItemNext ( itrItem, error ); if (S_CHK_ERR(error, S_CONTERR, "create_phrase_instance", "Call to \"SItemNext\" failed")) return NULL; crfsuite_instance_append(result, &itemToken, tokenID); crfsuite_item_finish(&itemToken); } return result; }
static SObject *Run(const SFeatProcessor *self, const SItem *item, s_erc *error) { SObject *extractedFeat = NULL; const SItem *phraseItem; const SItem *wordItem; sint32 num_content = 0; S_CLR_ERR(error); if (item == NULL) return NULL; /* get current phrase */ phraseItem = SItemPathToItem(item, "R:Phrase.parent", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemPathToItem\" failed")) goto quit_error; if (phraseItem == NULL) { S_CTX_ERR(error, S_FAILURE, "Run", "Failed to get phrase of given word"); goto quit_error; } wordItem = SItemDaughter(phraseItem, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemDaughter\" failed")) goto quit_error; while (wordItem != NULL) { s_bool is_content; s_bool is_current_word; is_current_word = SItemEqual(wordItem, item, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemEqual\" failed")) goto quit_error; if (is_current_word) break; is_content = word_is_content(wordItem, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"word_is_content\" failed")) goto quit_error; if (is_content) num_content++; wordItem = SItemNext(wordItem, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemNext\" failed")) goto quit_error; } extractedFeat = SObjectSetInt(num_content, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SObjectSetInt\" failed")) goto quit_error; /* all OK here */ return extractedFeat; /* error cleanup */ quit_error: if (extractedFeat != NULL) S_DELETE(extractedFeat, "Run", error); return NULL; S_UNUSED(self); }
static void Save(const SObject *object, const char *path, s_erc *error) { int rc; SUtterance *utt = S_UTTERANCE(object); SDatasource *ds; xmlTextWriterPtr writer; xmlOutputBufferPtr out; const char * xsi = "http://www.w3.org/2001/XMLSchema-instance"; S_CLR_ERR(error); ds = SFilesourceOpenFile(path, "wt", error); if (S_CHK_ERR(error, S_CONTERR, "Save", "Call to \"SFilesourceOpenFile\" failed")) return; out = xmlOutputBufferCreateIO(_ds_write, _ds_close, ds, NULL); if (out == NULL) { S_CTX_ERR(error, S_CONTERR, "Save", "Call to \"xmlOutputBufferCreateIO\" failed"); return; } writer = xmlNewTextWriter(out); if (writer == NULL) { xmlOutputBufferClose(out); S_CTX_ERR(error, S_CONTERR, "Save", "Call to \"xmlNewTextWriter\" failed"); return; } /* Start Document */ rc = xmlTextWriterStartDocument(writer, NULL, ENCODING, NULL); if (rc < 0) { S_CTX_ERR(error, S_CONTERR, "Save", "Call to \"xmlTextWriterStartDocument\" failed"); goto s_write_utt_exit; } /* Write the maryxml namespace */ rc = xmlTextWriterStartElement(writer, BAD_CAST "xml"); if (rc < 0) { S_CTX_ERR(error, S_CONTERR, "Save", "Call to \"xmlTextWriterStartElement\" failed"); goto s_write_utt_exit; } rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "xmlns:xsi", BAD_CAST xsi); if (rc < 0) { S_CTX_ERR(error, S_CONTERR, "Save", "Call to \"xmlTextWriterWriteAttribute\" failed"); goto s_write_utt_exit; } rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "version", BAD_CAST "0.5"); if (rc < 0) { S_CTX_ERR(error, S_CONTERR, "Save", "Call to \"xmlTextWriterWriteAttribute\" failed"); goto s_write_utt_exit; } // print labels /* get to the first syllable of the current word */ const SItem* itrSegments = SRelationHead(SUtteranceGetRelation(utt, "Segment", error), error); if (S_CHK_ERR(error, S_CONTERR, "Save", "Call to \"SItemPathToItem\" failed")) goto s_write_utt_exit; while (itrSegments != NULL) { /* get segment content */ const char* label = SItemGetString(itrSegments, "hts_label", error); if (S_CHK_ERR(error, S_CONTERR, "Save", "Call to \"SItemGetName\" failed")) goto s_write_utt_exit; /* get next segment */ itrSegments = SItemNext(itrSegments, error); if (S_CHK_ERR(error, S_CONTERR, "Save", "Call to \"SItemNext\" failed")) goto s_write_utt_exit; xmlTextWriterWriteElement(writer, BAD_CAST "label", BAD_CAST label); } /* Close the tag xml */ rc = xmlTextWriterEndElement(writer); if (rc < 0) { S_CTX_ERR(error, S_CONTERR, "Save", "Call to \"xmlTextWriterEndDocument\" failed"); goto s_write_utt_exit; } /* Close the document */ rc = xmlTextWriterEndDocument(writer); if (rc < 0) { S_CTX_ERR(error, S_CONTERR, "Save", "Call to \"xmlTextWriterEndDocument\" failed"); goto s_write_utt_exit; } s_write_utt_exit: xmlFreeTextWriter(writer); }
static SObject *Run(const SFeatProcessor *self, const SItem *item, s_erc *error) { SObject *extractedFeat = NULL; const SItem *itemInSylStructRel; const SItem *itrItem = NULL; const SObject* feature = NULL; sint32 count; S_CLR_ERR(error); if (item == NULL) return NULL; itemInSylStructRel = SItemAs(item, "SylStructure", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemAs\" failed")) goto quit_error; if (itemInSylStructRel == NULL) { extractedFeat = SObjectSetInt(0, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SObjectSetInt\" failed")) goto quit_error; } else { itrItem = SItemDaughter(itemInSylStructRel, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemDaughter\" failed")) goto quit_error; } count = 0; while (itrItem != NULL) { feature = SItemPathToFeatProc(itrItem, "syllable_num_phones", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemPathToFeatProc\" failed")) goto quit_error; if(feature == NULL) { S_CTX_ERR(error, S_FAILURE, "Run", "Call to \"SItemPathToFeatProc\" returned null"); goto quit_error; } count += SObjectGetInt(feature, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SObjectGetInt\" 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; /* error cleanup */ quit_error: if (extractedFeat != NULL) S_DELETE(extractedFeat, "Run", error); return NULL; S_UNUSED(self); }
static void Run(const SUttProcessor *self, SUtterance *utt, s_erc *error) { SG2P *g2p = NULL; SLexicon *lexicon = NULL; SAddendum *addendum = NULL; SSyllabification *syllab = NULL; const SRelation *wordRel; SRelation *syllableRel = NULL; SRelation *sylStructRel = NULL; SRelation *segmentRel = NULL; SItem *wordItem; char *downcase_word; SList *phones; s_bool syllabified; SList *syllablesPhones; SItem *sylStructureWordItem; SItem *syllableItem; SItem *sylStructSylItem; SItem *segmentItem; SIterator *sylItr = NULL; SIterator *phoneItr = NULL; const SObject *phone; s_bool is_present; S_CLR_ERR(error); s_get_lexical_objects(self, utt, &g2p, &lexicon, &addendum, &syllab, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"s_get_lexical_objects\" failed")) goto quit_error; /* we require the word relation */ is_present = SUtteranceRelationIsPresent(utt, "Word", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SUtteranceRelationIsPresent\" failed")) goto quit_error; if (!is_present) { S_CTX_ERR(error, S_FAILURE, "Run", "Failed to find 'Word' relation in utterance"); goto quit_error; } wordRel = SUtteranceGetRelation(utt, "Word", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SUtteranceGetRelation\" failed")) goto quit_error; /* create relations */ syllableRel = SUtteranceNewRelation(utt, "Syllable", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SUtteranceNewRelation\" failed")) goto quit_error; sylStructRel = SUtteranceNewRelation(utt, "SylStructure", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SUtteranceNewRelation\" failed")) goto quit_error; segmentRel = SUtteranceNewRelation(utt, "Segment", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SUtteranceNewRelation\" failed")) goto quit_error; /* start at the first item in the word relation, cast away * const, we want to add daughter items. * iterate over the word relation and fill in the * phones and the associated structure. */ wordItem = (SItem*)SRelationHead(wordRel, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SRelationHead\" failed")) goto quit_error; while (wordItem != NULL) { /* get word and downcase it */ downcase_word = s_strlwr(s_strdup(SItemGetName(wordItem, error), error), error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Failed to down-case word item")) goto quit_error; if (downcase_word == NULL || s_strcmp(downcase_word, "", error) == 0) goto continue_cycle; phones = NULL; syllabified = FALSE; /* get phone sequence for word */ if (addendum != NULL) { phones = S_ADDENDUM_CALL(addendum, get_word)(addendum, downcase_word, NULL, &syllabified, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to method \"get_word\" (SAddendum) failed")) goto quit_error; } if ((phones == NULL) && (lexicon != NULL)) { phones = S_LEXICON_CALL(lexicon, get_word)(lexicon, downcase_word, NULL, &syllabified, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to method \"get_word\" (SLexicon) failed")) goto quit_error; } if ((phones == NULL) && (g2p != NULL)) { phones = S_G2P_CALL(g2p, apply)(g2p, downcase_word, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to method \"apply\" (SG2P) failed")) goto quit_error; } if (phones == NULL) { S_CTX_ERR(error, S_FAILURE, "Run", "Failed to get phone sequence for word '%s'", downcase_word); S_FREE(downcase_word); continue; } S_FREE(downcase_word); /* syllabify phone sequence */ if (syllabified == FALSE) { if (syllab != NULL) { syllablesPhones = S_SYLLABIFICATION_CALL(syllab, syllabify)(syllab, wordItem, phones, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to method \"syllabify\" failed")) goto quit_error; S_DELETE(phones, "Run", error); } else { syllablesPhones = S_LIST(S_NEW(SListList, error)); if (S_CHK_ERR(error, S_CONTERR, "Run", "Failed to create new 'SList' object")) goto quit_error; SListAppend(syllablesPhones, S_OBJECT(phones), error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SListAppend\" failed")) goto quit_error; } } else syllablesPhones = (SList*)phones; /* create new syllable structure word item, shares content * with word item. */ sylStructureWordItem = SRelationAppend(sylStructRel, wordItem, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SRelationAppend\" failed")) goto quit_error; /* iterate over syllables */ sylItr = S_ITERATOR_GET(syllablesPhones, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"S_ITERATOR_GET\" failed")) goto quit_error; while (sylItr != NULL) { /* new item in syllable relation */ syllableItem = SRelationAppend(syllableRel, NULL, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SRelationAppend\" failed")) goto quit_error; SItemSetName(syllableItem, "syl", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemSetName\" failed")) goto quit_error; /* daughter of above item, but in SylStructure */ sylStructSylItem = SItemAddDaughter(sylStructureWordItem, syllableItem, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemAddDaughter\" failed")) goto quit_error; /* iterate over phones and add segments */ phoneItr = S_ITERATOR_GET((SList*)SIteratorObject(sylItr, error), error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"S_ITERATOR_GET/SIteratorObject\" failed")) goto quit_error; while (phoneItr != NULL) { phone = SIteratorObject(phoneItr, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SIteratorObject\" failed")) goto quit_error; segmentItem = SRelationAppend(segmentRel, NULL, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SRelationAppend\" failed")) goto quit_error; SItemSetName(segmentItem, SObjectGetString(phone, error), error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemSetName/SObjectGetString\" failed")) goto quit_error; SItemAddDaughter(sylStructSylItem, segmentItem, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemAddDaughter\" failed")) goto quit_error; phoneItr = SIteratorNext(phoneItr); } sylItr = SIteratorNext(sylItr); } S_DELETE(syllablesPhones, "Run", error); continue_cycle: wordItem = SItemNext(wordItem, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemNext\" failed")) goto quit_error; } /* here all is OK */ return; /* error clean-up code */ quit_error: if (syllableRel != NULL) { SUtteranceDelRelation(utt, "Syllable", error); S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SUtteranceDelRelation\" failed"); } if (sylStructRel != NULL) { SUtteranceDelRelation(utt, "SylStructure", error); S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SUtteranceDelRelation\" failed"); } if (segmentRel != NULL) { SUtteranceDelRelation(utt, "Segment", error); S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SUtteranceDelRelation\" failed"); } if (sylItr != NULL) S_DELETE(sylItr, "Run", error); if (phoneItr != NULL) S_DELETE(phoneItr, "Run", error); self = NULL; }
static void s_compute_phonetic_features (SItem* word, s_erc *error ) { SItem *syllable; SItem * phone; char* position_in_syllable_string = NULL; /* Extract Phoneset from Voice*/ const SVoice* voice = SItemVoice (word, error); if (S_CHK_ERR(error, S_CONTERR, "s_compute_phonetic_features", "Call to \"SItemGetVoice\" failed")) return; const SPhoneset* phoneset = (SPhoneset*)SVoiceGetData(voice, "phoneset", error); if (S_CHK_ERR(error, S_CONTERR, "s_compute_phonetic_features", "Call to \"SVoiceGetData\" failed")) return; SItem *wordAsSylStructure = SItemAs(word, "SylStructure", error); if (S_CHK_ERR(error, S_CONTERR, "s_compute_stresses", "Call to \"SItemAs\" failed")) return; syllable = SItemDaughter(wordAsSylStructure, error); if (S_CHK_ERR(error, S_CONTERR, "s_compute_stresses", "Call to \"SItemDaughter\" failed")) return; while (syllable != NULL) { phone = SItemDaughter(syllable, error); if (S_CHK_ERR(error, S_CONTERR, "s_compute_phonetic_features", "Call to \"SItemDaughter\" failed")) return; s_bool nucleusFound = FALSE; while (phone != NULL) { const char* phone_value = SItemGetName(phone, error); if (S_CHK_ERR(error, S_CONTERR, "s_compute_phonetic_features", "Call to \"SItemGetName\" failed")) return; s_bool isVowel = S_PHONESET_CALL(phoneset, phone_has_feature) (phoneset, phone_value, "vowel", error); if (S_CHK_ERR(error, S_CONTERR, "s_compute_phonetic_features", "Call to \"phone_has_feature\" failed")) return; if( isVowel ) { nucleusFound = TRUE; position_in_syllable_string = "nucleus"; } else { if( nucleusFound == TRUE ) position_in_syllable_string = "coda"; else position_in_syllable_string = "onset"; } SItemSetString ( phone, "syllablepart", position_in_syllable_string, error ); if (S_CHK_ERR(error, S_CONTERR, "s_compute_phonetic_features", "Call to \"SItemSetString\" failed")) return; s_bool hasLong = S_PHONESET_CALL(phoneset, phone_has_feature) (phoneset, phone_value, "duration_long", error); if (S_CHK_ERR(error, S_CONTERR, "s_compute_phonetic_features", "Call to \"phone_has_feature\" failed")) return; s_bool hasShort = S_PHONESET_CALL(phoneset, phone_has_feature) (phoneset, phone_value, "duration_short", error); if (S_CHK_ERR(error, S_CONTERR, "s_compute_phonetic_features", "Call to \"phone_has_feature\" failed")) return; const char * feat = NULL; if( hasLong ) { feat = "+"; } else if( hasShort ) { feat = "-"; } if(feat != NULL) { SItemSetString ( phone, "duration", feat, error ); if (S_CHK_ERR(error, S_CONTERR, "s_compute_phonetic_features", "Call to \"SItemSetString\" failed")) return; } phone = SItemNext ( phone, error); if (S_CHK_ERR(error, S_CONTERR, "s_compute_phonetic_features", "Call to \"SItemNext\" failed")) return; } syllable = SItemNext (syllable, error); if (S_CHK_ERR(error, S_CONTERR, "s_compute_phonetic_features", "Call to \"SItemNext\" failed")) return; } }
/* 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 SObject *Run(const SFeatProcessor *self, const SItem *item, s_erc *error) { SObject *extractedFeat = NULL; const SUtterance *utt; const SRelation *sylRel; const SItem *itr; sint32 count; S_CLR_ERR(error); if (item == NULL) return NULL; /* get utterance */ utt = SItemUtterance(item, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemUtterance\" failed")) goto quit_error; if (utt == NULL) return NULL; /* get Syllable relation */ sylRel = SUtteranceGetRelation(utt, "Syllable", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SUtteranceGetRelation\" failed")) goto quit_error; if (sylRel == NULL) return NULL; itr = SRelationHead(sylRel, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SRelationHead\" failed")) goto quit_error; count = 0; while (itr != NULL) { count++; itr = SItemNext(itr, 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; /* error cleanup */ quit_error: if (extractedFeat != NULL) S_DELETE(extractedFeat, "Run", error); return NULL; S_UNUSED(self); }
static void read_relation_items(SEbmlRead *ebmlReader, SRelation *rel, s_hash_table *items_content_table, s_erc *error) { uint32 id; SItem *item = NULL; s_hash_table *relation_items = NULL; s_bool container_exhausted; S_CLR_ERR(error); /* read S_UTT_EBML_RELATION_ITEMS container */ id = S_EBMLREAD_CALL(ebmlReader, container)(ebmlReader, error); if (S_CHK_ERR(error, S_CONTERR, "read_relation_items", "ebmlRead method \"container\" failed")) goto quit_error; /* 2^8 = 256 items */ relation_items = s_hash_table_new(&relation_items_table_free_fp, 8, error); if (S_CHK_ERR(error, S_CONTERR, "read_relation_items", "Call to \"relation_items\" failed")) goto quit_error; while (1) { container_exhausted = S_EBMLREAD_CALL(ebmlReader, container_at_end)(ebmlReader, error); if (S_CHK_ERR(error, S_CONTERR, "read_relation_items", "ebmlRead method \"container_at_end\" failed")) goto quit_error; if (container_exhausted) { SItem *current; SItem *next; /* connect relation head and tail items * find the first item. */ current = get_item(relation_items, 1, error); if (S_CHK_ERR(error, S_CONTERR, "read_relation_items", "Call to \"get_item\" failed")) goto quit_error; rel->head = current; /* find the last item*/ while (current != NULL) { next = SItemNext(current, error); if (S_CHK_ERR(error, S_CONTERR, "read_relation_items", "Call to \"SItemNext\" failed")) goto quit_error; if (next == NULL) { rel->tail = current; break; } current = next; } s_hash_table_delete(relation_items, error); S_CHK_ERR(error, S_CONTERR, "read_relation_items", "Call to \"s_hash_table_delete\" failed"); break; } /* peek id */ id = S_EBMLREAD_CALL(ebmlReader, peek_id)(ebmlReader, error); if (S_CHK_ERR(error, S_CONTERR, "read_relation_items", "ebmlRead method \"peek_id\" failed")) goto quit_error; switch(id) { case S_UTT_EBML_RELATION_ITEM_NODE: { uint32 item_node_number; item_node_number = S_EBMLREAD_CALL(ebmlReader, read_uint)(ebmlReader, &id, error); if (S_CHK_ERR(error, S_CONTERR, "read_relation_items", "ebmlRead method \"read_uint\" failed")) goto quit_error; /* find the item. */ item = get_item(relation_items, item_node_number, error); if (S_CHK_ERR(error, S_CONTERR, "read_relation_items", "Call to \"get_item\" failed")) goto quit_error; item->relation = rel; break; } case S_UTT_EBML_RELATION_ITEM_CONTENT_ID: { uint32 item_content_id; const s_hash_element *he; s_items_content_container *ic; item_content_id = S_EBMLREAD_CALL(ebmlReader, read_uint)(ebmlReader, &id, error); if (S_CHK_ERR(error, S_CONTERR, "read_relation_items", "ebmlRead method \"read_uint\" failed")) goto quit_error; /* find the item content */ he = s_hash_table_find(items_content_table, (void*)&item_content_id, sizeof(uint32), error); if (S_CHK_ERR(error, S_CONTERR, "read_relation_items", "Call to \"s_hash_table_find\" failed")) goto quit_error; if (he == NULL) { S_CTX_ERR(error, S_FAILURE, "read_relation_items", "Failed to find item content for read content id"); goto quit_error; } ic = (s_items_content_container *)s_hash_element_get_data(he, error); if (S_CHK_ERR(error, S_CONTERR, "read_relation_items", "Call to \"s_hash_element_get_data\" failed")) goto quit_error; item->content = ic->content; ic->used = 1; /* add the item in item's content relations. This is for * the SItemAs and SItemIn item functions. */ SItmContentAdd(item->content, rel->name, item, error); if (S_CHK_ERR(error, S_CONTERR, "read_relation_items", "Call to \"SItmContentAdd\" failed")) goto quit_error; break; } case S_UTT_EBML_RELATION_ITEM_UP_NODE: { uint32 item_node_number; SItem *itemParent; item_node_number = S_EBMLREAD_CALL(ebmlReader, read_uint)(ebmlReader, &id, error); if (S_CHK_ERR(error, S_CONTERR, "read_relation_items", "ebmlRead method \"read_uint\" failed")) goto quit_error; /* find the item. */ itemParent = get_item(relation_items, item_node_number, error); if (S_CHK_ERR(error, S_CONTERR, "read_relation_items", "Call to \"get_item\" failed")) goto quit_error; item->up = itemParent; break; } case S_UTT_EBML_RELATION_ITEM_DOWN_NODE: { uint32 item_node_number; SItem *itemDaughter; item_node_number = S_EBMLREAD_CALL(ebmlReader, read_uint)(ebmlReader, &id, error); if (S_CHK_ERR(error, S_CONTERR, "read_relation_items", "ebmlRead method \"read_uint\" failed")) goto quit_error; /* find the item. */ itemDaughter = get_item(relation_items, item_node_number, error); if (S_CHK_ERR(error, S_CONTERR, "read_relation_items", "Call to \"get_item\" failed")) goto quit_error; item->down = itemDaughter; break; } case S_UTT_EBML_RELATION_ITEM_NEXT_NODE: { uint32 item_node_number; SItem *itemNext; item_node_number = S_EBMLREAD_CALL(ebmlReader, read_uint)(ebmlReader, &id, error); if (S_CHK_ERR(error, S_CONTERR, "read_relation_items", "ebmlRead method \"read_uint\" failed")) goto quit_error; /* find the item. */ itemNext = get_item(relation_items, item_node_number, error); if (S_CHK_ERR(error, S_CONTERR, "read_relation_items", "Call to \"get_item\" failed")) goto quit_error; item->next = itemNext; break; } case S_UTT_EBML_RELATION_ITEM_PREV_NODE: { uint32 item_node_number; SItem *itemPrev; item_node_number = S_EBMLREAD_CALL(ebmlReader, read_uint)(ebmlReader, &id, error); if (S_CHK_ERR(error, S_CONTERR, "read_relation_items", "ebmlRead method \"read_uint\" failed")) goto quit_error; /* find the item. */ itemPrev = get_item(relation_items, item_node_number, error); if (S_CHK_ERR(error, S_CONTERR, "read_relation_items", "Call to \"get_item\" failed")) goto quit_error;; item->prev = itemPrev; break; } default: /* unknown elements, skip */ S_WARNING(S_FAILURE, "read_relation_items", "Skipping element with unknown id '0x%x'", id); S_EBMLREAD_CALL(ebmlReader, element_skip)(ebmlReader, error); if (S_CHK_ERR(error, S_CONTERR, "read_relation_items", "ebmlRead method \"element_skip\" failed")) goto quit_error; } } /* here everything went OK */ return; /* errors here, clean up code */ quit_error: /* disconnect items from relation */ rel->head = NULL; rel->tail = NULL; /* delete them, can't use normal s_hash_table_delete * because the delete function pointer * (relation_items_table_free_fp) does not delete the items. */ if (relation_items != NULL) { s_hash_element *hte; s_hash_element *next; s_erc local_err = S_SUCCESS; hte = (s_hash_element *)s_hash_table_first(relation_items, &local_err); if (S_CHK_ERR(&local_err, S_CONTERR, "read_relation_items", "Call to \"s_hash_table_first\" failed")) return; while (hte != NULL) { SItem *item; uint32 *item_node_number; next = (s_hash_element *)s_hash_element_next(hte, &local_err); if (S_CHK_ERR(&local_err, S_CONTERR, "read_relation_items", "Call to \"s_hash_element_next\" failed")) next = NULL; item_node_number = (uint32 *)s_hash_element_key(hte, &local_err); if (!S_CHK_ERR(&local_err, S_CONTERR, "read_relation_items", "Call to \"s_hash_element_key\" failed")) S_FREE(item_node_number); item = (SItem *)s_hash_element_get_data(hte, &local_err); if (!S_CHK_ERR(&local_err, S_CONTERR, "read_relation_items", "Call to \"s_hash_element_get_data\" failed")) S_FORCE_DELETE(item, "read_relation_items", &local_err); s_hash_element_unlink(hte, &local_err); S_CHK_ERR(&local_err, S_CONTERR, "read_relation_items", "Call to \"s_hash_element_unlink\" failed"); hte = next; } s_hash_table_delete(relation_items, &local_err); S_CHK_ERR(&local_err, S_CONTERR, "read_relation_items", "Call to \"s_hash_table_delete\" failed"); } }
static void copy_lpc_residuals(SRelp *self, const SRelation *unitRel, s_erc *error) { const SItem *unit; SItem *sourceUnit; STrackFloat *sourceTrack; SArrayInt *sourceResidual; uint32 i; uint32 j; float prev_time = 0.0; S_CLR_ERR(error); unit = SRelationHead(unitRel, error); if (S_CHK_ERR(error, S_CONTERR, "copy_lpc_residuals", "Call to \"SRelationHead\" failed")) return; i = 0; while (unit != NULL) { sourceUnit = (SItem*)SItemGetObject(unit, "source-unit", error); if (S_CHK_ERR(error, S_CONTERR, "copy_lpc_residuals", "Call to \"SItemGetObject\" failed")) return; sourceTrack = (STrackFloat*)SItemGetObject(sourceUnit, "lpc-coefs", error); if (S_CHK_ERR(error, S_CONTERR, "copy_lpc_residuals", "Call to \"SItemGetObject\" failed")) return; sourceResidual = (SArrayInt*)SItemGetObject(sourceUnit, "residuals", error); if (S_CHK_ERR(error, S_CONTERR, "copy_lpc_residuals", "Call to \"SItemGetObject\" failed")) return; window_residuals(self, sourceResidual, sourceTrack, i, error); if (S_CHK_ERR(error, S_CONTERR, "copy_lpc_residuals", "Call to \"window_residuals\" failed")) return; for (j = 0; j < sourceTrack->data->row_count; j++, i++) { self->track->data->f[i] = S_CALLOC(float, self->num_channels); if (self->track->data->f[i] == NULL) { S_FTL_ERR(error, S_MEMERROR, "copy_lpc_residuals", "Failed to allocate memory for 'float' object"); return; } memcpy(self->track->data->f[i], sourceTrack->data->f[j], self->num_channels * sizeof(float)); self->track->time[i] = sourceTrack->time[j] + prev_time; } prev_time = self->track->time[i - 1]; SItemSetFloat(sourceUnit, "source_end", prev_time, error); if (S_CHK_ERR(error, S_CONTERR, "copy_lpc_residuals", "Call to \"SItemSetFloat\" failed")) return; SItemSetInt(sourceUnit, "num_frames", sourceTrack->data->row_count, error); if (S_CHK_ERR(error, S_CONTERR, "copy_lpc_residuals", "Call to \"SItemSetInt\" failed")) return; unit = SItemNext(unit, error); if (S_CHK_ERR(error, S_CONTERR, "copy_lpc_residuals", "Call to \"SItemNext\" failed")) return; } }
static SObject *Run(const SFeatProcessor *self, const SItem *item, s_erc *error) { SObject *extractedFeat = NULL; const SItem *itemInSylStructRel; const SItem *syllableItem; const SItem *itr; sint32 count; S_CLR_ERR(error); if (item == NULL) return NULL; itemInSylStructRel = SItemAs(item, "SylStructure", error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemAs\" failed")) goto quit_error; if (itemInSylStructRel == NULL) { extractedFeat = SObjectSetInt(0, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SObjectSetInt\" failed")) goto quit_error; return extractedFeat; } syllableItem = SItemParent(itemInSylStructRel, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemParent\" failed")) goto quit_error; if (syllableItem == NULL) { extractedFeat = SObjectSetInt(0, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SObjectSetInt\" failed")) goto quit_error; return extractedFeat; } itr = SItemDaughter(syllableItem, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemDaughter\" failed")) goto quit_error; count = 0; while (itr != NULL) { s_bool is_equal; is_equal = SItemEqual(itr, itemInSylStructRel, error); if (S_CHK_ERR(error, S_CONTERR, "Run", "Call to \"SItemEqual\" failed")) goto quit_error; if (is_equal) break; count++; itr = SItemNext(itr, 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; /* error cleanup */ quit_error: if (extractedFeat != NULL) S_DELETE(extractedFeat, "Run", error); return NULL; S_UNUSED(self); }
/** * Prepare the structure for the next call to hunpos. * @private * * @param relation_head starting SItem for this new phrase. It must be a Phrase SItem if @p is_phrase_present is true, a Token otherwise. * @param data data structure to fill with new SItem pointers. * @param is_phrase_present tells if we're using phrases or directly tokens * @param error Error code. * */ static void call_hunpos(const SHunposUttProc *hunposProc, const SItem* relation_head, const SItem** data, s_bool is_phrase_present, s_erc *error) { const SItem* phrase_start_item; const SItem* current_token; const SItem* start_token; /* start point of the next phrase */ const SItem* last_safe_cut_token; /* last token from where we can cut for the next phrase */ int last_safe_cut_count; int tokens_count; S_CLR_ERR(error); phrase_start_item = relation_head; /* we already have the starting item if we have only tokens, else grab the token */ start_token = phrase_start_item; if (is_phrase_present) { /* go grab the first token of this phrase */ start_token = SItemPathToItem(relation_head, "R:Phrase.daughter.R:Token.parent", error); if (S_CHK_ERR(error, S_CONTERR, "call_hunpos", "Call to \"SItemPathToItem\" failed")) return; } current_token = start_token; /* for each phrase */ while (current_token != NULL) { tokens_count = 0; last_safe_cut_count = 0; start_token = current_token; last_safe_cut_token = current_token; /* we stop collecting tokens if the next item is null or if we reached the maximum number of tokens allowed */ while (current_token != NULL && tokens_count < hunposProc->max_tokens_number) { data[tokens_count] = current_token; /* check if it's a safe cut point */ s_bool is_present = SItemFeatureIsPresent(current_token, "IsPunctuation", error); if (S_CHK_ERR(error, S_CONTERR, "call_hunpos", "Call to \"SItemFeatureIsPresent\" failed")) return; if (is_present) { /* is it a punctiation token? */ sint32 is_punctuation = SItemGetInt(current_token, "IsPunctuation", error); if (is_punctuation > 0) { last_safe_cut_token = current_token; last_safe_cut_count = tokens_count + 1; } } current_token = SItemNext(current_token, error); if (S_CHK_ERR(error, S_CONTERR, "call_hunpos", "Call to \"SItemNext\" failed")) return; /* if we're using phrases, check if the current phrase finished */ if (is_phrase_present) { if (current_token != NULL) { const SItem* next_token_parent = SItemPathToItem(current_token, "R:Token.daughter.R:Phrase.parent", error); if (S_CHK_ERR(error, S_CONTERR, "call_hunpos", "Call to \"SItemPathToItem\" failed")) return; if (next_token_parent != phrase_start_item) { /* save the next phrase start and stop the cycle*/ phrase_start_item = next_token_parent; start_token = current_token; current_token = NULL; } } else { phrase_start_item = NULL; start_token = NULL; } } tokens_count++; } /* do we need to cut it? */ if (current_token != NULL && last_safe_cut_token != start_token) { tokens_count = last_safe_cut_count; current_token = SItemNext(last_safe_cut_token, error); if (S_CHK_ERR(error, S_CONTERR, "call_hunpos", "Call to \"SItemNext\" failed")) return; } /* do the tagging */ int hunpos_error = 0; hunpos_tagger_tag(hunposProc->hunpos_instance, tokens_count, data, &read_token, data, &set_tag, &hunpos_error); if (hunpos_error !=0) { S_CTX_ERR(error, S_FAILURE, "call_hunpos", "Call to \"hunpos_tagger_tag\" failed"); return; } /* if we're using phrases, go on with the next one */ if (is_phrase_present && current_token == NULL) current_token = start_token; } return; }