Verified Commit df1a78ab authored by Vladimír Štill's avatar Vladimír Štill
Browse files

CFL: A simple big speed optimization for random generation

parent 473edc7b
......@@ -826,9 +826,13 @@ class CFGRandom:
weights: List[int] = []
for prod in self.cfg.rules[sym]:
cand = sentence[:i] + prod + sentence[i + 1:]
w = self._materialize_prod(cand, length)
suff = prod + sentence[i + 1:]
# note: no nonterminals before i ~> the weight of the whole
# candidate is the same as the weight of its suffix starting at
# position i
w = self._materialize_prod(suff, length - i)
if w > 0:
cand = sentence[:i] + suff
candidates.append(cand)
weights.append(w)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment