Package jazzparser :: Package formalisms
[hide private]
[frames] | no frames]

Source Code for Package jazzparser.formalisms

  1  """All available formalism modules. 
  2   
  3  A formalism defines a bundle of grammar components that may interact  
  4  with each other: syntax, semantics, rules, modalities, etc. Each module  
  5  may assume that the others of the same formalism are all in use at once,  
  6  but no code outside the formalism may depend on anything  
  7  formalism-specific. 
  8  This means a full formalism module can be added and may specify all the  
  9  details of how the syntax, semantics and rules behave and interact. 
 10   
 11  This structure has allowed me to develop radically different versions of  
 12  the musical CCG formalism in parallel. However, there currently exists only one  
 13  formalism. 
 14   
 15  The base package contains a lot of useful base classes for code that is  
 16  either common to all formalisms, or that is frequently used across many  
 17  formalisms. 
 18   
 19  """ 
 20  """ 
 21  ============================== License ======================================== 
 22   Copyright (C) 2008, 2010-12 University of Edinburgh, Mark Granroth-Wilding 
 23    
 24   This file is part of The Jazz Parser. 
 25    
 26   The Jazz Parser is free software: you can redistribute it and/or modify 
 27   it under the terms of the GNU General Public License as published by 
 28   the Free Software Foundation, either version 3 of the License, or 
 29   (at your option) any later version. 
 30    
 31   The Jazz Parser is distributed in the hope that it will be useful, 
 32   but WITHOUT ANY WARRANTY; without even the implied warranty of 
 33   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 34   GNU General Public License for more details. 
 35    
 36   You should have received a copy of the GNU General Public License 
 37   along with The Jazz Parser.  If not, see <http://www.gnu.org/licenses/>. 
 38   
 39  ============================ End license ====================================== 
 40   
 41  """ 
 42  __author__ = "Mark Granroth-Wilding <mark.granroth-wilding@ed.ac.uk>"  
 43   
 44  import sys 
 45   
 46  # Make this easily accessible here 
 47  from loader import get_formalism, get_default_formalism 
 48  from jazzparser.utils.options import ModuleOption, options_help_text 
 49  from jazzparser import settings 
 50   
 51  # List all formalisms here 
 52  FORMALISMS = [ 
 53      'music_halfspan', 
 54  ] 
55 56 -class FormalismBase(object):
57 """ 58 Superclass of all Formalism structures. These tie together all the 59 components of a formalism in an easily accessible way: rules, 60 syntax, semantics, etc. 61 """ 62 literal_functions = {} 63 shell_tools = [] 64
65 - class __metaclass__(type):
66 - def __init__(cls, name, bases, dict):
67 # Skip all this when the base class if created 68 if name != "FormalismBase": 69 # Initialize all the output options 70 # If they're never set by whatever script is running, this 71 # ensures that their default values are available 72 formalism = cls.get_name() 73 opts = ModuleOption.process_option_dict({}, cls.output_options) 74 # Store this so it's globally available to the formalism 75 settings.OPTIONS.OUTPUT[formalism] = opts
76 77 output_options = [] 78 """ 79 ModuleOptions that can be used to set global settings that will 80 affect the formatting of output. 81 82 Don't use any required options. All options must be able to be initialized 83 to a default. 84 85 The values of the options will be set to their default when the formalism 86 class is first encountered. Call L{process_output_options} to customize 87 the options. 88 89 Although the options are globally available for simplicity of access, 90 you should only ever access them from within formalism-specific code in 91 practice. 92 93 """ 94 95 @classmethod
96 - def process_output_options(cls, optdict):
97 """ 98 Makes output options globally available, based on a dictionary. 99 100 @see: L{output_options}. 101 102 """ 103 formalism = cls.get_name() 104 opts = ModuleOption.process_option_dict(optdict, cls.output_options) 105 settings.OPTIONS.OUTPUT[formalism] = opts
106 107 @classmethod
108 - def cl_output_options(cls, string):
109 """ 110 Convenience method so you don't have to do this lots of times over. 111 112 Take a string of output options from the command line and set the 113 output options from it. 114 115 Should only be used in command-line scripts. 116 117 """ 118 if string is not None and string.lower() == "help": 119 print "Available output options" 120 print "========================" 121 print options_help_text(cls.output_options) 122 sys.exit(0) 123 optdict = ModuleOption.process_option_string(string) 124 cls.process_output_options(optdict)
125 126 semantics_to_coordinates = None 127 """Function to generate a list of coordinates from a parse result (semantic part).""" 128 129 @classmethod
130 - def sign_to_coordinates(cls, sign):
131 """ 132 Function to generate a list of coordinates from a parse result. 133 By default, uses C{semantics_to_coordinates} applied to the semantic 134 part of the sign. 135 136 """ 137 return cls.semantics_to_coordinates(sign.semantics)
138 139 semantics_distance_metrics = [] 140 """ 141 List of distance metrics available in the formalism. Each item is a 142 subclass of L{jazzparser.formalisms.base.semantics.distance.DistanceMetric}. 143 144 """ 145 PcfgModel = None 146 """ 147 Pcfg model class appropriate for the formalism. 148 149 """ 150 151 @classmethod
152 - def get_name(cls):
153 return cls.__module__.rpartition(".")[2]
154
155 - class Syntax:
156 """ 157 Formalisms should define override this nested class with their 158 own, which will specify values for these attributes. 159 """ 160 Sign = None 161 ComplexCategory = None 162 AtomicCategory = None 163 DummyCategory = None 164 165 @classmethod
166 - def is_category(cls, obj):
167 return isinstance(obj, cls.Category)
168 169 @classmethod
170 - def is_complex_category(cls, obj):
171 return isinstance(obj, cls.ComplexCategory)
172 173 @classmethod
174 - def is_atomic_category(cls, obj):
175 return isinstance(obj, cls.AtomicCategory)
176 177 @staticmethod
178 - def merge_equal_signs(existing_sign, new_sign):
179 pass
180
181 - class Semantics:
182 """ 183 Formalisms should define override this nested class with their 184 own, which will specify values for these attributes. 185 """ 186 Semantics = None 187 apply = None 188 compose = None
189
190 - class Evaluation:
191 """ 192 Functions for evaluating tonal space paths and parse results. 193 194 """ 195 tonal_space_alignment_costs = None 196 """ 197 Performs an alignment of two tonal space paths and returns a dictionary 198 of costs incurred in the optimal alignment. 199 The values must include 'deletions', 'substitutions' and 'insertions' 200 and may also include more fine-grained information, like 'root_subs' 201 and 'function_subs'. 202 203 """ 204 tonal_space_distance = None 205 """ 206 Performs an alignment of two tonal space paths and computes a distance 207 metric between them. The metric is assumed to be based on edit distance, 208 so to have a minimum value of 0 (identity) and maximum the length of 209 the longer sequence. 210 211 """
212