#!/usr/bin/env python from __future__ import absolute_import, division, print_function import codecs import sys import tarfile import pandas import re import unicodedata import threading from multiprocessing.pool import ThreadPool from six.moves import urllib from glob import glob from os import makedirs, path from bs4 import BeautifulSoup from tensorflow.python.platform import gfile from util.downloader import maybe_download """The number of jobs to run in parallel""" NUM_PARALLEL = 8 """Lambda function returns the filename of a path""" filename_of = lambda x: path.split(x)[1] class AtomicCounter(object): """A class that atomically increments a counter""" def __init__(self, start_count=0): """Initialize the counter :param start_count: the number to start counting at """ self.__lock = threading.Lock() self.__count = start_count def increment(self, amount=1): """Increments the counter by the given amount :param amount: the amount to increment by (default 1) :return: the incremented value of the counter """ self.__lock.acquire() self.__count += amount v = self.value() self.__lock.release() return v def value(self): """Returns the current value of the counter (not atomic)""" return self.__count def _parallel_downloader(voxforge_url, archive_dir, total, counter): """Generate a function to download a file based on given parameters This works by currying the above given arguments into a closure in the form of the following function. :param voxforge_url: the base voxforge URL :param archive_dir: the location to store the downloaded file :param total: the total number of files to download :param counter: an atomic counter to keep track of # of downloaded files :return: a function that actually downloads a file given these params """ def download(d): """Binds voxforge_url, archive_dir, total, and counter into this scope Downloads the given file :param d: a tuple consisting of (index, file) where index is the index of the file to download and file is the name of the file to download """ (i, file) = d download_url = voxforge_url + '/' + file c = counter.increment() print('Downloading file {} ({}/{})...'.format(i+1, c, total)) maybe_download(filename_of(download_url), archive_dir, download_url) return download def _parallel_extracter(data_dir, number_of_test, number_of_dev, total, counter): """Generate a function to extract a tar file based on given parameters This works by currying the above given arguments into a closure in the form of the following function. :param data_dir: the target directory to extract into :param number_of_test: the number of files to keep as the test set :param number_of_dev: the number of files to keep as the dev set :param total: the total number of files to extract :param counter: an atomic counter to keep track of # of extracted files :return: a function that actually extracts a tar file given these params """ def extract(d): """Binds data_dir, number_of_test, number_of_dev, total, and counter into this scope Extracts the given file :param d: a tuple consisting of (index, file) where index is the index of the file to extract and file is the name of the file to extract """ (i, archive) = d if i < number_of_test: dataset_dir = path.join(data_dir, "test") elif i0.5 and (wav_filesize/32000)<20 and transcript!="" and \ wav_filesize/len(transcript)>1400: files.append((path.abspath(wav_file), wav_filesize, transcript)) return pandas.DataFrame(data=files, columns=["wav_filename", "wav_filesize", "transcript"]) if __name__=="__main__": _download_and_preprocess_data(sys.argv[1])