<commit_before>"""A set of tools to process files"""

import logging
import multiprocessing
from pathlib import Path
import random
import signal

import numpy as np

from guesslang.extractor import extract


LOGGER = logging.getLogger(__name__)

random.seed()

_FILE_ENCODINGS = ('utf-8', 'latin-1', 'windows-1250', 'windows-1252')
_NB_LINES = 100
_NB_FILES_MIN = 10


class GuesslangError(Exception):
    """`guesslang` base exception class"""


def search_files(source, extensions):
    """Name of the files with the right extensions
    found in source directory and its subdirectories.

    :raise GuesslangError: when there is not enough files in the directory
    :param str source: directory name
    :param str extensions: list of file extensions
    :return: filenames
    :rtype: list
    """
    files = [
        path for path in Path(source).glob('**/*')
        if path.is_file() and path.suffix.lstrip('.') in extensions]
    nb_files = len(files)
    LOGGER.debug("Total files found: %d", nb_files)

    if nb_files < _NB_FILES_MIN:
        LOGGER.error("Too few source files")
        raise GuesslangError(
            '{} source files found in {}. {} files minimum is required'.format(
                nb_files, source, _NB_FILES_MIN))

    random.shuffle(files)
    return files


def extract_from_files(files, languages):
    """Extract arrays of features from the given files.

    :param list files: list of filenames
    :param dict languages: language name => associated file extension list
    :return: features
    :rtype: tuple<commit_msg>Fix `search_files` documentation<commit_after>"""A set of tools to process files"""

import logging
import multiprocessing
from pathlib import Path
import random
import signal

import numpy as np

from guesslang.extractor import extract


LOGGER = logging.getLogger(__name__)

random.seed()

_FILE_ENCODINGS = ('utf-8', 'latin-1', 'windows-1250', 'windows-1252')
_NB_LINES = 100
_NB_FILES_MIN = 10


class GuesslangError(Exception):
    """`guesslang` base exception class"""


def search_files(source, extensions):
    """Name of the files with the right extensions
    found in source directory and its subdirectories.

    :raise GuesslangError: when there is not enough files in the directory
    :param str source: directory name
    :param list extensions: list of file extensions
    :return: filenames
    :rtype: list
    """
    files = [
        path for path in Path(source).glob('**/*')
        if path.is_file() and path.suffix.lstrip('.') in extensions]
    nb_files = len(files)
    LOGGER.debug("Total files found: %d", nb_files)

    if nb_files < _NB_FILES_MIN:
        LOGGER.error("Too few source files")
        raise GuesslangError(
            '{} source files found in {}. {} files minimum is required'.format(
                nb_files, source, _NB_FILES_MIN))

    random.shuffle(files)
    return files


def extract_from_files(files, languages):
    """Extract arrays of features from the given files.

    :param list files: list of filenames
    :param dict languages: language name => associated file extension list
    :return: features
    :rtype: tuple
{'fraction_non_alphanumeric': 0.09454314720812183, 'fraction_numerical': 0.00951776649746193, 'mean_word_length': 3.472340425531915, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7416897', 'n_tokens_mistral': 963, 'n_tokens_neox': 914, 'n_words': 325}