from parlai.agents.programr.mappings.base import BasePatternMapping
import json, re

from parlai.agents.programr.mappings.storage.brainstorage import BrainStorage
# from parlai.agents.programr.utils.logging.ylogger import YLogger
import parlai.utils.logging as logging

class DenormalMapping(BasePatternMapping):

    def __init__(self):
        super().__init__()

    def denormalise_string(self, string):
        return self.replace_by_pattern(string)

    def load_from_database(self, storage: BrainStorage, collection_name):
        pairs = {}
        try:
            pairs = storage.load_denormal(collection_name)
            self._process_pairs(pairs)
        except Exception:
            # YLogger.warning(self, message="Can't connect to database")
            logging.warning("Can't connect to database")
            pairs = {}

        return len(pairs)