Package jazzparser :: Module grammar :: Class Grammar
[hide private]
[frames] | no frames]

Class Grammar

source code

object --+
         |
        Grammar

Represents a grammar read in from an XML grammar file. Initialised with the location of the XML file. Can be consulted to retrieve lexical entries in the grammar.

Instance Methods [hide private]
 
__init__(self, grammar_name=None)
Creates a new grammar by reading from an XML grammar file.
source code
 
get_signs_for_word(self, word, tags=None, extra_features=None)
Given a word string, returns a list of the possible signs (as CCGSigns) that the grammar can assign to it.
source code
 
get_sign_for_word_by_tag(self, word, tag, extra_features=None)
Returns a sign that can be assigned to the given word and that has the given tag.
source code
 
get_signs_for_tag(self, tag, features)
Instantiates all the signs associated with a pos tag, using the given dictionary of lexical features.
source code
 
_get_entries_by_tag(self)
Gets all the tags recognised by the grammar.
source code
 
_get_pos_tags(self)
Convenience function to get the full list of POS tags allowed by the grammar.
source code
 
tag_to_function(self, tag)
Given a POS tag (roughly denoting a category), returns the function of the chord that is implicit in this interpretation.
source code
 
_get_tags_by_function(self)
Returns a dictionary mapping chord functions to POS tags (i.e.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Variables [hide private]
  formalism = None
Formalism definition (jazzparser.formalisms.FormalismBase) for the formalism used by the grammar instance.
  grammar_file = None
Path to the grammar.xml file from which the grammar definition was loaded.
  literal_functions = None
Dictionary of builtin semantic literal functions.
  families = None
Dictionary of lexical entry families (each a list of lexical signs), keyed by POS.
  inactive_families = None
Like families.
  morphs = None
List of morph entries (MorphItem).
  morph_items = None
Same as morphs, but a dictionary indexed by the word attribute (identifier of word or word class) for easy lookup.
  modality_tree = None
Instance of jazzparser.formalisms.base.modalities.ModalityTree representing the hierarchical structure of modalities used in this grammar.
  rules = None
List of all the rules used by the grammar.
  unary_rules = None
List of unary rules.
  binary_rules = None
List of binary rules.
  rules_by_name = None
Dictionary of rules, containing same entries as rules, keyed by the internal_name attribute.
  lexical_rules = None
List of special unary rules that are applied to lexical families when the grammar is instantiated to expand the lexicon.
  chord_classes = None
Dict of ChordClass objects, indexed by name.
Properties [hide private]
  entries_by_tag
Gets all the tags recognised by the grammar.
  pos_tags
Convenience function to get the full list of POS tags allowed by the grammar.
  tags_by_function
Returns a dictionary mapping chord functions to POS tags (i.e.

Inherited from object: __class__

Method Details [hide private]

__init__(self, grammar_name=None)
(Constructor)

source code 

Creates a new grammar by reading from an XML grammar file.

Words (morph items) are stored in morph_items. Families (lexical families) are stored in families.

Instantiate this directly only if you want, for some reason, to be sure of getting a new instance of Grammar. Most of the time, you can load a named grammar using get_grammar, which will cache already loaded grammars and return the same instance again if you ask for the same name.

Parameters:
  • grammar_name (string) - name of the grammar definition to be loaded. Call get_grammar_names for a list of available grammars. If None, loads the default grammar.
Overrides: object.__init__

get_signs_for_word(self, word, tags=None, extra_features=None)

source code 

Given a word string, returns a list of the possible signs (as CCGSigns) that the grammar can assign to it. word may also be a Chord object. For now, this assumes that the input is a single chord in roman numeral notation and that spelling issues have already been resolved (e.g. that 6s have been removed).

If tags is given it should be a list of strings. Signs will be restricted to those whose entry's tag name/POS is in the list.

If you need to get an instantiated category from a lexical entry, use the methods on EntriesItem directly, or get_signs_for_tag.

get_sign_for_word_by_tag(self, word, tag, extra_features=None)

source code 

Returns a sign that can be assigned to the given word and that has the given tag. If the word cannot have that tag, returns None.

_get_entries_by_tag(self)

source code 

Gets all the tags recognised by the grammar. The tags are returned as indices in a dict to the entry they represent. Note that these tags are unique identifiers of lexical items, not pos tags. If you want entries indexed by POS tags, just use grammar.families.

tag_to_function(self, tag)

source code 

Given a POS tag (roughly denoting a category), returns the function of the chord that is implicit in this interpretation. If the tag is not in the grammar or a function is not given for this tag in the lexicon, returns None.

_get_tags_by_function(self)

source code 

Returns a dictionary mapping chord functions to POS tags (i.e. category family names).


Class Variable Details [hide private]

formalism

Formalism definition (jazzparser.formalisms.FormalismBase) for the formalism used by the grammar instance.

Always set.

Value:
None

grammar_file

Path to the grammar.xml file from which the grammar definition was loaded.

Always set.

Value:
None

literal_functions

Dictionary of builtin semantic literal functions.

May be None.

Deprecated: Only used in very early versions of the grammar. Left here for backwards compatibility. May be removed altogether soon.

Value:
None

inactive_families

Like families. Dictionary of families defined in the XML but marked as inactive in their definition.

Always a list. May be empty.

Value:
None

morphs

List of morph entries (MorphItem). Mapping from word classes to families. You probably don't want to access this directly usually, but to use Grammar's methods like get_signs_for_word.

Value:
None

rules

List of all the rules used by the grammar. These are instances of the rule classes, instantiated with the correct parameters and ready to apply to signs. Does not include lexical rules: see lexical_rules.

Value:
None

unary_rules

List of unary rules. Subset of rules.

May be empty, but should always be a list.

Value:
None

binary_rules

List of binary rules. Subset of rules.

May be empty, but should always be a list.

Value:
None

rules_by_name

Dictionary of rules, containing same entries as rules, keyed by the internal_name attribute. There should not be more than one rule instance with the same internal name. If multiple are created when the grammar is instantiated, a GrammarReadError will be raised.

Value:
None

lexical_rules

List of special unary rules that are applied to lexical families when the grammar is instantiated to expand the lexicon. The lexicon will then include all the original lexical entries, plus any results of applying these rules to them.

Empty list if no lexical rules are given.

Value:
None

Property Details [hide private]

entries_by_tag

Gets all the tags recognised by the grammar. The tags are returned as indices in a dict to the entry they represent. Note that these tags are unique identifiers of lexical items, not pos tags. If you want entries indexed by POS tags, just use grammar.families.

Get Method:
_get_entries_by_tag(self) - Gets all the tags recognised by the grammar.

pos_tags

Convenience function to get the full list of POS tags allowed by the grammar.

Get Method:
_get_pos_tags(self) - Convenience function to get the full list of POS tags allowed by the grammar.

tags_by_function

Returns a dictionary mapping chord functions to POS tags (i.e. category family names).

Get Method:
_get_tags_by_function(self) - Returns a dictionary mapping chord functions to POS tags (i.e.