import os

import nltk  # todo remove dependency on nltk when user wants to use only spacy

from parlai.agents.programr.config.brain.nlp import BrainNLPConfiguration
from parlai.agents.programr.nlp.linguistic_features.sentence_segmentation import SentenceSegmentation
from parlai.agents.programr.nlp.linguistic_features.spell_checker import SpellChecker
from parlai.agents.programr.nlp.linguistic_features.tokenizer import Tokenizer
# from parlai.agents.programr.utils.logging.ylogger import YLogger
import parlai.utils.logging as logging


class NLP(object):

    def __init__(self, configuraion: BrainNLPConfiguration):
        #todo should check and load only neccessary libraries
        self._tokenizer = Tokenizer.factory(configuraion.tokenizer.libname)
        self._sentence_segmentation = SentenceSegmentation.factory(configuraion.sentence_segmentation.libname)
        self._spell_checker = SpellChecker.factory(configuraion.spelling.method)
        self._load_libraries(configuraion.nltk_data_dir)

    @property
    def tokenizer(self):
        return self._tokenizer

    @property
    def sentence_segmentation(self):
        return self._sentence_segmentation

    @property
    def spell_checker(self):
        return self._spell_checker

    def _load_libraries(self, nltk_data_dir):
        if nltk_data_dir and os.path.isdir(nltk_data_dir):

            if nltk_data_dir not in nltk.data.path:
                nltk.data.path.append(nltk_data_dir)

            try:
                nltk.download("popular", download_dir=nltk_data_dir, quiet=True)
            except Exception as excp:
                #install data in home directory due to problems in provided dir
                nltk.download("popular", quiet=True)
                # YLogger.exception(self, "wrong nltk directory. Install in home directory", excp)
                logging.error(f"wrong nltk directory. Install in home directory {excp}")

        else:
            nltk.download("popular", quiet=True)
            # YLogger.error(self, "No nltk directory. Install in home directory")
            logging.error("No nltk directory. Install in home directory")
import os

import nltk  # todo remove dependency on nltk when user wants to use only spacy

from parlai.agents.programr.config.brain.nlp import BrainNLPConfiguration
from parlai.agents.programr.nlp.linguistic_features.sentence_segmentation import SentenceSegmentation
from parlai.agents.programr.nlp.linguistic_features.spell_checker import SpellChecker
from parlai.agents.programr.nlp.linguistic_features.tokenizer import Tokenizer
# from parlai.agents.programr.utils.logging.ylogger import YLogger
import parlai.utils.logging as logging


class NLP(object):

    def __init__(self, configuraion: BrainNLPConfiguration):
        #todo should check and load only neccessary libraries
        self._tokenizer = Tokenizer.factory(configuraion.tokenizer.libname)
        self._sentence_segmentation = SentenceSegmentation.factory(configuraion.sentence_segmentation.libname)
        self._spell_checker = SpellChecker.factory(configuraion.spelling.method)
        self._load_libraries(configuraion.nltk_data_dir)

    @property
    def tokenizer(self):
        return self._tokenizer

    @property
    def sentence_segmentation(self):
        return self._sentence_segmentation

    @property
    def spell_checker(self):
        return self._spell_checker

    def _load_libraries(self, nltk_data_dir):
        if nltk_data_dir and os.path.isdir(nltk_data_dir):

            if nltk_data_dir not in nltk.data.path:
                nltk.data.path.append(nltk_data_dir)

            try:
                nltk.download("popular", download_dir=nltk_data_dir, quiet=True)
            except Exception as excp:
                #install data in home directory due to problems in provided dir
                nltk.download("popular", quiet=True)
                # YLogger.exception(self, "wrong nltk directory. Install in home directory", excp)
                logging.error(f"wrong nltk directory. Install in home directory {excp}")

        else:
            nltk.download("popular", quiet=True)
            # YLogger.error(self, "No nltk directory. Install in home directory")
            logging.error("No nltk directory. Install in home directory")
# todo remove dependency on nltk when user wants to use only spacy # from parlai.agents.programr.utils.logging.ylogger import YLogger #todo should check and load only neccessary libraries #install data in home directory due to problems in provided dir # YLogger.exception(self, "wrong nltk directory. Install in home directory", excp) # YLogger.error(self, "No nltk directory. Install in home directory")