Skip to content
Snippets Groups Projects
Commit 92ea3f3b authored by Kia's avatar Kia
Browse files

Gzero and normalized_choice

parent 5312c7a6
No related branches found
No related tags found
No related merge requests found
import collections
import functools
import random
from enum import *
......@@ -504,6 +503,29 @@ class CFGBoltzmann:
# Now we do generation of the strings.
# This returns an *index* (not the item at the index) from a list, weighted
# by the integer at each element.
def normalized_choice(self, inlist):
total_weight = sum(inlist)
weights = [x / total_weight for x in inlist]
choice = random.choices(range(len(inlist)), weights=weights)
return choice
# Similar to Fzero and Fprim, we have Gzero and Gprim.
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)
z = CFGBoltzmann(rules, list_of_nonterminals, list_of_terminals)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment