Previous topic

Examples

Next topic

Error handling and Debugging Example

Heterogeneous Relation Graphs ExampleΒΆ

This code example recreates the HRG structure in figure 4. It can also be found at speect/engine/examples/hrg/create_docs_example.c, and be compiled with the WANT_EXAMPLES build option.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <stdio.h>
#include "speect.h"


/* name of utterance ebml read/write plug-in */
static const char *utt_plugin_path = "utt_ebml.spi";

/* structure to hold word-syllable combinations */
typedef struct
{
    const char *word;
    const char * const * syllables;
} word_syls;

# define num_words 2

/*
 * phone strings with syllable boundaries marked by a digit that
 * indicates syllable stress
 */
static const char * const syllable_0[] = { "t", "w", "eh", "n", "1", "t", "iy", "0", NULL };
static const char * const syllable_1[] = { "f", "@", "th", "1", NULL };

/* word - syllables */
static const word_syls lexical_info[] = {
    { "twenty", syllable_0 },
    { "fifth", syllable_1 },
    { NULL, NULL }
};



int main()
{
    s_erc error = S_SUCCESS;
    SUtterance *utt;
    SRelation *wordRel;
    SRelation *sylRel;
    SRelation *segmentRel;
    SRelation *sylStructRel;
    SItem *wordItem;
    SItem *sylItem;
    SItem *segmentItem;
    SItem *sylStructWordItem;
    SItem *sylStructSylItem;
    int i;
    int j;
    SPlugin *uttEBMLplugin = NULL;



    /*
     * initialize speect
     */
    error = speect_init(NULL);
    if (error != S_SUCCESS)
    {
        printf("Failed to initialize Speect\n");
        return 1;
    }

    /*
     * load the utt-ebml plug-in
     */
    uttEBMLplugin = s_pm_load_plugin(utt_plugin_path, &error);
    if (S_CHK_ERR(&error, S_CONTERR,
                  "main",
                  "Failed to load plug-in at '%s'",utt_plugin_path))
    {
        printf("Failed to load plug-in\n");
        goto quit;
    }

    /* Create a new utterance */
    utt = S_NEW(SUtterance, &error);
    if (S_CHK_ERR(&error, S_CONTERR,
                  "main",
                  "Failed to create new utterance"))
        goto quit;

    /* initialize utterance */
    SUtteranceInit(&utt, NULL, &error);
    if (S_CHK_ERR(&error, S_CONTERR,
                  "main",
                  "Failed to initialize new utterance"))
        goto quit;

    /* Create a word relation  */
    wordRel = SUtteranceNewRelation(utt, "Word", &error);
    if (S_CHK_ERR(&error, S_CONTERR,
                  "main",
                  "Failed to create new relation"))
        goto quit;

    /* Create a syllable relation  */
    sylRel = SUtteranceNewRelation(utt, "Syllable", &error);
    if (S_CHK_ERR(&error, S_CONTERR,
                  "main",
                  "Failed to create new relation"))
        goto quit;

    /* Create a segment relation  */
    segmentRel = SUtteranceNewRelation(utt, "Segment", &error);
    if (S_CHK_ERR(&error, S_CONTERR,
                  "main",
                  "Failed to create new relation"))
        goto quit;

    /* Create a syllable structure relation  */
    sylStructRel = SUtteranceNewRelation(utt, "SylStructure", &error);
    if (S_CHK_ERR(&error, S_CONTERR,
                  "main",
                  "Failed to create new relation"))
        goto quit;

    for (i = 0; lexical_info[i].word; i++)
    {
        /* add word in word relation */
        wordItem = SRelationAppend(wordRel, NULL, &error);
        if (S_CHK_ERR(&error, S_CONTERR,
                      "main",
                      "Failed to create new appended item"))
            goto quit;

        SItemSetName(wordItem, lexical_info[i].word, &error);
        if (S_CHK_ERR(&error, S_CONTERR,
                      "main",
                      "Failed to set item name"))
            goto quit;

        /* add word in syllable structure relation,
         * note the shared content wordItem
         */
        sylStructWordItem = SRelationAppend(sylStructRel, wordItem, &error);

        /* create a syllable item in the syllable relation */
        sylItem = SRelationAppend(sylRel, NULL, &error);
        if (S_CHK_ERR(&error, S_CONTERR,
                      "main",
                      "Failed to create new appended item"))
            goto quit;

        /* syllable in syllable structure relation is daughter of
         * word item in syllable structure relation, note
         * shared content with syllable item */
        sylStructSylItem = SItemAddDaughter(sylStructWordItem, sylItem, &error);
        if (S_CHK_ERR(&error, S_CONTERR,
                      "main",
                      "Failed to add daughter item of item"))
            goto quit;

        for (j = 0; lexical_info[i].syllables[j]; j++)
        {
            int isdigit;
            uint32 c;
            const char *phone = lexical_info[i].syllables[j];
            int stress;

            c = s_getc(phone, &error);
            if (S_CHK_ERR(&error, S_CONTERR,
                          "main",
                          "Failed to get first character of phone as UTF8 character"))
                goto quit;

            isdigit = s_isdigit(c, &error);
            if (S_CHK_ERR(&error, S_CONTERR,
                          "main",
                          "Failed to determine if UTF8 character is a digit"))
                goto quit;

            if (!isdigit)
            {
                /* create a segment item in the segment relation */
                segmentItem = SRelationAppend(segmentRel, NULL, &error);
                if (S_CHK_ERR(&error, S_CONTERR,
                              "main",
                              "Failed to create new appended item"))
                    goto quit;

                SItemSetName(segmentItem, phone, &error);
                if (S_CHK_ERR(&error, S_CONTERR,
                              "main",
                              "Failed to set item name"))
                    goto quit;

                /* segment item is daughter of syllable item in
                   syllable structure relation */
                SItemAddDaughter(sylStructSylItem, segmentItem, &error);
                if (S_CHK_ERR(&error, S_CONTERR,
                              "main",
                              "Failed to add daughter item of item"))
                    goto quit;
            }
            else
            {
                /* get stress value */
                stress = (int)s_strtol(phone, NULL, 10, &error);
                if (S_CHK_ERR(&error, S_CONTERR,
                              "main",
                              "Failed to convert string to integer"))
                    goto quit;

                SItemSetInt(sylItem, "stress", stress, &error);
                if (S_CHK_ERR(&error, S_CONTERR,
                              "main",
                              "Failed to set item integer feature"))
                    goto quit;

                if (lexical_info[i].syllables[j + 1] != NULL)
                {
                    /* create a syllable item in the syllable relation */
                    sylItem = SRelationAppend(sylRel, NULL, &error);
                    if (S_CHK_ERR(&error, S_CONTERR,
                                  "main",
                                  "Failed to create new appended item"))
                        goto quit;

                    /* syllable in syllable structure relation is daughter of
                     * word item in syllable structure relation, note
                     * shared content with syllable item */
                    sylStructSylItem = SItemAddDaughter(sylStructWordItem, sylItem, &error);
                    if (S_CHK_ERR(&error, S_CONTERR,
                                  "main",
                                  "Failed to add daughter item of item"))
                        goto quit;
                }
            }
        }
    }

    /* save utterance */
    SObjectSave(S_OBJECT(utt), "test.utt", "spct_utt", &error);
    if (S_CHK_ERR(&error, S_CONTERR,
                  "main",
                  "Failed to save utterance"))
        goto quit;


quit:
    /*
     * utterance will delete the
     * relation which in turn will
     * delete it's items.
     */
    if (utt != NULL)
        S_DELETE(utt, "main", &error);

    /* unload plug-ins by deleting them */
    if (uttEBMLplugin != NULL)
        S_DELETE(uttEBMLplugin, "main", &error);

    /*
     * quit speect
     */
    error = speect_quit();
    if (error != S_SUCCESS)
    {
        printf("Call to 'speect_quit' failed\n");
        return 1;
    }

    return 0;
}