Skip to content

Commit b121cac

Browse files
committed
update finite_automaton module docs
1 parent e31f629 commit b121cac

12 files changed

+747
-634
lines changed

pyformlang/finite_automaton/deterministic_finite_automaton.py

Lines changed: 87 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
"""
2-
Representation of a deterministic finite automaton
3-
"""
1+
"""Representation of a deterministic finite automaton."""
42

53
from typing import Iterable, AbstractSet, Optional, Hashable, Any
64

@@ -15,27 +13,24 @@
1513

1614

1715
class DeterministicFiniteAutomaton(NondeterministicFiniteAutomaton):
18-
""" Represents a deterministic finite automaton
19-
20-
This class represents a deterministic finite automaton.
16+
"""Representation of a deterministic finite automaton.
2117
2218
Parameters
2319
----------
24-
states : set of :class:`~pyformlang.finite_automaton.State`, optional
25-
A finite set of states
26-
input_symbols : set of :class:`~pyformlang.finite_automaton.Symbol`, optional
27-
A finite set of input symbols
28-
transition_function : \
29-
:class:`~pyformlang.finite_automaton.TransitionFunction`, optional
30-
Takes as arguments a state and an input symbol and returns a state.
31-
start_state : :class:`~pyformlang.finite_automaton.State`, optional
32-
A start state, element of states
33-
final_states : set of :class:`~pyformlang.finite_automaton.State`, optional
20+
states:
21+
A finite set of states.
22+
input_symbols:
23+
A finite set of input symbols.
24+
transition_function:
25+
A function that takes as arguments a state and an input symbol and
26+
returns a state.
27+
start_state:
28+
A start state, element of states.
29+
final_states:
3430
A set of final or accepting states. It is a subset of states.
3531
3632
Examples
3733
--------
38-
3934
>>> dfa = DeterministicFiniteAutomaton()
4035
4136
Creates an empty deterministic finite automaton.
@@ -64,7 +59,6 @@ class DeterministicFiniteAutomaton(NondeterministicFiniteAutomaton):
6459
6560
Checks if the automaton recognize the word composed of a single letter, \
6661
"abc".
67-
6862
"""
6963

7064
def __init__(self,
@@ -73,6 +67,7 @@ def __init__(self,
7367
transition_function: DeterministicTransitionFunction = None,
7468
start_state: Hashable = None,
7569
final_states: AbstractSet[Hashable] = None) -> None:
70+
"""Initializes the deterministic finite automaton."""
7671
start_states = {start_state} if start_state is not None else None
7772
super().__init__(states,
7873
input_symbols,
@@ -84,54 +79,48 @@ def __init__(self,
8479

8580
@property
8681
def start_state(self) -> Optional[State]:
87-
""" Gets the start state """
82+
"""Gets the start state of the DFA."""
8883
return list(self._start_states)[0] if self._start_states else None
8984

9085
def add_start_state(self, state: Hashable) -> int:
91-
""" Set an initial state
86+
"""Sets an initial state of the DFA.
9287
9388
Parameters
94-
-----------
95-
state : :class:`~pyformlang.finite_automaton.State`
96-
The new initial state
89+
----------
90+
state:
91+
The initial state to set.
9792
9893
Returns
99-
----------
100-
done : int
101-
1 is correctly added
94+
-------
95+
1 is correctly added.
10296
10397
Examples
10498
--------
105-
10699
>>> dfa = DeterministicFiniteAutomaton()
107100
>>> dfa.add_start_state(0)
108-
109101
"""
110102
state = to_state(state)
111103
self._start_states = {state}
112104
self._states.add(state)
113105
return 1
114106

