Previous topic

SDatasource Example

Next topic

Python Logging Configuration

Heterogeneous Relation Graphs ExampleΒΆ

This code example recreates the HRG structure in figure 4. It can also be found at speect/swig/python/examples/hrg_docs_example.py.

 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
lexical_info = {
    "twenty": [ "t", "w", "eh", "n", "1", "t", "iy", "0"],
    "fifth" : [  "f", "@", "th", "1" ]
    }
    

import speect
import speect.utt_ebml # load the utt-ebml plug-in


# create a new utterance
utt = speect.SUtterance()

# create a new "Word" relation
word_rel = utt.relation_new("Word")

# create a new "Syllable" relation
syl_rel = utt.relation_new("Syllable")

# create a new "Segment" relation
segment_rel = utt.relation_new("Segment")

# create a new "SylStructure" relation
sylstruct_rel = utt.relation_new("SylStructure")

for word in lexical_info:

    # add word in word relation
    word_item = word_rel.append()

    # set it's name feature
    word_item["name"] = word

    # add word in syllable structure relation,
    # note the shared content word_item
    sylstruct_word_item = sylstruct_rel.append(word_item)
    
    # create a syllable item in the syllable relation
    syl_item = syl_rel.append()

    # syllable in syllable structure relation is daughter of
    # word item in syllable structure relation, note
    # shared content with syllable item
    sylstruct_syl_item = sylstruct_word_item.add_daughter(syl_item)
    
    phone_counter = 0

    for phone in lexical_info[word]:
        if not phone.isdigit():
            # create a segment item in the segment relation
            seg_item = segment_rel.append()
            seg_item["name"] = phone

            # segment item is daughter of syllable item in
            # syllable structure relation
            sylstruct_syl_item.add_daughter(seg_item)

        else:
            syl_item["stress"] = int(phone)

            if phone_counter < len(lexical_info[word]) - 1:
                # create a syllable item in the syllable relation
                syl_item = syl_rel.append()

                # syllable in syllable structure relation is daughter of
                # word item in syllable structure relation, note
                # shared content with syllable item
                sylstruct_syl_item = sylstruct_word_item.add_daughter(syl_item)

        phone_counter += 1
    

# print utterance
print(utt)

# save utterance
utt.save_ebml("test.utt")

del utt