Exemple #1
0
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;
}
Exemple #2
0
static const char* removeDoubles ( const char* string, s_erc *error)
{
	char* result = NULL;
	char tmp[7];
	char tmp2[7];
	int i = 0;

	if (string == NULL)
		return NULL;

	while (string[i] != '\0')
	{
		/*check if next char is the same of the actual char, if so it skips it
		 * and check for the next one ( care for the UTF8 format )
		 */
		s_strncpy (tmp, string +i, 1, error);
		if (S_CHK_ERR(error, S_CONTERR,
				  "removeDoubles",
				  "Call to \"s_strncpy\" failed"))
			return NULL;

		s_strncpy (tmp2, string +i, 1, error);
		if (S_CHK_ERR(error, S_CONTERR,
				  "removeDoubles",
				  "Call to \"s_strncpy\" failed"))
			return NULL;

		int j = s_width(tmp2, error);
		if (S_CHK_ERR(error, S_CONTERR,
				  "removeDoubles",
				  "Call to \"s_width\" failed"))
			return NULL;

		while ( string[i] == string[i+j] )
		{
			s_strncpy (tmp2, string +i, 1, error);
			if (S_CHK_ERR(error, S_CONTERR,
					  "removeDoubles",
					  "Call to \"s_strncpy\" failed"))
				return NULL;
			j += s_width(tmp2, error);
		}

		s_sappend (&result, tmp, error);
		if (S_CHK_ERR(error, S_CONTERR,
				  "removeDoubles",
				  "Call to \"s_sappend\" failed"))
			return NULL;

		i += j;
		/*next char that is not repeated*/
	}

	result = s_strlwr( result, error );
	if (S_CHK_ERR(error, S_CONTERR,
			  "removeDoubles",
			  "Call to \"s_strlwr\" failed"))
		return NULL;

	return result;

}