115107
def remove_start_state(self, state: Hashable) -> int:
116-
""" remove an initial state
108+
"""Remove the initial state from the DFA.
117109
118110
Parameters
119-
-----------
120-
state : :class:`~pyformlang.finite_automaton.State`
121-
The new initial state
111+
----------
112+
state:
113+
The initial state to remove.
122114
123115
Returns
124116
----------
125-
done : int
126-
1 is correctly added
117+
1 is correctly removed.
127118
128119
Examples
129120
--------
130-
131121
>>> dfa = DeterministicFiniteAutomaton()
132122
>>> dfa.add_start_state(0)
133123
>>> dfa.remove_start_state(0)
134-
135124
"""
136125
state = to_state(state)
137126
if self._start_states == {state}:
@@ -141,34 +130,43 @@ def remove_start_state(self, state: Hashable) -> int:
141130

142131
def get_next_state(self, s_from: Hashable, symb_by: Hashable) \
143132
-> Optional[State]:
144-
""" Make a call of deterministic transition function """
133+
"""Makes a call of deterministic transition function.
134+
135+
Parameters
136+
----------
137+
s_from:
138+
A state to make a transition from.
139+
symb_by:
140+
A symbol to make a transition with.
141+
142+
Returns
143+
-------
144+
The next state defined by the transition function.
145+
"""
145146
s_from = to_state(s_from)
146147
symb_by = to_symbol(symb_by)
147148
return self._transition_function.get_next_state(s_from, symb_by)
148149

149150
def accepts(self, word: Iterable[Hashable]) -> bool:
150-
""" Checks whether the dfa accepts a given word
151+
"""Checks whether the DFA accepts a given word.
151152
152153
Parameters
153154
----------
154-
word : iterable of :class:`~pyformlang.finite_automaton.Symbol`
155-
A sequence of input symbols
155+
word:
156+
A sequence of input symbols.
156157
157158
Returns
158-
----------
159-
is_accepted : bool
160-
Whether the word is accepted or not
159+
-------
160+
Whether the word is accepted or not.
161161
162162
Examples
163163
--------
164-
165164
>>> dfa = DeterministicFiniteAutomaton()
166165
>>> dfa.add_transitions([(0, "abc", 1), (0, "d", 1)])
167166
>>> dfa.add_start_state(0)
168167
>>> dfa.add_final_state(1)
169168
>>> dfa.accepts(["abc"])
170169
True
171-
172170
"""
173171
word = [to_symbol(x) for x in word]
174172
current_state = self.start_state
@@ -179,43 +177,36 @@ def accepts(self, word: Iterable[Hashable]) -> bool:
179177
return current_state is not None and self.is_final_state(current_state)
180178

181179
def is_deterministic(self) -> bool:
182-
""" Checks whether an automaton is deterministic
180+
"""Checks whether an automaton is deterministic.
183181
184182
Returns
185-
----------
186-
is_deterministic : bool
187-
Whether the automaton is deterministic
183+
-------
184+
Whether the automaton is deterministic.
188185
189186
Examples
190187
--------
191-
192188
>>> dfa = DeterministicFiniteAutomaton()
193189
>>> dfa.is_deterministic()
194190
True
195-
196191
"""
197192
return True
198193

199194
def copy(self) -> "DeterministicFiniteAutomaton":
200-
""" Copies the current DFA
195+
"""Copies the current DFA.
201196
202197
Returns
203-
----------
204-
enfa : :class:`~pyformlang.finite_automaton\
205-
.DeterministicFiniteAutomaton`
206-
A copy of the current DFA
198+
-------
199+
A copy of the current DFA.
207200
208201
Examples
209202
--------
210-
211203
>>> dfa = DeterministicFiniteAutomaton()
212204
>>> dfa.add_transitions([(0, "abc", 1), (0, "d", 1)])
213205
>>> dfa.add_start_state(0)
214206
>>> dfa.add_final_state(1)
215207
>>> dfa_copy = dfa.copy()
216208
>>> dfa.is_equivalent_to(dfa_copy)
217209
True
218-
219210
"""
220211
return self._copy_to(DeterministicFiniteAutomaton())
221212

@@ -229,25 +220,21 @@ def _get_previous_transitions(self) -> PreviousTransitions:
229220
return previous_transitions
230221

