import re
import os
from saper.saperio.readers.reader import Reader


__author__ = 'blaz'


class PlainReader(Reader):
    """
    This reader reads plain text files and uses tagger to disambiguate it
    lexically and generate corpus2.Document.
    """
    def __init__(self, config, preserve_chunks=True):
        """
        The constructor creates tagger object.

        @type config: str
        @type preserve_chunks: str
        """
        super(PlainReader, self).__init__()
        self.preserve_chunks = preserve_chunks

    def read_file(self, file_path):
        """
        Reads selected file and returns lexically disambiguated
        corpus2.Document.

        @type file_path: str
        @rtype: corpus2.Document
        """
        fi = open(file_path, 'r')
        text = fi.read()
        fi.close()
        del(fi)
        tagged_document, _ = self.tagger.tagging_document(text, self.preserve_chunks,
                                                     path='%s;%s' % (file_path,
                                                                     file_path))
        return tagged_document

    def read_folder(self, folder_path, extension=None):
        """
        It reads the whole folder. The extension parameter selects the
        extension of files to be read.

        @type folder_path: str
        @type extension: str
        @rtype: corpus2.Document
        """
        files = os.listdir(folder_path)
        if extension:
            reg = re.compile("\.%s$" % extension)
            files = filter(reg.search, files)
        documents = []
        for f in files:
            path = '%s%s%s' % (folder_path,os.pathsep,f)
            documents.append(self.read_file(path))
        return documents