Skip to content

Commit d434cba

Browse files
committed
update cfg module docs
1 parent d0c6c9d commit d434cba

File tree

10 files changed

+360
-318
lines changed

10 files changed

+360
-318
lines changed

pyformlang/cfg/cfg.py

Lines changed: 137 additions & 154 deletions
Large diffs are not rendered by default.

pyformlang/cfg/cfg_variable_converter.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""A CFG Variable Converter"""
1+
"""A converter into CFG variables."""
22

33
from typing import Dict, List, AbstractSet, Tuple, Optional, Hashable
44

@@ -7,11 +7,12 @@
77

88

99
class CFGVariableConverter:
10-
"""A CFG Variable Converter"""
10+
"""A converter into CFG variables."""
1111

1212
def __init__(self,
1313
states: AbstractSet[FormalObject],
1414
stack_symbols: AbstractSet[FormalObject]) -> None:
15+
"""Initializes the converter."""
1516
self._counter = 0
1617
self._inverse_states_d: Dict[FormalObject, int] = {}
1718
self._counter_state = 0
@@ -31,7 +32,7 @@ def __init__(self,
3132
range(len(states))]
3233

3334
def _get_state_index(self, state: FormalObject) -> int:
34-
"""Get the state index"""
35+
"""Gets the state index."""
3536
if state.index is None:
3637
if state not in self._inverse_states_d:
3738
self._inverse_states_d[state] = self._counter_state
@@ -40,7 +41,7 @@ def _get_state_index(self, state: FormalObject) -> int:
4041
return state.index
4142

4243
def _get_symbol_index(self, symbol: FormalObject) -> int:
43-
"""Get the symbol index"""
44+
"""Gets the symbol index."""
4445
if symbol.index is None:
4546
if symbol not in self._inverse_stack_symbol_d:
4647
self._inverse_stack_symbol_d[symbol] = self._counter_symbol
@@ -52,7 +53,7 @@ def to_cfg_combined_variable(self,
5253
state0: FormalObject,
5354
stack_symbol: FormalObject,
5455
state1: FormalObject) -> Variable:
55-
""" Conversion used in the to_pda method """
56+
"""Conversion used in the PDA to CFG transformation."""
5657
i_stack_symbol, i_state0, i_state1 = self._get_indexes(
5758
stack_symbol, state0, state1)
5859
prev = self._conversions[i_state0][i_stack_symbol][i_state1]
@@ -78,7 +79,7 @@ def set_valid(self,
7879
state0: FormalObject,
7980
stack_symbol: FormalObject,
8081
state1: FormalObject) -> None:
81-
"""Set valid"""
82+
"""Set valid."""
8283
i_stack_symbol, i_state0, i_state1 = self._get_indexes(
8384
stack_symbol, state0, state1)
8485
prev = self._conversions[i_state0][i_stack_symbol][i_state1]
@@ -88,7 +89,7 @@ def is_valid_and_get(self,
8889
state0: FormalObject,
8990
stack_symbol: FormalObject,
9091
state1: FormalObject) -> Optional[Variable]:
91-
"""Check if valid and get"""
92+
"""Check if valid and get."""
9293
i_state0 = self._get_state_index(state0)
9394
i_stack_symbol = self._get_symbol_index(stack_symbol)
9495
i_state1 = self._get_state_index(state1)

pyformlang/cfg/cyk_table.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
"""
2-
Representation of a CYK table
3-
"""
1+
"""Representation of a CYK parsing table."""
42

53
from typing import Dict, List, Set, Iterable, Tuple, Any
64

@@ -13,17 +11,18 @@
1311

1412

1513
class CYKTable:
16-
"""
17-
A CYK table
14+
"""Representation of a CYK parsing table.
1815
1916
Parameters
2017
----------
21-
cfg : A context-free grammar
22-
word : iterable of Terminals
23-
The word from which we construct the CYK table
18+
grammar:
19+
A grammar to create CYK table from.
20+
word:
21+
A word from which we construct the CYK table.
2422
"""
2523

2624
def __init__(self, grammar: FormalGrammar, word: List[Terminal]) -> None:
25+
"""Initializes the CYK table."""
2726
self._normal_form: FormalGrammar = grammar.to_normal_form()
2827
self._generate_epsilon: bool = grammar.generate_epsilon()
2928
self._word: List[Terminal] = word
@@ -83,12 +82,11 @@ def _initialize_cyk_table(self) -> None:
8382
(start_window, start_window + window_size)] = set()
8483

8584
def generate_word(self) -> bool:
86-
"""
87-
Checks is the word is generated
85+
"""Checks is the word is generated by the grammar.
86+
8887
Returns
8988
-------
90-
is_generated : bool
91-
89+
Whether the grammar contains the word.
9290
"""
9391
if not self._word:
9492
return self._generate_epsilon
@@ -103,12 +101,16 @@ def _generates_all_terminals(self) -> bool:
103101
return generate_all_terminals
104102

105103
def get_parse_tree(self) -> ParseTree:
106-
"""
107-
Give the parse tree associated with this CYK Table
104+
"""Gets the parse tree associated with this CYK Table.
108105
109106
Returns
110107
-------
111-
parse_tree : :class:`~pyformlang.cfg.ParseTree`
108+
A parsing tree of the word in the CNF.
109+
110+
Raises
111+
------
112+
DerivationDoesNotExistError
113+
If the word cannot be derived.
112114
"""
113115
if not self._normal_form.start_symbol or not self.generate_word():
114116
raise DerivationDoesNotExistError
@@ -122,12 +124,13 @@ def get_parse_tree(self) -> ParseTree:
122124

123125

124126
class CYKNode(ParseTree):
125-
"""A node in the CYK table"""
127+
"""A node in the CYK table."""
126128

127129
def __init__(self,
128130
value: CFGObject,
129131
left_son: "CYKNode" = None,
130132
right_son: "CYKNode" = None) -> None:
133+
"""Initializes the node."""
131134
super().__init__(value)
132135
self.value = value
133136
self.left_son = left_son
@@ -138,9 +141,11 @@ def __init__(self,
138141
self.sons.append(right_son)
139142

140143
def __eq__(self, other: Any) -> bool:
144+
"""Checks if the node is equal to the given object."""
141145
if isinstance(other, CYKNode):
142146
return self.value == other.value
143147
return self.value == other
144148

145149
def __hash__(self) -> int:
150+
"""Gets the hash of the node."""
146151
return hash(self.value)

0 commit comments

Comments
 (0)