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


__author__ = 'blaz'


class CclReader(Reader):
    """
    CclReader is as the name says, the ccl reader. It reads ccl files and
    returns corpus2.Document
    """
    def __init__(self, config):
        pass

    def read_file(self, file_path):
        """
        This method reads selected ccl file and returns corpsu2.DocumentPtr.

        @type file_path: str
        @rtype: corpus2.DocumentPtr
        """
        tagset = corpus2.get_named_tagset('nkjp')
        doc_reader = corpus2.CclRelReader(tagset, str(file_path),
                                          str(file_path))
        doc_reader.set_option("disamb_only")
        document = doc_reader.read()
        del doc_reader
        return document

    def read_folder(self, folder_path, extension="xml"):
        """
        This method reads folder and returns the list of corpus2.DocumentPtrs.
        The extension parameter indicates which files should be read.

        @type folder_path: str
        @type extension: str
        """
        files = os.listdir(folder_path)
        reg = re.compile('\.%s$' % extension)
        files = filter(reg.search, files)
        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