concepts.language.ccg.grammar.CCG#

class CCG[source]#

Bases: object

The data structure of a CCG grammar and the implementation for parsing.

Methods

add_entry(word, lexicon)

Add a lexicon entry.

add_entry_simple(word, syntax, semantics[, ...])

Add a lexicon entry with a syntax type and a semantic form.

clear_entries(word)

Clear all lexicon entries for a word.

clone([deep])

Clone the CCG grammar.

define()

A helper function to define a CCG grammar.

format_summary()

Format the summary of the CCG grammar.

make_node(arg1[, arg2, composition_type])

Make a CCG node by trying to compose two nodes or retriving an lexicon entry.

parse(sentence[, beam, preserve_syntax_types])

Parse a sentence.

print_summary()

Print the summary of the CCG grammar.

update_entries(entries_dict)

Update the lexicon entries.

Attributes

Semantics

A syntax sugar for defining semantics.

Syntax

A syntax sugar for defining syntax types.

function_domain

The function domain.

syntax_system

The syntax system, containing primitive and conjunctive syntax types.

semantics_sugar

The semantics sugar, which is a helper function to convert lambda functions to a concepts.language.ccg.semantics.CCGSemantics instance.

composition_system

The composition system, containing allowed composition rules.

lexicons

The lexicons entries, which is a dictionary from word to a list of Lexicon instances.

__init__(domain, syntax_system, composition_system=None)[source]#

Initialize the CCG parser.

Parameters:
  • domain (FunctionDomain) – the function domain.

  • syntax_system (CCGSyntaxSystem) – the syntax system, containing primitive and conjunctive syntax types.

  • composition_system (CCGCompositionSystem | None) – the composition system, containing allowed composition rules.

__new__(**kwargs)#
add_entry(word, lexicon)[source]#

Add a lexicon entry.

Parameters:
  • word (str) – the word.

  • lexicon (Lexicon) – the lexicon entry.

add_entry_simple(word, syntax, semantics, weight=0)[source]#

Add a lexicon entry with a syntax type and a semantic form.

Parameters:
clear_entries(word)[source]#

Clear all lexicon entries for a word.

Parameters:

word (str) – the word.

clone(deep=False)[source]#

Clone the CCG grammar.

Parameters:

deep (bool) – whether to make a deep copy of the class. If False, only the lexicons will be copied.

Returns:

The cloned CCG grammar.

Return type:

CCG

define()[source]#

A helper function to define a CCG grammar. For example:

with ccg.define():
    red: 'N/N', lambda x: filter(x, 'red')
    blue: 'N/N', lambda x: filter(x, 'blue'), 0.5  # weight = 0.5
format_summary()[source]#

Format the summary of the CCG grammar.

Return type:

str

make_node(arg1, arg2=None, *, composition_type=None)[source]#

Make a CCG node by trying to compose two nodes or retriving an lexicon entry. There are three use cases:

  • make_node(lexicon): make a node from a Lexicon instance.

  • make_node(word): make a node from a word, which will be looked up in the lexicon. When there are multiple lexicon entries, an error will be raised.

  • make_node(lhs, rhs): make a node by composing two nodes.

Parameters:
  • arg1 (str | Lexicon | CCGNode) – the first argument.

  • arg2 (CCGNode | None) – the second argument.

  • composition_type (CCGCompositionType | None) – the composition type. If not specified, it will be guessed.

Returns:

The composed CCG node.

Return type:

CCGNode

parse(sentence, beam=None, preserve_syntax_types=True)[source]#

Parse a sentence.

Parameters:
  • sentence (str | Iterable[str]) – the sentence to parse. It can be a string or a list of words.

  • beam (int | None) – the beam size. If not specified, it will perform the full chart parsing (beam size = Infinity).

  • preserve_syntax_types (bool) – whether to preserve the syntax types in beam searcm. When it is False, the algorithm will sort all nodes by their weights and return the top-k nodes. When it is True, the algorithm will sort all nodes by their weights and return the top-k nodes for each syntax type.

Returns:

A list of CCGNode instances.

Return type:

List[CCGNode]

print_summary()[source]#

Print the summary of the CCG grammar.

update_entries(entries_dict)[source]#

Update the lexicon entries.

Parameters:

entries_dict (Dict[str, Iterable[Lexicon]]) – the lexicon entries, which is a dictionary from word to a list of Lexicon instances.

property Semantics#

A syntax sugar for defining semantics. For example, use self.Semantics[lambda x: f(x)] to define a semantics.

property Syntax#

A syntax sugar for defining syntax types. For example, use self.Syntax['S/NP'] to define a syntax type.

composition_system: CCGCompositionSystem#

The composition system, containing allowed composition rules.

function_domain: FunctionDomain#

The function domain.

lexicons: Dict[str, List[Lexicon]]#

The lexicons entries, which is a dictionary from word to a list of Lexicon instances.

semantics_sugar: CCGSemanticsSugar#

The semantics sugar, which is a helper function to convert lambda functions to a concepts.language.ccg.semantics.CCGSemantics instance.

syntax_system: CCGSyntaxSystem#

The syntax system, containing primitive and conjunctive syntax types.