Skip to content

Commit

Permalink
Add a way for generation items to not cache every example
Browse files Browse the repository at this point in the history
  • Loading branch information
SimGus committed Nov 3, 2019
1 parent d5acfdf commit b1ac3f8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
36 changes: 30 additions & 6 deletions chatette/units/generating_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def __init__(self, name, leading_space):

self._leading_space = leading_space

self._max_nb_cached_ex = None
self._total_nb_possibilities = None

# Cache: can contain a certain number of examples previously generated
Expand All @@ -53,6 +54,34 @@ def get_max_nb_possibilities(self):
def _compute_nb_possibilities(self):
"""Returns the number of possible examples this item can generate."""
raise NotImplementedError()

def get_max_cache_size(self):
"""
Returns the maximum number of examples
that should be cached for this item.
"""
if self._max_nb_cached_ex is None:
# raise ValueError("max number of cached examples was not set")
return 0
return self._max_nb_cached_ex
def configure_max_cache_level(self, level):
"""
Sets the maximum number of examples given the level of caching
that it is provided.
This number can be reconfigured if it was already set.
`level` is a number out of 100,
with 0 meaning no caching at all and 100 meaning caching everything.
The levels in-between are not clearly defined
and leave room for implementation.
@pre: `level` is a number between 0 and 100 (included).
"""
self._max_nb_cached_ex = \
float(self.get_max_nb_possibilities * level) / 100

def _reset_caches(self):
"""Resets the caches of examples and number of possibilities."""
self._total_nb_possibilities = None
self._cached_examples = []


def generate_random(self, **kwargs):
Expand Down Expand Up @@ -92,7 +121,7 @@ def generate_all(self):
if self._leading_space:
for ex in all_examples:
ex.prepend(' ')
if len(self._cached_examples) == 0:
if len(self._cached_examples) == 0 and self.get_max_cache_size() > 0:
# TODO don't cache it all in all cases
self._cached_examples = deepcopy(all_examples)
self._total_nb_possibilities = len(all_examples)
Expand Down Expand Up @@ -140,11 +169,6 @@ def _generate_n_strategy(self, n, **kwargs):
if loop_count > 10*n: # QUESTION is that a good idea?
break
return generated_examples

def _reset_caches(self):
"""Resets the caches of examples and number of possibilities."""
self._total_nb_possibilities = None
self._cached_examples = []

# @abstractmethod
# def short_description(self):
Expand Down
9 changes: 6 additions & 3 deletions chatette/units/modifiable/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,16 @@ def generate_random(self, **kwargs):
# TODO this is quite hacky to avoid code duplication in subclasses (and not use the decorator pattern to avoid having too many objects)
def generate_all(self, **kwargs):
"""Overriding."""
if len(self._cached_examples) == 0:
if len(self._cached_examples) < self.get_max_nb_possibilities():
basic_examples = self._generate_all_strategy(**kwargs)
if self._leading_space:
for ex in basic_examples:
ex.prepend(' ')
self._cached_examples = \
self._apply_modifiers_to_all(basic_examples)
all_examples = self._apply_modifiers_to_all(basic_examples)
if self.get_max_cache_size() > 0:
self._cached_examples = \
self._apply_modifiers_to_all(basic_examples)
return all_examples
return deepcopy(self._cached_examples)


Expand Down

0 comments on commit b1ac3f8

Please sign in to comment.