1 """Base classes for grammarless tonal space models.
2
3 """
4 """
5 ============================== License ========================================
6 Copyright (C) 2008, 2010-12 University of Edinburgh, Mark Granroth-Wilding
7
8 This file is part of The Jazz Parser.
9
10 The Jazz Parser is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14
15 The Jazz Parser is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with The Jazz Parser. If not, see <http://www.gnu.org/licenses/>.
22
23 ============================ End license ======================================
24
25 """
26 __author__ = "Mark Granroth-Wilding <mark.granroth-wilding@ed.ac.uk>"
27
28 import os
29 from jazzparser import settings
30 from jazzparser.taggers.models import TaggerModel
31 from jazzparser.utils.options import ModuleOption
32 from jazzparser.data.input import detect_input_type
33 from jazzparser.utils.loggers import create_plain_stderr_logger
36 """
37 A model to be used by a L{BackoffBuilder}.
38
39 The model-handling interface is inherited directly from
40 L{TaggerModel<jazzparser.taggers.models.TaggerModel>} and simply
41 stores the models to a different location.
42
43 @note: if you're subclassing this, take a look at the
44 L{TaggerModel<jazzparser.taggers.models.TaggerModel>} interface
45 for requirements.
46
47 """
48 @classmethod
53
56 """
57 Defines the interface and common functions for models that assign
58 a semantics directly to an input sequence.
59
60 The evaluation interface is similar to the
61 L{Tagger<jazzparser.taggers.Tagger>} interface.
62
63 """
64
65 BUILDER_OPTIONS = []
66
67 - def __init__(self, input, options={}, logger=None):
84
85 @classmethod
88
89 @property
92
94 return type(self).__module__.rpartition(".")[2]
95 name = property(_get_name)
96
105 input_length = property(_get_input_length)
106
108 """
109 This is the main interface method.
110
111 @type rank: int
112 @param rank: the rank of the path the get, where 0 is the
113 highest ranked path
114
115 @rtype: L{jazzparser.formalisms.base.semantics.lambdacalc.Semantics}
116 subclass instance
117 @return: the C{rank}th highest ranked path through the tonal
118 space for this sequence. Returns C{None} if there is no
119 path with this rank.
120
121 """
122 raise NotImplementedError, "called get_tonal_space_path() on "\
123 "base BackoffBuilder instance."
124
126 """
127 Gets a list of all the tonal space paths, highest rank first.
128 Just a convenience method to get all the paths using
129 L{get_tonal_space_path} for every rank (self.num_paths).
130
131 """
132 return [self.get_tonal_space_path(i) for i in range(self.num_paths)]
133
136 """
137 Subclass of L{BackoffBuilder} that handles model loading.
138
139 """
140 MODEL_CLASS = None
141
142 BUILDER_OPTIONS = BackoffBuilder.BUILDER_OPTIONS + [
143 ModuleOption('model', filter=str,
144 help_text="Model name. This model must have been previously trained. Required",
145 usage="model=X, where X is the name of a trained model",
146 required=True),
147 ModuleOption('partition', filter=int,
148 help_text="If given, the numbered partition of the partitioned "\
149 "model will be used. (This generally involves appending the "\
150 "partition number to the model name.)",
151 usage="partition=P, where P is an int",
152 default=None
153 ),
154 ]
155
172
173 @staticmethod
175 """
176 The model name to use when the given partition number is requested.
177 The default implementation simply appends the number to the model
178 name. Subclasses may override this if they want to do something
179 different.
180
181 """
182 return "%s%d" % (model_name, partition_number)
183
186 """
187 A tonal space path, represented as a list of TonalDenotations,
188 gets generated by the models. It may be sensible for a model to
189 generate exactly one point per chord, in which case repeated
190 points ought to be removed from the path before evaluating.
191
192 This removes repeated points and returns the result.
193
194 """
195 new_path = [path[0]]
196 last_point = path[0]
197
198 for point in path[1:]:
199 if last_point.root_number != point.root_number or \
200 last_point.function != point.function:
201 new_path.append(point)
202 return new_path
203