From a0e28c97819b892c144b0a8f72b7c95ae913a2c5 Mon Sep 17 00:00:00 2001 From: Kia <kia@special-circumstanc.es> Date: Mon, 17 Aug 2020 18:30:19 -0600 Subject: [PATCH] use arrays not strings --- cfg_utils.py | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/cfg_utils.py b/cfg_utils.py index d7843d3..b09f493 100644 --- a/cfg_utils.py +++ b/cfg_utils.py @@ -403,9 +403,9 @@ class CFGBoltzmann: retlist = [] for rhs_index in range(len(possible_RHSes)): - print("IN Fzero, trying rhs_index", rhs_index) +# print("IN Fzero, trying rhs_index", rhs_index) retlist.append(sum(self.Fprim(nonterminal_index, rhs_index, 0, exact_length_total))) # we index arrays starting at zero. like everyone else. - print ("Fzero returns", retlist) +# print ("Fzero returns", retlist) return retlist @@ -418,14 +418,14 @@ class CFGBoltzmann: # first, handle the ultimate degenerate case: if (exact_length_total == 0): - print("LENGTH ZERO RETURNING []") + #print("LENGTH ZERO RETURNING []") return [] # The case analysis hinges on what is at X_ijk. RHS_in_question = self.processed_rulepack[nonterminal_index][1][which_RHS] - print("Our RHS is", RHS_in_question) - print("WE ARE PROCESSING index", how_far_into_the_RHS, "and our remaining lengthis", exact_length_total) + #print("Our RHS is", RHS_in_question) + #print("WE ARE PROCESSING index", how_far_into_the_RHS, "and our remaining lengthis", exact_length_total) xijk = RHS_in_question[how_far_into_the_RHS] @@ -437,19 +437,19 @@ class CFGBoltzmann: if (how_far_into_the_RHS == (len(RHS_in_question) - 1)): # CASE A if (exact_length_total == 1): - print("CASE A LEN 1") - print ("Fprim returns SPECIAL CASE", [1]) +# print("CASE A LEN 1") +# print ("Fprim returns SPECIAL CASE", [1]) return [1] else: - print("CASE A LEN NON-ONE") - print ("Fprim returns BAD SPECIAL CASE", [0]) + # print("CASE A LEN NON-ONE") + # print ("Fprim returns BAD SPECIAL CASE", [0]) return [0] else: # CASE B - print("CASE B") + # print("CASE B") reduct = self.Fprim(nonterminal_index, which_RHS, how_far_into_the_RHS + 1, exact_length_total - 1) retlist = [sum(reduct)] - print ("Fprim returns", retlist) + # print ("Fprim returns", retlist) return retlist @@ -465,10 +465,10 @@ class CFGBoltzmann: if (how_far_into_the_RHS == (len(RHS_in_question) - 1)): # CASE C - print("CASE C") + # print("CASE C") reduct = self.Fzero(new_nonterminal_index, exact_length_total) retlist = [sum(reduct)] - print ("Fprim returns", retlist) + # print ("Fprim returns", retlist) return retlist else: @@ -489,15 +489,15 @@ class CFGBoltzmann: # Total Characters Requested - (len(rule)-k-1) # exact_length_total - len(rule) + how_far_into_the_RHS + 1 - print("CASE D") + # print("CASE D") retlist = [] - print(exact_length_total - len(RHS_in_question) + how_far_into_the_RHS + 2) + #print(exact_length_total - len(RHS_in_question) + how_far_into_the_RHS + 2) for l in range(1, exact_length_total - len(RHS_in_question) + how_far_into_the_RHS + 2): - print("L IS", l) + # print("L IS", l) nonterminal_possibilities = sum(self.Fzero(new_nonterminal_index, l)) remainder_possibilities = sum(self.Fprim(nonterminal_index, which_RHS, how_far_into_the_RHS + 1, exact_length_total - l)) retlist.append(nonterminal_possibilities * remainder_possibilities) - print ("Fprim returns", retlist) + # print ("Fprim returns", retlist) return retlist @@ -522,7 +522,7 @@ class CFGBoltzmann: def Gzero(self, nonterminal_index, requested_length): possibilities = self.Fzero(nonterminal_index, requested_length) chosen_production = self.normalized_choice(possibilities) - generated_string = self.Gprim(nonterminal_index, chosen_production, 1, requested_length) + generated_string = self.Gprim(nonterminal_index, chosen_production, 0, requested_length) return generated_string @@ -562,12 +562,12 @@ class CFGBoltzmann: def Gprim(self, nonterminal_index, chosen_production, how_far_into_the_RHS, exact_length_total): - print("arguments are", nonterminal_index, chosen_production, how_far_into_the_RHS, exact_length_total) + print("GPRIM arguments are", nonterminal_index, chosen_production, how_far_into_the_RHS, exact_length_total) # first, handle the ultimate degenerate case: if (exact_length_total == 0): - print("LENGTH ZERO RETURNING \"\"") - return "" + print("LENGTH ZERO RETURNING []") + return [] # The case analysis hinges on what is at X_ijk. @@ -585,12 +585,12 @@ class CFGBoltzmann: if (how_far_into_the_RHS == (len(RHS_in_question) - 1)): # CASE A print("GPRIM CASE A ") - return xijk + return [xijk] else: # CASE B print("CASE B") reduct = self.Gprim(nonterminal_index, chosen_production, how_far_into_the_RHS + 1, exact_length_total - 1) - retstring = xijk + reduct + retstring = [xijk] + reduct return retstring @@ -624,7 +624,7 @@ class CFGBoltzmann: # and then the remaining part of the rule - rest_of_rule_generates = self.Gprim(nonterminal_index, chosen_production, how_far_into_the_RHS, exact_length_total - split_choice) + rest_of_rule_generates = self.Gprim(nonterminal_index, chosen_production, how_far_into_the_RHS + 1, exact_length_total - split_choice) retstring = nonterminal_generates + rest_of_rule_generates print ("Gprim returns", retstring) @@ -640,7 +640,7 @@ z = CFGBoltzmann(rules, list_of_nonterminals, list_of_terminals) rulepack_cooked = z.preprocessor() -print("XXXXXXXXXXXXXXXX", z.Gzero(0, 3)) +print("XXXXXXXXXXXXXXXX", z.Gzero(0, 5)) # Furthermore, we also note that the description of a context-free grammar is *itself* context-free -- GitLab