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 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 the summary of the CCG grammar.
update_entries
(entries_dict)Update the lexicon entries.
Attributes
A syntax sugar for defining semantics.
A syntax sugar for defining syntax types.
The function domain.
The syntax system, containing primitive and conjunctive syntax types.
The semantics sugar, which is a helper function to convert lambda functions to a
concepts.language.ccg.semantics.CCGSemantics
instance.The composition system, containing allowed composition rules.
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_simple(word, syntax, semantics, weight=0)[source]#
Add a lexicon entry with a syntax type and a semantic form.
- Parameters:
word (str) – the word.
syntax (None | str | CCGSyntaxType) – the syntax type. It can be a string, a
CCGSyntaxType
instance, or None.semantics (None | Value | Function | ConstantExpression | FunctionApplicationExpression | Callable) – the semantics. It can be a value, a function, a constant expression, a function application expression, or a callable Python function.
weight (float) – the weight of the lexicon entry.
- clear_entries(word)[source]#
Clear all lexicon entries for a word.
- Parameters:
word (str) – the word.
- 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
- 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 aLexicon
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.
- 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:
- 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.