231222
def minimize(self) -> "DeterministicFiniteAutomaton":
232-
""" Minimize the current DFA
223+
"""Minimize the current DFA.
233224
234225
Returns
235-
----------
236-
dfa : :class:`~pyformlang.deterministic_finite_automaton\
237-
.DeterministicFiniteAutomaton`
238-
The minimal DFA
226+
-------
227+
A minimal DFA equivalent to the current one.
239228
240229
Examples
241230
--------
242-
243231
>>> dfa = DeterministicFiniteAutomaton()
244232
>>> dfa.add_transitions([(0, "abc", 1), (0, "d", 1)])
245233
>>> dfa.add_start_state(0)
246234
>>> dfa.add_final_state(1)
247235
>>> dfa_minimal = dfa.minimize()
248236
>>> dfa.is_equivalent_to(dfa_minimal)
249237
True
250-
251238
"""
252239
if not self._start_states or not self._final_states:
253240
res = DeterministicFiniteAutomaton()
@@ -286,30 +273,50 @@ def minimize(self) -> "DeterministicFiniteAutomaton":
286273
@classmethod
287274
def from_epsilon_nfa(cls, enfa: EpsilonNFA) \
288275
-> "DeterministicFiniteAutomaton":
289-
""" Builds dfa equivalent to the given enfa """
276+
"""Builds DFA equivalent to the given ENFA.
277+
278+
Parameters
279+
----------
280+
enfa:
281+
A nondeterministic FA with epsilon transitions.
282+
283+
Returns
284+
-------
285+
A deterministic automaton equivalent to `enfa`.
286+
"""
290287
return cls._from_epsilon_nfa_internal(enfa, True)
291288

292289
@classmethod
293290
def from_nfa(cls, nfa: NondeterministicFiniteAutomaton) \
294291
-> "DeterministicFiniteAutomaton":
295-
""" Builds dfa equivalent to the given nfa """
292+
"""Builds DFA equivalent to the given NFA.
293+
294+
Parameters
295+
----------
296+
nfa:
297+
A nondeterministic FA without epsilon transitions.
298+
299+
Returns
300+
-------
301+
A deterministic automaton equivalent to `nfa`.
302+
"""
296303
return cls._from_epsilon_nfa_internal(nfa, False)
297304

298305
@classmethod
299306
def _from_epsilon_nfa_internal(cls, enfa: EpsilonNFA, eclose: bool) \
300307
-> "DeterministicFiniteAutomaton":
301-
""" Builds dfa equivalent to the given automaton
308+
"""Builds DFA equivalent to the given automaton.
302309
303310
Parameters
304311
----------
305-
eclose : bool
306-
Whether to use the epsilon closure or not
312+
enfa:
313+
A nondeterministic FA with epsilon transitions.
314+
eclose:
315+
Whether to use the epsilon closure or not.
307316
308317
Returns
309-
----------
310-
dfa : :class:`~pyformlang.finite_automaton\
311-
.DeterministicFiniteAutomaton`
312-
A dfa equivalent to the current nfa
318+
-------
319+
A deterministic automaton equivalent to `enfa`.
313320
"""
314321
dfa = DeterministicFiniteAutomaton()
315322
# Add Eclose
@@ -386,35 +393,32 @@ def _get_partition(self) -> Partition:
386393
return partition
387394

388395
def __eq__(self, other: Any) -> bool:
396+
"""Checks whether the DFA is equal to the given object."""
389397
if not isinstance(other, DeterministicFiniteAutomaton):
390398
return False
391399
return self.is_equivalent_to(other)
392400

393401
def is_equivalent_to(self, other: "DeterministicFiniteAutomaton") -> bool:
394-
""" Check whether two automata are equivalent
402+
"""Checks whether two automata are equivalent.
395403
396404
Parameters
397405
----------
398-
other : :class:`~pyformlang.deterministic_finite_automaton\
399-
.FiniteAutomaton`
400-
A sequence of input symbols
406+
other:
407+
An automaton to check the equivalence to.
401408
402409
Returns
403-
----------
404-
are_equivalent : bool
405-
Whether the two automata are equivalent or not
410+
-------
411+
Whether the two automata are equivalent or not
406412
407413
Examples
408414
--------
409-
410415
>>> dfa = DeterministicFiniteAutomaton()
411416
>>> dfa.add_transitions([(0, "abc", 1), (0, "d", 1)])
412417
>>> dfa.add_start_state(0)
413418
>>> dfa.add_final_state(1)
414419
>>> dfa_minimal = dfa.minimize()
415420
>>> dfa.is_equivalent_to(dfa_minimal)
416421
True
417-
418422
"""
419423
self_minimal = self.minimize()
420424
other_minimal = other.minimize()

0 commit comments

Comments
 (0)