Skip to content

Commit 94af59a

Browse files
committed
correct generic type naming, simplify _to_epsilon_nfa_internal method
1 parent b5a0735 commit 94af59a

File tree

5 files changed

+18
-24
lines changed

5 files changed

+18
-24
lines changed

pyformlang/finite_automaton/deterministic_transition_function.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
A deterministic transition function
33
"""
44

5-
# pylint: disable=function-redefined
6-
75
from typing import Optional
86

97
from .state import State

pyformlang/finite_automaton/finite_automaton.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
""" A general finite automaton representation """
22

3-
# pylint: disable=function-redefined
4-
53
from typing import Dict, List, Set, Tuple, \
64
Iterable, Iterator, Optional, Hashable, Any, TypeVar
75
from abc import abstractmethod
@@ -17,7 +15,7 @@
1715
from .transition_function import TransitionFunction
1816
from .utils import to_state, to_symbol
1917

20-
fa_type = TypeVar("fa_type", bound="FiniteAutomaton")
18+
AutomatonT = TypeVar("AutomatonT", bound="FiniteAutomaton")
2119

2220

2321
class FiniteAutomaton(Iterable[Tuple[State, Symbol, State]]):
@@ -41,6 +39,7 @@ class FiniteAutomaton(Iterable[Tuple[State, Symbol, State]]):
4139
A set of final or accepting states. It is a subset of states.
4240
"""
4341

42+
@abstractmethod
4443
def __init__(self) -> None:
4544
self._states: Set[State]
4645
self._input_symbols: Set[Symbol]
@@ -553,13 +552,18 @@ def write_as_dot(self, filename: str) -> None:
553552
"""
554553
write_dot(self.to_networkx(), filename)
555554

555+
@abstractmethod
556+
def accepts(self, word: Iterable[Hashable]) -> bool:
557+
""" Checks whether the finite automaton accepts a given word """
558+
raise NotImplementedError
559+
556560
def get_accepted_words(self, max_length: Optional[int] = None) \
557561
-> Iterable[List[Symbol]]:
558562
"""
559563
Gets words accepted by the finite automaton.
560564
"""
561565
if max_length is not None and max_length < 0:
562-
return []
566+
return
563567
states_to_visit = deque((start_state, [])
564568
for start_state in self.start_states)
565569
states_leading_to_final = self._get_states_leading_to_final()
@@ -657,14 +661,14 @@ def to_dict(self) -> Dict[State, Dict[Symbol, Set[State]]]:
657661
return self._transition_function.to_dict()
658662

659663
@abstractmethod
660-
def copy(self: fa_type) -> fa_type:
664+
def copy(self: AutomatonT) -> AutomatonT:
661665
""" Copies the current Finite Automaton instance """
662666
raise NotImplementedError
663667

664-
def __copy__(self: fa_type) -> fa_type:
668+
def __copy__(self: AutomatonT) -> AutomatonT:
665669
return self.copy()
666670

667-
def _copy_to(self, fa_to_copy_to: fa_type) -> fa_type:
671+
def _copy_to(self, fa_to_copy_to: AutomatonT) -> AutomatonT:
668672
""" Copies current automaton properties to the given one """
669673
for start in self._start_states:
670674
fa_to_copy_to.add_start_state(start)

pyformlang/finite_automaton/nondeterministic_transition_function.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
A nondeterministic transition function
33
"""
44

5-
# pylint: disable=function-redefined
6-
75
from typing import Dict, Set, Iterable, Tuple
86
from copy import deepcopy
97

pyformlang/finite_automaton/transition_function.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
General transition function representation
33
"""
44

5-
# pylint: disable=function-redefined
6-
75
from typing import Dict, Set, Tuple, Iterable, Iterator
86
from abc import abstractmethod
97

pyformlang/regular_expression/regex.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ class Regex(RegexReader):
8888
"""
8989

9090
def __init__(self, regex: str) -> None:
91-
self.sons: List[Regex] = [] # type: ignore
9291
super().__init__(regex)
92+
self.sons: List[Regex] # type: ignore
9393
self._counter = 0
9494
self._enfa: Optional[EpsilonNFA] = None
9595

@@ -138,7 +138,7 @@ def get_number_operators(self) -> int:
138138

139139
def to_minimal_dfa(self) -> DeterministicFiniteAutomaton:
140140
""" Builds minimal dfa from current regex """
141-
enfa = self.to_epsilon_nfa()
141+
enfa = self._to_epsilon_nfa_internal()
142142
dfa = DeterministicFiniteAutomaton.from_epsilon_nfa(enfa)
143143
return dfa.minimize()
144144

@@ -157,19 +157,16 @@ def to_epsilon_nfa(self) -> EpsilonNFA:
157157
>>> regex.to_epsilon_nfa()
158158
159159
"""
160-
return self._to_epsilon_nfa_internal(True)
160+
return self._to_epsilon_nfa_internal().copy()
161161

162-
def _to_epsilon_nfa_internal(self, copy: bool) -> EpsilonNFA:
163-
"""
164-
Transforms the regular expression into an epsilon NFA.
165-
Copy enfa in case of external usage.
166-
"""
162+
def _to_epsilon_nfa_internal(self) -> EpsilonNFA:
163+
""" Transforms the regular expression into an epsilon NFA """
167164
if self._enfa is None:
168165
self._enfa = EpsilonNFA()
169166
s_initial = self._set_and_get_initial_state_in_enfa(self._enfa)
170167
s_final = self._set_and_get_final_state_in_enfa(self._enfa)
171168
self._process_to_enfa(self._enfa, s_initial, s_final)
172-
return self._enfa.copy() if copy else self._enfa
169+
return self._enfa
173170

174171
def _set_and_get_final_state_in_enfa(self, enfa: EpsilonNFA) -> State:
175172
s_final = self._get_next_state_enfa()
@@ -567,8 +564,7 @@ def accepts(self, word: Iterable[str]) -> bool:
567564
True
568565
569566
"""
570-
self._enfa = self._to_epsilon_nfa_internal(False)
571-
return self._enfa.accepts(word)
567+
return self._to_epsilon_nfa_internal().accepts(word)
572568

573569
@classmethod
574570
def from_finite_automaton(cls, automaton: FiniteAutomaton) -> "Regex":

0 commit comments

Comments
 (0)