~ubuntu-branches/ubuntu/saucy/sphinxtrain/saucy

« back to all changes in this revision

Viewing changes to python/cmusphinx/corpus.py

  • Committer: Package Import Robot
  • Author(s): Samuel Thibault
  • Date: 2013-01-02 04:10:21 UTC
  • Revision ID: package-import@ubuntu.com-20130102041021-ynsizmz33fx02hea
Tags: upstream-1.0.8
ImportĀ upstreamĀ versionĀ 1.0.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2007 Carnegie Mellon University
 
2
#
 
3
# You may copy and modify this freely under the same terms as
 
4
# Sphinx-III
 
5
 
 
6
"""Corpus classes for acoustic model training.
 
7
 
 
8
This module provides classes for representing a corpus of utterances
 
9
for acoustic modeling.  The Corpus class implements the iterator
 
10
protocol, acting as a list of Utterance objects.
 
11
"""
 
12
__author__ = "David Huggins-Daines <dhuggins@cs.cmu.edu>"
 
13
__version__ = "$Revision: 10058 $"
 
14
 
 
15
import os
 
16
 
 
17
class Resource(object):
 
18
    """Resource associated with an utterance in a speech corpus.
 
19
 
 
20
    Any utterance has an arbitrary set of resources associated with
 
21
    it.  These are things such as waveforms, acoustic feature files,
 
22
    transcriptions and other forms of supervision, etc.
 
23
    """
 
24
    pass
 
25
 
 
26
class FileResourceIterator(object):
 
27
    """
 
28
    Iterator over items in a FileResource.
 
29
    """
 
30
    def __init__(self, resource):
 
31
        self.res = resource
 
32
        self.ctl = iter(resource.ctl_file)
 
33
 
 
34
    def next(self):
 
35
        # This will raise StopIteration for us at EOF
 
36
        entry = self.ctl.next()
 
37
        if isinstance(entry, CtlEntry):
 
38
            path = os.path.join(self.res.base_dir, entry.fileid + self.res.file_ext)
 
39
        else:
 
40
            path = os.path.join(self.res.base_dir, entry + self.res.file_ext)
 
41
        if self.res.data_type:
 
42
            return self.res.data_type(path)
 
43
        else:
 
44
            return path
 
45
 
 
46
class FileResource(Resource):
 
47
    def __init__(self, ctl_file, base_dir, file_ext, data_type=None):
 
48
        """
 
49
        Initialize a file-based resource.
 
50
        @param ctl_file: Control file resource on which this is based
 
51
        @ptype ctl_file: iterator(CtlEntry)
 
52
        @param base_dir: Base directory to prepend to control entries
 
53
        @param file_ext: Filename extension to append to control entries
 
54
        @param data_type: Class to construct from entries.
 
55
        @ptype data_type: type
 
56
        """
 
57
        self.ctl_file = ctl_file
 
58
        self.base_dir = base_dir
 
59
        self.file_ext = file_ext
 
60
        self.data_type = data_type
 
61
 
 
62
    def __iter__(self):
 
63
        return FileResourceIterator(self)
 
64
 
 
65
class CtlEntry(object):
 
66
    """Entry in a control file"""
 
67
    def __init__(self, str):
 
68
        fields = str.split()
 
69
        if len(fields) == 4:
 
70
            self.fileid, self.sf, self.ef, self.uttid = fields
 
71
            self.sf = int(self.sf)
 
72
            self.ef = int(self.ef)
 
73
        else:
 
74
            self.fileid = self.uttid = str
 
75
            self.sf = 0
 
76
            self.ef = -1
 
77
 
 
78
class ListResourceIterator(object):
 
79
    """
 
80
    Iterator over items in a ListResource.
 
81
    """
 
82
    def __init__(self, resource):
 
83
        self.fh = open(resource.file_name)
 
84
        self.data_type = resource.data_type
 
85
 
 
86
    def next(self):
 
87
        spam = self.fh.readline()
 
88
        if spam == "":
 
89
            raise StopIteration
 
90
        if self.data_type:
 
91
            return self.data_type(spam.rstrip())
 
92
        else:
 
93
            return spam.rstrip()
 
94
 
 
95
class ListResource(Resource):
 
96
    """
 
97
    Corpus resource consisting of lines in a text file, of some data
 
98
    type.  This includes things like control and transcript files.
 
99
    """
 
100
    def __init__(self, file_name, data_type=None):
 
101
        """
 
102
        Initialize a listing-based resource.
 
103
 
 
104
        If no data_type argument is specified, each item in the list
 
105
        will be returned as a string.
 
106
        
 
107
        @param file_name: File to read resource from
 
108
        @ptype file_name: string
 
109
        @param data_type: Class implementing the data type of each item
 
110
        @ptype data_type: type
 
111
        """
 
112
        self.data_type = data_type
 
113
        self.file_name = file_name
 
114
 
 
115
    def __iter__(self):
 
116
        return ListResourceIterator(self)
 
117
 
 
118
class CorpusIterator(object):
 
119
    """
 
120
    Iterator over elements in a Corpus.
 
121
    """
 
122
    def __init__(self, corpus, part=1, npart=1):
 
123
        self.corpus = corpus
 
124
        self.iters = {}
 
125
        if npart > 1:
 
126
            pass
 
127
        else:
 
128
            for k, v in corpus.resources.iteritems():
 
129
                self.iters[k] = iter(v)
 
130
 
 
131
    def next(self):
 
132
        utt = {}
 
133
        for k,v in self.iters.iteritems():
 
134
            utt[k] = v.next()
 
135
        return utt
 
136
 
 
137
class Corpus(object):
 
138
    """Corpus of speech data."""
 
139
    def __init__(self, ctl_file):
 
140
        self.ctl = ListResource(ctl_file, CtlEntry)
 
141
        self.resources = { 'ctl' : self.ctl }
 
142
 
 
143
    def __iter__(self):
 
144
        return CorpusIterator(self)
 
145
 
 
146
    def add_resource(self, name, res):
 
147
        self.resources[name] = res