Esempio n. 1
0
 def test_should_initialize_without_argument(self):
     # Given
     size_limit = 10
     # When
     d = LimitedSizeDict(size_limit=size_limit)
     # Then
     self.assertListEqual(list(d), [])
Esempio n. 2
0
 def test_should_initialize_with_argument(self):
     # Given
     sequence = [("a", 1), ("b", 2)]
     size_limit = 3
     # When
     d = LimitedSizeDict(sequence, size_limit=size_limit)
     # Then
     items = sorted(iteritems(d), key=lambda i: i[0])
     self.assertListEqual(items, sequence)
Esempio n. 3
0
 def test_should_wrong_when_initialization_should_raise_error(self):
     # Given
     sequence = [("a", 1), ("b", 2), ("c", 3)]
     size_limit = len(sequence) - 1
     # When/Then
     with self.assertRaises(ValueError) as ctx:
         LimitedSizeDict(sequence, size_limit=size_limit)
     self.assertEqual(str(ctx.exception.args[0]),
                      "Tried to initialize LimitedSizedDict with more "
                      "value than permitted with 'limit_size'")
Esempio n. 4
0
 def test_should_erase_items_when_updating(self):
     # Given
     sequence = [("a", 1), ("b", 2), ("c", 3), ("d", 4)]
     size_limit = len(sequence) - 2
     # When
     my_dict = LimitedSizeDict(sequence[:size_limit], size_limit=size_limit)
     for k, v in sequence[size_limit:]:
         my_dict[k] = v
     # Then
     items = sorted(list(iteritems(my_dict)), key=lambda i: i[0])
     self.assertListEqual(items, sequence[size_limit:])
Esempio n. 5
0
from __future__ import unicode_literals

from copy import deepcopy

from snips_nlu_utils import compute_all_ngrams

from snips_nlu.constants import (END, RES_MATCH_RANGE, START)
from snips_nlu.utils import LimitedSizeDict

_NGRAMS_CACHE = LimitedSizeDict(size_limit=1000)


def get_all_ngrams(tokens):
    if not tokens:
        return []
    key = "<||>".join(tokens)
    if key not in _NGRAMS_CACHE:
        ngrams = compute_all_ngrams(tokens, len(tokens))
        _NGRAMS_CACHE[key] = ngrams
    return deepcopy(_NGRAMS_CACHE[key])


def get_word_chunk(word, chunk_size, chunk_start, reverse=False):
    if chunk_size < 1:
        raise ValueError("chunk size should be >= 1")
    if chunk_size > len(word):
        return None
    start = chunk_start - chunk_size if reverse else chunk_start
    end = chunk_start if reverse else chunk_start + chunk_size
    return word[start:end]
Esempio n. 6
0
 def test_should_raise_when_no_size_limit(self):
     # Given/When/Then
     with self.assertRaises(ValueError) as ctx:
         LimitedSizeDict()
     self.assertEqual(str(ctx.exception.args[0]),
                      "'size_limit' must be passed as a keyword argument")
Esempio n. 7
0
 def __init__(self, language):
     self.language = language
     self.parser = _BuiltinEntityParser(language)
     self.supported_entities = get_supported_entities(language)
     self._cache = LimitedSizeDict(size_limit=1000)
Esempio n. 8
0
 def __init__(self, parser):
     self._parser = parser
     self._cache = LimitedSizeDict(size_limit=1000)