<commit_before>"""
Copyright 2016 <NAME> and <NAME>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import os
import subprocess
import ast


def audio_generator(dict_dir, text, output_dest):

    with open(dict_dir + "/myDict.py") as f:
        myDict = ast.literal_eval(f.read())

    # Open a text file to put in the data we receive from this module
    output_file = open(dict_dir + '/list.txt', 'w')

    # Split the text on spaces and create a list with them
    textList = text.split(" ")

    # Loop through the list to receive the address of the audio file
    # associated with the input given in speech.text
    for i in range(len(textList)):
        if textList[i] in myDict.keys():
            output_file.write("file " + myDict[textList[i]] + "\n")

    # If a file with the default name exits, create a new name with a
    # new suffix
    res = 0
    while(os.path.exists(output_dest + "/output" + str(res) + ".wav")):
        res += 1

    # Use ffmpeg to process the generated list.txt and create a new audio file
    # containing the speech of the text you had as input
    subprocess.Popen('ffmpeg -y -f concat -i ' + dict_dir +
                     '/list.txt -c copy ' + output_dest + '/output' +
                     str(res) + '.wav', shell=True, stdout=subprocess.PIPE,
                     stderr=subprocess.PIPE)
    print ("Success! Your requested audio file is now at " + output_dest +
           "/output" + str(res) + ".wav")<commit_msg>Improve and Bug fix for generator<commit_after>"""
Copyright 2016 <NAME>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import os
import subprocess
import ast
from distutils.spawn import find_executable


def audio_generator(dict_dir, text, output_dest):

    with open(dict_dir + "/myDict.py") as f:
        myDict = ast.literal_eval(f.read())

    # Open a text file to put in the data we receive from this module
    with open(dict_dir + '/list.txt', 'w') as f:
        # output_file = f.read()

        # Split the text on spaces and create a list with them
        textList = text.split(" ")

        # Loop through the list to receive the address of the audio file
        # associated with the input given in speech.text
        for i in range(len(textList)):
            if textList[i] in myDict.keys():
                f.write("file " + myDict[textList[i]] + "\n")

    # If a file with the default name exits, create a new name with a
    # new suffix
    res = 0
    while(os.path.exists(output_dest + "/output" + str(res) + ".wav")):
        res += 1

    # Check to see if either ffmpeg or avconv is installed and assigns it
    ffmpeg = os.path.basename(find_executable("ffmpeg") or
                              find_executable("avconv"))
    if ffmpeg is None:
        raise Exception("Either ffmpeg or avconv is needed. " +
                        "Neither is installed or accessible")

    # Use ffmpeg to process the generated list.txt and create a new audio file
    # containing the speech of the text you had as input
    subprocess.Popen(ffmpeg + ' -y -f concat -i ' + dict_dir +
                     '/list.txt -c copy ' + output_dest + '/output' +
                     str(res) + '.wav', shell=True,
                     universal_newlines=True).communicate()
    # Delete the unecessary files that was made for ffmpeg use
    os.remove(dict_dir + '/list.txt')

    if os.path.exists(output_dest + "/output" + str(res) + ".wav"):
        print ('\033[94m' + "Speech-Hacker: " +
               "Your audio was generated at: " + output_dest + "/output" +
               str(res) + ".wav" + '\033[0m')
    else:
        print ("Speech-Hacker: " '\033[91m' +
            "Failed to generate your requested audio." + '\033[0m')
{'fraction_non_alphanumeric': 0.09681555888010258, 'fraction_numerical': 0.008121393460141057, 'mean_word_length': 2.6335403726708075, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3595279', 'n_tokens_mistral': 1388, 'n_tokens_neox': 1319, 'n_words': 546}