Skip to content

Commit 17ac22d

Browse files
committed
update rsa module docs
1 parent e1b585b commit 17ac22d

File tree

2 files changed

+64
-73
lines changed

2 files changed

+64
-73
lines changed

pyformlang/rsa/box.py

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
"""
2-
Representation of a box for recursive automaton
3-
"""
1+
"""Representation of a box for recursive automaton."""
42

53
from typing import Set, Hashable, Any
64

@@ -11,71 +9,70 @@
119

1210

1311
class Box:
14-
""" Represents a box for recursive automaton
15-
16-
This class represents a box for recursive automaton
12+
"""Representation of a box for recursive automaton.
1713
1814
Parameters
1915
----------
20-
enfa : :class:`~pyformlang.finite_automaton.EpsilonNFA`
21-
A epsilon nfa
22-
nonterminal : :class:`~pyformlang.finite_automaton.Symbol`
23-
A nonterminal for epsilon nfa
24-
16+
dfa:
17+
The deterministic automaton to describe the box.
18+
nonterminal:
19+
A nonterminal for `dfa`.
2520
"""
2621

2722
def __init__(self,
2823
dfa: DeterministicFiniteAutomaton,
2924
nonterminal: Hashable) -> None:
25+
"""Initializes the box."""
3026
self._dfa = dfa
3127
self._nonterminal = to_symbol(nonterminal)
3228

3329
@property
3430
def dfa(self) -> DeterministicFiniteAutomaton:
35-
""" Box's dfa """
31+
"""Gets the deterministic automaton of the box."""
3632
return self._dfa
3733

3834
@property
3935
def nonterminal(self) -> Symbol:
40-
""" Box's nonterminal """
36+
"""Gets the nonterminal of the box."""
4137
return self._nonterminal
4238

4339
@property
4440
def start_states(self) -> Set[State]:
45-
""" The start states """
41+
"""Gets the start states of the box."""
4642
return self._dfa.start_states
4743

4844
@property
4945
def final_states(self) -> Set[State]:
50-
""" The final states """
46+
"""Gets the final states of the box."""
5147
return self._dfa.final_states
5248

5349
def is_equivalent_to(self, other: "Box") -> bool:
54-
""" Check whether two boxes are equivalent
50+
"""Checks whether two boxes are equivalent.
5551
5652
Parameters
5753
----------
58-
other : :class:`~pyformlang.rsa.Box`
59-
A sequence of input symbols
54+
other:
55+
An other box.
6056
6157
Returns
62-
----------
63-
are_equivalent : bool
64-
Whether the two boxes are equivalent or not
58+
-------
59+
Whether the two boxes are equivalent or not.
6560
"""
6661
return self._dfa.is_equivalent_to(other.dfa) \
6762
and self.nonterminal == other.nonterminal
6863

6964
def __eq__(self, other: Any) -> bool:
65+
"""Checks whether the current box is equal to the given object."""
7066
if not isinstance(other, Box):
7167
return False
7268
return self.is_equivalent_to(other)
7369

7470
def __hash__(self) -> int:
71+
"""Gets the hash of the box."""
7572
return hash(self.nonterminal)
7673

7774
def to_subgraph_dot(self) -> str:
78-
"""Creates a named subgraph representing a box"""
75+
"""Creates a named subgraph representing a box."""
7976
graph = self._dfa.to_networkx()
8077
strange_nodes = []
8178
nonterminal = str(self.nonterminal) \

pyformlang/rsa/recursive_automaton.py

Lines changed: 45 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
"""
2-
Representation of a recursive automaton
3-
"""
1+
"""Representation of a recursive automaton."""
42

53
from typing import Dict, Set, AbstractSet, Optional, Hashable, Any
64

@@ -13,22 +11,20 @@
1311

1412

1513
class RecursiveAutomaton:
16-
""" Represents a recursive automaton
17-
18-
This class represents a recursive automaton.
14+
"""Representation of a recursive automaton.
1915
2016
Parameters
2117
----------
22-
start_box : :class:`~pyformlang.rsa.Box`
23-
Start box
24-
boxes : set of :class:`~pyformlang.rsa.Box`
25-
A finite set of boxes
26-
18+
start_box:
19+
A start box of the recursive automaton.
20+
boxes:
21+
A finite set of boxes.
2722
"""
2823

