class PhraseCache: """An LRU cache for ttable lookups""" def __init__(self, max_size): self.max_size = max_size self.pairs_to_scores = OrderedDict() def get(self, source, target): key = (source,target) scores = self.pairs_to_scores.get(key,None) if scores: # cache hit - update access time del self.pairs_to_scores[key] self.pairs_to_scores[key] = scores return scores def add(self,source,target,scores): key = (source,target) self.pairs_to_scores[key] = scores while len(self.pairs_to_scores) > self.max_size: self.pairs_to_scores.popitem(last=False)
def __init__(self, max_size): self.max_size = max_size self.pairs_to_scores = OrderedDict()