-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow using non-terminals as right-hand side of simple rules
- Loading branch information
1 parent
4b7110e
commit b04d4aa
Showing
10 changed files
with
119 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from src.grammar.cnf_grammar_template import CnfGrammarTemplate, Symbol | ||
from src.graph.label_decomposed_graph import LabelDecomposedGraph | ||
|
||
|
||
def explode_indices( | ||
graph: LabelDecomposedGraph, | ||
grammar: CnfGrammarTemplate | ||
) -> (LabelDecomposedGraph, CnfGrammarTemplate): | ||
block_matrix_space = graph.block_matrix_space | ||
block_count = block_matrix_space.block_count | ||
|
||
matrices = dict() | ||
for symbol, matrix in graph.matrices.items(): | ||
if block_matrix_space.is_single_cell(matrix.shape): | ||
matrices[symbol] = matrix | ||
else: | ||
for i, block in enumerate(block_matrix_space.get_hyper_vector_blocks(matrix)): | ||
matrices[_index_symbol(symbol, i)] = block | ||
|
||
epsilon_rules = [] | ||
for non_terminal in grammar.epsilon_rules: | ||
if non_terminal.is_indexed: | ||
for i in range(block_count): | ||
epsilon_rules.append(_index_symbol(non_terminal, i)) | ||
else: | ||
epsilon_rules.append(non_terminal) | ||
|
||
simple_rules = [] | ||
for (non_terminal, terminal) in grammar.simple_rules: | ||
if non_terminal.is_indexed or terminal.is_indexed: | ||
for i in range(block_count): | ||
simple_rules.append((_index_symbol(non_terminal, i), _index_symbol(terminal, i))) | ||
else: | ||
simple_rules.append((non_terminal, terminal)) | ||
|
||
complex_rules = [] | ||
for (non_terminal, symbol1, symbol2) in grammar.complex_rules: | ||
if non_terminal.is_indexed or symbol1.is_indexed or symbol2.is_indexed: | ||
for i in range(block_count): | ||
complex_rules.append(( | ||
_index_symbol(non_terminal, i), | ||
_index_symbol(symbol1, i), | ||
_index_symbol(symbol2, i), | ||
)) | ||
else: | ||
complex_rules.append((non_terminal, symbol1, symbol2)) | ||
|
||
return ( | ||
LabelDecomposedGraph( | ||
vertex_count=graph.vertex_count, | ||
block_matrix_space=block_matrix_space, | ||
dtype=graph.dtype, | ||
matrices=matrices | ||
), | ||
CnfGrammarTemplate( | ||
start_nonterm=grammar.start_nonterm, | ||
epsilon_rules=epsilon_rules, | ||
simple_rules=simple_rules, | ||
complex_rules=complex_rules | ||
) | ||
) | ||
|
||
|
||
def _index_symbol(symbol: Symbol, index: int) -> Symbol: | ||
return Symbol(f"{symbol.label}_{index}") if symbol.is_indexed else symbol |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,14 @@ | ||
S H1 H0 | ||
S H2 H0 | ||
V H3 V3 | ||
V V2 V3 | ||
V V1 V3 | ||
V V1 V2 | ||
V H4 V3 | ||
V H5 V3 | ||
V H5 V2 | ||
V a | ||
V H1 H0 | ||
V H2 H0 | ||
V H6 V1 | ||
V V2 H7 | ||
V H7 V1 | ||
V a_r | ||
V1 H6 V1 | ||
V1 V2 H7 | ||
V1 H7 V1 | ||
V1 a_r | ||
V2 H1 H0 | ||
V2 H2 H0 | ||
V3 H4 V3 | ||
V3 H5 V3 | ||
V3 H5 V2 | ||
V3 a | ||
H0 d | ||
H1 H2 V | ||
H2 d_r | ||
H3 V1 V2 | ||
H4 H5 V2 | ||
H5 a | ||
H6 V2 H7 | ||
H7 a_r | ||
S d_r V_d | ||
V1 | ||
V3 a V2_V3 | ||
V2_V3 V2 V3 | ||
V2 d_r V_d | ||
a_r_V1 a_r V1 | ||
V V1 V2_V3 | ||
V3 | ||
V2 | ||
V_d V d | ||
V1 V2 a_r_V1 | ||
|
||
Count: | ||
S | ||
S |