2924
def __init__(self,
3025
start_box: Box,
3126
boxes: AbstractSet[Box]) -> None:
27+
"""Initializes the recursive automaton."""
3228
self._nonterminal_to_box: Dict[Symbol, Box] = {}
3329
self._start_nonterminal = start_box.nonterminal
3430
if start_box not in boxes:
@@ -38,62 +34,58 @@ def __init__(self,
3834

3935
@property
4036
def nonterminals(self) -> Set[Symbol]:
41-
""" The set of nonterminals """
37+
"""Gets the set of nonterminals of the automaton."""
4238
return set(self._nonterminal_to_box.keys())
4339

4440
@property
4541
def boxes(self) -> Set[Box]:
46-
""" The set of boxes """
42+
"""Gets the set of boxes of the automaton."""
4743
return set(self._nonterminal_to_box.values())
4844

4945
@property
5046
def start_nonterminal(self) -> Symbol:
51-
""" The start nonterminal """
47+
"""Gets the start nonterminal of the automaton."""
5248
return self._start_nonterminal
5349

5450
@property
5551
def start_box(self) -> Box:
56-
""" The start box """
52+
"""Gets the start box of the automaton."""
5753
return self._nonterminal_to_box[self.start_nonterminal]
5854

5955
def get_box_by_nonterminal(self, nonterminal: Hashable) -> Optional[Box]:
60-
"""
61-
Box by nonterminal
56+
"""Gets a box by the given nonterminal.
6257
6358
Parameters
6459
----------
65-
nonterminal: :class:`~pyformlang.finite_automaton.Symbol` | str
66-
the nonterminal of which represents a box
60+
nonterminal:
61+
A nonterminal representing a box.
6762
6863
Returns
69-
-----------
70-
box : :class:`~pyformlang.rsa.Box` | None
71-
box represented by given nonterminal
64+
-------
65+
The box represented by the given nonterminal.
7266
"""
73-
7467
nonterminal = to_symbol(nonterminal)
7568
return self._nonterminal_to_box.get(nonterminal, None)
7669

7770
def get_number_boxes(self) -> int:
78-
""" Size of set of boxes """
71+
"""Gets the number of boxes in the current automaton."""
7972
return len(self._nonterminal_to_box)
8073

8174
@classmethod
8275
def from_regex(cls, regex: Regex, start_nonterminal: Hashable) \
8376
-> "RecursiveAutomaton":
84-
""" Create a recursive automaton from regular expression
77+
"""Creates a recursive automaton from regular expression.
8578
8679
Parameters
87-
-----------
88-
regex : :class:`~pyformlang.regular_expression.Regex`
89-
The regular expression
90-
start_nonterminal : :class:`~pyformlang.finite_automaton.Symbol` | str
91-
The start nonterminal for the recursive automaton
80+
----------
81+
regex:
82+
The regular expression to create automaton from.
83+
start_nonterminal:
84+
The start nonterminal for the recursive automaton.
9285
9386
Returns
94-
-----------
95-
rsa : :class:`~pyformlang.rsa.RecursiveAutomaton`
96-
The new recursive automaton built from regular expression
87+
-------
88+
The new recursive automaton built from regular expression.
9789
"""
9890
start_nonterminal = to_symbol(start_nonterminal)
9991
box = Box(regex.to_minimal_dfa(), start_nonterminal)
@@ -102,21 +94,18 @@ def from_regex(cls, regex: Regex, start_nonterminal: Hashable) \
10294
@classmethod
10395
def from_ebnf(cls, text: str, start_nonterminal: Hashable = "S") \
10496
-> "RecursiveAutomaton":
105-
""" Create a recursive automaton from ebnf \
106-
(ebnf = Extended Backus-Naur Form)
97+
"""Creates a recursive automaton from Extended Backus-Naur Form.
10798
10899
Parameters
109100
-----------
110-
text : str
111-
The text of transform
112-
start_nonterminal : \
113-
:class:`~pyformlang.finite_automaton.Symbol` | str, optional
114-
The start nonterminal, S by default
101+
text:
102+
The text of transform.
103+
start_nonterminal:
104+
The start nonterminal.
115105
116106
Returns
117-
-----------
118-
rsa : :class:`~pyformlang.rsa.RecursiveAutomaton`
119-
The new recursive automaton built from context-free grammar
107+
-------
108+
The new recursive automaton built from context-free grammar.
120109
"""
121110
start_nonterminal = to_symbol(start_nonterminal)
122111
productions: Dict[Hashable, str] = {}
@@ -148,29 +137,34 @@ def from_ebnf(cls, text: str, start_nonterminal: Hashable = "S") \
148137
return RecursiveAutomaton(start_box, boxes)
149138

150139
def is_equal_to(self, other: "RecursiveAutomaton") -> bool:
151-
"""
152-
Check whether two recursive automata are equals by boxes.
140+
"""Check whether two recursive automata are equal by boxes.
141+
153142
Not equivalency in terms of formal languages theory, just mapping boxes
154143
155144
Parameters
156145
----------
157-
other : :class:`~pyformlang.rsa.RecursiveAutomaton`
158-
The input recursive automaton
146+
other:
147+
The other recursive automaton.
159148
160149
Returns
161-
----------
162-
are_equivalent : bool
163-
Whether the two recursive automata are equals or not
150+
-------
151+
Whether the two recursive automata are equal or not.
164152
"""
165153
return self.boxes == other.boxes
166154

167155
def __eq__(self, other: Any) -> bool:
156+
"""Checks if the current automaton is equal to the given object."""
168157
if not isinstance(other, RecursiveAutomaton):
169158
return False
170159
return self.is_equal_to(other)
171160

172161
def to_dot(self) -> str:
173-
""" Create dot representation of recursive automaton """
162+
"""Creates dot representation of recursive automaton.
163+
164+
Returns
165+
-------
166+
The dot representation of current recursive automaton.
167+
"""
174168
dot_string = 'digraph "" {'
175169
for box in self._nonterminal_to_box.values():
176170
dot_string += f'\n{box.to_subgraph_dot()}'

0 commit comments

Comments
 (0)