diff --git a/egs/aishell2/s5/local/word_segmentation.py b/egs/aishell2/s5/local/word_segmentation.py index eb7bb648970..4ce55a2003e 100644 --- a/egs/aishell2/s5/local/word_segmentation.py +++ b/egs/aishell2/s5/local/word_segmentation.py @@ -4,6 +4,7 @@ # 2018 Beijing Shell Shell Tech. Co. Ltd. (Author: Hui BU) # Apache 2.0 +from __future__ import print_function import sys import jieba reload(sys) diff --git a/egs/ami/s5/local/sort_bad_utts.py b/egs/ami/s5/local/sort_bad_utts.py index f84fcb12608..baabdc73508 100644 --- a/egs/ami/s5/local/sort_bad_utts.py +++ b/egs/ami/s5/local/sort_bad_utts.py @@ -1,5 +1,6 @@ #!/usr/bin/env python +from __future__ import print_function import sys import argparse import logging @@ -38,10 +39,10 @@ def GetSortedWers(utt_info_file): utt_wer_sorted = sorted(utt_wer, key = lambda k : k[1]) try: import numpy as np - bins = range(0,105,5) + bins = list(range(0,105,5)) bins.append(sys.float_info.max) - hist, bin_edges = np.histogram(map(lambda x: x[1], utt_wer_sorted), + hist, bin_edges = np.histogram([x[1] for x in utt_wer_sorted], bins = bins) num_utts = len(utt_wer) string = '' diff --git a/egs/an4/s5/local/data_prep.py b/egs/an4/s5/local/data_prep.py index 24cb9bffb07..9d8083f3b60 100644 --- a/egs/an4/s5/local/data_prep.py +++ b/egs/an4/s5/local/data_prep.py @@ -15,6 +15,7 @@ # See the Apache 2 License for the specific language governing permissions and # limitations under the License. +from __future__ import print_function import os import re import sys diff --git a/egs/an4/s5/local/lexicon_prep.py b/egs/an4/s5/local/lexicon_prep.py index 8d451daf869..3584fa86dfb 100644 --- a/egs/an4/s5/local/lexicon_prep.py +++ b/egs/an4/s5/local/lexicon_prep.py @@ -15,6 +15,7 @@ # See the Apache 2 License for the specific language governing permissions and # limitations under the License. +from __future__ import print_function import os import re import sys diff --git a/egs/aspire/s5/local/multi_condition/create_uniform_segments.py b/egs/aspire/s5/local/multi_condition/create_uniform_segments.py index e7baafc028c..010811490ef 100755 --- a/egs/aspire/s5/local/multi_condition/create_uniform_segments.py +++ b/egs/aspire/s5/local/multi_condition/create_uniform_segments.py @@ -4,13 +4,14 @@ # creates a segments file in the provided data directory # into uniform segments with specified window and overlap +from __future__ import division import imp, sys, argparse, os, math, subprocess min_segment_length = 10 # in seconds def segment(total_length, window_length, overlap = 0): increment = window_length - overlap num_windows = int(math.ceil(float(total_length)/increment)) - segments = map(lambda x: (x * increment, min( total_length, (x * increment) + window_length)), range(0, num_windows)) + segments = [(x * increment, min( total_length, (x * increment) + window_length)) for x in range(0, num_windows)] if segments[-1][1] - segments[-1][0] < min_segment_length: segments[-2] = (segments[-2][0], segments[-1][1]) segments.pop() @@ -53,7 +54,7 @@ def prepare_segments_file(kaldi_data_dir, window_length, overlap): parser = argparse.ArgumentParser() parser.add_argument('--window-length', type = float, default = 30.0, help = 'length of the window used to cut the segment') parser.add_argument('--overlap', type = float, default = 5.0, help = 'overlap of neighboring windows') - parser.add_argument('data_dir', type=str, help='directory such as data/train') + parser.add_argument('data_dir', help='directory such as data/train') params = parser.parse_args() diff --git a/egs/aspire/s5/local/multi_condition/fill_missing_recordings.py b/egs/aspire/s5/local/multi_condition/fill_missing_recordings.py index e249e54e5f6..2b4bcddda69 100755 --- a/egs/aspire/s5/local/multi_condition/fill_missing_recordings.py +++ b/egs/aspire/s5/local/multi_condition/fill_missing_recordings.py @@ -38,14 +38,14 @@ def fill_ctm(input_ctm_file, output_ctm_file, recording_names): sys.stderr.write(str(" ".join(sys.argv))) parser = argparse.ArgumentParser(usage) - parser.add_argument('input_ctm_file', type=str, help='ctm file for the recordings') - parser.add_argument('output_ctm_file', type=str, help='ctm file for the recordings') - parser.add_argument('recording_name_file', type=str, help='file with names of the recordings') + parser.add_argument('input_ctm_file', help='ctm file for the recordings') + parser.add_argument('output_ctm_file', help='ctm file for the recordings') + parser.add_argument('recording_name_file', help='file with names of the recordings') params = parser.parse_args() try: - file_names = map(lambda x: x.strip(), open("{0}".format(params.recording_name_file)).readlines()) + file_names = [x.strip() for x in open("{0}".format(params.recording_name_file)).readlines()] except IOError: raise Exception("Expected to find {0}".format(params.recording_name_file)) diff --git a/egs/aspire/s5/local/multi_condition/get_air_file_patterns.py b/egs/aspire/s5/local/multi_condition/get_air_file_patterns.py index cc06f58616a..1f06d3e7c3b 100755 --- a/egs/aspire/s5/local/multi_condition/get_air_file_patterns.py +++ b/egs/aspire/s5/local/multi_condition/get_air_file_patterns.py @@ -3,6 +3,7 @@ # script to generate the file_patterns of the AIR database # see load_air.m file in AIR db to understand the naming convention +from __future__ import print_function import sys, glob, re, os.path air_dir = sys.argv[1] @@ -45,4 +46,4 @@ file_patterns.append(file_pattern+" "+output_file_name) file_patterns = list(set(file_patterns)) file_patterns.sort() -print "\n".join(file_patterns) +print("\n".join(file_patterns)) diff --git a/egs/aspire/s5/local/multi_condition/normalize_wavs.py b/egs/aspire/s5/local/multi_condition/normalize_wavs.py index dabf420d9f8..6e67d2113c1 100755 --- a/egs/aspire/s5/local/multi_condition/normalize_wavs.py +++ b/egs/aspire/s5/local/multi_condition/normalize_wavs.py @@ -3,6 +3,8 @@ # normalizes the wave files provided in input file list with a common scaling factor # the common scaling factor is computed to 1/\sqrt(1/(total_samples) * \sum_i{\sum_j x_i(j)^2}) where total_samples is sum of all samples of all wavefiles. If the data is multi-channel then each channel is treated as a seperate wave files +from __future__ import division +from __future__ import print_function import argparse, scipy.io.wavfile, warnings, numpy as np, math def get_normalization_coefficient(file_list, is_rir, additional_scaling): @@ -29,7 +31,7 @@ def get_normalization_coefficient(file_list, is_rir, additional_scaling): assert(rate == sampling_rate) else: sampling_rate = rate - data = data / dtype_max_value + data = data/dtype_max_value if is_rir: # just count the energy of the direct impulse response # this is treated as energy of signal from 0.001 seconds before impulse @@ -55,8 +57,8 @@ def get_normalization_coefficient(file_list, is_rir, additional_scaling): except IOError: warnings.warn("Did not find the file {0}.".format(file)) assert(total_samples > 0) - scaling_coefficient = np.sqrt(total_samples / total_energy) - print "Scaling coefficient is {0}.".format(scaling_coefficient) + scaling_coefficient = np.sqrt(total_samples/total_energy) + print("Scaling coefficient is {0}.".format(scaling_coefficient)) if math.isnan(scaling_coefficient): raise Exception(" Nan encountered while computing scaling coefficient. This is mostly due to numerical overflow") return scaling_coefficient diff --git a/egs/aspire/s5/local/multi_condition/read_rir.py b/egs/aspire/s5/local/multi_condition/read_rir.py index a2e1c2052e2..04898bda760 100755 --- a/egs/aspire/s5/local/multi_condition/read_rir.py +++ b/egs/aspire/s5/local/multi_condition/read_rir.py @@ -29,9 +29,9 @@ def usage(): #sys.stderr.write(" ".join(sys.argv)+"\n") parser = argparse.ArgumentParser(usage()) parser.add_argument('--output-sampling-rate', type = int, default = 8000, help = 'sampling rate of the output') - parser.add_argument('type', type = str, default = None, help = 'database type', choices = ['air']) - parser.add_argument('input', type = str, default = None, help = 'directory containing the multi-channel data for a particular recording, or file name or file-regex-pattern') - parser.add_argument('output_filename', type = str, default = None, help = 'output filename (if "-" then output is written to output pipe)') + parser.add_argument('type', default = None, help = 'database type', choices = ['air']) + parser.add_argument('input', default = None, help = 'directory containing the multi-channel data for a particular recording, or file name or file-regex-pattern') + parser.add_argument('output_filename', default = None, help = 'output filename (if "-" then output is written to output pipe)') params = parser.parse_args() if params.output_filename == "-": diff --git a/egs/aspire/s5/local/multi_condition/reverberate_wavs.py b/egs/aspire/s5/local/multi_condition/reverberate_wavs.py index 998a3ed5e74..f43e4a2f894 100755 --- a/egs/aspire/s5/local/multi_condition/reverberate_wavs.py +++ b/egs/aspire/s5/local/multi_condition/reverberate_wavs.py @@ -4,18 +4,20 @@ # script to generate multicondition training data / dev data / test data import argparse, glob, math, os, random, scipy.io.wavfile, sys -class list_cyclic_iterator: +class list_cyclic_iterator(object): def __init__(self, list, random_seed = 0): self.list_index = 0 self.list = list random.seed(random_seed) random.shuffle(self.list) - def next(self): + def __next__(self): item = self.list[self.list_index] self.list_index = (self.list_index + 1) % len(self.list) return item + next = __next__ # for Python 2 + def return_nonempty_lines(lines): new_lines = [] for line in lines: @@ -71,15 +73,15 @@ def return_nonempty_lines(lines): for i in range(len(wav_files)): wav_file = " ".join(wav_files[i].split()[1:]) output_wav_file = wav_out_files[i] - impulse_file = impulses.next() + impulse_file = next(impulses) noise_file = '' snr = '' found_impulse = False if add_noise: - for i in xrange(len(impulse_noise_index)): + for i in range(len(impulse_noise_index)): if impulse_file in impulse_noise_index[i][0]: - noise_file = impulse_noise_index[i][1].next() - snr = snrs.next() + noise_file = next(impulse_noise_index[i][1]) + snr = next(snrs) assert(len(wav_file.strip()) > 0) assert(len(impulse_file.strip()) > 0) assert(len(noise_file.strip()) > 0) diff --git a/egs/babel/s5b/local/lonestar.py b/egs/babel/s5b/local/lonestar.py index e1594e55ada..809f99b22cf 100755 --- a/egs/babel/s5b/local/lonestar.py +++ b/egs/babel/s5b/local/lonestar.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function from pylauncher import * import pylauncher import sys @@ -39,7 +40,7 @@ def KaldiLauncher(lo, **kwargs): logfiles = list() commands = list() - for q in xrange(lo.jobstart, lo.jobend+1): + for q in range(lo.jobstart, lo.jobend+1): s = "bash " + lo.queue_scriptfile + " " + str(q) commands.append(s) @@ -74,7 +75,7 @@ def KaldiLauncher(lo, **kwargs): time.sleep(delay); lines=tail(10, logfile) - with_status=filter(lambda x:re.search(r'with status (\d+)', x), lines) + with_status=[x for x in lines if re.search(r'with status (\d+)', x)] if len(with_status) == 0: sys.stderr.write("The last line(s) of the log-file " + logfile + " does not seem" @@ -98,7 +99,7 @@ def KaldiLauncher(lo, **kwargs): sys.exit(-1); #Remove service files. Be careful not to remove something that might be needed in problem diagnostics - for i in xrange(len(commands)): + for i in range(len(commands)): out_file=os.path.join(qdir, ce.outstring+str(i)) #First, let's wait on files missing (it might be that those are missing @@ -149,7 +150,7 @@ def KaldiLauncher(lo, **kwargs): #print job.final_report() -class LauncherOpts: +class LauncherOpts(object): def __init__(self): self.sync=0 self.nof_threads = 1 @@ -199,7 +200,7 @@ def CmdLineParser(argv): jobend=int(m.group(2)) argv.pop(0) elif re.match("^.+=.*:.*$", argv[0]): - print >> sys.stderr, "warning: suspicious JOB argument " + argv[0]; + print("warning: suspicious JOB argument " + argv[0], file=sys.stderr); if jobstart > jobend: sys.stderr.write("lonestar.py: JOBSTART("+ str(jobstart) + ") must be lower than JOBEND(" + str(jobend) + ")\n") @@ -238,8 +239,8 @@ def setup_paths_and_vars(opts): cwd = os.getcwd() if opts.varname and (opts.varname not in opts.logfile ) and (opts.jobstart != opts.jobend): - print >>sys.stderr, "lonestar.py: you are trying to run a parallel job" \ - "but you are putting the output into just one log file (" + opts.logfile + ")"; + print("lonestar.py: you are trying to run a parallel job" \ + "but you are putting the output into just one log file (" + opts.logfile + ")", file=sys.stderr); sys.exit(1) if not os.path.isabs(opts.logfile): @@ -261,8 +262,8 @@ def setup_paths_and_vars(opts): taskname=os.path.basename(queue_logfile) taskname = taskname.replace(".log", ""); if taskname == "": - print >> sys.stderr, "lonestar.py: you specified the log file name in such form " \ - "that leads to an empty task name ("+logfile + ")"; + print("lonestar.py: you specified the log file name in such form " \ + "that leads to an empty task name ("+logfile + ")", file=sys.stderr); sys.exit(1) if not os.path.isabs(queue_logfile): diff --git a/egs/babel/s5b/local/resegment/segmentation.py b/egs/babel/s5b/local/resegment/segmentation.py index 7c5c8665a16..aed65a4ca14 100755 --- a/egs/babel/s5b/local/resegment/segmentation.py +++ b/egs/babel/s5b/local/resegment/segmentation.py @@ -3,6 +3,7 @@ # Copyright 2014 Vimal Manohar # Apache 2.0 +from __future__ import division import os, glob, argparse, sys, re, time from argparse import ArgumentParser @@ -19,12 +20,12 @@ def mean(l): if len(l) > 0: - return float(sum(l)) / len(l) + return (float(sum(l))/len(l)) return 0 # Analysis class # Stores statistics like the confusion matrix, length of the segments etc. -class Analysis: +class Analysis(object): def __init__(self, file_id, frame_shift, prefix): self.confusion_matrix = [0] * 9 self.type_counts = [ [[] for j in range(0,9)] for i in range(0,3) ] @@ -274,8 +275,8 @@ def read_rttm_file(rttm_file, temp_dir, frame_shift): i = len(this_file) category = splits[6] word = splits[5] - start_time = int(float(splits[3])/frame_shift + 0.5) - duration = int(float(splits[4])/frame_shift + 0.5) + start_time = int((float(splits[3])/frame_shift) + 0.5) + duration = int((float(splits[4])/frame_shift) + 0.5) if i < start_time: this_file.extend(["0"]*(start_time - i)) if type1 == "NON-LEX": @@ -295,7 +296,7 @@ def read_rttm_file(rttm_file, temp_dir, frame_shift): # Stats class to store some basic stats about the number of # times the post-processor goes through particular loops or blocks # of code in the algorithm. This is just for debugging. -class Stats: +class Stats(object): def __init__(self): self.inter_utt_nonspeech = 0 self.merge_nonspeech_segment = 0 @@ -321,7 +322,7 @@ def reset(self): self.noise_only = 0 # Timer class to time functions -class Timer: +class Timer(object): def __enter__(self): self.start = time.clock() return self @@ -332,7 +333,7 @@ def __exit__(self, *args): # The main class for post-processing a file. # This does the segmentation either looking at the file isolated # or by looking at both classes simultaneously -class JointResegmenter: +class JointResegmenter(object): def __init__(self, P, A, f, options, phone_map, stats = None, reference = None): # Pointers to prediction arrays and Initialization @@ -1290,22 +1291,22 @@ def main(): dest='hard_max_segment_length', default=15.0, \ help="Hard maximum on the segment length above which the segment " \ + "will be broken even if in the middle of speech (default: %(default)s)") - parser.add_argument('--first-separator', type=str, \ + parser.add_argument('--first-separator', \ dest='first_separator', default="-", \ help="Separator between recording-id and start-time (default: %(default)s)") - parser.add_argument('--second-separator', type=str, \ + parser.add_argument('--second-separator', \ dest='second_separator', default="-", \ help="Separator between start-time and end-time (default: %(default)s)") - parser.add_argument('--remove-noise-only-segments', type=str, \ + parser.add_argument('--remove-noise-only-segments', \ dest='remove_noise_only_segments', default="true", choices=("true", "false"), \ help="Remove segments that have only noise. (default: %(default)s)") parser.add_argument('--min-inter-utt-silence-length', type=float, \ dest='min_inter_utt_silence_length', default=1.0, \ help="Minimum silence that must exist between two separate utterances (default: %(default)s)"); - parser.add_argument('--channel1-file', type=str, \ + parser.add_argument('--channel1-file', \ dest='channel1_file', default="inLine", \ help="String that matches with the channel 1 file (default: %(default)s)") - parser.add_argument('--channel2-file', type=str, \ + parser.add_argument('--channel2-file', \ dest='channel2_file', default="outLine", \ help="String that matches with the channel 2 file (default: %(default)s)") parser.add_argument('--isolated-resegmentation', \ @@ -1388,7 +1389,7 @@ def main(): speech_cap = None if options.speech_cap_length != None: - speech_cap = int( options.speech_cap_length / options.frame_shift ) + speech_cap = int(options.speech_cap_length/options.frame_shift) # End if for f in pred_files: @@ -1454,7 +1455,7 @@ def main(): f2 = f3 # End if - if (len(A1) - len(A2)) > options.max_length_diff / options.frame_shift: + if (len(A1) - len(A2)) > int(options.max_length_diff/options.frame_shift): sys.stderr.write( \ "%s: Warning: Lengths of %s and %s differ by more than %f. " \ % (sys.argv[0], f1,f2, options.max_length_diff) \ diff --git a/egs/babel/s5c/local/lonestar.py b/egs/babel/s5c/local/lonestar.py index e1594e55ada..809f99b22cf 100755 --- a/egs/babel/s5c/local/lonestar.py +++ b/egs/babel/s5c/local/lonestar.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from __future__ import print_function from pylauncher import * import pylauncher import sys @@ -39,7 +40,7 @@ def KaldiLauncher(lo, **kwargs): logfiles = list() commands = list() - for q in xrange(lo.jobstart, lo.jobend+1): + for q in range(lo.jobstart, lo.jobend+1): s = "bash " + lo.queue_scriptfile + " " + str(q) commands.append(s) @@ -74,7 +75,7 @@ def KaldiLauncher(lo, **kwargs): time.sleep(delay); lines=tail(10, logfile) - with_status=filter(lambda x:re.search(r'with status (\d+)', x), lines) + with_status=[x for x in lines if re.search(r'with status (\d+)', x)] if len(with_status) == 0: sys.stderr.write("The last line(s) of the log-file " + logfile + " does not seem" @@ -98,7 +99,7 @@ def KaldiLauncher(lo, **kwargs): sys.exit(-1); #Remove service files. Be careful not to remove something that might be needed in problem diagnostics - for i in xrange(len(commands)): + for i in range(len(commands)): out_file=os.path.join(qdir, ce.outstring+str(i)) #First, let's wait on files missing (it might be that those are missing @@ -149,7 +150,7 @@ def KaldiLauncher(lo, **kwargs): #print job.final_report() -class LauncherOpts: +class LauncherOpts(object): def __init__(self): self.sync=0 self.nof_threads = 1 @@ -199,7 +200,7 @@ def CmdLineParser(argv): jobend=int(m.group(2)) argv.pop(0) elif re.match("^.+=.*:.*$", argv[0]): - print >> sys.stderr, "warning: suspicious JOB argument " + argv[0]; + print("warning: suspicious JOB argument " + argv[0], file=sys.stderr); if jobstart > jobend: sys.stderr.write("lonestar.py: JOBSTART("+ str(jobstart) + ") must be lower than JOBEND(" + str(jobend) + ")\n") @@ -238,8 +239,8 @@ def setup_paths_and_vars(opts): cwd = os.getcwd() if opts.varname and (opts.varname not in opts.logfile ) and (opts.jobstart != opts.jobend): - print >>sys.stderr, "lonestar.py: you are trying to run a parallel job" \ - "but you are putting the output into just one log file (" + opts.logfile + ")"; + print("lonestar.py: you are trying to run a parallel job" \ + "but you are putting the output into just one log file (" + opts.logfile + ")", file=sys.stderr); sys.exit(1) if not os.path.isabs(opts.logfile): @@ -261,8 +262,8 @@ def setup_paths_and_vars(opts): taskname=os.path.basename(queue_logfile) taskname = taskname.replace(".log", ""); if taskname == "": - print >> sys.stderr, "lonestar.py: you specified the log file name in such form " \ - "that leads to an empty task name ("+logfile + ")"; + print("lonestar.py: you specified the log file name in such form " \ + "that leads to an empty task name ("+logfile + ")", file=sys.stderr); sys.exit(1) if not os.path.isabs(queue_logfile): diff --git a/egs/babel/s5c/local/resegment/segmentation.py b/egs/babel/s5c/local/resegment/segmentation.py index 7c5c8665a16..4bdb0fea75c 100755 --- a/egs/babel/s5c/local/resegment/segmentation.py +++ b/egs/babel/s5c/local/resegment/segmentation.py @@ -3,6 +3,7 @@ # Copyright 2014 Vimal Manohar # Apache 2.0 +from __future__ import division import os, glob, argparse, sys, re, time from argparse import ArgumentParser @@ -19,12 +20,12 @@ def mean(l): if len(l) > 0: - return float(sum(l)) / len(l) + return (float(sum(l))/len(l)) return 0 # Analysis class # Stores statistics like the confusion matrix, length of the segments etc. -class Analysis: +class Analysis(object): def __init__(self, file_id, frame_shift, prefix): self.confusion_matrix = [0] * 9 self.type_counts = [ [[] for j in range(0,9)] for i in range(0,3) ] @@ -274,7 +275,7 @@ def read_rttm_file(rttm_file, temp_dir, frame_shift): i = len(this_file) category = splits[6] word = splits[5] - start_time = int(float(splits[3])/frame_shift + 0.5) + start_time = int((float(splits[3])/frame_shift) + 0.5) duration = int(float(splits[4])/frame_shift + 0.5) if i < start_time: this_file.extend(["0"]*(start_time - i)) @@ -295,7 +296,7 @@ def read_rttm_file(rttm_file, temp_dir, frame_shift): # Stats class to store some basic stats about the number of # times the post-processor goes through particular loops or blocks # of code in the algorithm. This is just for debugging. -class Stats: +class Stats(object): def __init__(self): self.inter_utt_nonspeech = 0 self.merge_nonspeech_segment = 0 @@ -321,7 +322,7 @@ def reset(self): self.noise_only = 0 # Timer class to time functions -class Timer: +class Timer(object): def __enter__(self): self.start = time.clock() return self @@ -332,7 +333,7 @@ def __exit__(self, *args): # The main class for post-processing a file. # This does the segmentation either looking at the file isolated # or by looking at both classes simultaneously -class JointResegmenter: +class JointResegmenter(object): def __init__(self, P, A, f, options, phone_map, stats = None, reference = None): # Pointers to prediction arrays and Initialization @@ -351,9 +352,9 @@ def __init__(self, P, A, f, options, phone_map, stats = None, reference = None): self.frame_shift = options.frame_shift # Convert length in seconds to frames - self.max_frames = int(options.max_segment_length / options.frame_shift) - self.hard_max_frames = int(options.hard_max_segment_length / options.frame_shift) - self.min_inter_utt_nonspeech_length = int(options.min_inter_utt_silence_length / options.frame_shift) + self.max_frames = int(options.max_segment_length/options.frame_shift) + self.hard_max_frames = int(options.hard_max_segment_length/options.frame_shift) + self.min_inter_utt_nonspeech_length = int(options.min_inter_utt_silence_length, options.frame_shift) if ( options.remove_noise_only_segments == "false" ): self.remove_noise_segments = False elif ( options.remove_noise_only_segments == "true" ): @@ -540,7 +541,7 @@ def set_nonspeech_proportion(self): # Set the number of non-speech frames to be added depending on the # silence proportion. The target number of frames in the segments # is computed as below: - target_segment_frames = int(num_speech_frames / (1.0 - self.options.silence_proportion)) + target_segment_frames = int(num_speech_frames/(1.0 - self.options.silence_proportion)) # The number of frames currently in the segments num_segment_frames = num_speech_frames @@ -599,7 +600,7 @@ def set_nonspeech_proportion(self): if not changed: # avoid an infinite loop. if no changes, then break. break if num_segment_frames < target_segment_frames: - proportion = float(num_segment_frames - num_speech_frames) / num_segment_frames + proportion = float(num_segment_frames - num_speech_frames)/num_segment_frames sys.stderr.write("%s: Warning: for recording %s, only got a proportion %f of non-speech frames, versus target %f\n" % (sys.argv[0], self.file_id, proportion, self.options.silence_proportion)) ########################################################################### @@ -863,14 +864,14 @@ def split_long_segments(self): # Count the number of times long segments are split self.stats.split_segments += 1 - num_pieces = int((float(segment_length) / self.hard_max_frames) + 0.99999) + num_pieces = int((float(segment_length)/self.hard_max_frames) + 0.99999) sys.stderr.write("%s: Warning: for recording %s, " \ % (sys.argv[0], self.file_id) \ + "splitting segment of length %f seconds into %d pieces " \ % (segment_length * self.frame_shift, num_pieces) \ + "(--hard-max-segment-length %f)\n" \ % self.options.hard_max_segment_length) - frames_per_piece = int(segment_length / num_pieces) + frames_per_piece = int(segment_length/num_pieces) for i in range(1,num_pieces): q = n + i * frames_per_piece self.S[q] = True @@ -1290,22 +1291,22 @@ def main(): dest='hard_max_segment_length', default=15.0, \ help="Hard maximum on the segment length above which the segment " \ + "will be broken even if in the middle of speech (default: %(default)s)") - parser.add_argument('--first-separator', type=str, \ + parser.add_argument('--first-separator', \ dest='first_separator', default="-", \ help="Separator between recording-id and start-time (default: %(default)s)") - parser.add_argument('--second-separator', type=str, \ + parser.add_argument('--second-separator', \ dest='second_separator', default="-", \ help="Separator between start-time and end-time (default: %(default)s)") - parser.add_argument('--remove-noise-only-segments', type=str, \ + parser.add_argument('--remove-noise-only-segments', \ dest='remove_noise_only_segments', default="true", choices=("true", "false"), \ help="Remove segments that have only noise. (default: %(default)s)") parser.add_argument('--min-inter-utt-silence-length', type=float, \ dest='min_inter_utt_silence_length', default=1.0, \ help="Minimum silence that must exist between two separate utterances (default: %(default)s)"); - parser.add_argument('--channel1-file', type=str, \ + parser.add_argument('--channel1-file', \ dest='channel1_file', default="inLine", \ help="String that matches with the channel 1 file (default: %(default)s)") - parser.add_argument('--channel2-file', type=str, \ + parser.add_argument('--channel2-file', \ dest='channel2_file', default="outLine", \ help="String that matches with the channel 2 file (default: %(default)s)") parser.add_argument('--isolated-resegmentation', \ @@ -1388,7 +1389,7 @@ def main(): speech_cap = None if options.speech_cap_length != None: - speech_cap = int( options.speech_cap_length / options.frame_shift ) + speech_cap = int(options.speech_cap_length/options.frame_shift) # End if for f in pred_files: @@ -1454,7 +1455,7 @@ def main(): f2 = f3 # End if - if (len(A1) - len(A2)) > options.max_length_diff / options.frame_shift: + if (len(A1) - len(A2)) > int(options.max_length_diff/options.frame_shift): sys.stderr.write( \ "%s: Warning: Lengths of %s and %s differ by more than %f. " \ % (sys.argv[0], f1,f2, options.max_length_diff) \ diff --git a/egs/babel/s5d/local/lexicon/make_unicode_lexicon.py b/egs/babel/s5d/local/lexicon/make_unicode_lexicon.py index 68280762597..91419f6e920 100755 --- a/egs/babel/s5d/local/lexicon/make_unicode_lexicon.py +++ b/egs/babel/s5d/local/lexicon/make_unicode_lexicon.py @@ -106,6 +106,7 @@ # Import Statements from __future__ import print_function +from __future__ import division import codecs import argparse import unicodedata @@ -340,7 +341,7 @@ def encode(unicode_transcription, tag_percentage, log=False): int2graph = {v: k for k, v in graph2int.items()} graph_list_int = [graph2int[g] for g in graph_list] bin_edges = range(0, len(int2graph.keys()) + 1) - graph_counts = np.histogram(graph_list_int, bins=bin_edges)[0] / float(len(graph_list_int)) + graph_counts = np.histogram(graph_list_int, bins=bin_edges)[0]/float(len(graph_list_int)) # Set count threshold to frequency that tags the bottom 10% of graphemes bottom_idx = int(np.floor(tag_percentage * len(graph_counts))) count_thresh = sorted(graph_counts)[bottom_idx] @@ -465,7 +466,7 @@ def encode(unicode_transcription, tag_percentage, log=False): for g_dict in table: g_map = "" map_number = 0 - for g_field, g_val in sorted(g_dict.iteritems()): + for g_field, g_val in sorted(g_dict.items()): if(g_field == ("MAP" + str(map_number))): g_map = g_map + g_val + " " map_number = map_number + 1 @@ -561,7 +562,7 @@ def write_table(table, outfile): # Start writing to output with codecs.open(outfile, "w", "utf-8") as fo: # Get header names - header_names = sorted(set().union(*[d.keys() for d in table])) + header_names = sorted(set().union(*[list(d.keys()) for d in table])) # Write headers for h in header_names[:-1]: fo.write("%s\t" % h) @@ -595,7 +596,7 @@ def write_map(grapheme_map, mapfile): ''' with codecs.open(mapfile, 'w', encoding='utf-8') as f: - for g, g_map in grapheme_map.iteritems(): + for g, g_map in grapheme_map.items(): print(g, g_map, file=f) @@ -613,14 +614,14 @@ def write_lexicon(baseforms, encoded_transcription, outfile, sil_lex=None, with codecs.open(outfile, "w", "utf-8") as f: # First write the non-speech words try: - for w in sil_lex.iterkeys(): + for w in sil_lex.keys(): f.write("%s\t%s\n" % (w, sil_lex[w])) except AttributeError: pass # Then write extra-speech words try: - for w in extra_lex.iterkeys(): + for w in extra_lex.keys(): f.write("%s\t%s\n" % (w, extra_lex[w])) except AttributeError: pass @@ -629,9 +630,9 @@ def write_lexicon(baseforms, encoded_transcription, outfile, sil_lex=None, for idx, w in enumerate(baseforms): # This is really just for BABEL in case is written as a word if(w[0].lower() == ""): - f.write("%s\t\n" % (unicode(w[0]))) + f.write("%s\t\n" % (w[0])) else: - f.write("%s\t%s\n" % (unicode(w[0]), + f.write("%s\t%s\n" % (w[0], encoded_transcription[idx])) if __name__ == "__main__": diff --git a/egs/babel/s5d/local/lexicon/make_word_list.py b/egs/babel/s5d/local/lexicon/make_word_list.py index 9a9e17f6c60..c1473b8ced8 100755 --- a/egs/babel/s5d/local/lexicon/make_word_list.py +++ b/egs/babel/s5d/local/lexicon/make_word_list.py @@ -85,7 +85,7 @@ def main(): # Print the word list with codecs.open(args.word_list, "w", encoding="utf-8") as f: for word, count in words: - f.write("%d %s\n" % (count, unicode(word))) + f.write("%d %s\n" % (count, word)) if args.misprons is not None: with codecs.open(args.misprons, "w", encoding="utf-8") as f: diff --git a/egs/babel/s5d/local/prepare_unicode_lexicon.py b/egs/babel/s5d/local/prepare_unicode_lexicon.py index 86fa4d60ba1..3b9dc1abd86 100755 --- a/egs/babel/s5d/local/prepare_unicode_lexicon.py +++ b/egs/babel/s5d/local/prepare_unicode_lexicon.py @@ -89,7 +89,7 @@ def extract_phonemes(lexicon): # Read all baseform units into dictionary with {a: [a, a_1, a_2], # b: [b_1, b_3], ...} phonemes_dict = {} - for word, pron in lexicon.iteritems(): + for word, pron in lexicon.items(): for p in pron.split(): try: base = p.split("_",1)[0] @@ -98,11 +98,11 @@ def extract_phonemes(lexicon): phonemes_dict[base] = [p] # Makes sure there are no repeats in the list - phonemes_dict = {k: set(v) for k, v in phonemes_dict.iteritems()} + phonemes_dict = {k: set(v) for k, v in phonemes_dict.items()} # Get all unique phonemes phonemes = [] - for v in phonemes_dict.itervalues(): + for v in phonemes_dict.values(): for p in v: phonemes.append(p) @@ -137,11 +137,11 @@ def write_extra_questions(nonsil_phonemes, nonsil_phonemes_dict, # Write all possible phone_tag combinations that occur in the lexicon for tag in tags: - for p in nonsil_phonemes_dict.iterkeys(): + for p in nonsil_phonemes_dict.keys(): tagged_phoneme = "_".join([p, tag]) if(tagged_phoneme in nonsil_phonemes_dict[p]): fp.write("%s " % tagged_phoneme) - for p in sil_phonemes_dict.iterkeys(): + for p in sil_phonemes_dict.keys(): tagged_phoneme = "_".join([p, tag]) if(tagged_phoneme in sil_phonemes_dict[p]): fp.write("%s " % tagged_phoneme) diff --git a/egs/babel/s5d/local/resegment/segmentation.py b/egs/babel/s5d/local/resegment/segmentation.py index 7c5c8665a16..02fd7646b96 100755 --- a/egs/babel/s5d/local/resegment/segmentation.py +++ b/egs/babel/s5d/local/resegment/segmentation.py @@ -3,6 +3,7 @@ # Copyright 2014 Vimal Manohar # Apache 2.0 +from __future__ import division import os, glob, argparse, sys, re, time from argparse import ArgumentParser @@ -19,12 +20,12 @@ def mean(l): if len(l) > 0: - return float(sum(l)) / len(l) + return float(sum(l))/len(l) return 0 # Analysis class # Stores statistics like the confusion matrix, length of the segments etc. -class Analysis: +class Analysis(object): def __init__(self, file_id, frame_shift, prefix): self.confusion_matrix = [0] * 9 self.type_counts = [ [[] for j in range(0,9)] for i in range(0,3) ] @@ -274,8 +275,8 @@ def read_rttm_file(rttm_file, temp_dir, frame_shift): i = len(this_file) category = splits[6] word = splits[5] - start_time = int(float(splits[3])/frame_shift + 0.5) - duration = int(float(splits[4])/frame_shift + 0.5) + start_time = int((float(splits[3])/frame_shift) + 0.5) + duration = int((float(splits[4])/frame_shift) + 0.5) if i < start_time: this_file.extend(["0"]*(start_time - i)) if type1 == "NON-LEX": @@ -295,7 +296,7 @@ def read_rttm_file(rttm_file, temp_dir, frame_shift): # Stats class to store some basic stats about the number of # times the post-processor goes through particular loops or blocks # of code in the algorithm. This is just for debugging. -class Stats: +class Stats(object): def __init__(self): self.inter_utt_nonspeech = 0 self.merge_nonspeech_segment = 0 @@ -321,7 +322,7 @@ def reset(self): self.noise_only = 0 # Timer class to time functions -class Timer: +class Timer(object): def __enter__(self): self.start = time.clock() return self @@ -332,7 +333,7 @@ def __exit__(self, *args): # The main class for post-processing a file. # This does the segmentation either looking at the file isolated # or by looking at both classes simultaneously -class JointResegmenter: +class JointResegmenter(object): def __init__(self, P, A, f, options, phone_map, stats = None, reference = None): # Pointers to prediction arrays and Initialization @@ -351,8 +352,8 @@ def __init__(self, P, A, f, options, phone_map, stats = None, reference = None): self.frame_shift = options.frame_shift # Convert length in seconds to frames - self.max_frames = int(options.max_segment_length / options.frame_shift) - self.hard_max_frames = int(options.hard_max_segment_length / options.frame_shift) + self.max_frames = int(options.max_segment_length/options.frame_shift) + self.hard_max_frames = int(options.hard_max_segment_length/options.frame_shift) self.min_inter_utt_nonspeech_length = int(options.min_inter_utt_silence_length / options.frame_shift) if ( options.remove_noise_only_segments == "false" ): self.remove_noise_segments = False @@ -540,7 +541,7 @@ def set_nonspeech_proportion(self): # Set the number of non-speech frames to be added depending on the # silence proportion. The target number of frames in the segments # is computed as below: - target_segment_frames = int(num_speech_frames / (1.0 - self.options.silence_proportion)) + target_segment_frames = int(num_speech_frames/(1.0 - self.options.silence_proportion)) # The number of frames currently in the segments num_segment_frames = num_speech_frames @@ -599,7 +600,7 @@ def set_nonspeech_proportion(self): if not changed: # avoid an infinite loop. if no changes, then break. break if num_segment_frames < target_segment_frames: - proportion = float(num_segment_frames - num_speech_frames) / num_segment_frames + proportion = float(num_segment_frames - num_speech_frames)/ num_segment_frames sys.stderr.write("%s: Warning: for recording %s, only got a proportion %f of non-speech frames, versus target %f\n" % (sys.argv[0], self.file_id, proportion, self.options.silence_proportion)) ########################################################################### @@ -863,14 +864,14 @@ def split_long_segments(self): # Count the number of times long segments are split self.stats.split_segments += 1 - num_pieces = int((float(segment_length) / self.hard_max_frames) + 0.99999) + num_pieces = int((float(segment_length)/self.hard_max_frames) + 0.99999) sys.stderr.write("%s: Warning: for recording %s, " \ % (sys.argv[0], self.file_id) \ + "splitting segment of length %f seconds into %d pieces " \ % (segment_length * self.frame_shift, num_pieces) \ + "(--hard-max-segment-length %f)\n" \ % self.options.hard_max_segment_length) - frames_per_piece = int(segment_length / num_pieces) + frames_per_piece = int(segment_length/num_pieces) for i in range(1,num_pieces): q = n + i * frames_per_piece self.S[q] = True @@ -1388,7 +1389,7 @@ def main(): speech_cap = None if options.speech_cap_length != None: - speech_cap = int( options.speech_cap_length / options.frame_shift ) + speech_cap = int(options.speech_cap_length/options.frame_shift) # End if for f in pred_files: @@ -1454,7 +1455,7 @@ def main(): f2 = f3 # End if - if (len(A1) - len(A2)) > options.max_length_diff / options.frame_shift: + if (len(A1) - len(A2)) > options.max_length_diff/options.frame_shift: sys.stderr.write( \ "%s: Warning: Lengths of %s and %s differ by more than %f. " \ % (sys.argv[0], f1,f2, options.max_length_diff) \ diff --git a/egs/bentham/v1/local/gen_topo.py b/egs/bentham/v1/local/gen_topo.py index 540bfbcf270..af9e20317d8 100755 --- a/egs/bentham/v1/local/gen_topo.py +++ b/egs/bentham/v1/local/gen_topo.py @@ -9,6 +9,7 @@ # the number of states for other characters. from __future__ import print_function +from __future__ import division import argparse import string @@ -19,11 +20,11 @@ parser.add_argument("num_nonsil_states", type=int, help="number of states for nonsilence phones"); parser.add_argument("num_sil_states", type=int, help="number of states for silence phones"); parser.add_argument("num_punctuation_states", type=int, help="number of states for punctuation"); -parser.add_argument("nonsilence_phones", type=str, +parser.add_argument("nonsilence_phones", help="List of non-silence phones as integers, separated by colons, e.g. 4:5:6:7:8:9"); -parser.add_argument("silence_phones", type=str, +parser.add_argument("silence_phones", help="List of silence phones as integers, separated by colons, e.g. 1:2:3"); -parser.add_argument("phone_list", type=str, help="file containing all phones and their corresponding number."); +parser.add_argument("phone_list", help="file containing all phones and their corresponding number."); args = parser.parse_args() @@ -47,8 +48,8 @@ print("") for x in range(0, args.num_nonsil_states): xp1 = x + 1 - print(" " + str(x) + " " + str(x) + " " + str(x) + " 0.75 " + str(xp1) + " 0.25 ") -print(" " + str(args.num_nonsil_states) + " ") + print(" {0} {0} {0} 0.75 {1} 0.25 ".format(x, xp1)) +print(" {} ".format(args.num_nonsil_states)) print("") # For nonsilence phones that ar punctuations @@ -58,8 +59,8 @@ print("") for x in range(0, args.num_punctuation_states): xp1 = x + 1 - print(" " + str(x) + " " + str(x) + " " + str(x) + " 0.75 " + str(xp1) + " 0.25 ") -print(" " + str(args.num_punctuation_states) + " ") + print(" {0} {0} {0} 0.75 {1} 0.25 ".format(x, xp1)) +print(" {} ".format(args.num_punctuation_states)) print("") # For silence phones @@ -68,25 +69,25 @@ print(" ".join([str(x) for x in silence_phones])) print("") if(args.num_sil_states > 1): - transp = 1.0 / (args.num_sil_states - 1) + transp = 1.0/(args.num_sil_states - 1) state_str = " 0 0 " for x in range(0, (args.num_sil_states - 1)): - state_str = state_str + " " + str(x) + " " + str(transp) + " " + state_str = "{} {} {} ".format(state_str, x, transp) state_str = state_str + "" print(state_str) for x in range(1, (args.num_sil_states - 1)): - state_str = " " + str(x) + " " + str(x) + " " + state_str = " {0} {0} ".format(x) for y in range(1, args.num_sil_states): - state_str = state_str + " " + str(y) + " " + str(transp) + " " + state_str = "{} {} {} ".format(state_str, y, transp) state_str = state_str + "" print(state_str) second_last = args.num_sil_states - 1 - print(" " + str(second_last) + " " + str(second_last) + " " + str(second_last) + " 0.75 " + str(args.num_sil_states) + " 0.25 ") - print(" " + str(args.num_sil_states) + " ") + print(" {0} {0} {0} 0.75 {1} 0.25 ".format(second_last, args.num_sil_states)) + print(" {} ".format(args.num_sil_states)) else: print(" 0 0 0 0.75 1 0.25 ") - print(" " + str(args.num_sil_states) + " ") + print(" {} ".format(args.num_sil_states)) print("") print("") diff --git a/egs/bn_music_speech/v1/local/make_annotations_bn.py b/egs/bn_music_speech/v1/local/make_annotations_bn.py index 53cebf52ea4..86bec7b16ae 100755 --- a/egs/bn_music_speech/v1/local/make_annotations_bn.py +++ b/egs/bn_music_speech/v1/local/make_annotations_bn.py @@ -9,6 +9,7 @@ # # This file is meant to be invoked by make_bn.sh. +from __future__ import print_function import sys, re, os def is_speech(line): @@ -37,7 +38,7 @@ def extract_speech(line): m = re.search('(?<=E_time=)\d+.\d+', line) end = float(m.group(0)) if start > end: - print "Skipping annotation where end time is before start time:", line + print("Skipping annotation where end time is before start time: {}".format(line)) return start, end def extract_other_type2(line): @@ -46,7 +47,7 @@ def extract_other_type2(line): m = re.search('(?<=E_time=)\d+.\d+', line) end = float(m.group(0)) if start > end: - print "Skipping annotation where end time is before start time:", line + print("Skipping annotation where end time is before start time: {}".format(line)) return start, end def extract_music(line): @@ -60,7 +61,7 @@ def extract_music(line): elif level == "O": is_on = False else: - print "Encountered bad token on line:", line + print("Encountered bad token on line: {}".format(line)) sys.exit() return time, is_on @@ -75,7 +76,7 @@ def extract_other_type1(line): elif level == "O": is_on = False else: - print "Encountered bad token on line:", line + print("Encountered bad token on line: {}".format(line)) sys.exit() return time, is_on @@ -92,11 +93,11 @@ def process_file(annos): for line in annos: if is_speech(line): speech_start, speech_end = extract_speech(line) - speech = speech + str(speech_start) + " " + str(speech_end) + "\n" + speech = "{}{} {}\n".format(speech, speech_start, speech_end) max_time = max(speech_end, max_time) elif is_other_type2(line): other_type2_start, other_type2_end = extract_other_type2(line) - other_type2 = other_type2 + str(other_type2_start) + " " + str(other_type2_end) + "\n" + other_type2 = "{}{} {}\n".format(other_type2, other_type2_start, other_type2_end) max_time = max(other_type2_end, max_time) elif is_music(line): time, is_on = extract_music(line) @@ -105,7 +106,7 @@ def process_file(annos): prev_music_time = time start_new_music_segment = False elif not is_on and not start_new_music_segment: - music = music + str(prev_music_time) + " " + str(time) + "\n" + music = "{}{} {}\n".format(music, prev_music_time, time) start_new_music_segment = True elif is_other_type1(line): time, is_on = extract_other_type1(line) @@ -114,13 +115,13 @@ def process_file(annos): prev_other_time = time start_new_other_segment = False elif not is_on and not start_new_other_segment: - other_type1 = other_type1 + str(prev_other_time) + " " + str(time) + "\n" + other_type1 = "{}{} {}\n".format(other_type1, prev_other_time, time) start_new_other_segment = True if not start_new_music_segment: - music = music + str(prev_music_time) + " " + str(max_time) + "\n" + music = "{}{} {}\n".format(music, prev_music_time, max_time) if not start_new_other_segment: - other_type1 = other_type1 + str(prev_other_time) + " " + str(max_time) + "\n" + other_type1 = "{}{} {}\n".format(other_type1, prev_other_time, max_time) other = other_type1 + other_type2 return speech, music, other diff --git a/egs/bn_music_speech/v1/local/make_bn.py b/egs/bn_music_speech/v1/local/make_bn.py index 98836d32534..7ec9aabcbdf 100755 --- a/egs/bn_music_speech/v1/local/make_bn.py +++ b/egs/bn_music_speech/v1/local/make_bn.py @@ -20,7 +20,7 @@ for file in files: utt = str(file).replace(".sph", "") if file.endswith(".sph") and utt in utts: - wav = wav + utt + " sox " + subdir + "/" + utt + ".sph" + " -c 1 -r 16000 -t wav - |\n" + wav = "{0}{1} sox {2}/{1}.sph -c 1 -r 16000 -t -wav - |\n".format(wav, utt, subdir) wav_fi = open(os.path.join(out_dir, "wav.scp"), 'w') wav_fi.write(wav) @@ -32,14 +32,14 @@ count = 1 for line in music_fi: left, right = line.rstrip().split(" ") - segments = segments + utt + "-music-" + str(count) + " " + utt + " " + left + " " + right + "\n" - utt2spk = utt2spk + utt + "-music-" + str(count) + " " + utt + "-music-" + str(count) + "\n" + segments = "{0}{1}-music-{2} {1} {3} {4}\n".format(segments, utt, count, left, right) + utt2spk = "{0}{1}-music-{2} {1}-music-{2}".format(utt2spk, utt,count) count += 1 count = 1 for line in speech_fi: left, right = line.rstrip().split(" ") - segments = segments + utt + "-speech-" + str(count) + " " + utt + " " + left + " " + right + "\n" - utt2spk = utt2spk + utt + "-speech-" + str(count) + " " + utt + "-speech-" + str(count) + "\n" + segments = "{0}{1}-speech-{2} {1} {3} {4}\n".format(segments, utt, count, left, right) + utt2spk = "{0}{1}-speech-{2} {1}-music-{2}".format(utt2spk, utt, count) count += 1 utt2spk_fi = open(os.path.join(out_dir, "utt2spk"), 'w') utt2spk_fi.write(utt2spk) diff --git a/egs/bn_music_speech/v1/local/make_musan.py b/egs/bn_music_speech/v1/local/make_musan.py index b3795fe2b7d..942973cfc65 100755 --- a/egs/bn_music_speech/v1/local/make_musan.py +++ b/egs/bn_music_speech/v1/local/make_musan.py @@ -43,9 +43,9 @@ def prepare_music(root_dir, use_vocals): utt2wav_str = utt2wav_str + utt + " " + utt2wav[utt] + "\n" num_good_files += 1 else: - print("Missing file", utt) + print("Missing file {}".format(utt)) num_bad_files += 1 - print("In music directory, processed", num_good_files, "files;", num_bad_files, "had missing wav data") + print(("In music directory, processed {} files: {} had missing wav data".format(num_good_files, num_bad_files)) return utt2spk_str, utt2wav_str def prepare_speech(root_dir): @@ -69,9 +69,9 @@ def prepare_speech(root_dir): utt2wav_str = utt2wav_str + utt + " " + utt2wav[utt] + "\n" num_good_files += 1 else: - print("Missing file", utt) + print("Missing file {}".format(utt)) num_bad_files += 1 - print("In speech directory, processed", num_good_files, "files;", num_bad_files, "had missing wav data") + print(("In speech directory, processed {} files: {} had missing wav data".format(num_good_files, num_bad_files)) return utt2spk_str, utt2wav_str def prepare_noise(root_dir): @@ -95,9 +95,9 @@ def prepare_noise(root_dir): utt2wav_str = utt2wav_str + utt + " " + utt2wav[utt] + "\n" num_good_files += 1 else: - print("Missing file", utt) + print("Missing file {}".format(utt)) num_bad_files += 1 - print("In noise directory, processed", num_good_files, "files;", num_bad_files, "had missing wav data") + print(("In noise directory, processed {} files: {} had missing wav data".format(num_good_files, num_bad_files)) return utt2spk_str, utt2wav_str def main(): diff --git a/egs/bn_music_speech/v1/local/print_scores.py b/egs/bn_music_speech/v1/local/print_scores.py index c2b587cdcad..e563afb63d7 100755 --- a/egs/bn_music_speech/v1/local/print_scores.py +++ b/egs/bn_music_speech/v1/local/print_scores.py @@ -11,6 +11,7 @@ # those strings to determine if it is a target or nontarget # utterance. We arbitrarily pick music to be the target class. +from __future__ import print_function import sys utt2score = open(sys.argv[1], 'r').readlines() for i in range(0, len(utt2score)): @@ -19,4 +20,4 @@ type = "target" else: type = "nontarget" - print score, type + print(score, type) diff --git a/egs/bn_music_speech/v1/local/refine_annotations_bn.py b/egs/bn_music_speech/v1/local/refine_annotations_bn.py index 52ac87c8640..31cb1803f57 100755 --- a/egs/bn_music_speech/v1/local/refine_annotations_bn.py +++ b/egs/bn_music_speech/v1/local/refine_annotations_bn.py @@ -10,6 +10,7 @@ # designated length are created. # # This file is meant to be invoked from make_bn.sh. +from __future__ import division import sys, os def seg_to_string(seg): @@ -23,7 +24,7 @@ def seg_to_string(seg): def process_segs(raw_segs): segs = [] for seg in raw_segs: - lower, upper = map(float, seg.rstrip().split(" ")) + lower, upper = [float(i) for i in seg.rstrip().split(" ")] segs.append((lower, upper)) return segs @@ -60,8 +61,8 @@ def resegment(music, speech, other, frame_length, min_seg): start_frame = 0 for i in range(1, len(frame2classes)): if curr_class != frame2classes[i]: - start = float(start_frame) / frame_length - end = float(i) / frame_length + start = float(start_frame)/frame_length + end = float(i)/frame_length if end - start > min_seg: if curr_class == "music": new_music.append((start, end)) diff --git a/egs/callhome_diarization/v1/local/make_musan.py b/egs/callhome_diarization/v1/local/make_musan.py index b3f6652ba40..974e73e0777 100755 --- a/egs/callhome_diarization/v1/local/make_musan.py +++ b/egs/callhome_diarization/v1/local/make_musan.py @@ -43,9 +43,9 @@ def prepare_music(root_dir, use_vocals): utt2wav_str = utt2wav_str + utt + " sox -t wav " + utt2wav[utt] + " -r 8k -t wav - |\n" num_good_files += 1 else: - print("Missing file", utt) + print("Missing file: {}".format(utt)) num_bad_files += 1 - print("In music directory, processed", num_good_files, "files;", num_bad_files, "had missing wav data") + print("In music directory, processed {} files: {} had missing wav data".format(num_good_files, num_bad_files) return utt2spk_str, utt2wav_str def prepare_speech(root_dir): @@ -69,9 +69,9 @@ def prepare_speech(root_dir): utt2wav_str = utt2wav_str + utt + " sox -t wav " + utt2wav[utt] + " -r 8k -t wav - |\n" num_good_files += 1 else: - print("Missing file", utt) + print("Missing file: {}".format(utt)) num_bad_files += 1 - print("In speech directory, processed", num_good_files, "files;", num_bad_files, "had missing wav data") + print("In speech directory, processed {} files: {} had missing wav data".format(num_good_files, num_bad_files) return utt2spk_str, utt2wav_str def prepare_noise(root_dir): @@ -95,9 +95,9 @@ def prepare_noise(root_dir): utt2wav_str = utt2wav_str + utt + " sox -t wav " + utt2wav[utt] + " -r 8k -t wav - |\n" num_good_files += 1 else: - print("Missing file", utt) + print("Missing file: {}".format(utt)) num_bad_files += 1 - print("In noise directory, processed", num_good_files, "files;", num_bad_files, "had missing wav data") + print("In noise directory, processed {} files: {} had missing wav data".format(num_good_files, num_bad_files) return utt2spk_str, utt2wav_str def main(): diff --git a/egs/callhome_egyptian/s5/local/convert_symtable_to_utf.py b/egs/callhome_egyptian/s5/local/convert_symtable_to_utf.py index f5b69a1ff86..7192ff7a1cc 100644 --- a/egs/callhome_egyptian/s5/local/convert_symtable_to_utf.py +++ b/egs/callhome_egyptian/s5/local/convert_symtable_to_utf.py @@ -1,3 +1,4 @@ +from __future__ import print_function #!/usr/bin/env py # Converts a romanized ECA word list (symbol table) to @@ -7,9 +8,9 @@ import codecs if len(sys.argv) < 3: - print "USAGE: local/convert_symtable_to_utf.py [SYMTABLE] [ECA-LEXICON]" - print "E.g., local/convert_symtable_to_utf.py data/lang/words.txt \ - /export/corpora/LDC/LDC99L22" + print("USAGE: local/convert_symtable_to_utf.py [SYMTABLE] [ECA-LEXICON]") + print("E.g., local/convert_symtable_to_utf.py data/lang/words.txt \ + /export/corpora/LDC/LDC99L22") sys.exit(1) # Note that the ECA lexicon's default encoding is ISO-8859-6, not UTF8 diff --git a/egs/callhome_egyptian/s5/local/splits/get_conversation.py b/egs/callhome_egyptian/s5/local/splits/get_conversation.py index c999d3e597e..80f66174e2b 100755 --- a/egs/callhome_egyptian/s5/local/splits/get_conversation.py +++ b/egs/callhome_egyptian/s5/local/splits/get_conversation.py @@ -1,5 +1,6 @@ #!/usr/bin/env python +from __future__ import print_function import os import re @@ -37,14 +38,14 @@ evaltest[pathComponents[12]] = numberOfConversations testConv = testConv + numberOfConversations -print "==============Train===============" -print train -print "Total Conversations in train = " + str(trainConv) -print "==============Dev===============" -print devtest -print "Total Conversations in dev = " + str(devConv) -print "==============Test===============" -print evaltest -print "Total Conversations in test = " + str(testConv) -print "=================================" -print "Total Conversations in Corpus = " + str(trainConv + devConv + testConv) +print("==============Train===============") +print(train) +print("Total Conversations in train = {}".format(trainConv)) +print("==============Dev===============") +print(devtest) +print("Total Conversations in dev = {}".format(devConv)) +print("==============Test===============") +print(evaltest) +print("Total Conversations in test = {}".format(testConv)) +print("=================================") +print("Total Conversations in Corpus = {}".format(trainConv + devConv + testConv)) diff --git a/egs/chime5/s5/local/json2text.py b/egs/chime5/s5/local/json2text.py index 4df0160efb6..a0142ad916e 100755 --- a/egs/chime5/s5/local/json2text.py +++ b/egs/chime5/s5/local/json2text.py @@ -25,8 +25,8 @@ def hms_to_seconds(hms): if __name__ == '__main__': parser = argparse.ArgumentParser() - parser.add_argument('json', type=str, help='JSON transcription file') - parser.add_argument('--mictype', type=str, + parser.add_argument('json', help='JSON transcription file') + parser.add_argument('--mictype', choices=['ref', 'worn', 'u01', 'u02', 'u03', 'u04', 'u05', 'u06'], help='Type of microphones') args = parser.parse_args() diff --git a/egs/cifar/v1/image/get_allowed_lengths.py b/egs/cifar/v1/image/get_allowed_lengths.py index 44e17028695..33996c8eef1 100755 --- a/egs/cifar/v1/image/get_allowed_lengths.py +++ b/egs/cifar/v1/image/get_allowed_lengths.py @@ -10,6 +10,7 @@ file is later used by make_features.py to pad each image sufficiently so that they all have an allowed length. This is intended for end2end chain training. """ +from __future__ import division import argparse import os @@ -124,7 +125,7 @@ def find_allowed_durations(start_len, end_len, args): def main(): args = get_args() - args.factor = 1.0 + args.factor / 100.0 + args.factor = 1.0 + args.factor/100.0 image2length = read_kaldi_mapfile(os.path.join(args.srcdir, 'image2num_frames')) @@ -133,7 +134,7 @@ def main(): "Coverage rate: {}%".format(start_dur, end_dur, 100.0 - args.coverage_factor * 2)) logger.info("There will be {} unique allowed lengths " - "for the images.".format(int(math.log(end_dur / start_dur) / + "for the images.".format(int((math.log(float(end_dur)/start_dur))/ math.log(args.factor)))) allowed_durations = find_allowed_durations(start_dur, end_dur, args) diff --git a/egs/cifar/v1/image/matrix_to_image.py b/egs/cifar/v1/image/matrix_to_image.py index 52dcead7479..908b1f8b3ed 100755 --- a/egs/cifar/v1/image/matrix_to_image.py +++ b/egs/cifar/v1/image/matrix_to_image.py @@ -26,6 +26,7 @@ copy-feats --binary=false $(grep $imgid data/train/feats.scp | cut -d' ' -f2) - | \ image/matrix_to_image.py --color=1 > $imgid.bmp """ +from __future__ import division import argparse import sys @@ -59,7 +60,7 @@ num_cols = len(line) # initialize if len(line) != num_cols: raise Exception("All rows should be of the same length") - line = map(float, line) # string to float + line = [float(i) for i in line] # string to float if max(line) > 1: raise Excetion("Element value in the matrix should be normalized and no larger than 1") line = [int(x * 255) for x in line] # float to integer ranging from 0 to 255 @@ -70,7 +71,7 @@ if num_cols % 3 != 0: raise Exception("Number of columns should be a multiple of 3 in the color mode") width = num_rows - height = num_cols / 3 + height = num_cols/3 # reform the image matrix image_array = [[0 for i in range(width * 3)] for j in range(height)] for i in range(height): diff --git a/egs/cifar/v1/image/select_image_in_egs.py b/egs/cifar/v1/image/select_image_in_egs.py index 88d7d568e66..dbf48e6403d 100755 --- a/egs/cifar/v1/image/select_image_in_egs.py +++ b/egs/cifar/v1/image/select_image_in_egs.py @@ -9,6 +9,7 @@ # --vertical-shift=0.3 --srand=27 --num-channels=3 ark:exp/cifar10_egs/egs.1.ark ark,t:- | \ # image/select_image_in_egs.py $id | image/matrix_to_image.py --color 3 > $id.bmp +from __future__ import print_function import argparse import sys diff --git a/egs/cifar/v1/local/process_data.py b/egs/cifar/v1/local/process_data.py index 51173dafc6f..38a599297d2 100755 --- a/egs/cifar/v1/local/process_data.py +++ b/egs/cifar/v1/local/process_data.py @@ -6,6 +6,7 @@ """ This script prepares the training and test data for CIFAR-10 or CIFAR-100. """ +from __future__ import division import argparse import os @@ -14,13 +15,13 @@ parser = argparse.ArgumentParser(description="""Converts train/test data of CIFAR-10 or CIFAR-100 to Kaldi feature format""") -parser.add_argument('database', type=str, +parser.add_argument('database', default='data/dl/cifar-10-batches-bin', help='path to downloaded cifar data (binary version)') -parser.add_argument('dir', type=str, help='output dir') -parser.add_argument('--cifar-version', type=str, default='CIFAR-10', choices=['CIFAR-10', 'CIFAR-100']) -parser.add_argument('--dataset', type=str, default='train', choices=['train', 'test']) -parser.add_argument('--out-ark', type=str, default='-', help='where to write output feature data') +parser.add_argument('dir', help='output dir') +parser.add_argument('--cifar-version', default='CIFAR-10', choices=['CIFAR-10', 'CIFAR-100']) +parser.add_argument('--dataset', default='train', choices=['train', 'test']) +parser.add_argument('--out-ark', default='-', help='where to write output feature data') args = parser.parse_args() @@ -37,7 +38,7 @@ def load_cifar10_data_batch(datafile): for i in range(num_images_in_batch): label = ord(fh.read(1)) bin_img = fh.read(C * H * W) - img = [[[ord(byte) / 255.0 for byte in bin_img[channel*H*W+row*W:channel*H*W+(row+1)*W]] + img = [[[ord(byte)/255.0 for byte in bin_img[channel*H*W+row*W:channel*H*W+(row+1)*W]] for row in range(H)] for channel in range(C)] labels += [label] data += [img] @@ -52,7 +53,7 @@ def load_cifar100_data_batch(datafile, num_images_in_batch): coarse_label = ord(fh.read(1)) fine_label = ord(fh.read(1)) bin_img = fh.read(C * H * W) - img = [[[ord(byte) / 255.0 for byte in bin_img[channel*H*W+row*W:channel*H*W+(row+1)*W]] + img = [[[ord(byte)/255.0 for byte in bin_img[channel*H*W+row*W:channel*H*W+(row+1)*W]] for row in range(H)] for channel in range(C)] fine_labels += [fine_label] coarse_labels += [coarse_label] @@ -80,7 +81,7 @@ def write_kaldi_matrix(file_handle, matrix, key): if num_cols != len(matrix[row_index]): raise Exception("All the rows of a matrix are expected to " "have the same length") - file_handle.write(" ".join(map(lambda x: str(x), matrix[row_index]))) + file_handle.write(" ".join([str(x) for x in matrix[row_index]])) if row_index != num_rows - 1: file_handle.write("\n") file_handle.write(" ]\n") diff --git a/egs/dihard_2018/v1/local/make_dihard_2018_dev.py b/egs/dihard_2018/v1/local/make_dihard_2018_dev.py index 71b2b1b0143..fa652da8b4c 100755 --- a/egs/dihard_2018/v1/local/make_dihard_2018_dev.py +++ b/egs/dihard_2018/v1/local/make_dihard_2018_dev.py @@ -35,7 +35,7 @@ def prepare_dihard_2018_dev(src_dir, data_dir): rttm_fi.write(rttm_str) with open("{}/data/rttm/{}.rttm".format(src_dir, utt), 'r') as fh: rttm_list = fh.readlines() - spk_list = map(lambda x: (x.split())[7], rttm_list) + spk_list = [(x.split())[7] for x in rttm_list] num_spk = len(set(spk_list)) reco2num_spk_fi.write("{} {}\n".format(utt, num_spk)) wavscp_fi.close() diff --git a/egs/dihard_2018/v1/local/make_dihard_2018_eval.py b/egs/dihard_2018/v1/local/make_dihard_2018_eval.py index f8bd434f51a..2a8acbee58d 100755 --- a/egs/dihard_2018/v1/local/make_dihard_2018_eval.py +++ b/egs/dihard_2018/v1/local/make_dihard_2018_eval.py @@ -35,7 +35,7 @@ def prepare_dihard_2018_eval(src_dir, data_dir): rttm_fi.write(rttm_str) with open("{}/data/rttm/{}.rttm".format(src_dir, utt), 'r') as fh: rttm_list = fh.readlines() - spk_list = map(lambda x: (x.split())[7], rttm_list) + spk_list = [(x.split())[7] for x in rttm_list] num_spk = len(set(spk_list)) reco2num_spk_fi.write("{} {}\n".format(utt, num_spk)) wavscp_fi.close() diff --git a/egs/dihard_2018/v2/local/make_musan.py b/egs/dihard_2018/v2/local/make_musan.py index 74c434990fb..c4b5c9359b4 100755 --- a/egs/dihard_2018/v2/local/make_musan.py +++ b/egs/dihard_2018/v2/local/make_musan.py @@ -47,9 +47,9 @@ def prepare_music(root_dir, use_vocals): utt2wav_str = utt2wav_str + utt + " " + utt2wav[utt] + "\n" num_good_files += 1 else: - print("Missing file", utt) + print("Missing file {}".format(utt)) num_bad_files += 1 - print("In music directory, processed", num_good_files, "files;", num_bad_files, "had missing wav data") + print(("In music directory, processed {} files: {} had missing wav data".format(num_good_files, num_bad_files)) return utt2spk_str, utt2wav_str def prepare_speech(root_dir): @@ -73,9 +73,9 @@ def prepare_speech(root_dir): utt2wav_str = utt2wav_str + utt + " " + utt2wav[utt] + "\n" num_good_files += 1 else: - print("Missing file", utt) + print("Missing file {}".format(utt)) num_bad_files += 1 - print("In speech directory, processed", num_good_files, "files;", num_bad_files, "had missing wav data") + print(("In speech directory, processed {} files: {} had missing wav data".format(num_good_files, num_bad_files)) return utt2spk_str, utt2wav_str def prepare_noise(root_dir): @@ -99,9 +99,9 @@ def prepare_noise(root_dir): utt2wav_str = utt2wav_str + utt + " " + utt2wav[utt] + "\n" num_good_files += 1 else: - print("Missing file", utt) + print("Missing file {}".format(utt)) num_bad_files += 1 - print("In noise directory, processed", num_good_files, "files;", num_bad_files, "had missing wav data") + print(("In noise directory, processed {} files: {} had missing wav data".format(num_good_files, num_bad_files)) return utt2spk_str, utt2wav_str def main(): diff --git a/egs/fame/v1/local/prepare_for_eer.py b/egs/fame/v1/local/prepare_for_eer.py index 59d2985e7c2..f1dbcfa9ab6 100755 --- a/egs/fame/v1/local/prepare_for_eer.py +++ b/egs/fame/v1/local/prepare_for_eer.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Copyright 2015 David Snyder # Apache 2.0. # @@ -12,4 +13,4 @@ spkrutt2target[spkr+utt]=target for line in scores: spkr, utt, score = line.strip().split() - print score, spkrutt2target[spkr+utt] + print(score, spkrutt2target[spkr+utt]) diff --git a/egs/fisher_callhome_spanish/s5/local/callhome_get_lattices.py b/egs/fisher_callhome_spanish/s5/local/callhome_get_lattices.py index 9112d868c25..4c96e01ce7e 100755 --- a/egs/fisher_callhome_spanish/s5/local/callhome_get_lattices.py +++ b/egs/fisher_callhome_spanish/s5/local/callhome_get_lattices.py @@ -5,6 +5,7 @@ # The list of files in the conversations for which 1 best output has to be extracted # words.txt +from __future__ import print_function import os import sys import subprocess @@ -76,7 +77,7 @@ def findLattice(timeDetail): # Concatenate lattices mergedTranslation = latticeConcatenate(mergedTranslation, tmp) - print mergedTranslation + print(mergedTranslation) if mergedTranslation != "": # Sanjeev's Recipe : Remove epsilons and topo sort @@ -95,16 +96,16 @@ def findLattice(timeDetail): # file so it can be checked later proc = subprocess.Popen("/export/a04/gkumar/moses/mosesdecoder/checkplf < " + finalPLFFile + " 2>&1 | awk 'FNR == 2 {print}'", stdout=subprocess.PIPE, shell=True) line = proc.stdout.readline() - print line + " " + str(lineNo) + print("{} {}".format(line, lineNo)) if line.strip() != "PLF format appears to be correct.": os.system("cp " + finalFST + " " + invalidplfdir + "/" + timeInfo[0]) invalidPLF.write(invalidplfdir + "/" + timeInfo[0] + "\n") - rmLines.write(str(lineNo) + "\n") + rmLines.write("{}\n".format(lineNo)) else: provFile.write(PLFline) else: blankPLF.write(timeInfo[0] + "\n") - rmLines.write(str(lineNo) + "\n") + rmLines.write("{}\n".format(lineNo)) # Now convert to PLF lineNo += 1 diff --git a/egs/fisher_callhome_spanish/s5/local/decode_report.py b/egs/fisher_callhome_spanish/s5/local/decode_report.py index 0331ab5d6f4..6f3d3f80c95 100755 --- a/egs/fisher_callhome_spanish/s5/local/decode_report.py +++ b/egs/fisher_callhome_spanish/s5/local/decode_report.py @@ -7,6 +7,7 @@ # This script is specific to my partitions and needs to be made more general # or modified +from __future__ import print_function import subprocess import os @@ -46,8 +47,8 @@ def get_best_wer(decode_dir): best_iteration = 0 best_wer = 100.0 for i in range(16): - if os.path.isfile(decode_dir + "/wer_" + str(i)): - result = subprocess.check_output("tail -n 3 " + decode_dir + "/wer_" + str(i), shell=True) + if os.path.isfile("{}/wer_{}".format(decode_dir, i)): + result = subprocess.check_output("tail -n 3 {}/wer_{}".format(decode_dir, i), shell=True) wer_string = result.split("\n")[0] wer_details = wer_string.split(' ') # Get max WER @@ -58,8 +59,8 @@ def get_best_wer(decode_dir): return best_iteration, best_wer for decode_dir in decode_directories[:6]: - print decode_dir - print get_best_wer(decode_dir) + print(decode_dir) + print(get_best_wer(decode_dir)) # Separate processing for bMMI stuff best_wer = 100.0 @@ -73,8 +74,8 @@ def get_best_wer(decode_dir): best_dir = decode_dir best_iteration = iteration -print best_dir -print (best_iteration, best_wer) +print(best_dir) +print((best_iteration, best_wer)) best_wer = 100.0 best_dir = "" @@ -87,8 +88,8 @@ def get_best_wer(decode_dir): best_dir = decode_dir best_iteration = iteration -print best_dir -print (best_iteration, best_wer) +print(best_dir) +print((best_iteration, best_wer)) best_wer = 100.0 best_dir = "" @@ -101,8 +102,8 @@ def get_best_wer(decode_dir): best_dir = decode_dir best_iteration = iteration -print best_dir -print (best_iteration, best_wer) +print(best_dir) +print((best_iteration, best_wer)) best_wer = 100.0 best_dir = "" @@ -115,8 +116,8 @@ def get_best_wer(decode_dir): best_dir = decode_dir best_iteration = iteration -print best_dir -print (best_iteration, best_wer) +print(best_dir) +print((best_iteration, best_wer)) best_wer = 100.0 best_dir = "" @@ -129,8 +130,8 @@ def get_best_wer(decode_dir): best_dir = decode_dir best_iteration = iteration -print best_dir -print (best_iteration, best_wer) +print(best_dir) +print((best_iteration, best_wer)) best_wer = 100.0 best_dir = "" @@ -143,5 +144,5 @@ def get_best_wer(decode_dir): best_dir = decode_dir best_iteration = iteration -print best_dir -print (best_iteration, best_wer) +print(best_dir) +print((best_iteration, best_wer)) diff --git a/egs/fisher_callhome_spanish/s5/local/get_lattices.py b/egs/fisher_callhome_spanish/s5/local/get_lattices.py index a44facbce44..5430c18bb5b 100755 --- a/egs/fisher_callhome_spanish/s5/local/get_lattices.py +++ b/egs/fisher_callhome_spanish/s5/local/get_lattices.py @@ -5,6 +5,7 @@ # The list of files in the conversations for which 1 best output has to be extracted # words.txt +from __future__ import print_function import os import sys import subprocess @@ -76,7 +77,7 @@ def findLattice(timeDetail): # Concatenate lattices mergedTranslation = latticeConcatenate(mergedTranslation, tmp) - print mergedTranslation + print(mergedTranslation) if mergedTranslation != "": # Sanjeev's Recipe : Remove epsilons and topo sort @@ -95,16 +96,16 @@ def findLattice(timeDetail): # file so it can be checked later proc = subprocess.Popen("/export/a04/gkumar/moses/mosesdecoder/checkplf < " + finalPLFFile + " 2>&1 | awk 'FNR == 2 {print}'", stdout=subprocess.PIPE, shell=True) line = proc.stdout.readline() - print line + " " + str(lineNo) + print("{} {}".format(line, lineNo)) if line.strip() != "PLF format appears to be correct.": os.system("cp " + finalFST + " " + invalidplfdir + "/" + timeInfo[0]) invalidPLF.write(invalidplfdir + "/" + timeInfo[0] + "\n") - rmLines.write(str(lineNo) + "\n") + rmLines.write("{}\n".format(lineNo)) else: provFile.write(PLFline) else: blankPLF.write(timeInfo[0] + "\n") - rmLines.write(str(lineNo) + "\n") + rmLines.write("{}\n".format(lineNo)) # Now convert to PLF lineNo += 1 diff --git a/egs/fisher_callhome_spanish/s5/local/merge_lexicons.py b/egs/fisher_callhome_spanish/s5/local/merge_lexicons.py index 5c09f09bc35..864b76b671b 100755 --- a/egs/fisher_callhome_spanish/s5/local/merge_lexicons.py +++ b/egs/fisher_callhome_spanish/s5/local/merge_lexicons.py @@ -4,6 +4,7 @@ # # Merges unique words from Spanish Fisher, Gigaword and the LDC spanish lexicon +from __future__ import print_function import sys import json import codecs @@ -24,8 +25,7 @@ merged_lexicon.append(line.strip()) fisher.close() -print "After adding the fisher data, the lexicon contains " \ - + str(len(merged_lexicon)) + " entries." +print("After adding the fisher data, the lexicon contains {} entries".format(len(merged_lexicon))) # Now add data from the LDC lexicon ldc = codecs.open(uw_LDC, encoding='iso-8859-1') @@ -34,12 +34,11 @@ if entries[0].lower() not in merged_lexicon: merged_lexicon.append(entries[0].lower()) -print "After adding the LDC data, the lexicon contains " \ - + str(len(merged_lexicon)) + " entries." +print("After adding the LDC data, the lexicon contains {} entries".format(len(merged_lexicon))) # Finally add the gigaword data gigaword = json.load(open(uw_gigaword)) -gigaword = reversed(sorted(gigaword.iteritems(), key=operator.itemgetter(1))) +gigaword = reversed(sorted(gigaword.items(), key=operator.itemgetter(1))) for item in gigaword: # We need a maximum of wordlimit words in the lexicon @@ -49,8 +48,7 @@ if item[0].lower() not in merged_lexicon: merged_lexicon.append(item[0].lower()) -print "After adding the Gigaword data, the lexicon contains " \ - + str(len(merged_lexicon)) + " entries." +print("After adding the Gigaword data, the lexicon contains {} entries".format(len(merged_lexicon))) # Now write the uniquewords to a file lf = codecs.open(tmpdir + '/uniquewords64k', encoding='utf-8', mode='w+') @@ -61,4 +59,4 @@ lf.close() -print "Finshed writing unique words" +print("Finshed writing unique words") diff --git a/egs/fisher_callhome_spanish/s5/local/train_get_lattices.py b/egs/fisher_callhome_spanish/s5/local/train_get_lattices.py index 3b6755d6540..b9f906b27da 100755 --- a/egs/fisher_callhome_spanish/s5/local/train_get_lattices.py +++ b/egs/fisher_callhome_spanish/s5/local/train_get_lattices.py @@ -1,6 +1,7 @@ #!/usr/bin/env python # Copyright 2014 Gaurav Kumar. Apache 2.0 +from __future__ import print_function import os import sys import subprocess @@ -18,7 +19,7 @@ latticeDict = {} -for key,location in latticeLocation.iteritems(): +for key,location in latticeLocation.items(): for root, dirs, filenames in os.walk(location): for f in filenames: latticeDict[f] = str(key) @@ -105,16 +106,16 @@ def findLattice(timeDetail): # file so it can be checked later proc = subprocess.Popen("/export/a04/gkumar/moses/mosesdecoder/checkplf < " + finalPLFFile + " 2>&1 | awk 'FNR == 2 {print}'", stdout=subprocess.PIPE, shell=True) line = proc.stdout.readline() - print line + " " + str(lineNo) + print("{} {}".format(line, lineNo)) if line.strip() != "PLF format appears to be correct.": os.system("cp " + finalFST + " " + invalidplfdir + "/" + timeInfo[0]) invalidPLF.write(invalidplfdir + "/" + timeInfo[0] + "\n") - rmLines.write(str(lineNo) + "\n") + rmLines.write("{}\n".format(lineNo)) else: provFile.write(PLFline) else: blankPLF.write(timeInfo[0] + "\n") - rmLines.write(str(lineNo) + "\n") + rmLines.write("{}\n".format(lineNo)) # Now convert to PLF lineNo += 1 diff --git a/egs/fisher_swbd/s5/local/format_acronyms_ctm_eval2000.py b/egs/fisher_swbd/s5/local/format_acronyms_ctm_eval2000.py index 3c447c5976a..75cc4458d85 100755 --- a/egs/fisher_swbd/s5/local/format_acronyms_ctm_eval2000.py +++ b/egs/fisher_swbd/s5/local/format_acronyms_ctm_eval2000.py @@ -10,6 +10,7 @@ # en_4156 B 414.58 0.16 l # en_4156 B 414.74 0.17 a +from __future__ import division import argparse,re __author__ = 'Minhua Wu' @@ -27,7 +28,7 @@ if items[4].find(".") != -1: letters = items[4].split("._") acronym_period = round(float(items[3]), 2) - letter_slot = round(acronym_period / len(letters), 2) + letter_slot = round(acronym_period/len(letters), 2) time_start = round(float(items[2]), 2) for l in letters[:-1]: time = " %.2f %.2f " % (time_start, letter_slot) diff --git a/egs/fisher_swbd/s5/local/format_acronyms_ctm_rt03.py b/egs/fisher_swbd/s5/local/format_acronyms_ctm_rt03.py index 59814beb4ea..c3f9af09c99 100755 --- a/egs/fisher_swbd/s5/local/format_acronyms_ctm_rt03.py +++ b/egs/fisher_swbd/s5/local/format_acronyms_ctm_rt03.py @@ -10,6 +10,7 @@ # en_4156 B 414.58 0.16 l # en_4156 B 414.74 0.17 a +from __future__ import division import argparse,re __author__ = 'Minhua Wu' @@ -27,7 +28,7 @@ if items[4].find(".") != -1: letters = items[4].split("._") acronym_period = round(float(items[3]), 2) - letter_slot = round(acronym_period / len(letters), 2) + letter_slot = round(acronym_period/ len(letters), 2) time_start = round(float(items[2]), 2) for l in letters[:-1]: time = " %.2f %.2f " % (time_start, letter_slot) diff --git a/egs/gale_mandarin/s5/local/gale_segment.py b/egs/gale_mandarin/s5/local/gale_segment.py index 975ddb9c143..d652eb837f3 100755 --- a/egs/gale_mandarin/s5/local/gale_segment.py +++ b/egs/gale_mandarin/s5/local/gale_segment.py @@ -1,6 +1,7 @@ #!/usr/bin/env python #coding:utf-8 #!/usr/bin/env python +from __future__ import print_function import sys from mmseg import seg_txt for line in sys.stdin: @@ -12,4 +13,4 @@ continue for j in seg_txt(blks[i]): out_line += " " + j - print out_line + print(out_line) diff --git a/egs/hub4_english/s5/local/data_prep/process_1995_bn_annotation.py b/egs/hub4_english/s5/local/data_prep/process_1995_bn_annotation.py index be0c7ad8e0d..5675dc3fbd9 100755 --- a/egs/hub4_english/s5/local/data_prep/process_1995_bn_annotation.py +++ b/egs/hub4_english/s5/local/data_prep/process_1995_bn_annotation.py @@ -31,9 +31,9 @@ def get_args(): parser = argparse.ArgumentParser("Process 1995 CSR-IV HUB4 transcripts") - parser.add_argument("--noise-word", type=str, default="", + parser.add_argument("--noise-word", default="", help="Word to add in-place of noise words") - parser.add_argument("--spoken-noise-word", type=str, + parser.add_argument("--spoken-noise-word", default="", help="Word to add in-place of speaker noise words") parser.add_argument("in_file", type=argparse.FileType('r'), @@ -230,7 +230,7 @@ def run(args): start_time = story_end_time segments = process_story_content( args, reco_id, - ' '.join([unicode(x) for x in s.children]), + ' '.join([str(x) for x in s.children]), start_time=story_begin_time, end_time=story_end_time) write_segments(segments, args) elif (s.name is not None and s.name != "language" @@ -240,9 +240,9 @@ def run(args): "or or ; got {0}".format(s)) elif s.name == "language" or s.name == "sung": non_story_contents.append( - ' '.join([unicode(x) for x in s.children])) + ' '.join([str(x) for x in s.children])) else: - non_story_contents.append(unicode(s)) + non_story_contents.append(str(s)) except RuntimeError: raise except Exception: diff --git a/egs/hub4_english/s5/local/data_prep/process_1996_csr_hub4_lm_filelist.py b/egs/hub4_english/s5/local/data_prep/process_1996_csr_hub4_lm_filelist.py index 95aa7ddb831..fb5ba7a64ee 100755 --- a/egs/hub4_english/s5/local/data_prep/process_1996_csr_hub4_lm_filelist.py +++ b/egs/hub4_english/s5/local/data_prep/process_1996_csr_hub4_lm_filelist.py @@ -36,9 +36,9 @@ def get_args(): corpus (LDC98T31).""") parser.add_argument("--verbose", choices=[0,1,2,3], type=int, default=0, help="Set higher for more verbose logging.") - parser.add_argument("file_list", type=str, + parser.add_argument("file_list", help="""List of compressed source files""") - parser.add_argument("dir", type=str, + parser.add_argument("dir", help="Output directory to dump processed files to") args = parser.parse_args() @@ -83,7 +83,7 @@ def process_file_lines(lines, out_file_handle): for x in para.contents: try: if x.name is None: - normalized_text = normalize_text(unicode(x)) + normalized_text = normalize_text(str(x)) if len(normalized_text) == 0: continue out_file_handle.write("{0}\n".format( diff --git a/egs/hub4_english/s5/local/data_prep/process_na_news_text.py b/egs/hub4_english/s5/local/data_prep/process_na_news_text.py index 94b02a766a9..08203f7ada1 100755 --- a/egs/hub4_english/s5/local/data_prep/process_na_news_text.py +++ b/egs/hub4_english/s5/local/data_prep/process_na_news_text.py @@ -38,10 +38,10 @@ def get_args(): parser = argparse.ArgumentParser("Prepare NA News Text corpus (LDC95T21).") parser.add_argument("--verbose", type=int, choices=[0, 1, 2, 3], default=0, help="Use larger verbosity for more verbose logging.") - parser.add_argument("file_list", type=str, + parser.add_argument("file_list", help="List of compressed source files for NA News Text. " "e.g: /export/corpora/LDC/LDC95T21/na_news_1/latwp/1994") - parser.add_argument("out_file", type=str, + parser.add_argument("out_file", help="Output file to write to.") args = parser.parse_args() @@ -85,7 +85,7 @@ def process_file_lines(lines, out_file_handle): continue for para in art.find_all('p'): assert para.name == 'p' - text = ' '.join([unicode(x).strip() for x in para.contents]) + text = ' '.join([str(x).strip() for x in para.contents]) normalized_text = normalize_text(text) out_file_handle.write("{0}\n".format( normalized_text.encode('ascii'))) diff --git a/egs/hub4_english/s5/local/lm/merge_word_counts.py b/egs/hub4_english/s5/local/lm/merge_word_counts.py index 6338cbbf875..85e15d8dc07 100755 --- a/egs/hub4_english/s5/local/lm/merge_word_counts.py +++ b/egs/hub4_english/s5/local/lm/merge_word_counts.py @@ -7,6 +7,7 @@ A min-count argument is required to only write counts that are above the specified minimum count. """ +from __future__ import print_function import sys @@ -21,7 +22,7 @@ def main(): parts = line.strip().split() words[parts[1]] = words.get(parts[1], 0) + int(parts[0]) - for word, count in words.iteritems(): + for word, count in words.items(): if count >= int(sys.argv[1]): print ("{0} {1}".format(count, word)) diff --git a/egs/hub4_spanish/s5/local/lexicon/make_unicode_lexicon.py b/egs/hub4_spanish/s5/local/lexicon/make_unicode_lexicon.py index 25f26f38a4f..69b4e374b6e 100755 --- a/egs/hub4_spanish/s5/local/lexicon/make_unicode_lexicon.py +++ b/egs/hub4_spanish/s5/local/lexicon/make_unicode_lexicon.py @@ -106,6 +106,7 @@ # Import Statements from __future__ import print_function +from __future__ import division import codecs import argparse import unicodedata @@ -338,8 +339,8 @@ def encode(unicode_transcription, tag_percentage, log=False): graph2int = {v: k for k, v in enumerate(set(graph_list))} int2graph = {v: k for k, v in graph2int.items()} graph_list_int = [graph2int[g] for g in graph_list] - bin_edges = range(0, len(int2graph.keys()) + 1) - graph_counts = np.histogram(graph_list_int, bins=bin_edges)[0] / float(len(graph_list_int)) + bin_edges = list(range(0, len(int2graph.keys()) + 1)) + graph_counts = np.histogram(graph_list_int, bins=bin_edges)[0]/ float(len(graph_list_int)) # Set count threshold to frequency that tags the bottom 10% of graphemes bottom_idx = int(np.floor(tag_percentage * len(graph_counts))) count_thresh = sorted(graph_counts)[bottom_idx] @@ -464,7 +465,7 @@ def encode(unicode_transcription, tag_percentage, log=False): for g_dict in table: g_map = "" map_number = 0 - for g_field, g_val in sorted(g_dict.iteritems()): + for g_field, g_val in sorted(g_dict.items()): if(g_field == ("MAP" + str(map_number))): g_map = g_map + g_val + " " map_number = map_number + 1 @@ -594,7 +595,7 @@ def write_map(grapheme_map, mapfile): ''' with codecs.open(mapfile, 'w', encoding='utf-8') as f: - for g, g_map in grapheme_map.iteritems(): + for g, g_map in grapheme_map.items(): print(g, g_map, file=f) @@ -612,14 +613,14 @@ def write_lexicon(baseforms, encoded_transcription, outfile, sil_lex=None, with codecs.open(outfile, "w", "utf-8") as f: # First write the non-speech words try: - for w in sil_lex.iterkeys(): + for w in sil_lex.keys(): f.write("%s\t%s\n" % (w, sil_lex[w])) except AttributeError: pass # Then write extra-speech words try: - for w in extra_lex.iterkeys(): + for w in extra_lex.keys(): f.write("%s\t%s\n" % (w, extra_lex[w])) except AttributeError: pass @@ -628,9 +629,9 @@ def write_lexicon(baseforms, encoded_transcription, outfile, sil_lex=None, for idx, w in enumerate(baseforms): # This is really just for BABEL in case is written as a word if(w[0].lower() == ""): - f.write("%s\t\n" % (unicode(w[0]))) + f.write("%s\t\n" % (str(w[0]))) else: - f.write("%s\t%s\n" % (unicode(w[0]), + f.write("%s\t%s\n" % (str(w[0]), encoded_transcription[idx])) if __name__ == "__main__": diff --git a/egs/hub4_spanish/s5/local/prepare_unicode_dict.py b/egs/hub4_spanish/s5/local/prepare_unicode_dict.py index 86fa4d60ba1..3b9dc1abd86 100755 --- a/egs/hub4_spanish/s5/local/prepare_unicode_dict.py +++ b/egs/hub4_spanish/s5/local/prepare_unicode_dict.py @@ -89,7 +89,7 @@ def extract_phonemes(lexicon): # Read all baseform units into dictionary with {a: [a, a_1, a_2], # b: [b_1, b_3], ...} phonemes_dict = {} - for word, pron in lexicon.iteritems(): + for word, pron in lexicon.items(): for p in pron.split(): try: base = p.split("_",1)[0] @@ -98,11 +98,11 @@ def extract_phonemes(lexicon): phonemes_dict[base] = [p] # Makes sure there are no repeats in the list - phonemes_dict = {k: set(v) for k, v in phonemes_dict.iteritems()} + phonemes_dict = {k: set(v) for k, v in phonemes_dict.items()} # Get all unique phonemes phonemes = [] - for v in phonemes_dict.itervalues(): + for v in phonemes_dict.values(): for p in v: phonemes.append(p) @@ -137,11 +137,11 @@ def write_extra_questions(nonsil_phonemes, nonsil_phonemes_dict, # Write all possible phone_tag combinations that occur in the lexicon for tag in tags: - for p in nonsil_phonemes_dict.iterkeys(): + for p in nonsil_phonemes_dict.keys(): tagged_phoneme = "_".join([p, tag]) if(tagged_phoneme in nonsil_phonemes_dict[p]): fp.write("%s " % tagged_phoneme) - for p in sil_phonemes_dict.iterkeys(): + for p in sil_phonemes_dict.keys(): tagged_phoneme = "_".join([p, tag]) if(tagged_phoneme in sil_phonemes_dict[p]): fp.write("%s " % tagged_phoneme) diff --git a/egs/iam/v2/local/gen_topo.py b/egs/iam/v2/local/gen_topo.py index 540bfbcf270..8ffc59c5788 100755 --- a/egs/iam/v2/local/gen_topo.py +++ b/egs/iam/v2/local/gen_topo.py @@ -9,6 +9,7 @@ # the number of states for other characters. from __future__ import print_function +from __future__ import division import argparse import string @@ -19,11 +20,11 @@ parser.add_argument("num_nonsil_states", type=int, help="number of states for nonsilence phones"); parser.add_argument("num_sil_states", type=int, help="number of states for silence phones"); parser.add_argument("num_punctuation_states", type=int, help="number of states for punctuation"); -parser.add_argument("nonsilence_phones", type=str, +parser.add_argument("nonsilence_phones", help="List of non-silence phones as integers, separated by colons, e.g. 4:5:6:7:8:9"); -parser.add_argument("silence_phones", type=str, +parser.add_argument("silence_phones", help="List of silence phones as integers, separated by colons, e.g. 1:2:3"); -parser.add_argument("phone_list", type=str, help="file containing all phones and their corresponding number."); +parser.add_argument("phone_list", help="file containing all phones and their corresponding number."); args = parser.parse_args() @@ -47,8 +48,8 @@ print("") for x in range(0, args.num_nonsil_states): xp1 = x + 1 - print(" " + str(x) + " " + str(x) + " " + str(x) + " 0.75 " + str(xp1) + " 0.25 ") -print(" " + str(args.num_nonsil_states) + " ") + print(" {0} {0} {0} 0.75 {1} 0.25 ".format(x, xp1)) +print(" {} ".format(args.num_nonsil_states)) print("") # For nonsilence phones that ar punctuations @@ -58,8 +59,8 @@ print("") for x in range(0, args.num_punctuation_states): xp1 = x + 1 - print(" " + str(x) + " " + str(x) + " " + str(x) + " 0.75 " + str(xp1) + " 0.25 ") -print(" " + str(args.num_punctuation_states) + " ") + print(" {0} {0} {0} 0.75 {1} 0.25 ".format(x, xp1)) +print(" {} ".format(args.num_punctuation_states)) print("") # For silence phones @@ -72,21 +73,21 @@ state_str = " 0 0 " for x in range(0, (args.num_sil_states - 1)): - state_str = state_str + " " + str(x) + " " + str(transp) + " " + state_str = "{} {} {} ".format(state_str, x, transp)) state_str = state_str + "" print(state_str) for x in range(1, (args.num_sil_states - 1)): - state_str = " " + str(x) + " " + str(x) + " " + state_str = " {0} " + str(y) + " " + str(transp) + " " + state_str = "{} {} {} ".format(state_str, y, transp)) state_str = state_str + "" print(state_str) second_last = args.num_sil_states - 1 - print(" " + str(second_last) + " " + str(second_last) + " " + str(second_last) + " 0.75 " + str(args.num_sil_states) + " 0.25 ") - print(" " + str(args.num_sil_states) + " ") + print(" {0} {0} {0} 0.75 {1} 0.25 ".format(second_last, args.num_sil_states)) + print(" {} ".format(args.num_sil_states)) else: print(" 0 0 0 0.75 1 0.25 ") - print(" " + str(args.num_sil_states) + " ") + print(" {} ".format(args.num_sil_states)) print("") print("") diff --git a/egs/ifnenit/v1/local/make_features.py b/egs/ifnenit/v1/local/make_features.py index 3a485e32eb1..87afa37c00a 100755 --- a/egs/ifnenit/v1/local/make_features.py +++ b/egs/ifnenit/v1/local/make_features.py @@ -10,7 +10,7 @@ eg. local/make_features.py data/train --feat-dim 40 """ - +from __future__ import division import argparse import os @@ -24,8 +24,8 @@ signal(SIGPIPE,SIG_DFL) parser = argparse.ArgumentParser(description="""Generates and saves the feature vectors""") -parser.add_argument('dir', type=str, help='directory of images.scp and is also output directory') -parser.add_argument('--out-ark', type=str, default='-', help='where to write the output feature file') +parser.add_argument('dir', help='directory of images.scp and is also output directory') +parser.add_argument('--out-ark', default='-', help='where to write the output feature file') parser.add_argument('--feat-dim', type=int, default=40, help='size to scale the height of all images') parser.add_argument('--padding', type=int, default=5, help='size to scale the height of all images') args = parser.parse_args() @@ -42,7 +42,7 @@ def write_kaldi_matrix(file_handle, matrix, key): if num_cols != len(matrix[row_index]): raise Exception("All the rows of a matrix are expected to " "have the same length") - file_handle.write(" ".join(map(lambda x: str(x), matrix[row_index]))) + file_handle.write(" ".join([str(x) for x in matrix[row_index]])) if row_index != num_rows - 1: file_handle.write("\n") file_handle.write(" ]\n") @@ -51,7 +51,7 @@ def get_scaled_image(im): scale_size = args.feat_dim sx = im.shape[1] sy = im.shape[0] - scale = (1.0 * scale_size) / sy + scale = (1.0 * scale_size)/ sy nx = int(scale_size) ny = int(scale * sx) im = misc.imresize(im, (nx, ny)) diff --git a/egs/librispeech/s5/local/lm/python/text_post_process.py b/egs/librispeech/s5/local/lm/python/text_post_process.py index 4ffbbe04b1f..344c1b291bd 100755 --- a/egs/librispeech/s5/local/lm/python/text_post_process.py +++ b/egs/librispeech/s5/local/lm/python/text_post_process.py @@ -21,10 +21,10 @@ def parse_args(): parser.add_argument('--abort-long-sent', type=bool, default=False, help='If True and a sentence longer than "max-sent-len" detected' +\ 'exit with error code 1. If False, just split the long sentences.') - parser.add_argument('--sent-end-marker', type=str, default="DOTDOTDOT") - parser.add_argument("in_text", type=str, help="Input text") - parser.add_argument("out_text", type=str, help="Output text") - parser.add_argument("sent_bounds", type=str, + parser.add_argument('--sent-end-marker', default="DOTDOTDOT") + parser.add_argument("in_text", help="Input text") + parser.add_argument("out_text", help="Output text") + parser.add_argument("sent_bounds", help="A file that will contain a comma separated list of numbers, s.t. if" + "i is in this list, then there is a sententence break after token i") return parser.parse_args() @@ -66,7 +66,7 @@ def parse_args(): n_tokens += 1 start_scan = 4 current_line.append('SUN') - for i in xrange(start_scan, len(opl_tokens)): + for i in range(start_scan, len(opl_tokens)): m = re.match("^[A-Z]+\'?[A-Z\']*$", opl_tokens[i]) if m is not None: n_tokens += 1 diff --git a/egs/librispeech/s5/local/lm/python/text_pre_process.py b/egs/librispeech/s5/local/lm/python/text_pre_process.py index 6228079b3a3..b75d0711d13 100755 --- a/egs/librispeech/s5/local/lm/python/text_pre_process.py +++ b/egs/librispeech/s5/local/lm/python/text_pre_process.py @@ -20,13 +20,13 @@ def parse_args(): parser = argparse.ArgumentParser(description="Pre-process a book's text") - parser.add_argument("--in-encoding", type=str, default="utf-8", + parser.add_argument("--in-encoding", default="utf-8", help="Encoding to use when reading the input text") - parser.add_argument("--out-encoding", type=str, default="ascii", + parser.add_argument("--out-encoding", default="ascii", help="Encoding to use when writing the output text") - parser.add_argument('--sent-end-marker', type=str, default="DOTDOTDOT") - parser.add_argument("in_text", type=str, help="Input text") - parser.add_argument("out_text", type=str, help="Output text") + parser.add_argument('--sent-end-marker', default="DOTDOTDOT") + parser.add_argument("in_text", help="Input text") + parser.add_argument("out_text", help="Output text") return parser.parse_args() # http://rosettacode.org/wiki/Roman_numerals/Decode#Python diff --git a/egs/madcat_ar/v1/local/create_line_image_from_page_image.py b/egs/madcat_ar/v1/local/create_line_image_from_page_image.py index 778555c427e..650a0704d80 100755 --- a/egs/madcat_ar/v1/local/create_line_image_from_page_image.py +++ b/egs/madcat_ar/v1/local/create_line_image_from_page_image.py @@ -13,6 +13,7 @@ be vertically or horizontally aligned). Hence to extract line image from line bounding box, page image is rotated and line image is cropped and saved. """ +from __future__ import division import sys import argparse @@ -87,8 +88,8 @@ def unit_vector(pt0, pt1): (float, float): unit vector """ dis_0_to_1 = sqrt((pt0[0] - pt1[0])**2 + (pt0[1] - pt1[1])**2) - return (pt1[0] - pt0[0]) / dis_0_to_1, \ - (pt1[1] - pt0[1]) / dis_0_to_1 + return (pt1[0] - pt0[0])/ dis_0_to_1, \ + (pt1[1] - pt0[1])/ dis_0_to_1 def orthogonal_vector(vector): @@ -130,7 +131,7 @@ def bounding_area(index, hull): return {'area': len_p * len_o, 'length_parallel': len_p, 'length_orthogonal': len_o, - 'rectangle_center': (min_p + len_p / 2, min_o + len_o / 2), + 'rectangle_center': (min_p + float(len_p)/ 2, min_o + float(len_o)/ 2), 'unit_vector': unit_vector_p, } @@ -143,7 +144,7 @@ def to_xy_coordinates(unit_vector_angle, point): ------ (float, float): converted x,y coordinate of the unit vector. """ - angle_orthogonal = unit_vector_angle + pi / 2 + angle_orthogonal = unit_vector_angle + pi/ 2 return point[0] * cos(unit_vector_angle) + point[1] * cos(angle_orthogonal), \ point[0] * sin(unit_vector_angle) + point[1] * sin(angle_orthogonal) @@ -235,8 +236,8 @@ def get_center(im): ------- (int, int): center of the image """ - center_x = im.size[0] / 2 - center_y = im.size[1] / 2 + center_x = float(im.size[0])/ 2 + center_y = float(im.size[1])/ 2 return int(center_x), int(center_y) @@ -248,9 +249,9 @@ def get_horizontal_angle(unit_vector_angle): (float): updated angle of the unit vector to be in radians. It is only in first or fourth quadrant. """ - if unit_vector_angle > pi / 2 and unit_vector_angle <= pi: + if unit_vector_angle > pi/ 2 and unit_vector_angle <= pi: unit_vector_angle = unit_vector_angle - pi - elif unit_vector_angle > -pi and unit_vector_angle < -pi / 2: + elif unit_vector_angle > -pi and unit_vector_angle < -pi/ 2: unit_vector_angle = unit_vector_angle + pi return unit_vector_angle @@ -354,7 +355,7 @@ def dilate_polygon(points, amount_increase): bisect = np.divide(bisect, np.linalg.norm(bisect)) cos_theta = np.dot(next_normal, bisect) - hyp = amount_increase / cos_theta + hyp = float(amount_increase)/ cos_theta new_point = np.around(point + hyp * bisect) new_point = new_point.astype(int) diff --git a/egs/madcat_ar/v1/local/process_data.py b/egs/madcat_ar/v1/local/process_data.py index e476b67cb96..71f7f39d632 100755 --- a/egs/madcat_ar/v1/local/process_data.py +++ b/egs/madcat_ar/v1/local/process_data.py @@ -24,23 +24,23 @@ " data/LDC2013T09 data/LDC2013T15 data/madcat.train.raw.lineid " " data/train data/local/lines ", formatter_class=argparse.ArgumentDefaultsHelpFormatter) -parser.add_argument('database_path1', type=str, +parser.add_argument('database_path1', help='Path to the downloaded (and extracted) madcat data') -parser.add_argument('database_path2', type=str, +parser.add_argument('database_path2', help='Path to the downloaded (and extracted) madcat data') -parser.add_argument('database_path3', type=str, +parser.add_argument('database_path3', help='Path to the downloaded (and extracted) madcat data') -parser.add_argument('data_splits', type=str, +parser.add_argument('data_splits', help='Path to file that contains the train/test/dev split information') -parser.add_argument('out_dir', type=str, +parser.add_argument('out_dir', help='directory location to write output files.') -parser.add_argument('images_scp_path', type=str, +parser.add_argument('images_scp_path', help='Path of input images.scp file(maps line image and location)') -parser.add_argument('writing_condition1', type=str, +parser.add_argument('writing_condition1', help='Path to the downloaded (and extracted) writing conditions file 1') -parser.add_argument('writing_condition2', type=str, +parser.add_argument('writing_condition2', help='Path to the downloaded (and extracted) writing conditions file 2') -parser.add_argument('writing_condition3', type=str, +parser.add_argument('writing_condition3', help='Path to the downloaded (and extracted) writing conditions file 3') parser.add_argument("--augment", type=lambda x: (str(x).lower()=='true'), default=False, help="performs image augmentation") @@ -192,25 +192,25 @@ def get_line_image_location(): if args.augment: key = (line_id + '.')[:-1] for i in range(0, 3): - location_id = '_' + line_id + '_scale' + str(i) + location_id = "_{}_scale{}".format(line_id, i) line_image_file_name = base_name + location_id + '.png' location = image_loc_dict[line_image_file_name] image_file_path = os.path.join(location, line_image_file_name) line = text_line_word_dict[key] text = ' '.join(line) base_line_image_file_name = line_image_file_name.split('.png')[0] - utt_id = writer_id + '_' + str(image_num).zfill(6) + '_' + base_line_image_file_name + utt_id = "{}_{}_{}".format(writer_id, str(image_num).zfill(6), base_line_image_file_name) text_fh.write(utt_id + ' ' + text + '\n') utt2spk_fh.write(utt_id + ' ' + writer_id + '\n') image_fh.write(utt_id + ' ' + image_file_path + '\n') image_num += 1 else: - updated_base_name = base_name + '_' + str(line_id).zfill(4) +'.png' + updated_base_name = "{}_{}.png".format(base_name, str(line_id).zfill(4)) location = image_loc_dict[updated_base_name] image_file_path = os.path.join(location, updated_base_name) line = text_line_word_dict[line_id] text = ' '.join(line) - utt_id = writer_id + '_' + str(image_num).zfill(6) + '_' + base_name + '_' + str(line_id).zfill(4) + utt_id = "{}_{}_{}_{}".format(writer_id, str(image_num).zfill(6), base_line_image_file_name, str(line_id).zfill(4)) text_fh.write(utt_id + ' ' + text + '\n') utt2spk_fh.write(utt_id + ' ' + writer_id + '\n') image_fh.write(utt_id + ' ' + image_file_path + '\n') diff --git a/egs/madcat_zh/v1/local/create_line_image_from_page_image.py b/egs/madcat_zh/v1/local/create_line_image_from_page_image.py index be0afe6d9fc..22af571fc04 100755 --- a/egs/madcat_zh/v1/local/create_line_image_from_page_image.py +++ b/egs/madcat_zh/v1/local/create_line_image_from_page_image.py @@ -76,8 +76,8 @@ def unit_vector(pt0, pt1): Eg. 0.31622776601683794, 0.9486832980505138 """ dis_0_to_1 = sqrt((pt0[0] - pt1[0])**2 + (pt0[1] - pt1[1])**2) - return (pt1[0] - pt0[0]) / dis_0_to_1, \ - (pt1[1] - pt0[1]) / dis_0_to_1 + return (pt1[0] - pt0[0])/ dis_0_to_1, \ + (pt1[1] - pt0[1])/ dis_0_to_1 def orthogonal_vector(vector): @@ -124,7 +124,7 @@ def bounding_area(index, hull): return {'area': len_p * len_o, 'length_parallel': len_p, 'length_orthogonal': len_o, - 'rectangle_center': (min_p + len_p / 2, min_o + len_o / 2), + 'rectangle_center': (min_p + float(len_p)/ 2, min_o + float(len_o)/ 2), 'unit_vector': unit_vector_p, } @@ -140,7 +140,7 @@ def to_xy_coordinates(unit_vector_angle, point): (float, float): converted x,y coordinate of the unit vector. Eg. 0.680742447866183, 2.1299271629971663 """ - angle_orthogonal = unit_vector_angle + pi / 2 + angle_orthogonal = unit_vector_angle + pi/ 2 return point[0] * cos(unit_vector_angle) + point[1] * cos(angle_orthogonal), \ point[0] * sin(unit_vector_angle) + point[1] * sin(angle_orthogonal) @@ -246,8 +246,8 @@ def get_center(im): (int, int): center of the image Eg. 2550, 3300 """ - center_x = im.size[0] / 2 - center_y = im.size[1] / 2 + center_x = float(im.size[0])/ 2 + center_y = float(im.size[1])/ 2 return int(center_x), int(center_y) @@ -262,9 +262,9 @@ def get_horizontal_angle(unit_vector_angle): Eg. 0.01543. """ - if unit_vector_angle > pi / 2 and unit_vector_angle <= pi: + if unit_vector_angle > pi/ 2 and unit_vector_angle <= pi: unit_vector_angle = unit_vector_angle - pi - elif unit_vector_angle > -pi and unit_vector_angle < -pi / 2: + elif unit_vector_angle > -pi and unit_vector_angle < -pi/ 2: unit_vector_angle = unit_vector_angle + pi return unit_vector_angle diff --git a/egs/madcat_zh/v1/local/process_data.py b/egs/madcat_zh/v1/local/process_data.py index dbee815953a..994a4486420 100755 --- a/egs/madcat_zh/v1/local/process_data.py +++ b/egs/madcat_zh/v1/local/process_data.py @@ -23,11 +23,11 @@ " data/LDC2013T09 data/LDC2013T15 data/madcat.train.raw.lineid " " data/train data/local/lines ", formatter_class=argparse.ArgumentDefaultsHelpFormatter) -parser.add_argument('database_path1', type=str, +parser.add_argument('database_path1', help='Path to the downloaded (and extracted) madcat data') -parser.add_argument('data_splits', type=str, +parser.add_argument('data_splits', help='Path to file that contains the train/test/dev split information') -parser.add_argument('out_dir', type=str, +parser.add_argument('out_dir', help='directory location to write output files.') args = parser.parse_args() @@ -185,12 +185,12 @@ def get_line_image_location(): base_name = os.path.basename(image_file_path) base_name, b = base_name.split('.tif') for lineID in sorted(text_line_word_dict): - updated_base_name = base_name + '_' + str(lineID).zfill(4) +'.png' + updated_base_name = "{}_{}.png".format(base_name, str(lineID).zfill(4)) location = image_loc_dict[updated_base_name] image_file_path = os.path.join(location, updated_base_name) line = text_line_word_dict[lineID] text = ' '.join(''.join(line)) - utt_id = writer_id + '_' + str(image_num).zfill(6) + '_' + base_name + '_' + str(lineID).zfill(4) + utt_id = "{}_{}_{}_{}".format(writer_id, str(image_num).zfill(6), base_name, str(lineID).zfill(4)) text_fh.write(utt_id + ' ' + text + '\n') utt2spk_fh.write(utt_id + ' ' + writer_id + '\n') image_fh.write(utt_id + ' ' + image_file_path + '\n') diff --git a/egs/multi_en/s5/local/format_acronyms_ctm_eval2000.py b/egs/multi_en/s5/local/format_acronyms_ctm_eval2000.py index 3c447c5976a..75cc4458d85 100755 --- a/egs/multi_en/s5/local/format_acronyms_ctm_eval2000.py +++ b/egs/multi_en/s5/local/format_acronyms_ctm_eval2000.py @@ -10,6 +10,7 @@ # en_4156 B 414.58 0.16 l # en_4156 B 414.74 0.17 a +from __future__ import division import argparse,re __author__ = 'Minhua Wu' @@ -27,7 +28,7 @@ if items[4].find(".") != -1: letters = items[4].split("._") acronym_period = round(float(items[3]), 2) - letter_slot = round(acronym_period / len(letters), 2) + letter_slot = round(acronym_period/len(letters), 2) time_start = round(float(items[2]), 2) for l in letters[:-1]: time = " %.2f %.2f " % (time_start, letter_slot) diff --git a/egs/multi_en/s5/local/format_acronyms_ctm_rt03.py b/egs/multi_en/s5/local/format_acronyms_ctm_rt03.py index 59814beb4ea..8438bbdaf81 100755 --- a/egs/multi_en/s5/local/format_acronyms_ctm_rt03.py +++ b/egs/multi_en/s5/local/format_acronyms_ctm_rt03.py @@ -10,6 +10,7 @@ # en_4156 B 414.58 0.16 l # en_4156 B 414.74 0.17 a +from __future__ import division import argparse,re __author__ = 'Minhua Wu' @@ -27,7 +28,7 @@ if items[4].find(".") != -1: letters = items[4].split("._") acronym_period = round(float(items[3]), 2) - letter_slot = round(acronym_period / len(letters), 2) + letter_slot = round(acronym_period/len(letters), 2) time_start = round(float(items[2]), 2) for l in letters[:-1]: time = " %.2f %.2f " % (time_start, letter_slot) diff --git a/egs/multi_en/s5/local/normalize_transcript.py b/egs/multi_en/s5/local/normalize_transcript.py index 4572f4d658d..c640723a885 100755 --- a/egs/multi_en/s5/local/normalize_transcript.py +++ b/egs/multi_en/s5/local/normalize_transcript.py @@ -7,6 +7,7 @@ # This script normalizes the given "text" (transcript) file. The normalized result # is printed to STDOUT. This normalization should be applied to all corpora. +from __future__ import print_function import re import sys @@ -26,7 +27,7 @@ def normalize(utt): def main(): if len(sys.argv) != 2: - print 'Usage: local/normalize_transcript.py [text_file]' + print('Usage: local/normalize_transcript.py [text_file]') sys.exit(1) with open(sys.argv[1], 'r') as f: for line in f.readlines(): diff --git a/egs/multi_en/s5/local/tedlium_join_suffix.py b/egs/multi_en/s5/local/tedlium_join_suffix.py index c85e8f364f6..47db4ce0b05 100755 --- a/egs/multi_en/s5/local/tedlium_join_suffix.py +++ b/egs/multi_en/s5/local/tedlium_join_suffix.py @@ -12,6 +12,7 @@ # Apache 2.0 +from __future__ import print_function import sys from codecs import open diff --git a/egs/sitw/v1/local/make_musan.py b/egs/sitw/v1/local/make_musan.py index 74c434990fb..c4b5c9359b4 100755 --- a/egs/sitw/v1/local/make_musan.py +++ b/egs/sitw/v1/local/make_musan.py @@ -47,9 +47,9 @@ def prepare_music(root_dir, use_vocals): utt2wav_str = utt2wav_str + utt + " " + utt2wav[utt] + "\n" num_good_files += 1 else: - print("Missing file", utt) + print("Missing file {}".format(utt)) num_bad_files += 1 - print("In music directory, processed", num_good_files, "files;", num_bad_files, "had missing wav data") + print(("In music directory, processed {} files: {} had missing wav data".format(num_good_files, num_bad_files)) return utt2spk_str, utt2wav_str def prepare_speech(root_dir): @@ -73,9 +73,9 @@ def prepare_speech(root_dir): utt2wav_str = utt2wav_str + utt + " " + utt2wav[utt] + "\n" num_good_files += 1 else: - print("Missing file", utt) + print("Missing file {}".format(utt)) num_bad_files += 1 - print("In speech directory, processed", num_good_files, "files;", num_bad_files, "had missing wav data") + print(("In speech directory, processed {} files: {} had missing wav data".format(num_good_files, num_bad_files)) return utt2spk_str, utt2wav_str def prepare_noise(root_dir): @@ -99,9 +99,9 @@ def prepare_noise(root_dir): utt2wav_str = utt2wav_str + utt + " " + utt2wav[utt] + "\n" num_good_files += 1 else: - print("Missing file", utt) + print("Missing file {}".format(utt)) num_bad_files += 1 - print("In noise directory, processed", num_good_files, "files;", num_bad_files, "had missing wav data") + print(("In noise directory, processed {} files: {} had missing wav data".format(num_good_files, num_bad_files)) return utt2spk_str, utt2wav_str def main(): diff --git a/egs/sprakbanken/s5/local/normalize_transcript.py b/egs/sprakbanken/s5/local/normalize_transcript.py index 2374418bee7..21d70864f04 100755 --- a/egs/sprakbanken/s5/local/normalize_transcript.py +++ b/egs/sprakbanken/s5/local/normalize_transcript.py @@ -17,8 +17,8 @@ "\t": " " } -from_chars = ''.join(normdict.keys()) -to_chars = ''.join(normdict.values()) +from_chars = ''.join(list(normdict.keys())) +to_chars = ''.join(list(normdict.values())) #t_table = maketrans(from_chars, to_chars) diff --git a/egs/sprakbanken/s5/local/sprak2kaldi.py b/egs/sprakbanken/s5/local/sprak2kaldi.py index f3abf1d9a38..5fa4baa1fa2 100755 --- a/egs/sprakbanken/s5/local/sprak2kaldi.py +++ b/egs/sprakbanken/s5/local/sprak2kaldi.py @@ -16,6 +16,7 @@ # limitations under the License. ''' +from __future__ import print_function import sys @@ -59,8 +60,8 @@ def create_parallel_file_list(session, sndlist, txtlist): if len(os.listdir(session.sessiondir)) != 0: # Check if there are files in the directory global n n += 1 - session.sessiondir = session.sessiondir + "_" + str(n) - session.speaker_id = session.speaker_id + "_" + str(n) + session.sessiondir = "{}_{}".format(session.sessiondir, n) + session.speaker_id = "{}_{}".format(session.speaker_id, n) os.mkdir(session.sessiondir) shadow = True else: diff --git a/egs/sprakbanken/s5/local/sprak2parallel.py b/egs/sprakbanken/s5/local/sprak2parallel.py index b5fe56fd60f..3dc82e30ac2 100755 --- a/egs/sprakbanken/s5/local/sprak2parallel.py +++ b/egs/sprakbanken/s5/local/sprak2parallel.py @@ -76,8 +76,8 @@ def make_speech_corpus(top, dest, srcfolder): session.sessiondir = os.path.join(dest, session.filestem) +"."+ session.speaker_id if os.path.exists(session.sessiondir): n += 1 - session.sessiondir = session.sessiondir+ "_" +str(n) - session.speaker_id+ "_" +str(n) + session.sessiondir = "{}_{}".format(session.sessiondir, n) + session.speaker_id = "{}_{}".format(session.speaker_id, n) os.mkdir(session.sessiondir) create_parallel_files(session) diff --git a/egs/sprakbanken/s5/local/sprakparser.py b/egs/sprakbanken/s5/local/sprakparser.py index 7bdf6ac94e3..1221cf0b023 100755 --- a/egs/sprakbanken/s5/local/sprakparser.py +++ b/egs/sprakbanken/s5/local/sprakparser.py @@ -22,11 +22,12 @@ ''' +from __future__ import print_function import codecs import os -class Session: +class Session(object): delimit = ">-<" @@ -151,7 +152,7 @@ def set_channel_vars(self, handle): pass def create_filename(self, uid, file_ending): - return self.filestem+ "." +self.speaker_id+ "." +str(uid)+ "." +file_ending + return "{}.{}.{}.{}".format(self.filestem, self.speaker_id, uid, file_ending) def wavpath(self, topfolder): prefix, suffix = topfolder.rsplit('/data/', 1) diff --git a/egs/sprakbanken/s5/local/writenumbers.py b/egs/sprakbanken/s5/local/writenumbers.py index df3235243d4..c419b3c7550 100755 --- a/egs/sprakbanken/s5/local/writenumbers.py +++ b/egs/sprakbanken/s5/local/writenumbers.py @@ -22,6 +22,7 @@ Changed to write output to file to prevent problems with shell ascii codec. ''' +from __future__ import print_function import sys import os @@ -215,7 +216,7 @@ def rmPvAnnotation(string): def normNumber(line, table): tokens = line.split() - keys = table.keys() + keys = list(table.keys()) for num, tok in enumerate(tokens): newtoks = splitNumeric(tok) if newtoks != False: diff --git a/egs/sprakbanken_swe/s5/local/normalize_transcript.py b/egs/sprakbanken_swe/s5/local/normalize_transcript.py index 90e45744e2a..150a9563aba 100755 --- a/egs/sprakbanken_swe/s5/local/normalize_transcript.py +++ b/egs/sprakbanken_swe/s5/local/normalize_transcript.py @@ -18,8 +18,8 @@ } #removes all the above signs -from_chars = ''.join(normdict.keys()) -to_chars = ''.join(normdict.values()) +from_chars = ''.join(list(normdict.keys())) +to_chars = ''.join(list(normdict.values())) t_table = str.maketrans(normdict) diff --git a/egs/sprakbanken_swe/s5/local/sprak2kaldi.py b/egs/sprakbanken_swe/s5/local/sprak2kaldi.py index cc67344c36e..8f723762e50 100755 --- a/egs/sprakbanken_swe/s5/local/sprak2kaldi.py +++ b/egs/sprakbanken_swe/s5/local/sprak2kaldi.py @@ -16,6 +16,7 @@ # limitations under the License. ''' +from __future__ import print_function import sys @@ -59,8 +60,8 @@ def create_parallel_file_list(session, sndlist, txtlist): if len(os.listdir(session.sessiondir)) != 0: # Check if there are files in the directory global n n += 1 - session.sessiondir = session.sessiondir + "_" + str(n) - session.speaker_id = session.speaker_id + "_" + str(n) + session.sessiondir = "{}_{}".format(session.sessiondir, n) + session.speaker_id = "{}_{}".format(session.speaker_id, n) os.mkdir(session.sessiondir) shadow = True else: diff --git a/egs/sprakbanken_swe/s5/local/sprakparser.py b/egs/sprakbanken_swe/s5/local/sprakparser.py index 4775328b56b..0951f7f39e7 100755 --- a/egs/sprakbanken_swe/s5/local/sprakparser.py +++ b/egs/sprakbanken_swe/s5/local/sprakparser.py @@ -26,7 +26,7 @@ import codecs import os -class Session: +class Session(object): delimit = ">-<" @@ -151,7 +151,7 @@ def set_channel_vars(self, handle): pass def create_filename(self, uid, file_ending): - return self.filestem+ "." +self.speaker_id+ "." +str(uid)+ "." +file_ending + return "{}.{}.{}.{}".format(self.filestem, self.speaker_id, uid, file_ending) def wavpath(self, topfolder): prefix, suffix = topfolder.rsplit('/data/', 1) diff --git a/egs/sre08/v1/sid/nnet3/xvector/allocate_egs.py b/egs/sre08/v1/sid/nnet3/xvector/allocate_egs.py index 72a4572d9a0..e1a4fc534e0 100755 --- a/egs/sre08/v1/sid/nnet3/xvector/allocate_egs.py +++ b/egs/sre08/v1/sid/nnet3/xvector/allocate_egs.py @@ -65,6 +65,7 @@ # We're using python 3.x style print but want it to work in python 2.x. from __future__ import print_function +from __future__ import division import re, os, argparse, sys, math, warnings, random def get_args(): @@ -196,7 +197,7 @@ def deterministic_chunk_length(archive_id, num_archives, min_frames_per_chunk, m elif num_archives == 1: return int(max_frames_per_chunk); else: - return int(math.pow(float(max_frames_per_chunk) / + return int(math.pow(float(max_frames_per_chunk)/ min_frames_per_chunk, float(archive_id) / (num_archives-1)) * min_frames_per_chunk + 0.5) @@ -247,7 +248,7 @@ def main(): length = deterministic_chunk_length(archive_index, args.num_archives, args.min_frames_per_chunk, args.max_frames_per_chunk); print("{0} {1}".format(archive_index + 1, length), file=info_f) archive_chunk_lengths.append(length) - this_num_egs = int((args.frames_per_iter / length) + 1) + this_num_egs = int(float(args.frames_per_iter) / length + 1) this_egs = [ ] # A 2-tuple of the form (utt-id, start-frame) spkrs = args.num_repeats * list(spk2utt.keys()) random.shuffle(spkrs) diff --git a/egs/sre10/v1/local/prepare_for_eer.py b/egs/sre10/v1/local/prepare_for_eer.py index 59d2985e7c2..bb4e666f0ab 100755 --- a/egs/sre10/v1/local/prepare_for_eer.py +++ b/egs/sre10/v1/local/prepare_for_eer.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Copyright 2015 David Snyder # Apache 2.0. # @@ -12,4 +13,4 @@ spkrutt2target[spkr+utt]=target for line in scores: spkr, utt, score = line.strip().split() - print score, spkrutt2target[spkr+utt] + print("{} {}".format(score, spkrutt2target[spkr+utt])) diff --git a/egs/sre16/v1/local/make_musan.py b/egs/sre16/v1/local/make_musan.py index b3f6652ba40..7735bd28818 100755 --- a/egs/sre16/v1/local/make_musan.py +++ b/egs/sre16/v1/local/make_musan.py @@ -43,9 +43,9 @@ def prepare_music(root_dir, use_vocals): utt2wav_str = utt2wav_str + utt + " sox -t wav " + utt2wav[utt] + " -r 8k -t wav - |\n" num_good_files += 1 else: - print("Missing file", utt) + print("Missing file {}".format(utt)) num_bad_files += 1 - print("In music directory, processed", num_good_files, "files;", num_bad_files, "had missing wav data") + print("In music directory, processed {} files; {} had missing wav data".format(num_good_files, num_bad_files)) return utt2spk_str, utt2wav_str def prepare_speech(root_dir): @@ -69,9 +69,9 @@ def prepare_speech(root_dir): utt2wav_str = utt2wav_str + utt + " sox -t wav " + utt2wav[utt] + " -r 8k -t wav - |\n" num_good_files += 1 else: - print("Missing file", utt) + print("Missing file {}".format(utt)) num_bad_files += 1 - print("In speech directory, processed", num_good_files, "files;", num_bad_files, "had missing wav data") + print("In speech directory, processed {} files; {} had missing wav data".format(num_good_files, num_bad_files)) return utt2spk_str, utt2wav_str def prepare_noise(root_dir): @@ -95,9 +95,9 @@ def prepare_noise(root_dir): utt2wav_str = utt2wav_str + utt + " sox -t wav " + utt2wav[utt] + " -r 8k -t wav - |\n" num_good_files += 1 else: - print("Missing file", utt) + print("Missing file {}".format(utt)) num_bad_files += 1 - print("In noise directory, processed", num_good_files, "files;", num_bad_files, "had missing wav data") + print("In noise directory, processed {} files; {} had missing wav data".format(num_good_files, num_bad_files)) return utt2spk_str, utt2wav_str def main(): diff --git a/egs/svhn/v1/local/process_data.py b/egs/svhn/v1/local/process_data.py index f6ea85118f9..2a5bfc9a0d6 100755 --- a/egs/svhn/v1/local/process_data.py +++ b/egs/svhn/v1/local/process_data.py @@ -6,6 +6,7 @@ """ This script prepares the training and test data for SVHN. """ +from __future__ import division import argparse import os @@ -16,11 +17,11 @@ parser = argparse.ArgumentParser(description="""Converts train/test data of SVHN (Street View House Numbers) dataset to Kaldi feature format""") -parser.add_argument('matlab_file', type=str, +parser.add_argument('matlab_file', help='path to SVHN matlab data file (cropped version)') -parser.add_argument('dir', type=str, +parser.add_argument('dir', help='output dir') -parser.add_argument('--out-ark', type=str, +parser.add_argument('--out-ark', default='-', help='where to write output feature data') args = parser.parse_args() @@ -48,7 +49,7 @@ def write_kaldi_matrix(file_handle, matrix, key): if num_cols != len(matrix[row_index]): raise Exception("All the rows of a matrix are expected to " "have the same length") - file_handle.write(" ".join(map(lambda x: str(x), matrix[row_index]))) + file_handle.write(" ".join([str(x) for x in matrix[row_index]])) if row_index != num_rows - 1: file_handle.write("\n") file_handle.write(" ]\n") @@ -80,7 +81,7 @@ def zeropad(x, length): lbl = labels[i, 0] if lbl == 10: lbl = 0 - labels_fh.write(key + ' ' + str(lbl) + '\n') + labels_fh.write("{} {}\n".format(key, lbl)) img = data[i] write_kaldi_matrix(out_fh, img, key) img_id += 1 diff --git a/egs/swbd/s5c/local/map_acronyms_ctm.py b/egs/swbd/s5c/local/map_acronyms_ctm.py index bee488f73b0..7ae59d2a1d0 100755 --- a/egs/swbd/s5c/local/map_acronyms_ctm.py +++ b/egs/swbd/s5c/local/map_acronyms_ctm.py @@ -10,6 +10,7 @@ # en_4156 B 414.58 0.16 l # en_4156 B 414.74 0.17 a +from __future__ import division import argparse,re __author__ = 'Minhua Wu' diff --git a/egs/tedlium/s5/local/join_suffix.py b/egs/tedlium/s5/local/join_suffix.py index 64c62964331..c36b96a07f9 100755 --- a/egs/tedlium/s5/local/join_suffix.py +++ b/egs/tedlium/s5/local/join_suffix.py @@ -5,6 +5,7 @@ # Apache 2.0 +from __future__ import print_function import sys from codecs import open diff --git a/egs/tedlium/s5_r2/local/join_suffix.py b/egs/tedlium/s5_r2/local/join_suffix.py index 64c62964331..c36b96a07f9 100755 --- a/egs/tedlium/s5_r2/local/join_suffix.py +++ b/egs/tedlium/s5_r2/local/join_suffix.py @@ -5,6 +5,7 @@ # Apache 2.0 +from __future__ import print_function import sys from codecs import open diff --git a/egs/tedlium/s5_r2_wsj/local/lm/merge_word_counts.py b/egs/tedlium/s5_r2_wsj/local/lm/merge_word_counts.py index 6338cbbf875..85e15d8dc07 100755 --- a/egs/tedlium/s5_r2_wsj/local/lm/merge_word_counts.py +++ b/egs/tedlium/s5_r2_wsj/local/lm/merge_word_counts.py @@ -7,6 +7,7 @@ A min-count argument is required to only write counts that are above the specified minimum count. """ +from __future__ import print_function import sys @@ -21,7 +22,7 @@ def main(): parts = line.strip().split() words[parts[1]] = words.get(parts[1], 0) + int(parts[0]) - for word, count in words.iteritems(): + for word, count in words.items(): if count >= int(sys.argv[1]): print ("{0} {1}".format(count, word)) diff --git a/egs/tedlium/s5_r3/local/join_suffix.py b/egs/tedlium/s5_r3/local/join_suffix.py index 64c62964331..c36b96a07f9 100755 --- a/egs/tedlium/s5_r3/local/join_suffix.py +++ b/egs/tedlium/s5_r3/local/join_suffix.py @@ -5,6 +5,7 @@ # Apache 2.0 +from __future__ import print_function import sys from codecs import open diff --git a/egs/thchs30/s5/local/dae/add-noise-mod.py b/egs/thchs30/s5/local/dae/add-noise-mod.py index 8327fc325ee..4486fd0fdc7 100755 --- a/egs/thchs30/s5/local/dae/add-noise-mod.py +++ b/egs/thchs30/s5/local/dae/add-noise-mod.py @@ -3,6 +3,7 @@ from __future__ import print_function +from __future__ import division import optparse import random import bisect @@ -26,7 +27,7 @@ def energy(mat): def mix(mat, noise, pos, scale): ret = [] l = len(noise) - for i in xrange(len(mat)): + for i in range(len(mat)): x = mat[i] d = int(x + scale * noise[pos]) #if d > 32767 or d < -32768: @@ -41,8 +42,8 @@ def mix(mat, noise, pos, scale): def dirichlet(params): samples = [random.gammavariate(x, 1) if x > 0 else 0. for x in params] - samples = [x / sum(samples) for x in samples] - for x in xrange(1, len(samples)): + samples = [(x / sum(samples)) for x in samples] + for x in range(1, len(samples)): samples[x] += samples[x - 1] return bisect.bisect_left(samples, random.random()) @@ -125,7 +126,7 @@ def main(): mat = wave_mat(wav) signal = energy(mat) logging.debug('signal energy: %f', signal) - noise = signal / (10 ** (noise_level / 10.)) + noise = signal / (10 ** (noise_level / 10)) logging.debug('noise energy: %f', noise) type = dirichlet(params) logging.debug('selected type: %d', type) diff --git a/egs/tunisian_msa/s5/local/buckwalter2unicode.py b/egs/tunisian_msa/s5/local/buckwalter2unicode.py index 94fec3225dd..f81841261ce 100755 --- a/egs/tunisian_msa/s5/local/buckwalter2unicode.py +++ b/egs/tunisian_msa/s5/local/buckwalter2unicode.py @@ -27,6 +27,7 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # +from __future__ import print_function import sys, getopt, codecs, os, re # Declare a dictionary with Buckwalter's ASCII symbols as the keys, and @@ -87,7 +88,7 @@ uni2buck = {} # Iterate through all the items in the buck2uni dict. -for (key, value) in buck2uni.iteritems(): +for (key, value) in buck2uni.items(): # The value from buck2uni becomes a key in uni2buck, and vice # versa for the keys. uni2buck[value] = key @@ -108,103 +109,103 @@ # A function to print to screen the usage details of this script. def usage(): - print "Usage:", sys.argv[0], "-i INFILE -o OUTFILE [-g CHARS -c RANGE -d CHAR" - print " -r -e INPUT_ENCODING, -E OUTPUT ENCODING]" - print " ", sys.argv[0], "-l" - print " ", sys.argv[0], "-h" - print "" - print " -i INFILE, --input=INFILE:" - print " Path to text file to be transliterated to Unicode." - print " -o OUTFILE, --output=OUTFILE:" - print " Path of file to output the newly transliterated text." - print " -e ENC, --input-encoding=ENC:" - print " Specify the text encoding of the source file. Default: latin_1." - print " -E ENC, --output-encoding=ENC:" - print " Specify the text encoding of the target file. Default: utf_8." - print " -g CHARS, --ignore-lines=CHARS:" - print " Will not transliterate lines that start with any of the CHARS" - print " given. E.g., -g #; will not alter lines starting with # or ;." - print " (May need to be -g \#\; on some platforms. See README.txt.)" - print " -c RANGE, --columns=RANGE:" - print " If in columns, select columns to apply transliteration. Can be" - print " comma separated numbers, or a range. E.g., -c 1, -c 1-3, -c 1,3." - print " -d CHAR, --delimiter=CHAR:" - print " Specify the delimiter that defines the column if using the -c" - print " option above. Default is ' ' (space)." - print " -r, --reverse:" - print " Reverses the transliteration, i.e., Arabic to Buckwalter." - print " When used, it will change the default input encoding to utf_8 and" - print " output encoding to latin_1" - print " -l, --list-encodings:" - print " Displays all supported file encodings." - print " -h, --help:" - print " Displays this page." - print "" + print("Usage: {} -i INFILE -o OUTFILE [-g CHARS -c RANGE -d CHAR".format(sys.argv[0])) + print(" -r -e INPUT_ENCODING, -E OUTPUT ENCODING]") + print(" {} -l".format(sys.argv[0])) + print(" {} -h".format(sys.argv[0])) + print("") + print(" -i INFILE, --input=INFILE:") + print(" Path to text file to be transliterated to Unicode.") + print(" -o OUTFILE, --output=OUTFILE:") + print(" Path of file to output the newly transliterated text.") + print(" -e ENC, --input-encoding=ENC:") + print(" Specify the text encoding of the source file. Default: latin_1.") + print(" -E ENC, --output-encoding=ENC:") + print(" Specify the text encoding of the target file. Default: utf_8.") + print(" -g CHARS, --ignore-lines=CHARS:") + print(" Will not transliterate lines that start with any of the CHARS") + print(" given. E.g., -g #; will not alter lines starting with # or ;.") + print(" (May need to be -g \#\; on some platforms. See README.txt.)") + print(" -c RANGE, --columns=RANGE:") + print(" If in columns, select columns to apply transliteration. Can be") + print(" comma separated numbers, or a range. E.g., -c 1, -c 1-3, -c 1,3.") + print(" -d CHAR, --delimiter=CHAR:") + print(" Specify the delimiter that defines the column if using the -c") + print(" option above. Default is ' ' (space).") + print(" -r, --reverse:") + print(" Reverses the transliteration, i.e., Arabic to Buckwalter.") + print(" When used, it will change the default input encoding to utf_8 and") + print(" output encoding to latin_1") + print(" -l, --list-encodings:") + print(" Displays all supported file encodings.") + print(" -h, --help:") + print(" Displays this page.") + print("") # A function to print to screen all the available encodings supported by # Python. def displayEncodings(): - print "Codec Aliases Languages" - print "ascii 646, us-ascii English" - print "cp037 IBM037, IBM039 English" - print "cp424 EBCDIC-CP-HE, IBM424 Hebrew" - print "cp437 437, IBM437 English" - print "cp500 EBCDIC-CP-BE, EBCDIC-CP-CH, IBM500 Western Europe" - print "cp737 Greek" - print "cp775 IBM775 Baltic languages" - print "cp850 850, IBM850 Western Europe" - print "cp852 852, IBM852 Central and Eastern Europe" - print "cp855 855, IBM855 Bulgarian, Byelorussian, Macedonian, Russian, Serbian" - print "cp856 Hebrew" - print "cp857 857, IBM857 Turkish" - print "cp860 860, IBM860 Portuguese" - print "cp861 861, CP-IS, IBM861 Icelandic" - print "cp862 862, IBM862 Hebrew" - print "cp863 863, IBM863 Canadian" - print "cp864 IBM864 Arabic" - print "cp865 865, IBM865 Danish, Norwegian" - print "cp869 869, CP-GR, IBM869 Greek" - print "cp874 Thai" - print "cp875 Greek" - print "cp1006 Urdu" - print "cp1026 ibm1026 Turkish" - print "cp1140 ibm1140 Western Europe" - print "cp1250 windows-1250 Central and Eastern Europe" - print "cp1251 windows-1251 Bulgarian, Byelorussian, Macedonian, Russian, Serbian" - print "cp1252 windows-1252 Western Europe" - print "cp1253 windows-1253 Greek" - print "cp1254 windows-1254 Turkish" - print "cp1255 windows-1255 Hebrew" - print "cp1256 windows-1256 Arabic" - print "cp1257 windows-1257 Baltic languages" - print "cp1258 windows-1258 Vietnamese" - print "latin_1 iso-8859-1, iso8859-1, 8859, cp819, latin, latin1, L1 West Europe" - print "iso8859_2 iso-8859-2, latin2, L2 Central and Eastern Europe" - print "iso8859_3 iso-8859-3, latin3, L3 Esperanto, Maltese" - print "iso8859_4 iso-8859-4, latin4, L4 Baltic languagues" - print "iso8859_5 iso-8859-5, cyrillic Bulgarian, Byelorussian, Macedonian, Russian, Serbian" - print "iso8859_6 iso-8859-6, arabic Arabic" - print "iso8859_7 iso-8859-7, greek, greek8 Greek" - print "iso8859_8 iso-8859-8, hebrew Hebrew" - print "iso8859_9 iso-8859-9, latin5, L5 Turkish" - print "iso8859_10 iso-8859-10, latin6, L6 Nordic languages" - print "iso8859_13 iso-8859-13 Baltic languages" - print "iso8859_14 iso-8859-14, latin8, L8 Celtic languages" - print "iso8859_15 iso-8859-15 Western Europe" - print "koi8_r Russian" - print "koi8_u Ukrainian" - print "mac_cyrillic maccyrillic Bulgarian, Byelorussian, Macedonian, Russian, Serbian" - print "mac_greek macgreek Greek" - print "mac_iceland maciceland Icelandic" - print "mac_latin2 maclatin2, maccentraleurope Central and Eastern Europe" - print "mac_roman macroman Western Europe" - print "mac_turkish macturkish Turkish" - print "utf_16 U16, utf16 all languages" - print "utf_16_be UTF-16BE all languages (BMP only)" - print "utf_16_le UTF-16LE all languages (BMP only)" - print "utf_7 U7 all languages" - print "utf_8 U8, UTF, utf8 all languages" + print("Codec Aliases Languages") + print("ascii 646, us-ascii English") + print("cp037 IBM037, IBM039 English") + print("cp424 EBCDIC-CP-HE, IBM424 Hebrew") + print("cp437 437, IBM437 English") + print("cp500 EBCDIC-CP-BE, EBCDIC-CP-CH, IBM500 Western Europe") + print("cp737 Greek") + print("cp775 IBM775 Baltic languages") + print("cp850 850, IBM850 Western Europe") + print("cp852 852, IBM852 Central and Eastern Europe") + print("cp855 855, IBM855 Bulgarian, Byelorussian, Macedonian, Russian, Serbian") + print("cp856 Hebrew") + print("cp857 857, IBM857 Turkish") + print("cp860 860, IBM860 Portuguese") + print("cp861 861, CP-IS, IBM861 Icelandic") + print("cp862 862, IBM862 Hebrew") + print("cp863 863, IBM863 Canadian") + print("cp864 IBM864 Arabic") + print("cp865 865, IBM865 Danish, Norwegian") + print("cp869 869, CP-GR, IBM869 Greek") + print("cp874 Thai") + print("cp875 Greek") + print("cp1006 Urdu") + print("cp1026 ibm1026 Turkish") + print("cp1140 ibm1140 Western Europe") + print("cp1250 windows-1250 Central and Eastern Europe") + print("cp1251 windows-1251 Bulgarian, Byelorussian, Macedonian, Russian, Serbian") + print("cp1252 windows-1252 Western Europe") + print("cp1253 windows-1253 Greek") + print("cp1254 windows-1254 Turkish") + print("cp1255 windows-1255 Hebrew") + print("cp1256 windows-1256 Arabic") + print("cp1257 windows-1257 Baltic languages") + print("cp1258 windows-1258 Vietnamese") + print("latin_1 iso-8859-1, iso8859-1, 8859, cp819, latin, latin1, L1 West Europe") + print("iso8859_2 iso-8859-2, latin2, L2 Central and Eastern Europe") + print("iso8859_3 iso-8859-3, latin3, L3 Esperanto, Maltese") + print("iso8859_4 iso-8859-4, latin4, L4 Baltic languagues") + print("iso8859_5 iso-8859-5, cyrillic Bulgarian, Byelorussian, Macedonian, Russian, Serbian") + print("iso8859_6 iso-8859-6, arabic Arabic") + print("iso8859_7 iso-8859-7, greek, greek8 Greek") + print("iso8859_8 iso-8859-8, hebrew Hebrew") + print("iso8859_9 iso-8859-9, latin5, L5 Turkish") + print("iso8859_10 iso-8859-10, latin6, L6 Nordic languages") + print("iso8859_13 iso-8859-13 Baltic languages") + print("iso8859_14 iso-8859-14, latin8, L8 Celtic languages") + print("iso8859_15 iso-8859-15 Western Europe") + print("koi8_r Russian") + print("koi8_u Ukrainian") + print("mac_cyrillic maccyrillic Bulgarian, Byelorussian, Macedonian, Russian, Serbian") + print("mac_greek macgreek Greek") + print("mac_iceland maciceland Icelandic") + print("mac_latin2 maclatin2, maccentraleurope Central and Eastern Europe") + print("mac_roman macroman Western Europe") + print("mac_turkish macturkish Turkish") + print("utf_16 U16, utf16 all languages") + print("utf_16_be UTF-16BE all languages (BMP only)") + print("utf_16_le UTF-16LE all languages (BMP only)") + print("utf_7 U7 all languages") + print("utf_8 U8, UTF, utf8 all languages") def parseIgnoreString(string): @@ -254,13 +255,13 @@ def parseIgnoreString(string): delimiter = delimiter.replace("\\t", "\t") # Do some error checking if len(delimiter) > 1: - print >>sys.stderr, "Delimeter should only be a single character. Using first character" + delimiter[0] + print("Delimeter should only be a single character. Using first character" + delimiter[0], file=sys.stderr) delimiter = delimiter[0] if buck2uni.get(delimiter): - print >> sys.stderr, "Invalid delimiter. \"" + delimiter + "\" is part of the Buckwalter character set." - print >> sys.stderr, "This will obviously cause much confusion as a delimiter!" - print >> sys.stderr, "Please try again. Aborting..." + print("Invalid delimiter. \"" + delimiter + "\" is part of the Buckwalter character set.", file=sys.stderr) + print("This will obviously cause much confusion as a delimiter!", file=sys.stderr) + print("Please try again. Aborting...", file=sys.stderr) sys.exit(1) # If no delimiter was set then, set the default to " " (space) @@ -303,16 +304,16 @@ def parseIgnoreString(string): # specified output encoding. outFile = codecs.open(outFilename, "w", outEnc) - except IOError, msg: + except IOError as msg: # A problem occurred when trying to open this file. Report to # user... - print msg + print(msg) sys.exit(1) # Script can not work without somewhere to store the transliteration. # Exit. else: - print "Must specify a file to use store the output! Aborting..." + print("Must specify a file to use store the output! Aborting...") sys.exit(1) # Providing a file for input was specified... @@ -322,15 +323,15 @@ def parseIgnoreString(string): # specified input encoding. inFile = codecs.open(inFilename, "r", inEnc) - except IOError, msg: + except IOError as msg: # A problem occurred when trying to open this file. Report to # user... - print msg + print(msg) sys.exit(1) # This script requires a file to read from. Exit. else: - print "Must specify a file to use as input! Aborting..." + print("Must specify a file to use as input! Aborting...") sys.exit(1) def getColsFromRange(cRange): @@ -344,7 +345,7 @@ def getColsFromRange(cRange): # If it contains a hyphen (e.g., 1-3) if hyphenSearch.search(i): [start, end] = i.split("-") - columns = columns + range(int(start)-1,int(end)) + columns = columns + list(range(int(start)-1,int(end))) else: columns.append(int(i)-1) @@ -441,9 +442,9 @@ def transliterateString(inString): currentLineNumber = currentLineNumber + 1 - except UnicodeError, msg: + except UnicodeError as msg: # A problem when writing occurred. Report to user... - print msg + print(msg) sys.exit(1) # All done! Better close the files used before terminating... diff --git a/egs/uw3/v1/local/make_features.py b/egs/uw3/v1/local/make_features.py index dd0a30a19d7..e0211963e39 100755 --- a/egs/uw3/v1/local/make_features.py +++ b/egs/uw3/v1/local/make_features.py @@ -24,8 +24,8 @@ parser = argparse.ArgumentParser(description="""Converts images (in 'dir'/images.scp) to features and writes them to standard output in text format.""") -parser.add_argument('dir', type=str, help='data directory (should contain images.scp)') -parser.add_argument('--out-ark', type=str, default='-', help='where to write the output feature file.') +parser.add_argument('dir', help='data directory (should contain images.scp)') +parser.add_argument('--out-ark', default='-', help='where to write the output feature file.') parser.add_argument('--feat-dim', type=int, default=40, help='size to scale the height of all images (i.e. the dimension of the resulting features)') parser.add_argument('--pad', type=bool, default=False, help='pad the left and right of the images with 10 white pixels.') @@ -43,7 +43,7 @@ def write_kaldi_matrix(file_handle, matrix, key): if num_cols != len(matrix[row_index]): raise Exception("All the rows of a matrix are expected to " "have the same length") - file_handle.write(" ".join(map(lambda x: str(x), matrix[row_index]))) + file_handle.write(" ".join([str(x) for x in matrix[row_index]])) if row_index != num_rows - 1: file_handle.write("\n") file_handle.write(" ]\n") diff --git a/egs/uw3/v1/local/process_data.py b/egs/uw3/v1/local/process_data.py index f5b37b04c2f..3643c0aca89 100755 --- a/egs/uw3/v1/local/process_data.py +++ b/egs/uw3/v1/local/process_data.py @@ -14,8 +14,8 @@ import random parser = argparse.ArgumentParser(description="""Creates data/train and data/test.""") -parser.add_argument('database_path', type=str, help='path to downloaded (and extracted) UW3 corpus') -parser.add_argument('out_dir', type=str, default='data', +parser.add_argument('database_path', help='path to downloaded (and extracted) UW3 corpus') +parser.add_argument('out_dir', default='data', help='where to create the train and test data directories') args = parser.parse_args() @@ -53,9 +53,9 @@ coin = random.randint(0, 20) if coin >= 1: train_text_fh.write(utt_id + ' ' + text + '\n') - train_utt2spk_fh.write(utt_id + ' ' + str(page_count) + '\n') - train_image_fh.write(utt_id + ' ' + image_path + '\n') + train_utt2spk_fh.write("{} {}\n".format(utt_id, page_count)) + train_image_fh.write("{} {}\n".format(utt_id, image_path) elif coin < 1: - test_text_fh.write(utt_id + ' ' + text + '\n') - test_utt2spk_fh.write(utt_id + ' ' + str(page_count) + '\n') - test_image_fh.write(utt_id + ' ' + image_path + '\n') + test_text_fh.write("{} {}\n".format(utt_id, text)) + test_utt2spk_fh.write("{} {}\n".format(utt_id, page_count)) + train_image_fh.write("{} {}\n".format(utt_id, image_path) diff --git a/egs/voxceleb/v1/local/make_musan.py b/egs/voxceleb/v1/local/make_musan.py index 74c434990fb..565bfce0cc9 100755 --- a/egs/voxceleb/v1/local/make_musan.py +++ b/egs/voxceleb/v1/local/make_musan.py @@ -47,9 +47,9 @@ def prepare_music(root_dir, use_vocals): utt2wav_str = utt2wav_str + utt + " " + utt2wav[utt] + "\n" num_good_files += 1 else: - print("Missing file", utt) + print("Missing file {}".format(utt)) num_bad_files += 1 - print("In music directory, processed", num_good_files, "files;", num_bad_files, "had missing wav data") + print("In music directory, processed {} files; {} had missing wav data".format(num_good_files, num_bad_files)) return utt2spk_str, utt2wav_str def prepare_speech(root_dir): @@ -73,9 +73,9 @@ def prepare_speech(root_dir): utt2wav_str = utt2wav_str + utt + " " + utt2wav[utt] + "\n" num_good_files += 1 else: - print("Missing file", utt) + print("Missing file {}".format(utt)) num_bad_files += 1 - print("In speech directory, processed", num_good_files, "files;", num_bad_files, "had missing wav data") + print("In speech directory, processed {} files; {} had missing wav data".format(num_good_files, num_bad_files)) return utt2spk_str, utt2wav_str def prepare_noise(root_dir): @@ -99,9 +99,9 @@ def prepare_noise(root_dir): utt2wav_str = utt2wav_str + utt + " " + utt2wav[utt] + "\n" num_good_files += 1 else: - print("Missing file", utt) + print("Missing file {}".format(utt)) num_bad_files += 1 - print("In noise directory, processed", num_good_files, "files;", num_bad_files, "had missing wav data") + print("In noise directory, processed {} files; {} had missing wav data".format(num_good_files, num_bad_files)) return utt2spk_str, utt2wav_str def main(): diff --git a/egs/voxceleb/v1/local/prepare_for_eer.py b/egs/voxceleb/v1/local/prepare_for_eer.py index 6bfa04e011b..2f569b70bc5 100755 --- a/egs/voxceleb/v1/local/prepare_for_eer.py +++ b/egs/voxceleb/v1/local/prepare_for_eer.py @@ -16,4 +16,4 @@ spkrutt2target[spkr+utt]=target for line in scores: spkr, utt, score = line.strip().split() - print(score, spkrutt2target[spkr+utt]) + print("{} {}".format(score, spkrutt2target[spkr+utt])) diff --git a/egs/voxforge/gst_demo/run-live.py b/egs/voxforge/gst_demo/run-live.py index 725a306c42c..7876e5f2046 100755 --- a/egs/voxforge/gst_demo/run-live.py +++ b/egs/voxforge/gst_demo/run-live.py @@ -6,6 +6,7 @@ # # Apache 2.0 +from __future__ import print_function import sys import os import gi @@ -46,7 +47,7 @@ def init_gst(self): """Initialize the speech components""" self.pulsesrc = Gst.ElementFactory.make("pulsesrc", "pulsesrc") if self.pulsesrc == None: - print >> sys.stderr, "Error loading pulsesrc GST plugin. You probably need the gstreamer1.0-pulseaudio package" + print("Error loading pulsesrc GST plugin. You probably need the gstreamer1.0-pulseaudio package", file=sys.stderr) sys.exit() self.audioconvert = Gst.ElementFactory.make("audioconvert", "audioconvert") self.audioresample = Gst.ElementFactory.make("audioresample", "audioresample") @@ -56,7 +57,7 @@ def init_gst(self): if self.asr: model_dir = "online-data/models/tri2b_mmi/" if not os.path.isdir(model_dir): - print >> sys.stderr, "Model (%s) not downloaded. Run run-simulated.sh first" % model_dir + print("Model (%s) not downloaded. Run run-simulated.sh first" % model_dir, file=sys.stderr) sys.exit(1) self.asr.set_property("fst", model_dir + "HCLG.fst") self.asr.set_property("lda-mat", model_dir + "matrix") @@ -67,12 +68,12 @@ def init_gst(self): self.asr.set_property("beam", 12.0) self.asr.set_property("acoustic-scale", 0.0769) else: - print >> sys.stderr, "Couldn't create the onlinegmmfasterdecoder element. " + print("Couldn't create the onlinegmmfasterdecoder element. ", file=sys.stderr) if "GST_PLUGIN_PATH" in os.environ: - print >> sys.stderr, "Have you compiled the Kaldi GStreamer plugin?" + print("Have you compiled the Kaldi GStreamer plugin?", file=sys.stderr) else: - print >> sys.stderr, "You probably need to set the GST_PLUGIN_PATH envoronment variable" - print >> sys.stderr, "Try running: GST_PLUGIN_PATH=../../../src/gst-plugin %s" % sys.argv[0] + print("You probably need to set the GST_PLUGIN_PATH envoronment variable", file=sys.stderr) + print("Try running: GST_PLUGIN_PATH=../../../src/gst-plugin %s" % sys.argv[0], file=sys.stderr) sys.exit(); # initially silence the decoder @@ -111,10 +112,10 @@ def button_clicked(self, button): if __name__ == '__main__': app = DemoApp() - print ''' + print(''' The (bigram) language model used to build the decoding graph was estimated on an audio book's text. The text in question is King Solomon's Mines" (http://www.gutenberg.org/ebooks/2166). - You may want to read some sentences from this book first ...''' + You may want to read some sentences from this book first ...''') Gtk.main() diff --git a/egs/voxforge/s5/local/make_trans.py b/egs/voxforge/s5/local/make_trans.py index 1b4f5c4136a..612755c8be4 100755 --- a/egs/voxforge/s5/local/make_trans.py +++ b/egs/voxforge/s5/local/make_trans.py @@ -12,11 +12,12 @@ if this is the case produces a transcript line for each file in the format: prefix_a0405 IT SEEMED THE ORDAINED ORDER OF THINGS THAT DOGS SHOULD WORK """ +from __future__ import print_function import sys def err(msg): - print >> sys.stderr, msg + print(msg, file=sys.stderr) if len(sys.argv) < 3: err("Usage: %s ... " % sys.argv[0]) @@ -46,5 +47,5 @@ def err(msg): if not uid in utt2trans: err("No transcript found for %s_%s" % (id_prefix, uid)) continue - print "%s-%s %s" % (id_prefix, uid, utt2trans[uid]) + print("%s-%s %s" % (id_prefix, uid, utt2trans[uid])) diff --git a/egs/vystadial_cz/online_demo/build_reference.py b/egs/vystadial_cz/online_demo/build_reference.py index 1be78391d2f..aea12a2c8bc 100755 --- a/egs/vystadial_cz/online_demo/build_reference.py +++ b/egs/vystadial_cz/online_demo/build_reference.py @@ -1,6 +1,7 @@ #!/usr/bin/env python # encoding: utf-8 from __future__ import unicode_literals +from __future__ import print_function import glob import sys @@ -8,7 +9,7 @@ import codecs def build_reference(wav_scp, ref_path): - print wav_scp, ref_path + print(wav_scp, ref_path) with codecs.open(ref_path, 'w', 'utf-8') as w: with codecs.open(wav_scp, 'r', 'utf-8') as scp: for line in scp: @@ -31,8 +32,8 @@ def build_reference(wav_scp, ref_path): usage_args = {'exec': sys.argv[0]} if len(sys.argv) != 3: - print >> sys.stderr, "Wrong number of arguments" - print >> sys.stderr, usage % {'exec': sys.argv[0]} + print("Wrong number of arguments", file=sys.stderr) + print(usage % {'exec': sys.argv[0]}, file=sys.stderr) sys.exit(1) if sys.argv[1].endswith('scp'): @@ -41,12 +42,12 @@ def build_reference(wav_scp, ref_path): scps = glob.glob(os.path.join(sys.argv[1], '*.scp')) target_dir = sys.argv[2] if not len(scps): - print >> sys.stderr, "No '*.scp' files found" - print >> sys.stderr, usage % {'exec': sys.argv[0]} + print("No '*.scp' files found", file=sys.stderr) + print(usage % {'exec': sys.argv[0]}, file=sys.stderr) sys.exit(1) if not os.path.isdir(target_dir): - print >> sys.stderr, "No '*.scp' files found" - print >> sys.stderr, usage % {'exec': sys.argv[0]} + print("No '*.scp' files found", file=sys.stderr) + print(usage % {'exec': sys.argv[0]}, file=sys.stderr) sys.exit(1) refers = [os.path.join(target_dir, os.path.basename(scp) + '.tra') for scp in scps] diff --git a/egs/vystadial_cz/online_demo/live-demo.py b/egs/vystadial_cz/online_demo/live-demo.py index 6b41c12c739..320a930735f 100755 --- a/egs/vystadial_cz/online_demo/live-demo.py +++ b/egs/vystadial_cz/online_demo/live-demo.py @@ -15,6 +15,7 @@ # See the Apache 2 License for the specific language governing permissions and # limitations under the License. # from __future__ import unicode_literals +from __future__ import print_function import pyaudio from kaldi.decoders import PyOnlineLatgenRecogniser @@ -29,7 +30,7 @@ CHANNELS, RATE, FORMAT = 1, 16000, pyaudio.paInt16 -class LiveDemo: +class LiveDemo(object): def __init__(self, audio_batch_size, wst, dec_args): self.batch_size = audio_batch_size @@ -127,7 +128,7 @@ def save_wav(self): if __name__ == '__main__': audio_batch_size, wst_path = int(sys.argv[1]), sys.argv[2] argv = sys.argv[3:] - print >> sys.stderr, 'Python args: %s' % str(sys.argv) + print('Python args: %s' % str(sys.argv), file=sys.stderr) wst = wst2dict(wst_path) demo = LiveDemo(audio_batch_size, wst, argv) diff --git a/egs/vystadial_cz/online_demo/pykaldi-online-latgen-recogniser.py b/egs/vystadial_cz/online_demo/pykaldi-online-latgen-recogniser.py index 02a0400921c..0008a4c01f1 100755 --- a/egs/vystadial_cz/online_demo/pykaldi-online-latgen-recogniser.py +++ b/egs/vystadial_cz/online_demo/pykaldi-online-latgen-recogniser.py @@ -14,6 +14,8 @@ # See the Apache 2 License for the specific language governing permissions and # limitations under the License. # from __future__ import unicode_literals +from __future__ import division +from __future__ import print_function from kaldi.utils import load_wav, wst2dict, lattice_to_nbest from kaldi.decoders import PyOnlineLatgenRecogniser @@ -31,14 +33,14 @@ def write_decoded(f, wav_name, word_ids, wst): if wst is not None: decoded = [wst[w] for w in best_path] else: - decoded = [unicode(w) for w in best_path] + decoded = [str(w) for w in best_path] line = u' '.join([wav_name] + decoded + ['\n']) if DEBUG: - print '%s best path %s' % (wav_name, decoded.encode('UTF-8')) + print('%s best path %s' % (wav_name, decoded.encode('UTF-8'))) for i, s in enumerate(word_ids): if i > 0: break - print 'best path %d: %s' % (i, str(s)) + print('best path %d: %s' % (i, str(s))) f.write(line.encode('UTF-8')) @@ -55,11 +57,11 @@ def decode(d, pcm): while dec_t > 0: decoded_frames += dec_t dec_t = d.decode(max_frames=10) - print "forward decode: %s secs" % str(time.time() - start) + print("forward decode: %s secs" % str(time.time() - start)) start = time.time() d.prune_final() lik, lat = d.get_lattice() - print "backward decode: %s secs" % str(time.time() - start) + print("backward decode: %s secs" % str(time.time() - start)) d.reset(keep_buffer_data=False) return (lat, lik, decoded_frames) @@ -72,7 +74,7 @@ def decode_wrap(argv, audio_batch_size, wav_paths, for wav_name, wav_path in wav_paths: sw, sr = 2, 16000 # 16-bit audio so 1 sample_width = 2 chars pcm = load_wav(wav_path, def_sample_width=sw, def_sample_rate=sr) - print '%s has %f sec' % (wav_name, (float(len(pcm)) / sw) / sr) + print('%s has %f sec' % (wav_name, (float(len(pcm)) / sw) / sr)) lat, lik, decoded_frames = decode(d, pcm) lat.isyms = lat.osyms = fst.read_symbols_text(wst_path) if DEBUG: @@ -80,8 +82,8 @@ def decode_wrap(argv, audio_batch_size, wav_paths, f.write(lat._repr_svg_()) lat.write('%s_pykaldi.fst' % wav_name) - print "Log-likelihood per frame for utterance %s is %f over %d frames" % ( - wav_name, (lik / decoded_frames), decoded_frames) + print("Log-likelihood per frame for utterance %s is %f over %d frames" % ( + wav_name, int(lik / decoded_frames), decoded_frames)) word_ids = lattice_to_nbest(lat, n=10) write_decoded(file_output, wav_name, word_ids, wst) @@ -90,7 +92,7 @@ def decode_wrap(argv, audio_batch_size, wav_paths, audio_scp, audio_batch_size = sys.argv[1], int(sys.argv[2]) dec_hypo, wst_path = sys.argv[3], sys.argv[4] argv = sys.argv[5:] - print >> sys.stderr, 'Python args: %s' % str(sys.argv) + print('Python args: %s' % str(sys.argv), file=sys.stderr) # open audio_scp, decode and write to dec_hypo file with open(audio_scp, 'rb') as r: diff --git a/egs/vystadial_cz/s5/local/results.py b/egs/vystadial_cz/s5/local/results.py index a7c19af214c..f37109d5fcb 100755 --- a/egs/vystadial_cz/s5/local/results.py +++ b/egs/vystadial_cz/s5/local/results.py @@ -14,6 +14,8 @@ # MERCHANTABLITY OR NON-INFRINGEMENT. # See the Apache 2 License for the specific language governing permissions and # limitations under the License. # +from __future__ import division +from __future__ import print_function import argparse import glob import sys @@ -29,8 +31,8 @@ def extract_stat(wer_file): ser = float(s[2].split()[1]) except Exception as e: - print sys.stderr, 'Error parsing file %s' % wer_file - print sys.stderr, str(e) + print(sys.stderr, 'Error parsing file %s' % wer_file) + print(sys.stderr, str(e)) return wer, ser @@ -47,8 +49,8 @@ def extractResults(path): wer, ser = extract_stat(wf) table.append((exp, dataset, lm, lm_w, wer, ser)) except Exception as e: - print >> sys.stderr, 'failed to parse %s' % wf - print >> sys.stderr, str(e) + print('failed to parse %s' % wf, file=sys.stderr) + print(str(e), file=sys.stderr) return table @@ -105,7 +107,7 @@ def Table2LatexTable(table): def createSmallTable(r): d = [] - for k, v in r.iteritems(): + for k, v in r.items(): w, s, r = v if w == []: minw = None @@ -115,7 +117,7 @@ def createSmallTable(r): mins = None else: mins = min(s) # returns tuple if s is list of tuples - mean_r = sum(r) / float(len(r)) + mean_r = float(sum(r)) / len(r) d.append([k, mean_r, minw, mins]) t = Table(d, ['exp', 'RT coef', 'WER', 'SER']) return t @@ -167,7 +169,7 @@ def createSmallTable(r): # remove duplicates: duplicates if equal mimimum wer in dev set min_dev_un = [(e, lm, lmw) for ((e, lm), lmw) in - dict([((e, lm), lmw) for e, lm, lmw in min_dev]).items()] + list(dict([((e, lm), lmw) for e, lm, lmw in min_dev]).items())] # sort according LM -> sort results according experiment & LMs min_dev_un.sort(key=lambda x: (x[1], x[0])) @@ -182,6 +184,6 @@ def createSmallTable(r): d.append(x[0]) t = Table(data=d, colnames=['exp', 'set', 'LM', 'LMW', 'WER', 'SER']) - print str(t) + print(str(t)) if args.latex: - print Table2LatexTable(t) + print(Table2LatexTable(t)) diff --git a/egs/vystadial_en/s5/local/results.py b/egs/vystadial_en/s5/local/results.py index a7c19af214c..f37109d5fcb 100755 --- a/egs/vystadial_en/s5/local/results.py +++ b/egs/vystadial_en/s5/local/results.py @@ -14,6 +14,8 @@ # MERCHANTABLITY OR NON-INFRINGEMENT. # See the Apache 2 License for the specific language governing permissions and # limitations under the License. # +from __future__ import division +from __future__ import print_function import argparse import glob import sys @@ -29,8 +31,8 @@ def extract_stat(wer_file): ser = float(s[2].split()[1]) except Exception as e: - print sys.stderr, 'Error parsing file %s' % wer_file - print sys.stderr, str(e) + print(sys.stderr, 'Error parsing file %s' % wer_file) + print(sys.stderr, str(e)) return wer, ser @@ -47,8 +49,8 @@ def extractResults(path): wer, ser = extract_stat(wf) table.append((exp, dataset, lm, lm_w, wer, ser)) except Exception as e: - print >> sys.stderr, 'failed to parse %s' % wf - print >> sys.stderr, str(e) + print('failed to parse %s' % wf, file=sys.stderr) + print(str(e), file=sys.stderr) return table @@ -105,7 +107,7 @@ def Table2LatexTable(table): def createSmallTable(r): d = [] - for k, v in r.iteritems(): + for k, v in r.items(): w, s, r = v if w == []: minw = None @@ -115,7 +117,7 @@ def createSmallTable(r): mins = None else: mins = min(s) # returns tuple if s is list of tuples - mean_r = sum(r) / float(len(r)) + mean_r = float(sum(r)) / len(r) d.append([k, mean_r, minw, mins]) t = Table(d, ['exp', 'RT coef', 'WER', 'SER']) return t @@ -167,7 +169,7 @@ def createSmallTable(r): # remove duplicates: duplicates if equal mimimum wer in dev set min_dev_un = [(e, lm, lmw) for ((e, lm), lmw) in - dict([((e, lm), lmw) for e, lm, lmw in min_dev]).items()] + list(dict([((e, lm), lmw) for e, lm, lmw in min_dev]).items())] # sort according LM -> sort results according experiment & LMs min_dev_un.sort(key=lambda x: (x[1], x[0])) @@ -182,6 +184,6 @@ def createSmallTable(r): d.append(x[0]) t = Table(data=d, colnames=['exp', 'set', 'LM', 'LMW', 'WER', 'SER']) - print str(t) + print(str(t)) if args.latex: - print Table2LatexTable(t) + print(Table2LatexTable(t)) diff --git a/egs/wsj/s5/steps/cleanup/combine_short_segments.py b/egs/wsj/s5/steps/cleanup/combine_short_segments.py index 1d14bd2a57f..099b92882a9 100755 --- a/egs/wsj/s5/steps/cleanup/combine_short_segments.py +++ b/egs/wsj/s5/steps/cleanup/combine_short_segments.py @@ -284,7 +284,7 @@ def CombineSegments(input_dir, output_dir, minimum_duration): assert(cur_utt_dur == combined_duration) # now modify the utts list - combined_indices = range(left_index, right_index + 1) + combined_indices = list(range(left_index, right_index + 1)) # start popping from the largest index so that the lower # indexes are valid for i in combined_indices[::-1]: diff --git a/egs/wsj/s5/steps/cleanup/internal/get_pron_stats.py b/egs/wsj/s5/steps/cleanup/internal/get_pron_stats.py index 414875f9013..a33ba85d9fa 100755 --- a/egs/wsj/s5/steps/cleanup/internal/get_pron_stats.py +++ b/egs/wsj/s5/steps/cleanup/internal/get_pron_stats.py @@ -4,6 +4,7 @@ # Apache 2.0. from __future__ import print_function +from __future__ import division import argparse import sys import warnings @@ -211,7 +212,7 @@ def GetStatsFromCtmProns(silphones, optional_silence, non_scored_words, ctm_pron return stats def WriteStats(stats, file_handle): - for word_pron, count in stats.iteritems(): + for word_pron, count in stats.items(): print('{0} {1} {2}'.format(count, word_pron[0], word_pron[1]), file=file_handle) file_handle.close() diff --git a/egs/wsj/s5/steps/cleanup/internal/make_one_biased_lm.py b/egs/wsj/s5/steps/cleanup/internal/make_one_biased_lm.py index f37fa866b0f..e41a67705e9 100755 --- a/egs/wsj/s5/steps/cleanup/internal/make_one_biased_lm.py +++ b/egs/wsj/s5/steps/cleanup/internal/make_one_biased_lm.py @@ -4,6 +4,7 @@ # Apache 2.0. from __future__ import print_function +from __future__ import division import sys import argparse import math @@ -47,7 +48,7 @@ -class NgramCounts: +class NgramCounts(object): ## A note on data-structure. ## Firstly, all words are represented as integers. ## We store n-gram counts as an array, indexed by (history-length == n-gram order minus one) @@ -139,7 +140,7 @@ def GetHistToTotalCount(self): # LM-states that would back off to 'this' lm-state, in the total. def CompletelyDiscountLowCountStates(self, min_count): hist_to_total_count = self.GetHistToTotalCount() - for n in reversed(range(2, self.ngram_order)): + for n in reversed(list(range(2, self.ngram_order))): this_order_counts = self.counts[n] for hist in this_order_counts.keys(): if hist_to_total_count[hist] < min_count: @@ -156,7 +157,7 @@ def CompletelyDiscountLowCountStates(self, min_count): # with interpolation). def ApplyBackoff(self, D): assert D > 0.0 and D < 1.0 - for n in reversed(range(1, self.ngram_order)): + for n in reversed(list(range(1, self.ngram_order))): this_order_counts = self.counts[n] for hist, word_to_count in this_order_counts.items(): backoff_hist = hist[1:] @@ -182,7 +183,7 @@ def Print(self, info_string): for this_order_counts in self.counts: for hist, word_to_count in this_order_counts.items(): this_total_count = sum(word_to_count.values()) - print(str(hist) + ': total={0} '.format(this_total_count), + print('{0}: total={1} '.format(hist, this_total_count), end='', file=sys.stderr) print(' '.join(['{0} -> {1} '.format(word, count) for word, count in word_to_count.items() ]), @@ -242,10 +243,10 @@ def GetHistToStateMap(self): def GetProb(self, hist, word, total_count_map): total_count = total_count_map[hist] word_to_count = self.counts[len(hist)][hist] - prob = word_to_count[word] / total_count + prob = float(word_to_count[word]) / total_count if len(hist) > 0 and word != self.backoff_symbol: prob_in_backoff = self.GetProb(hist[1:], word, total_count_map) - backoff_prob = word_to_count[self.backoff_symbol] / total_count + backoff_prob = float(word_to_count[self.backoff_symbol]) / total_count prob += backoff_prob * prob_in_backoff return prob @@ -262,7 +263,7 @@ def PrintAsFst(self, word_disambig_symbol): hist_to_state = self.GetHistToStateMap() total_count_map = self.GetTotalCountMap() - for n in [ 1, 0 ] + range(2, self.ngram_order): + for n in [ 1, 0 ] + list(range(2, self.ngram_order)): this_order_counts = self.counts[n] # For order 1, make sure the keys are sorted. keys = this_order_counts.keys() if n != 1 else sorted(this_order_counts.keys()) diff --git a/egs/wsj/s5/steps/cleanup/internal/resolve_ctm_edits_overlaps.py b/egs/wsj/s5/steps/cleanup/internal/resolve_ctm_edits_overlaps.py index ad03b557bfe..1dae735304f 100755 --- a/egs/wsj/s5/steps/cleanup/internal/resolve_ctm_edits_overlaps.py +++ b/egs/wsj/s5/steps/cleanup/internal/resolve_ctm_edits_overlaps.py @@ -15,6 +15,7 @@ """ from __future__ import print_function +from __future__ import division import argparse import collections import logging @@ -228,7 +229,7 @@ def resolve_overlaps(ctm_edits, segments): try: cur_utt_end_index = next( (i for i, line in enumerate(ctm_edits_for_cur_utt) - if line[2] + line[3] / 2.0 > window_length - overlap)) + if line[2] + line[3] / 2.0)> window_length - overlap)) except StopIteration: cur_utt_end_index = len(ctm_edits_for_cur_utt) @@ -299,7 +300,7 @@ def run(args): segments, reco2utt = read_segments(args.segments) ctm_edits = read_ctm_edits(args.ctm_edits_in, segments) - for reco, utts in reco2utt.iteritems(): + for reco, utts in reco2utt.items(): ctm_edits_for_reco = [] for utt in sorted(utts, key=lambda x: segments[x][1]): if (reco, utt) in ctm_edits: diff --git a/egs/wsj/s5/steps/cleanup/internal/retrieve_similar_docs.py b/egs/wsj/s5/steps/cleanup/internal/retrieve_similar_docs.py index eb0b18f0408..9594d2ecc60 100755 --- a/egs/wsj/s5/steps/cleanup/internal/retrieve_similar_docs.py +++ b/egs/wsj/s5/steps/cleanup/internal/retrieve_similar_docs.py @@ -223,7 +223,7 @@ def read_map(file_handle, num_values_per_key=None, def get_document_ids(source_docs, indexes): indexes = sorted( - [(key, value[0], value[1]) for key, value in indexes.iteritems()], + [(key, value[0], value[1]) for key, value in indexes.items()], key=lambda x: x[0]) doc_ids = [] @@ -273,7 +273,7 @@ def run(args): "Did not get scores for query {0}".format(query_id)) if args.verbose > 2: - for tup, score in scores.iteritems(): + for tup, score in scores.items(): logger.debug("Score, {num}: {0} {1} {2}".format( tup[0], tup[1], score, num=num_queries)) diff --git a/egs/wsj/s5/steps/cleanup/internal/segment_ctm_edits.py b/egs/wsj/s5/steps/cleanup/internal/segment_ctm_edits.py index 39f6d38d6bf..39d6cb6ed80 100755 --- a/egs/wsj/s5/steps/cleanup/internal/segment_ctm_edits.py +++ b/egs/wsj/s5/steps/cleanup/internal/segment_ctm_edits.py @@ -5,6 +5,7 @@ # Apache 2.0 from __future__ import print_function +from __future__ import division import sys, operator, argparse, os from collections import defaultdict @@ -171,7 +172,7 @@ def ComputeSegmentCores(split_lines_of_utt): return segment_ranges -class Segment: +class Segment(object): def __init__(self, split_lines_of_utt, start_index, end_index, debug_str = None): self.split_lines_of_utt = split_lines_of_utt # start_index is the index of the first line that appears in this @@ -551,7 +552,7 @@ def PossiblyTruncateStartForJunkProportion(self): if candidate_start_index is None: return # Nothing to do as there is no place to split. candidate_removed_piece_duration = candidate_start_time - self.StartTime() - if begin_junk_duration / candidate_removed_piece_duration < args.max_junk_proportion: + if float(begin_junk_duration) / candidate_removed_piece_duration < args.max_junk_proportion: return # Nothing to do as the candidate piece to remove has too # little junk. # OK, remove the piece. @@ -593,7 +594,7 @@ def PossiblyTruncateEndForJunkProportion(self): if candidate_end_index is None: return # Nothing to do as there is no place to split. candidate_removed_piece_duration = self.EndTime() - candidate_end_time - if end_junk_duration / candidate_removed_piece_duration < args.max_junk_proportion: + if float(end_junk_duration) / candidate_removed_piece_duration < args.max_junk_proportion: return # Nothing to do as the candidate piece to remove has too # little junk. # OK, remove the piece. @@ -807,7 +808,7 @@ def TimeToString(time, frame_length): def WriteSegmentsForUtterance(text_output_handle, segments_output_handle, old_utterance_name, segments): - num_digits = len(str(len(segments))) + num_digits = len('{}'.format(len(segments))) for n in range(len(segments)): segment = segments[n] # split utterances will be named foo-bar-1 foo-bar-2, etc. @@ -840,24 +841,24 @@ def PrintDebugInfoForUtterance(ctm_edits_out_handle, info_to_print = [] for n in range(len(segments_for_utterance)): segment = segments_for_utterance[n] - start_string = 'start-segment-' + str(n+1) + '[' + segment.DebugInfo() + ']' + start_string = 'start-segment-{0}[{1}]'.format(n+1, segment.DebugInfo()) info_to_print.append( (segment.StartTime(), start_string) ) - end_string = 'end-segment-' + str(n+1) + end_string = 'end-segment-{}'.format(n+1) info_to_print.append( (segment.EndTime(), end_string) ) # for segments that were deleted we print info like start-deleted-segment-1, and # otherwise similar info to segments that were retained. for n in range(len(deleted_segments_for_utterance)): segment = deleted_segments_for_utterance[n] - start_string = 'start-deleted-segment-' + str(n+1) + '[' + segment.DebugInfo() + ']' + start_string = 'start-deleted-segment-{0}[{1}]'.format(n+1, segment.DebugInfo()) info_to_print.append( (segment.StartTime(), start_string) ) - end_string = 'end-deleted-segment-' + str(n+1) + end_string = 'end-deleted-segment-{}'.format(n+1) info_to_print.append( (segment.EndTime(), end_string) ) info_to_print = sorted(info_to_print) for i in range(len(split_lines_of_cur_utterance)): split_line=split_lines_of_cur_utterance[i] - split_line[0] += '[' + str(i) + ']' # add an index like [0], [1], to + split_line[0] += '[{}]'.format(i) # add an index like [0], [1], to # the utterance-id so we can easily # look up segment indexes. start_time = float(split_line[2]) diff --git a/egs/wsj/s5/steps/cleanup/internal/segment_ctm_edits_mild.py b/egs/wsj/s5/steps/cleanup/internal/segment_ctm_edits_mild.py index 46a9369ae98..9fcc2e89360 100755 --- a/egs/wsj/s5/steps/cleanup/internal/segment_ctm_edits_mild.py +++ b/egs/wsj/s5/steps/cleanup/internal/segment_ctm_edits_mild.py @@ -5,6 +5,7 @@ # Apache 2.0 from __future__ import print_function +from __future__ import division import argparse import copy import logging @@ -869,8 +870,7 @@ def relax_boundary_truncation(self, min_segment_length, # a * (length_with_truncation - length_with_relaxed_boundaries) # -> a = (length_cutoff - length_with_relaxed_boundaries) # / (length_with_truncation - length_with_relaxed_boundaries) - a = ((length_cutoff - length_with_relaxed_boundaries) - / (length_with_truncation - length_with_relaxed_boundaries)) + a = (length_cutoff - length_with_relaxed_boundaries) / (length_with_truncation - length_with_relaxed_boundaries) if a < 0.0 or a > 1.0: # TODO(vimal): Should this be an error? _global_logger.warn("bad 'a' value = %.4f", a) @@ -1756,7 +1756,7 @@ def time_to_string(time, frame_length): """ Gives time in string form as an exact multiple of the frame-length, e.g. 0.01 (after rounding). """ - n = round(time / frame_length) + n = round(time /frame_length) assert n >= 0 # The next function call will remove trailing zeros while printing it, so # that e.g. 0.01 will be printed as 0.01 and not 0.0099999999999999. It diff --git a/egs/wsj/s5/steps/cleanup/internal/taint_ctm_edits.py b/egs/wsj/s5/steps/cleanup/internal/taint_ctm_edits.py index 85e1df997a7..4e0e1ae2283 100755 --- a/egs/wsj/s5/steps/cleanup/internal/taint_ctm_edits.py +++ b/egs/wsj/s5/steps/cleanup/internal/taint_ctm_edits.py @@ -201,7 +201,7 @@ def PrintNonScoredStats(): percent_modified, percent_of_incorrect_modified), file = sys.stderr) - keys = sorted(ref_change_stats.keys(), reverse=True, + keys = sorted(list(ref_change_stats.keys()), reverse=True, key = lambda x: ref_change_stats[x]) num_keys_to_print = 40 if args.verbose >= 2 else 10 @@ -219,7 +219,7 @@ def PrintStats(): return print("taint_ctm_edits.py: processed {0} input lines, whose edit-types were: ".format(tot_lines) + ', '.join([ '%s = %.2f%%' % (k, num_lines_of_type[k] * 100.0 / tot_lines) - for k in sorted(num_lines_of_type.keys(), reverse = True, + for k in sorted(list(num_lines_of_type.keys()), reverse = True, key = lambda k: num_lines_of_type[k]) ]), file = sys.stderr) diff --git a/egs/wsj/s5/steps/cleanup/internal/tf_idf.py b/egs/wsj/s5/steps/cleanup/internal/tf_idf.py index 9b2f4d693a6..a098d9f2a44 100644 --- a/egs/wsj/s5/steps/cleanup/internal/tf_idf.py +++ b/egs/wsj/s5/steps/cleanup/internal/tf_idf.py @@ -6,6 +6,7 @@ """ from __future__ import print_function +from __future__ import division import logging import math import re @@ -51,8 +52,7 @@ def get_inverse_document_frequency(self, term, weighting_scheme="log"): if weighting_scheme == "log-smoothed": return math.log(1.0 + float(self.num_docs) / (1.0 + n_t)) if weighting_scheme == "probabilitic": - return math.log((self.num_docs - n_t - 1) - / (1.0 + n_t)) + return math.log((self.num_docs - n_t - 1) / (1.0 + n_t)) def accumulate(self, term): """Adds one count to the number of docs containing the term "term". @@ -66,7 +66,7 @@ def write(self, file_handle): ... for n-gram (, ... ) """ - for term, num in self.num_docs_for_term.iteritems(): + for term, num in self.num_docs_for_term.items(): if num == 0: continue assert isinstance(term, tuple) @@ -135,7 +135,7 @@ def compute_term_stats(self, idf_stats=None): based on the stored raw counts.""" if len(self.raw_counts) == 0: raise RuntimeError("No (term, doc) found in tf-stats.") - for tup, counts in self.raw_counts.iteritems(): + for tup, counts in self.raw_counts.items(): term = tup[0] if counts > self.max_counts_for_term.get(term, 0): @@ -149,7 +149,7 @@ def __str__(self): ... """ lines = [] - for tup, counts in self.raw_counts.iteritems(): + for tup, counts in self.raw_counts.items(): term, doc = tup lines.append("{order} {term} {doc} {counts}".format( order=len(term), term=" ".join(term), @@ -225,7 +225,7 @@ def compute_similarity_scores(self, source_tfidf, source_docs=None, num_terms_per_doc = {} similarity_scores = {} - for tup, value in self.tf_idf.iteritems(): + for tup, value in self.tf_idf.items(): term, doc = tup num_terms_per_doc[doc] = num_terms_per_doc.get(doc, 0) + 1 @@ -253,19 +253,18 @@ def compute_similarity_scores(self, source_tfidf, source_docs=None, similarity_scores.get((doc, src_doc), 0) + src_value * value) else: - for src_tup, src_value in source_tfidf.tf_idf.iteritems(): + for src_tup, src_value in source_tfidf.tf_idf.items(): similarity_scores[(doc, src_doc)] = ( similarity_scores.get((doc, src_doc), 0) + src_value * value) if do_length_normalization: - for doc_pair, value in similarity_scores.iteritems(): + for doc_pair, value in similarity_scores.items(): doc, src_doc = doc_pair - similarity_scores[(doc, src_doc)] = (value - / num_terms_per_doc[doc]) + similarity_scores[(doc, src_doc)] = value / num_terms_per_doc[doc] if logger.isEnabledFor(logging.DEBUG): - for doc, count in num_terms_per_doc.iteritems(): + for doc, count in num_terms_per_doc.items(): logger.debug( 'Seen {0} terms in query document {1}'.format(count, doc)) @@ -329,7 +328,7 @@ def write(self, tf_idf_file): """Writes TFIDF object to file.""" print ("", file=tf_idf_file) - for tup, value in self.tf_idf.iteritems(): + for tup, value in self.tf_idf.items(): term, doc = tup print("{order} {term} {doc} {tfidf}".format( order=len(term), term=" ".join(term), diff --git a/egs/wsj/s5/steps/conf/append_eval_to_ctm.py b/egs/wsj/s5/steps/conf/append_eval_to_ctm.py index f8e2aad891d..90679d2b341 100755 --- a/egs/wsj/s5/steps/conf/append_eval_to_ctm.py +++ b/egs/wsj/s5/steps/conf/append_eval_to_ctm.py @@ -3,6 +3,7 @@ # Copyright 2015 Brno University of Technology (author: Karel Vesely) # Apache 2.0 +from __future__ import print_function import sys,operator # Append Levenshtein alignment of 'hypothesis' and 'reference' into 'CTM': @@ -15,7 +16,7 @@ # 'U' = unknown (not part of scored segment) if len(sys.argv) != 4: - print 'Usage: %s eval-in ctm-in ctm-eval-out' % __file__ + print('Usage: %s eval-in ctm-in ctm-eval-out' % __file__) sys.exit(1) dummy, eval_in, ctm_in, ctm_eval_out = sys.argv @@ -54,7 +55,7 @@ # Build the 'ctm' with 'eval' column added, ctm_eval = [] -for utt,ctm_part in ctm.iteritems(): +for utt,ctm_part in ctm.items(): ctm_part.sort(key = operator.itemgetter(2)) # Sort by 'beg' time, try: # merging 'tuples' by '+', the record has format: @@ -69,7 +70,7 @@ # append, ctm_eval.extend(merged) except KeyError: - print 'Missing key', utt, 'in the word-evaluation stats from scoring' + print('Missing key', utt, 'in the word-evaluation stats from scoring') # Sort again, ctm_eval.sort(key = operator.itemgetter(0,1,2)) diff --git a/egs/wsj/s5/steps/conf/append_prf_to_ctm.py b/egs/wsj/s5/steps/conf/append_prf_to_ctm.py index 547b6176c9f..42acc5e22b7 100755 --- a/egs/wsj/s5/steps/conf/append_prf_to_ctm.py +++ b/egs/wsj/s5/steps/conf/append_prf_to_ctm.py @@ -3,6 +3,7 @@ # Copyright 2015 Brno University of Technology (author: Karel Vesely) # Apache 2.0 +from __future__ import print_function import sys # Append Levenshtein alignment of 'hypothesis' and 'reference' into 'CTM': @@ -16,7 +17,7 @@ # Parse options, if len(sys.argv) != 4: - print "Usage: %s prf ctm_in ctm_out" % __file__ + print("Usage: %s prf ctm_in ctm_out" % __file__) sys.exit(1) prf_file, ctm_file, ctm_out_file = sys.argv[1:] diff --git a/egs/wsj/s5/steps/conf/convert_ctm_to_tra.py b/egs/wsj/s5/steps/conf/convert_ctm_to_tra.py index 8fec0064fd7..25899e19264 100755 --- a/egs/wsj/s5/steps/conf/convert_ctm_to_tra.py +++ b/egs/wsj/s5/steps/conf/convert_ctm_to_tra.py @@ -3,6 +3,7 @@ # Copyright 2015 Brno University of Technology (author: Karel Vesely) # Apache 2.0 +from __future__ import print_function import sys, operator # This scripts loads a 'ctm' file and converts it into the 'tra' format: @@ -14,7 +15,7 @@ # - confidences if len(sys.argv) != 3: - print 'Usage: %s ctm-in tra-out' % __file__ + print('Usage: %s ctm-in tra-out' % __file__) sys.exit(1) dummy, ctm_in, tra_out = sys.argv @@ -31,7 +32,7 @@ # Store the in 'tra' format, with open(tra_out,'w') as f: - for utt,tuples in tra.iteritems(): + for utt,tuples in tra.items(): tuples.sort(key = operator.itemgetter(0)) # Sort by 'beg' time, f.write('%s %s\n' % (utt,' '.join([t[1] for t in tuples]))) diff --git a/egs/wsj/s5/steps/conf/parse_arpa_unigrams.py b/egs/wsj/s5/steps/conf/parse_arpa_unigrams.py index 1be32d4c4d7..f0a2fe13497 100755 --- a/egs/wsj/s5/steps/conf/parse_arpa_unigrams.py +++ b/egs/wsj/s5/steps/conf/parse_arpa_unigrams.py @@ -3,11 +3,12 @@ # Copyright 2015 Brno University of Technology (author: Karel Vesely) # Apache 2.0 +from __future__ import print_function import sys, gzip, re # Parse options, if len(sys.argv) != 4: - print "Usage: %s " % __file__ + print("Usage: %s " % __file__) sys.exit(0) words_txt, arpa_gz, unigrams_out = sys.argv[1:] @@ -31,7 +32,7 @@ # Create list, 'wrd id log_p_unigram', words_unigram = [[wrd, id, (wrd_log10[wrd] if wrd in wrd_log10 else -99)] for wrd,id in words ] -print >>sys.stderr, words_unigram[0] +print(words_unigram[0], file=sys.stderr) # Store, with open(unigrams_out,'w') as f: f.writelines(['%s %s %g\n' % (w,i,p) for (w,i,p) in words_unigram]) diff --git a/egs/wsj/s5/steps/conf/prepare_calibration_data.py b/egs/wsj/s5/steps/conf/prepare_calibration_data.py index bc8f92a2f7f..c4da720ba71 100755 --- a/egs/wsj/s5/steps/conf/prepare_calibration_data.py +++ b/egs/wsj/s5/steps/conf/prepare_calibration_data.py @@ -3,6 +3,7 @@ # Copyright 2015 Brno University of Technology (author: Karel Vesely) # Apache 2.0 +from __future__ import division import sys, math from optparse import OptionParser @@ -82,7 +83,7 @@ depths = dict() for l in open(o.lattice_depth): utt,d = l.split(' ',1) - depths[utt] = map(int,d.split()) + depths[utt] = [int(i) for i in d.split()] # Load the 'word_categories' mapping for categorical input features derived from 'lang/words.txt', wrd_to_cat = [ l.split() for l in open(word_categories_file) ] diff --git a/egs/wsj/s5/steps/data/augment_data_dir.py b/egs/wsj/s5/steps/data/augment_data_dir.py index 432b136e3b1..7edcdda2636 100755 --- a/egs/wsj/s5/steps/data/augment_data_dir.py +++ b/egs/wsj/s5/steps/data/augment_data_dir.py @@ -103,8 +103,8 @@ def AugmentWav(utt, wav, dur, fg_snr_opts, bg_snr_opts, fg_noise_utts, \ tot_noise_dur += noise_dur + interval noises.append(noise) - start_times_str = "--start-times='" + ",".join(list(map(str,start_times))) + "'" - snrs_str = "--snrs='" + ",".join(list(map(str,snrs))) + "'" + start_times_str = "--start-times='" + ",".join([str(i) for i in start_times]) + "'" + snrs_str = "--snrs='" + ",".join([str(i) for i in snrs]) + "'" noises_str = "--additive-signals='" + ",".join(noises).strip() + "'" # If the wav is just a file @@ -130,11 +130,11 @@ def CopyFileIfExists(utt_suffix, filename, input_dir, output_dir): def main(): args = GetArgs() - fg_snrs = list(map(int, args.fg_snr_str.split(":"))) - bg_snrs = list(map(int, args.bg_snr_str.split(":"))) + fg_snrs = [int(i) for i in args.fg_snr_str.split(":")] + bg_snrs = [int(i) for i in args.bg_snr_str.split(":")] input_dir = args.input_dir output_dir = args.output_dir - num_bg_noises = list(map(int, args.num_bg_noises.split(":"))) + num_bg_noises = [int(i) for i in args.num_bg_noises.split(":")] reco2dur = ParseFileToDict(input_dir + "/reco2dur", value_processor = lambda x: float(x[0])) wav_scp_file = open(input_dir + "/wav.scp", 'r').readlines() diff --git a/egs/wsj/s5/steps/data/reverberate_data_dir.py b/egs/wsj/s5/steps/data/reverberate_data_dir.py index 570613855a0..189f4619ddb 100755 --- a/egs/wsj/s5/steps/data/reverberate_data_dir.py +++ b/egs/wsj/s5/steps/data/reverberate_data_dir.py @@ -5,7 +5,6 @@ # script to generate reverberated data # we're using python 3.x style print but want it to work in python 2.x, -from __future__ import print_function import argparse, shlex, glob, math, os, random, sys, warnings, copy, imp, ast data_lib = imp.load_source('dml', 'steps/data/data_dir_manipulation_lib.py') @@ -121,17 +120,18 @@ def CheckArgs(args): return args -class list_cyclic_iterator: +class list_cyclic_iterator(object): def __init__(self, list): self.list_index = 0 self.list = list random.shuffle(self.list) - def next(self): + def __next__(self): item = self.list[self.list_index] self.list_index = (self.list_index + 1) % len(self.list) return item + next = __next__ # for Python 2 # This functions picks an item from the collection according to the associated probability distribution. # The probability estimate of each item in the collection is stored in the "probability" field of @@ -218,11 +218,11 @@ def AddPointSourceNoise(noise_addition_descriptor, # descriptor to store the in if noise.bg_fg_type == "background": noise_rvb_command = """wav-reverberate --impulse-response="{0}" --duration={1}""".format(noise_rir.rir_rspecifier, speech_dur) noise_addition_descriptor['start_times'].append(0) - noise_addition_descriptor['snrs'].append(background_snrs.next()) + noise_addition_descriptor['snrs'].append(next(background_snrs)) else: noise_rvb_command = """wav-reverberate --impulse-response="{0}" """.format(noise_rir.rir_rspecifier) noise_addition_descriptor['start_times'].append(round(random.random() * speech_dur, 2)) - noise_addition_descriptor['snrs'].append(foreground_snrs.next()) + noise_addition_descriptor['snrs'].append(next(foreground_snrs)) # check if the rspecifier is a pipe or not if len(noise.noise_rspecifier.split()) == 1: @@ -273,7 +273,7 @@ def GenerateReverberationOpts(room_dict, # the room dictionary, please refer to else: noise_addition_descriptor['noise_io'].append("{0} wav-reverberate --duration={1} - - |".format(isotropic_noise.noise_rspecifier, speech_dur)) noise_addition_descriptor['start_times'].append(0) - noise_addition_descriptor['snrs'].append(background_snrs.next()) + noise_addition_descriptor['snrs'].append(next(background_snrs)) noise_addition_descriptor = AddPointSourceNoise(noise_addition_descriptor, # descriptor to store the information of the noise added room, # the room selected diff --git a/egs/wsj/s5/steps/diagnostic/analyze_lattice_depth_stats.py b/egs/wsj/s5/steps/diagnostic/analyze_lattice_depth_stats.py index 56b9f69b3c9..6ed2bf78115 100755 --- a/egs/wsj/s5/steps/diagnostic/analyze_lattice_depth_stats.py +++ b/egs/wsj/s5/steps/diagnostic/analyze_lattice_depth_stats.py @@ -5,6 +5,7 @@ # Apache 2.0. from __future__ import print_function +from __future__ import division import argparse import sys, os from collections import defaultdict diff --git a/egs/wsj/s5/steps/dict/get_pron_stats.py b/egs/wsj/s5/steps/dict/get_pron_stats.py index f6ce8e49807..e8106bdd1ac 100755 --- a/egs/wsj/s5/steps/dict/get_pron_stats.py +++ b/egs/wsj/s5/steps/dict/get_pron_stats.py @@ -76,14 +76,14 @@ def GetStatsFromArcInfo(arc_info_file_handle, phone_map_handle): prons[word].add(phones) stats_unmapped[(word, phones)] = stats_unmapped.get((word, phones), 0) + count - for word_pron, count in stats_unmapped.iteritems(): + for word_pron, count in stats_unmapped.items(): phones_unmapped = word_pron[1].split() phones = [phone_map[phone] for phone in phones_unmapped] stats[(word_pron[0], " ".join(phones))] = count return stats def WriteStats(stats, file_handle): - for word_pron, count in stats.iteritems(): + for word_pron, count in stats.items(): print('{2} {0} {1}'.format(word_pron[0], word_pron[1], count), file=file_handle) file_handle.close() diff --git a/egs/wsj/s5/steps/dict/prune_pron_candidates.py b/egs/wsj/s5/steps/dict/prune_pron_candidates.py index e32478cecea..cd90a389a7c 100755 --- a/egs/wsj/s5/steps/dict/prune_pron_candidates.py +++ b/egs/wsj/s5/steps/dict/prune_pron_candidates.py @@ -4,6 +4,7 @@ # Apache 2.0. from __future__ import print_function +from __future__ import division from collections import defaultdict import argparse import sys @@ -61,7 +62,7 @@ def ReadStats(pron_stats_handle): phones = ' '.join(splits[2:]) stats[word].append((phones, count)) - for word, entry in stats.iteritems(): + for word, entry in stats.items(): entry.sort(key=lambda x: x[1]) return stats @@ -86,12 +87,12 @@ def PruneProns(args, stats, ref_lexicon): # Compute the average # pron variants counts per word in the reference lexicon. num_words_ref = 0 num_prons_ref = 0 - for word, prons in ref_lexicon.iteritems(): + for word, prons in ref_lexicon.items(): num_words_ref += 1 num_prons_ref += len(prons) avg_variants_counts_ref = math.ceil(float(num_prons_ref) / float(num_words_ref)) - for word, entry in stats.iteritems(): + for word, entry in stats.items(): if word in ref_lexicon: variants_counts = args.r * len(ref_lexicon[word]) else: @@ -105,7 +106,7 @@ def PruneProns(args, stats, ref_lexicon): except IndexError: break - for word, entry in stats.iteritems(): + for word, entry in stats.items(): for pron, prob in entry: if word not in ref_lexicon or pron not in ref_lexicon[word]: print('{0} {1}'.format(word, pron), file=args.pruned_prons_handle) diff --git a/egs/wsj/s5/steps/dict/select_prons_bayesian.py b/egs/wsj/s5/steps/dict/select_prons_bayesian.py index 4ccca302ebf..893dd7cb818 100755 --- a/egs/wsj/s5/steps/dict/select_prons_bayesian.py +++ b/egs/wsj/s5/steps/dict/select_prons_bayesian.py @@ -4,6 +4,7 @@ # Apache 2.0. from __future__ import print_function +from __future__ import division from collections import defaultdict import argparse import sys @@ -162,7 +163,7 @@ def FilterPhoneticDecodingLexicon(args, phonetic_decoding_lexicon, stats): for line in args.silence_file_handle: silphones.add(line.strip()) rejected_candidates = set() - for word, prons in phonetic_decoding_lexicon.iteritems(): + for word, prons in phonetic_decoding_lexicon.items(): for pron in prons: for phone in pron.split(): if phone in silphones: @@ -194,7 +195,7 @@ def ComputePriorCounts(args, counts, ref_lexicon, g2p_lexicon, phonetic_decoding prior_mean[2] = 0 prior_mean_sum = sum(prior_mean) try: - prior_mean = [t / prior_mean_sum for t in prior_mean] + prior_mean = [float(t) / prior_mean_sum for t in prior_mean] except ZeroDivisionError: print('WARNING: word {} appears in train_counts but not in any lexicon.'.format(word), file=sys.stderr) prior_counts[word] = [t * args.prior_counts_tot for t in prior_mean] @@ -206,20 +207,20 @@ def ComputePosteriors(args, stats, ref_lexicon, g2p_lexicon, phonetic_decoding_l # The soft-counts were augmented by a user-specified prior count, according the source # (ref/G2P/phonetic-decoding) of this pronunciation. - for word, prons in ref_lexicon.iteritems(): + for word, prons in ref_lexicon.items(): for pron in prons: # c is the augmented soft count (observed count + prior count) - c = prior_counts[word][0] / len(ref_lexicon[word]) + stats.get((word, pron), 0) + c = float(prior_counts[word][0]) / len(ref_lexicon[word]) + stats.get((word, pron), 0) posteriors[word].append((pron, c)) - for word, prons in g2p_lexicon.iteritems(): + for word, prons in g2p_lexicon.items(): for pron in prons: - c = prior_counts[word][1] / len(g2p_lexicon[word]) + stats.get((word, pron), 0) + c = float(prior_counts[word][1]) / len(g2p_lexicon[word]) + stats.get((word, pron), 0) posteriors[word].append((pron, c)) - for word, prons in phonetic_decoding_lexicon.iteritems(): + for word, prons in phonetic_decoding_lexicon.items(): for pron in prons: - c = prior_counts[word][2] / len(phonetic_decoding_lexicon[word]) + stats.get((word, pron), 0) + c = float(prior_counts[word][2]) / len(phonetic_decoding_lexicon[word]) + stats.get((word, pron), 0) posteriors[word].append((pron, c)) num_prons_from_ref = sum(len(ref_lexicon[i]) for i in ref_lexicon) @@ -239,10 +240,10 @@ def ComputePosteriors(args, stats, ref_lexicon, g2p_lexicon, phonetic_decoding_l # each entry is a pair: (prounciation, count) count_sum[word] = sum([entry[1] for entry in posteriors[word]]) - for word, entry in posteriors.iteritems(): + for word, entry in posteriors.items(): new_entry = [] for pron, count in entry: - post = count / count_sum[word] + post = float(count) / count_sum[word] new_entry.append((pron, post)) source = 'R' if word in g2p_lexicon and pron in g2p_lexicon[word]: @@ -260,7 +261,7 @@ def SelectPronsBayesian(args, counts, posteriors, ref_lexicon, g2p_lexicon, phon phonetic_decoding_selected = 0 learned_lexicon = defaultdict(set) - for word, entry in posteriors.iteritems(): + for word, entry in posteriors.items(): num_variants = 0 post_tot = 0.0 variants_counts = args.variants_counts @@ -411,7 +412,7 @@ def WriteEditsAndSummary(args, learned_lexicon, ref_lexicon, phonetic_decoding_l print(' {} words\' selected prons came from G2P only.'.format(num_infreq_oovs_from_g2p), file=sys.stderr) def WriteLearnedLexiconOov(learned_lexicon, ref_lexicon, file_handle): - for word, prons in learned_lexicon.iteritems(): + for word, prons in learned_lexicon.items(): if word not in ref_lexicon: for pron in prons: print('{0} {1}'.format(word, pron), file=file_handle) diff --git a/egs/wsj/s5/steps/libs/common.py b/egs/wsj/s5/steps/libs/common.py index 503721c23d1..17b8ad89064 100644 --- a/egs/wsj/s5/steps/libs/common.py +++ b/egs/wsj/s5/steps/libs/common.py @@ -10,6 +10,7 @@ """ from __future__ import print_function +from __future__ import division import argparse import logging import math @@ -19,7 +20,7 @@ import threading try: - import thread as thread_module + import _thread as thread_module except: import _thread as thread_module @@ -316,7 +317,7 @@ def read_kaldi_matrix(matrix_file): 'matrix_file' and stores it as a list of rows, where each row is a list. """ try: - lines = map(lambda x: x.split(), open(matrix_file).readlines()) + lines = [x.split() for x in open(matrix_file).readlines()] first_field = lines[0][0] last_field = lines[-1][-1] lines[0] = lines[0][1:] @@ -326,7 +327,7 @@ def read_kaldi_matrix(matrix_file): "Kaldi matrix file has incorrect format, " "only text format matrix files can be read by this script") for i in range(len(lines)): - lines[i] = map(lambda x: int(float(x)), lines[i]) + lines[i] = [int(float(x)) for x in lines[i]] return lines except IOError: raise Exception("Error while reading the kaldi matrix file " @@ -348,7 +349,7 @@ def write_kaldi_matrix(output_file, matrix): if num_cols != len(matrix[row_index]): raise Exception("All the rows of a matrix are expected to " "have the same length") - f.write(" ".join(map(lambda x: str(x), matrix[row_index]))) + f.write(" ".join([str(x) for x in matrix[row_index]])) if row_index != num_rows - 1: f.write("\n") f.write(" ]") @@ -508,7 +509,7 @@ def compute_idct_matrix(K, N, cepstral_lifter=0): lifter_coeffs = compute_lifter_coeffs(cepstral_lifter, K) for k in range(0, K): for n in range(0, N): - matrix[n][k] = matrix[n][k] / lifter_coeffs[k] + matrix[n][k] = float(matrix[n][k]) / lifter_coeffs[k] return matrix diff --git a/egs/wsj/s5/steps/libs/nnet3/report/log_parse.py b/egs/wsj/s5/steps/libs/nnet3/report/log_parse.py index 1afc26ff163..97da5e04962 100755 --- a/egs/wsj/s5/steps/libs/nnet3/report/log_parse.py +++ b/egs/wsj/s5/steps/libs/nnet3/report/log_parse.py @@ -322,7 +322,7 @@ def parse_progress_logs_for_param_diff(exp_dir, pattern): groups = mat_obj.groups() iteration = groups[0] differences = parse_difference_string(groups[1]) - component_names = component_names.union(differences.keys()) + component_names = component_names.union(list(differences.keys())) progress_per_iter[int(iteration)] = differences component_names = list(component_names) @@ -435,14 +435,14 @@ def parse_prob_logs(exp_dir, key='accuracy', output="output"): raise KaldiLogParseException("Could not find any lines with {k} in " " {l}".format(k=key, l=valid_prob_files)) - iters = list(set(valid_objf.keys()).intersection(train_objf.keys())) + iters = list(set(valid_objf.keys()).intersection(list(train_objf.keys()))) if not iters: raise KaldiLogParseException("Could not any common iterations with" " key {k} in both {tl} and {vl}".format( k=key, tl=train_prob_files, vl=valid_prob_files)) iters.sort() - return list(map(lambda x: (int(x), float(train_objf[x]), - float(valid_objf[x])), iters)) + return list([(int(x), float(train_objf[x]), + float(valid_objf[x])) for x in iters]) def parse_rnnlm_prob_logs(exp_dir, key='objf'): train_prob_files = "%s/log/train.*.*.log" % (exp_dir) @@ -498,14 +498,14 @@ def parse_rnnlm_prob_logs(exp_dir, key='objf'): raise KaldiLogParseException("Could not find any lines with {k} in " " {l}".format(k=key, l=valid_prob_files)) - iters = list(set(valid_objf.keys()).intersection(train_objf.keys())) + iters = list(set(valid_objf.keys()).intersection(list(train_objf.keys()))) if not iters: raise KaldiLogParseException("Could not any common iterations with" " key {k} in both {tl} and {vl}".format( k=key, tl=train_prob_files, vl=valid_prob_files)) iters.sort() - return map(lambda x: (int(x), float(train_objf[x]), - float(valid_objf[x])), iters) + return [(int(x), float(train_objf[x]), + float(valid_objf[x])) for x in iters] diff --git a/egs/wsj/s5/steps/libs/nnet3/train/chain_objf/acoustic_model.py b/egs/wsj/s5/steps/libs/nnet3/train/chain_objf/acoustic_model.py index 6afb43824fd..c932a9c54f7 100644 --- a/egs/wsj/s5/steps/libs/nnet3/train/chain_objf/acoustic_model.py +++ b/egs/wsj/s5/steps/libs/nnet3/train/chain_objf/acoustic_model.py @@ -7,6 +7,8 @@ """ This is a module with methods which will be used by scripts for training of deep neural network acoustic model with chain objective. """ +from __future__ import division +from __future__ import print_function import logging import math @@ -413,8 +415,7 @@ def compute_preconditioning_matrix(dir, egs_dir, num_lda_jobs, run_opts, rand_prune=rand_prune)) # the above command would have generated dir/{1..num_lda_jobs}.lda_stats - lda_stat_files = list(map(lambda x: '{0}/{1}.lda_stats'.format(dir, x), - range(1, num_lda_jobs + 1))) + lda_stat_files = ['{0}/{1}.lda_stats'.format(dir, x) for x in range(1, num_lda_jobs + 1)] common_lib.execute_command( """{command} {dir}/log/sum_transform_stats.log \ diff --git a/egs/wsj/s5/steps/libs/nnet3/train/common.py b/egs/wsj/s5/steps/libs/nnet3/train/common.py index a2892a090f3..1a038cc23f2 100644 --- a/egs/wsj/s5/steps/libs/nnet3/train/common.py +++ b/egs/wsj/s5/steps/libs/nnet3/train/common.py @@ -7,6 +7,7 @@ """This module contains classes and methods common to training of nnet3 neural networks. """ +from __future__ import division import argparse import glob @@ -528,13 +529,13 @@ def smooth_presoftmax_prior_scale_vector(pdf_counts, presoftmax_prior_scale_power=-0.25, smooth=0.01): total = sum(pdf_counts) - average_count = total/len(pdf_counts) + average_count = float(total) / len(pdf_counts) scales = [] for i in range(len(pdf_counts)): scales.append(math.pow(pdf_counts[i] + smooth * average_count, presoftmax_prior_scale_power)) num_pdfs = len(pdf_counts) - scaled_counts = list(map(lambda x: x * float(num_pdfs) / sum(scales), scales)) + scaled_counts = [x * float(num_pdfs) / sum(scales) for x in scales] return scaled_counts @@ -564,7 +565,7 @@ def get_model_combine_iters(num_iters, num_epochs, in the final model-averaging phase. (note: it's a weighted average where the weights are worked out from a subset of training data.)""" - approx_iters_per_epoch_final = num_archives/num_jobs_final + approx_iters_per_epoch_final = float(num_archives) / num_jobs_final # Note: it used to be that we would combine over an entire epoch, # but in practice we very rarely would use any weights from towards # the end of that range, so we are changing it to use not @@ -581,8 +582,8 @@ def get_model_combine_iters(num_iters, num_epochs, # But if this value is > max_models_combine, then the models # are subsampled to get these many models to combine. - num_iters_combine_initial = min(approx_iters_per_epoch_final/2 + 1, - num_iters/2) + num_iters_combine_initial = min(int(approx_iters_per_epoch_final/2) + 1, + int(num_iters/2)) if num_iters_combine_initial > max_models_combine: subsample_model_factor = int( @@ -610,8 +611,7 @@ def get_learning_rate(iter, num_jobs, num_iters, num_archives_processed, effective_learning_rate = ( initial_effective_lrate * math.exp(num_archives_processed - * math.log(final_effective_lrate - / initial_effective_lrate) + * math.log(float(final_effective_lrate) / initial_effective_lrate) / num_archives_to_process)) return num_jobs * effective_learning_rate diff --git a/egs/wsj/s5/steps/libs/nnet3/train/frame_level_objf/common.py b/egs/wsj/s5/steps/libs/nnet3/train/frame_level_objf/common.py index cc5c9693a12..f2722350e41 100644 --- a/egs/wsj/s5/steps/libs/nnet3/train/frame_level_objf/common.py +++ b/egs/wsj/s5/steps/libs/nnet3/train/frame_level_objf/common.py @@ -348,8 +348,7 @@ def compute_preconditioning_matrix(dir, egs_dir, num_lda_jobs, run_opts, rand_prune=rand_prune)) # the above command would have generated dir/{1..num_lda_jobs}.lda_stats - lda_stat_files = list(map(lambda x: '{0}/{1}.lda_stats'.format(dir, x), - range(1, num_lda_jobs + 1))) + lda_stat_files = ['{0}/{1}.lda_stats'.format(dir, x) for x in range(1, num_lda_jobs + 1)] common_lib.execute_command( """{command} {dir}/log/sum_transform_stats.log \ diff --git a/egs/wsj/s5/steps/libs/nnet3/xconfig/attention.py b/egs/wsj/s5/steps/libs/nnet3/xconfig/attention.py index e870c1a60cf..db4cb392f10 100644 --- a/egs/wsj/s5/steps/libs/nnet3/xconfig/attention.py +++ b/egs/wsj/s5/steps/libs/nnet3/xconfig/attention.py @@ -6,6 +6,7 @@ """ from __future__ import print_function +from __future__ import division import math import re import sys diff --git a/egs/wsj/s5/steps/libs/nnet3/xconfig/basic_layers.py b/egs/wsj/s5/steps/libs/nnet3/xconfig/basic_layers.py index 9a856bc6fe1..7846c983b19 100644 --- a/egs/wsj/s5/steps/libs/nnet3/xconfig/basic_layers.py +++ b/egs/wsj/s5/steps/libs/nnet3/xconfig/basic_layers.py @@ -9,6 +9,7 @@ """ from __future__ import print_function +from __future__ import division import math import re import sys diff --git a/egs/wsj/s5/steps/libs/nnet3/xconfig/convolution.py b/egs/wsj/s5/steps/libs/nnet3/xconfig/convolution.py index be8bcaefedf..5597ff0e216 100644 --- a/egs/wsj/s5/steps/libs/nnet3/xconfig/convolution.py +++ b/egs/wsj/s5/steps/libs/nnet3/xconfig/convolution.py @@ -7,6 +7,7 @@ """ This module has the implementation of convolutional layers. """ from __future__ import print_function +from __future__ import division import math import re import sys @@ -880,7 +881,7 @@ def _generate_normal_resblock_config(self): num_filters_out = self.config['num-filters'] if height_out != height_in: - if height_out < height_in / 2 - 1 or height_out > height_in / 2 + 1: + if height_out < height_in / 2 - 1 or height_out > height_in / 2 + 1: raise RuntimeError("Expected height-out to be about half height-in, or the same: " "height-in={0} height-out={1}".format(height_in, height_out)) if not time_period_out % 2 == 0: @@ -1030,7 +1031,7 @@ def _generate_bottleneck_resblock_config(self): num_filters_out = self.config['num-filters'] if height_out != height_in: - if height_out < height_in / 2 - 1 or height_out > height_in / 2 + 1: + if height_out < height_in / 2 - 1 or height_out > height_in / 2 + 1: raise RuntimeError("Expected height-out to be about half height-in, or the same: " "height-in={0} height-out={1}".format(height_in, height_out)) height_subsample = 2 diff --git a/egs/wsj/s5/steps/libs/nnet3/xconfig/utils.py b/egs/wsj/s5/steps/libs/nnet3/xconfig/utils.py index 08de18167cd..0188248d694 100644 --- a/egs/wsj/s5/steps/libs/nnet3/xconfig/utils.py +++ b/egs/wsj/s5/steps/libs/nnet3/xconfig/utils.py @@ -184,7 +184,7 @@ def convert_value_to_type(key, dest_type, string_value): # Also, in any place a raw input/layer/output name can appear, we accept things # like [-1] meaning the previous input/layer/output's name, or [-2] meaning the # last-but-one input/layer/output, and so on. -class Descriptor: +class Descriptor(object): def __init__(self, descriptor_string = None, prev_names = None): @@ -595,7 +595,7 @@ def parse_config_line(orig_config_line): rest_of_line = ' '.join(fields) # rest of the line can be of the form 'a=1 b=" x=1 y=2 " c=Append( i1, i2)' - positions = list(map(lambda x: x.start(), re.finditer('"', rest_of_line))) + positions = [x.start() for x in re.finditer('"', rest_of_line)] if not len(positions) % 2 == 0: raise RuntimeError("Double-quotes should occur in pairs") diff --git a/egs/wsj/s5/steps/nnet2/make_multisplice_configs.py b/egs/wsj/s5/steps/nnet2/make_multisplice_configs.py index 6e7bff3fa17..b5338b516e8 100755 --- a/egs/wsj/s5/steps/nnet2/make_multisplice_configs.py +++ b/egs/wsj/s5/steps/nnet2/make_multisplice_configs.py @@ -4,14 +4,16 @@ # Creates the nnet.config and hidde_*.config scripts used in train_pnorm_multisplice.sh # Parses the splice string to generate relevant variables for get_egs.sh, get_lda.sh and nnet/hidden.config files +from __future__ import division +from __future__ import print_function import re, argparse, sys, math, warnings # returns the set of frame indices required to perform the convolution # between sequences with frame indices in x and y def get_convolution_index_set(x, y): z = [] - for i in xrange(len(x)): - for j in xrange(len(y)): + for i in range(len(x)): + for j in range(len(y)): z.append(x[i]+y[j]) z = list(set(z)) z.sort() @@ -19,7 +21,7 @@ def get_convolution_index_set(x, y): def parse_splice_string(splice_string): layerwise_splice_indexes = splice_string.split('layer')[1:] - print splice_string.split('layer') + print(splice_string.split('layer')) contexts={} first_right_context = 0 # default value first_left_context = 0 # default value @@ -29,14 +31,14 @@ def parse_splice_string(splice_string): try: for cur_splice_indexes in layerwise_splice_indexes: layer_index, frame_indexes = cur_splice_indexes.split("/") - frame_indexes = map(lambda x: int(x), frame_indexes.split(':')) + frame_indexes = [int(x) for x in frame_indexes.split(':')] layer_index = int(layer_index) assert(layer_index >= 0) if layer_index == 0: first_left_context = min(frame_indexes) first_right_context = max(frame_indexes) try: - assert(frame_indexes == range(first_left_context, first_right_context+1)) + assert(frame_indexes == list(range(first_left_context, first_right_context+1))) except AssertionError: raise Exception('Currently the first splice component just accepts contiguous context.') try: @@ -46,11 +48,11 @@ def parse_splice_string(splice_string): left context provided is %d and right context provided is %d.""" % (first_left_context, first_right_context)) # convolve the current splice indices with the splice indices until last layer nnet_frame_indexes = get_convolution_index_set(frame_indexes, nnet_frame_indexes) - cur_context = ":".join(map(lambda x: str(x), frame_indexes)) + cur_context = ":".join([str(x) for x in frame_indexes]) contexts[layer_index] = cur_context except ValueError: raise Exception('Unknown format in splice_indexes variable: {0}'.format(params.splice_indexes)) - print nnet_frame_indexes + print(nnet_frame_indexes) max_left_context = min(nnet_frame_indexes) max_right_context = max(nnet_frame_indexes) return [contexts, ' nnet_left_context={0};\n nnet_right_context={1}\n first_left_context={2};\n first_right_context={3}\n'.format(abs(max_left_context), abs(max_right_context), abs(first_left_context), abs(first_right_context) )] @@ -87,7 +89,7 @@ def create_config_files(output_dir, params): except KeyError: raise Exception('A splice layer is expected to be the first layer. Provide a context for the first layer.') - for i in xrange(1, params.num_hidden_layers): #just run till num_hidden_layers-1 since we do not add splice before the final affine transform + for i in range(1, params.num_hidden_layers): #just run till num_hidden_layers-1 since we do not add splice before the final affine transform lines=[] context_len = 1 if i in contexts: @@ -109,7 +111,7 @@ def create_config_files(output_dir, params): if __name__ == "__main__": - print " ".join(sys.argv) + print(" ".join(sys.argv)) parser = argparse.ArgumentParser() parser.add_argument('--splice-indexes', type=str, help='string specifying the indexes for the splice layers throughout the network') parser.add_argument('--total-input-dim', type=int, help='dimension of the input to the network') @@ -127,7 +129,7 @@ def create_config_files(output_dir, params): parser.add_argument("output_dir", type=str, help="output directory to store the files") params = parser.parse_args() - print params + print(params) if params.mode == "contexts": [context, context_variables] = parse_splice_string(params.splice_indexes) var_file = open("{0}/vars".format(params.output_dir), "w") diff --git a/egs/wsj/s5/steps/nnet3/chain/e2e/text_to_phones.py b/egs/wsj/s5/steps/nnet3/chain/e2e/text_to_phones.py index 0ff05e3c48e..2c51cb57750 100755 --- a/egs/wsj/s5/steps/nnet3/chain/e2e/text_to_phones.py +++ b/egs/wsj/s5/steps/nnet3/chain/e2e/text_to_phones.py @@ -8,6 +8,7 @@ to phone transcriptions using the provided lexicon, and writes them to standard output. """ +from __future__ import print_function import argparse from os.path import join diff --git a/egs/wsj/s5/steps/nnet3/chain/train.py b/egs/wsj/s5/steps/nnet3/chain/train.py index a832f57cd8f..40b65afe273 100755 --- a/egs/wsj/s5/steps/nnet3/chain/train.py +++ b/egs/wsj/s5/steps/nnet3/chain/train.py @@ -6,6 +6,8 @@ """ This script is based on steps/nnet3/chain/train.sh """ +from __future__ import division +from __future__ import print_function import argparse import logging diff --git a/egs/wsj/s5/steps/nnet3/components.py b/egs/wsj/s5/steps/nnet3/components.py index 34443d586ca..8e879579776 100644 --- a/egs/wsj/s5/steps/nnet3/components.py +++ b/egs/wsj/s5/steps/nnet3/components.py @@ -84,7 +84,7 @@ def AddBlockAffineLayer(config_lines, name, input, output_dim, num_blocks): def AddPermuteLayer(config_lines, name, input, column_map): components = config_lines['components'] component_nodes = config_lines['component-nodes'] - permute_indexes = ",".join(map(lambda x: str(x), column_map)) + permute_indexes = ",".join([str(x) for x in column_map]) components.append('component name={0}_permute type=PermuteComponent column-map={1}'.format(name, permute_indexes)) component_nodes.append('component-node name={0}_permute component={0}_permute input={1}'.format(name, input['descriptor'])) diff --git a/egs/wsj/s5/steps/nnet3/convert_nnet2_to_nnet3.py b/egs/wsj/s5/steps/nnet3/convert_nnet2_to_nnet3.py index f0a4341d12b..66ff633fbfc 100755 --- a/egs/wsj/s5/steps/nnet3/convert_nnet2_to_nnet3.py +++ b/egs/wsj/s5/steps/nnet3/convert_nnet2_to_nnet3.py @@ -6,6 +6,7 @@ # It requires knowledge of valid components which # can be modified in the configuration section below. +from __future__ import print_function import argparse, os, tempfile, logging, sys, shutil, fileinput, re from collections import defaultdict, namedtuple import numpy as np @@ -51,7 +52,7 @@ SPLICE_COMPONENTS = [c for c in NODE_NAMES if "Splice" in c] AFFINE_COMPONENTS = [c for c in NODE_NAMES if "Affine" in c] -KNOWN_COMPONENTS = NODE_NAMES.keys() +KNOWN_COMPONENTS = list(NODE_NAMES.keys()) # End configuration section logger = logging.getLogger(__name__) diff --git a/egs/wsj/s5/steps/nnet3/dot/descriptor_parser.py b/egs/wsj/s5/steps/nnet3/dot/descriptor_parser.py index a46d144d0b6..ee6fa11b5c9 100644 --- a/egs/wsj/s5/steps/nnet3/dot/descriptor_parser.py +++ b/egs/wsj/s5/steps/nnet3/dot/descriptor_parser.py @@ -33,7 +33,7 @@ def ParseSubsegmentsAndArguments(segment_endpoints, sub_segments, arguments, inp else: arguments.append(sub_segment_name) else: - arguments = map(lambda x: re.sub(',','', x.strip()), input_string[segment_endpoints[0]:segment_endpoints[1]+1].split()) + arguments = [re.sub(',','', x.strip()) for x in input_string[segment_endpoints[0]:segment_endpoints[1]+1].split()] sub_segments = [] return sub_segments, arguments diff --git a/egs/wsj/s5/steps/nnet3/dot/nnet3_to_dot.py b/egs/wsj/s5/steps/nnet3/dot/nnet3_to_dot.py index f8cd357fa3b..4230b32aa7c 100755 --- a/egs/wsj/s5/steps/nnet3/dot/nnet3_to_dot.py +++ b/egs/wsj/s5/steps/nnet3/dot/nnet3_to_dot.py @@ -189,7 +189,7 @@ def ProcessSumDescriptor(segment, parent_node_name, affix, edge_attributes = Non sub_segment = segment['sub_segments'][i] part_name = "{0}{1}{2}".format(desc_name, sub_segment['name'], i) names.append("<{0}> part {1}".format(GetDotNodeName(part_name)['node'], i)) - dot_graph += DescriptorSegmentToDot(sub_segment, "{0}:{1}".format(desc_name, part_name), desc_name+"_"+str(i)) + dot_graph += DescriptorSegmentToDot(sub_segment, "{0}:{1}".format(desc_name, part_name), "{0}_{1}".format(desc_name, i)) # link the sum node parts to corresponding segments part_index = len(segment['sub_segments']) @@ -321,7 +321,7 @@ def Nnet3ComponentToDot(component_config, component_attributes = None): label = '' if component_attributes is None: component_attributes = component_config.keys() - attributes_to_print = set(component_attributes).intersection(component_config.keys()) + attributes_to_print = set(component_attributes).intersection(list(component_config.keys())) # process the known fields for key in attributes_to_print: if key in component_config: diff --git a/egs/wsj/s5/steps/nnet3/get_successful_models.py b/egs/wsj/s5/steps/nnet3/get_successful_models.py index 3661d91b8d5..e6dcf376a51 100755 --- a/egs/wsj/s5/steps/nnet3/get_successful_models.py +++ b/egs/wsj/s5/steps/nnet3/get_successful_models.py @@ -56,7 +56,7 @@ if (loss[max_index] - loss[i]) <= args.difference_threshold: accepted_models.append(i+1) - model_list = " ".join(map(lambda x: str(x), accepted_models)) + model_list = " ".join([str(x) for x in accepted_models]) print(model_list) if len(accepted_models) != args.num_models: diff --git a/egs/wsj/s5/steps/nnet3/lstm/make_configs.py b/egs/wsj/s5/steps/nnet3/lstm/make_configs.py index b80a8d4045b..8a533465f07 100755 --- a/egs/wsj/s5/steps/nnet3/lstm/make_configs.py +++ b/egs/wsj/s5/steps/nnet3/lstm/make_configs.py @@ -181,7 +181,7 @@ def ParseSpliceString(splice_indexes, label_delay=None): splice_array = [] try: for i in range(len(split1)): - indexes = map(lambda x: int(x), split1[i].strip().split(",")) + indexes = [int(x) for x in split1[i].strip().split(",")] print(indexes) if len(indexes) < 1: raise ValueError("invalid --splice-indexes argument, too-short element: " @@ -214,12 +214,12 @@ def ParseLstmDelayString(lstm_delay): lstm_delay_array = [] try: for i in range(len(split1)): - indexes = map(lambda x: int(x), split1[i].strip().lstrip('[').rstrip(']').strip().split(",")) + indexes = [int(x) for x in split1[i].strip().lstrip('[').rstrip(']').strip().split(",")] if len(indexes) < 1: raise ValueError("invalid --lstm-delay argument, too-short element: " + lstm_delay) elif len(indexes) == 2 and indexes[0] * indexes[1] >= 0: - raise ValueError('Warning: ' + str(indexes) + ' is not a standard BLSTM mode. There should be a negative delay for the forward, and a postive delay for the backward.') + raise ValueError('Warning: {} is not a standard BLSTM mode. There should be a negative delay for the forward, and a postive delay for the backward.'.format(indexes)) if len(indexes) == 2 and indexes[0] > 0: # always a negative delay followed by a postive delay indexes[0], indexes[1] = indexes[1], indexes[0] lstm_delay_array.append(indexes) @@ -335,9 +335,9 @@ def ProcessSpliceIndexes(config_dir, splice_indexes, label_delay, num_lstm_layer # write the files used by other scripts like steps/nnet3/get_egs.sh f = open(config_dir + "/vars", "w") - print('model_left_context=' + str(left_context), file=f) - print('model_right_context=' + str(right_context), file=f) - print('num_hidden_layers=' + str(num_hidden_layers), file=f) + print('model_left_context={}'.format(left_context), file=f) + print('model_right_context={}'.format(right_context), file=f) + print('num_hidden_layers={}'.format(num_hidden_layers), file=f) # print('initial_right_context=' + str(splice_array[0][-1]), file=f) f.close() diff --git a/egs/wsj/s5/steps/nnet3/make_tdnn_configs.py b/egs/wsj/s5/steps/nnet3/make_tdnn_configs.py index 162fda16d16..d121be6d899 100644 --- a/egs/wsj/s5/steps/nnet3/make_tdnn_configs.py +++ b/egs/wsj/s5/steps/nnet3/make_tdnn_configs.py @@ -98,21 +98,21 @@ input_dim = len(splice_array[0]) * args.feat_dim + args.ivector_dim f = open(args.config_dir + "/vars", "w") -print('left_context=' + str(left_context), file=f) -print('right_context=' + str(right_context), file=f) +print('left_context={}'.format(left_context), file=f) +print('right_context={}'.format(right_context), file=f) # the initial l/r contexts are actually not needed. # print('initial_left_context=' + str(splice_array[0][0]), file=f) # print('initial_right_context=' + str(splice_array[0][-1]), file=f) -print('num_hidden_layers=' + str(num_hidden_layers), file=f) +print('num_hidden_layers={}'.format(num_hidden_layers), file=f) f.close() f = open(args.config_dir + "/init.config", "w") print('# Config file for initializing neural network prior to', file=f) print('# preconditioning matrix computation', file=f) -print('input-node name=input dim=' + str(args.feat_dim), file=f) +print('input-node name=input dim={}'.format(args.feat_dim), file=f) list=[ ('Offset(input, {0})'.format(n) if n != 0 else 'input' ) for n in splice_array[0] ] if args.ivector_dim > 0: - print('input-node name=ivector dim=' + str(args.ivector_dim), file=f) + print('input-node name=ivector dim={}'.format(args.ivector_dim), file=f) list.append('ReplaceIndex(ivector, t, 0)') # example of next line: # output-node name=output input="Append(Offset(input, -3), Offset(input, -2), Offset(input, -1), ... , Offset(input, 3), ReplaceIndex(ivector, t, 0))" diff --git a/egs/wsj/s5/steps/nnet3/multilingual/allocate_multilingual_examples.py b/egs/wsj/s5/steps/nnet3/multilingual/allocate_multilingual_examples.py index 54c65eb5403..a407869854d 100755 --- a/egs/wsj/s5/steps/nnet3/multilingual/allocate_multilingual_examples.py +++ b/egs/wsj/s5/steps/nnet3/multilingual/allocate_multilingual_examples.py @@ -40,7 +40,6 @@ """ -from __future__ import print_function import os, argparse, sys, random import logging import traceback @@ -163,7 +162,7 @@ def process_multilingual_egs(args): "not include any examples from this lang.") logger.info("The proportion of egs from lang {} is {:.2f}. The number of blocks " "per archive for this lang is approximately {:.2f}. " - "{}".format(lang, lang_to_num_examples[lang] / tot_num_egs, + "{}".format(lang, float(lang_to_num_examples[lang]) / tot_num_egs, blocks_per_archive_this_lang, warning)) @@ -173,11 +172,11 @@ def process_multilingual_egs(args): lang_to_num_remaining_egs = [n for n in lang_to_num_examples] for archive_index in range(num_archives + 1): # +1 is because we write to the last archive in two rounds num_remaining_archives = num_archives - archive_index - num_remaining_blocks = num_remaining_egs / args.block_size + num_remaining_blocks = float(num_remaining_egs) / args.block_size last_round = (archive_index == num_archives) if not last_round: - num_blocks_this_archive = int(round(num_remaining_blocks / num_remaining_archives)) + num_blocks_this_archive = int(round(float(num_remaining_blocks) / num_remaining_archives)) logger.info("Generating archive {} containing {} blocks...".format(archive_index, num_blocks_this_archive)) else: # This is the second round for the last archive. Flush all the remaining egs... archive_index = num_archives - 1 @@ -194,7 +193,7 @@ def process_multilingual_egs(args): for block_index in range(num_blocks_this_archive): # Find the lang with the highest proportion of remaining examples - remaining_proportions = [remain / tot for remain, tot in zip(lang_to_num_remaining_egs, lang_to_num_examples)] + remaining_proportions = [float(remain) / tot for remain, tot in zip(lang_to_num_remaining_egs, lang_to_num_examples)] lang_index, max_proportion = max(enumerate(remaining_proportions), key=lambda a: a[1]) # Read 'block_size' examples from the selected lang and write them to the current output scp file: diff --git a/egs/wsj/s5/steps/nnet3/report/generate_plots.py b/egs/wsj/s5/steps/nnet3/report/generate_plots.py index 93cbc940c33..572e2cf08b7 100755 --- a/egs/wsj/s5/steps/nnet3/report/generate_plots.py +++ b/egs/wsj/s5/steps/nnet3/report/generate_plots.py @@ -4,6 +4,7 @@ # 2016 Vimal Manohar # Apache 2.0. +from __future__ import division import argparse import errno import logging @@ -97,7 +98,7 @@ def get_args(): g_plot_colors = ['red', 'blue', 'green', 'black', 'magenta', 'yellow', 'cyan'] -class LatexReport: +class LatexReport(object): """Class for writing a Latex report""" def __init__(self, pdf_file): @@ -422,7 +423,7 @@ def generate_nonlin_stats_plots(exp_dir, output_dir, plot, comparison_dir=None, f.write("\n".join(iter_stat_report)) f.close() if plot: - main_component_names = main_stat_tables.keys() + main_component_names = list(main_stat_tables.keys()) main_component_names.sort() plot_component_names = set(main_component_names) @@ -528,13 +529,13 @@ def generate_clipped_proportion_plots(exp_dir, output_dir, plot, file = open("{dir}/clipped_proportion.log".format(dir=output_dir), "w") iter_stat_report = "" for row in main_cp_stats: - iter_stat_report += "\t".join(map(lambda x: str(x), row)) + "\n" + iter_stat_report += "\t".join([str(x) for x in row]) + "\n" file.write(iter_stat_report) file.close() if plot: main_component_names = ( - stats_per_dir[exp_dir]['cp_per_iter_per_component'].keys()) + list(stats_per_dir[exp_dir]['cp_per_iter_per_component'].keys())) main_component_names.sort() plot_component_names = set(main_component_names) for dir in dirs: @@ -635,22 +636,21 @@ def generate_parameter_diff_plots(exp_dir, output_dir, plot, except KeyError: total_missing_iterations += 1 iter_data.append("NA") - if (total_missing_iterations/len(component_names) > 20 + if (float(total_missing_iterations)/len(component_names) > 20 and not gave_user_warning): logger.warning("There are more than {0} missing " "iterations per component. " "Something might be wrong.".format( - total_missing_iterations - / len(component_names))) + float(total_missing_iterations)/ len(component_names))) gave_user_warning = True f.write(" ".join(iter_data)+"\n") if plot: # get the component names - diff_type = key_file.keys()[0] - main_component_names = stats_per_dir[exp_dir][diff_type][ - 'progress_per_component'].keys() + diff_type = list(key_file.keys())[0] + main_component_names = list(stats_per_dir[exp_dir][diff_type][ + 'progress_per_component'].keys()) main_component_names.sort() plot_component_names = set(main_component_names) diff --git a/egs/wsj/s5/steps/nnet3/report/summarize_compute_debug_timing.py b/egs/wsj/s5/steps/nnet3/report/summarize_compute_debug_timing.py index 442ca4e35cf..5c74eaf128c 100755 --- a/egs/wsj/s5/steps/nnet3/report/summarize_compute_debug_timing.py +++ b/egs/wsj/s5/steps/nnet3/report/summarize_compute_debug_timing.py @@ -7,6 +7,7 @@ # we're using python 3.x style print but want it to work in python 2.x, from __future__ import print_function +from __future__ import division import sys import re import argparse @@ -101,7 +102,7 @@ def Main(): total_time = sum(command_times.values()) sorted_commands = sorted(command_times.items(), key = lambda x: x[1], reverse = True) for item in sorted_commands: - print("{c} : time {t} : fraction {f}".format(c=item[0], t=item[1], f=item[1] / total_time)) + print("{c} : time {t} : fraction {f}".format(c=item[0], t=item[1], f=float(item[1]) / total_time)) if __name__ == "__main__": diff --git a/egs/wsj/s5/steps/nnet3/tdnn/make_configs.py b/egs/wsj/s5/steps/nnet3/tdnn/make_configs.py index 5445b16e165..9e7e92f6768 100755 --- a/egs/wsj/s5/steps/nnet3/tdnn/make_configs.py +++ b/egs/wsj/s5/steps/nnet3/tdnn/make_configs.py @@ -4,6 +4,7 @@ # we're using python 3.x style print but want it to work in python 2.x, from __future__ import print_function +from __future__ import division import os import argparse import shlex @@ -519,10 +520,10 @@ def MakeConfigs(config_dir, splice_indexes_string, # write the files used by other scripts like steps/nnet3/get_egs.sh f = open(config_dir + "/vars", "w") - print('model_left_context=' + str(left_context), file=f) - print('model_right_context=' + str(right_context), file=f) - print('num_hidden_layers=' + str(num_hidden_layers), file=f) - print('num_targets=' + str(num_targets), file=f) + print('model_left_context={}'.format(left_context), file=f) + print('model_right_context={}'.format(right_context), file=f) + print('num_hidden_layers={}'.format(num_hidden_layers), file=f) + print('num_targets={}'.format(num_targets), file=f) print('add_lda=' + ('true' if add_lda else 'false'), file=f) print('include_log_softmax=' + ('true' if include_log_softmax else 'false'), file=f) print('objective_type=' + objective_type, file=f) diff --git a/egs/wsj/s5/steps/nnet3/train_dnn.py b/egs/wsj/s5/steps/nnet3/train_dnn.py index 0c881b4dbdf..e72b29297a4 100755 --- a/egs/wsj/s5/steps/nnet3/train_dnn.py +++ b/egs/wsj/s5/steps/nnet3/train_dnn.py @@ -9,6 +9,7 @@ """ from __future__ import print_function +from __future__ import division import argparse import logging import os @@ -193,7 +194,7 @@ def train(args, run_opts): shutil.copy('{0}/tree'.format(args.ali_dir), args.dir) with open('{0}/num_jobs'.format(args.dir), 'w') as f: - f.write(str(num_jobs)) + f.write('{}'.format(num_jobs)) if args.input_model is None: config_dir = '{0}/configs'.format(args.dir) @@ -301,8 +302,7 @@ def train(args, run_opts): num_archives_expanded = num_archives * args.frames_per_eg num_archives_to_process = int(args.num_epochs * num_archives_expanded) num_archives_processed = 0 - num_iters = ((num_archives_to_process * 2) - / (args.num_jobs_initial + args.num_jobs_final)) + num_iters = int(num_archives_to_process * 2 / (args.num_jobs_initial + args.num_jobs_final)) # If do_final_combination is True, compute the set of models_to_combine. # Otherwise, models_to_combine will be none. diff --git a/egs/wsj/s5/steps/nnet3/train_raw_dnn.py b/egs/wsj/s5/steps/nnet3/train_raw_dnn.py index fc73cbc7f3f..ffccf443b99 100755 --- a/egs/wsj/s5/steps/nnet3/train_raw_dnn.py +++ b/egs/wsj/s5/steps/nnet3/train_raw_dnn.py @@ -9,6 +9,7 @@ """ from __future__ import print_function +from __future__ import division import argparse import logging import pprint @@ -321,8 +322,7 @@ def train(args, run_opts): num_archives_expanded = num_archives * args.frames_per_eg num_archives_to_process = int(args.num_epochs * num_archives_expanded) num_archives_processed = 0 - num_iters = int((num_archives_to_process * 2) - / (args.num_jobs_initial + args.num_jobs_final)) + num_iters = int((num_archives_to_process * 2) / (args.num_jobs_initial + args.num_jobs_final)) # If do_final_combination is True, compute the set of models_to_combine. # Otherwise, models_to_combine will be none. diff --git a/egs/wsj/s5/steps/nnet3/train_raw_rnn.py b/egs/wsj/s5/steps/nnet3/train_raw_rnn.py index bf2d7534f79..c704b0725d3 100755 --- a/egs/wsj/s5/steps/nnet3/train_raw_rnn.py +++ b/egs/wsj/s5/steps/nnet3/train_raw_rnn.py @@ -10,6 +10,7 @@ raw neural network instead of an acoustic model. """ from __future__ import print_function +from __future__ import division import argparse import logging import pprint @@ -368,8 +369,7 @@ def train(args, run_opts): # avg_num_jobs=(num_jobs_initial+num_jobs_final)/2. num_archives_to_process = int(args.num_epochs * num_archives) num_archives_processed = 0 - num_iters = ((num_archives_to_process * 2) - / (args.num_jobs_initial + args.num_jobs_final)) + num_iters = int((num_archives_to_process * 2) / (args.num_jobs_initial + args.num_jobs_final)) # If do_final_combination is True, compute the set of models_to_combine. # Otherwise, models_to_combine will be none. diff --git a/egs/wsj/s5/steps/nnet3/train_rnn.py b/egs/wsj/s5/steps/nnet3/train_rnn.py index 25e7dced19b..ab2aa0c4d8d 100755 --- a/egs/wsj/s5/steps/nnet3/train_rnn.py +++ b/egs/wsj/s5/steps/nnet3/train_rnn.py @@ -8,6 +8,7 @@ """ from __future__ import print_function +from __future__ import division import argparse import logging import os @@ -248,7 +249,7 @@ def train(args, run_opts): shutil.copy('{0}/tree'.format(args.ali_dir), args.dir) with open('{0}/num_jobs'.format(args.dir), 'w') as f: - f.write(str(num_jobs)) + f.write('{}'.format(num_jobs)) config_dir = '{0}/configs'.format(args.dir) var_file = '{0}/vars'.format(config_dir) @@ -369,8 +370,7 @@ def train(args, run_opts): # avg_num_jobs=(num_jobs_initial+num_jobs_final)/2. num_archives_to_process = int(args.num_epochs * num_archives) num_archives_processed = 0 - num_iters = ((num_archives_to_process * 2) - / (args.num_jobs_initial + args.num_jobs_final)) + num_iters = int((num_archives_to_process * 2) / (args.num_jobs_initial + args.num_jobs_final)) # If do_final_combination is True, compute the set of models_to_combine. # Otherwise, models_to_combine will be none. diff --git a/egs/wsj/s5/steps/nnet3/xconfig_to_configs.py b/egs/wsj/s5/steps/nnet3/xconfig_to_configs.py index 3b8dc82fe48..f025eb5b343 100755 --- a/egs/wsj/s5/steps/nnet3/xconfig_to_configs.py +++ b/egs/wsj/s5/steps/nnet3/xconfig_to_configs.py @@ -115,7 +115,7 @@ def write_expanded_xconfig_files(config_dir, all_layers): '# See also ./xconfig.expanded.2\n', file=xconfig_file_out) for layer in all_layers: - print(str(layer), file=xconfig_file_out) + print('{}'.format(layer), file=xconfig_file_out) xconfig_file_out.close() try: @@ -135,7 +135,7 @@ def write_expanded_xconfig_files(config_dir, all_layers): for layer in all_layers: layer.normalize_descriptors() - print(str(layer), file=xconfig_file_out) + print('{}'.format(layer), file=xconfig_file_out) xconfig_file_out.close() diff --git a/egs/wsj/s5/steps/segmentation/internal/find_oov_phone.py b/egs/wsj/s5/steps/segmentation/internal/find_oov_phone.py index 3e9cbbbf178..038640f6271 100644 --- a/egs/wsj/s5/steps/segmentation/internal/find_oov_phone.py +++ b/egs/wsj/s5/steps/segmentation/internal/find_oov_phone.py @@ -8,6 +8,7 @@ /phones/align_lexicon.int. It prints the OOV phone to stdout, if it can find a single phone mapping for the OOV word.""" +from __future__ import print_function import sys diff --git a/egs/wsj/s5/steps/segmentation/internal/get_default_targets_for_out_of_segments.py b/egs/wsj/s5/steps/segmentation/internal/get_default_targets_for_out_of_segments.py index e7000b9de00..0361999d904 100755 --- a/egs/wsj/s5/steps/segmentation/internal/get_default_targets_for_out_of_segments.py +++ b/egs/wsj/s5/steps/segmentation/internal/get_default_targets_for_out_of_segments.py @@ -14,6 +14,7 @@ the application and data, this could be [ 0 0 0 ] or [ 0 0 1 ] or something with fractional weights. """ +from __future__ import division import argparse import logging @@ -131,7 +132,7 @@ def run(args): and np.shape(default_targets)[1] == 3) with common_lib.smart_open(args.out_targets_ark, 'w') as f: - for reco, utts in reco2utt.iteritems(): + for reco, utts in reco2utt.items(): reco_mat = np.repeat(default_targets, reco2num_frames[reco], axis=0) utts.sort(key=lambda x: segments[x][1]) # sort on start time diff --git a/egs/wsj/s5/steps/segmentation/internal/merge_segment_targets_to_recording.py b/egs/wsj/s5/steps/segmentation/internal/merge_segment_targets_to_recording.py index 8c53e5e8db9..e48afbeb872 100755 --- a/egs/wsj/s5/steps/segmentation/internal/merge_segment_targets_to_recording.py +++ b/egs/wsj/s5/steps/segmentation/internal/merge_segment_targets_to_recording.py @@ -9,6 +9,7 @@ in any of the segments are assigned the default targets vector, specified by the option --default-targets or [ 0 0 0 ] if unspecified. """ +from __future__ import division import argparse import logging @@ -158,7 +159,7 @@ def run(args): num_reco = 0 with common_lib.smart_open(args.out_targets_ark, 'w') as fh: - for reco, utts in reco2utt.iteritems(): + for reco, utts in reco2utt.items(): # Read a recording and the list of its utterances from the # reco2utt dictionary reco_mat = np.repeat(default_targets, reco2num_frames[reco], diff --git a/egs/wsj/s5/steps/segmentation/internal/merge_targets.py b/egs/wsj/s5/steps/segmentation/internal/merge_targets.py index 8222eddad8f..a14aef151c2 100755 --- a/egs/wsj/s5/steps/segmentation/internal/merge_targets.py +++ b/egs/wsj/s5/steps/segmentation/internal/merge_targets.py @@ -17,6 +17,7 @@ """ from __future__ import print_function +from __future__ import division import argparse import logging import numpy as np diff --git a/egs/wsj/s5/steps/tfrnnlm/lstm.py b/egs/wsj/s5/steps/tfrnnlm/lstm.py index 5f175212c4b..433dc87b4c6 100644 --- a/egs/wsj/s5/steps/tfrnnlm/lstm.py +++ b/egs/wsj/s5/steps/tfrnnlm/lstm.py @@ -203,7 +203,7 @@ def attn_cell(): config.max_grad_norm) optimizer = tf.train.GradientDescentOptimizer(self._lr) self._train_op = optimizer.apply_gradients( - zip(grads, tvars), + list(zip(grads, tvars)), global_step=tf.contrib.framework.get_or_create_global_step()) self._new_lr = tf.placeholder( diff --git a/egs/wsj/s5/steps/tfrnnlm/lstm_fast.py b/egs/wsj/s5/steps/tfrnnlm/lstm_fast.py index 440962a3780..ff6c7263804 100644 --- a/egs/wsj/s5/steps/tfrnnlm/lstm_fast.py +++ b/egs/wsj/s5/steps/tfrnnlm/lstm_fast.py @@ -218,7 +218,7 @@ def attn_cell(): config.max_grad_norm) optimizer = tf.train.GradientDescentOptimizer(self._lr) self._train_op = optimizer.apply_gradients( - zip(grads, tvars), + list(zip(grads, tvars)), global_step=tf.contrib.framework.get_or_create_global_step()) self._new_lr = tf.placeholder( diff --git a/egs/wsj/s5/steps/tfrnnlm/reader.py b/egs/wsj/s5/steps/tfrnnlm/reader.py index fc3d4d0471c..80cdeccbb26 100644 --- a/egs/wsj/s5/steps/tfrnnlm/reader.py +++ b/egs/wsj/s5/steps/tfrnnlm/reader.py @@ -31,7 +31,7 @@ def _read_words(filename): def _build_vocab(filename): words = _read_words(filename) - word_to_id = dict(zip(words, range(len(words)))) + word_to_id = dict(list(zip(words, list(range(len(words)))))) return word_to_id diff --git a/egs/wsj/s5/steps/tfrnnlm/vanilla_rnnlm.py b/egs/wsj/s5/steps/tfrnnlm/vanilla_rnnlm.py index f3ce1a5c297..ae7a257906e 100644 --- a/egs/wsj/s5/steps/tfrnnlm/vanilla_rnnlm.py +++ b/egs/wsj/s5/steps/tfrnnlm/vanilla_rnnlm.py @@ -201,7 +201,7 @@ def attn_cell(): config.max_grad_norm) optimizer = tf.train.MomentumOptimizer(self._lr, 0.9) self._train_op = optimizer.apply_gradients( - zip(grads, tvars), + list(zip(grads, tvars)), global_step=tf.contrib.framework.get_or_create_global_step()) self._new_lr = tf.placeholder( diff --git a/egs/wsj/s5/utils/ctm/resolve_ctm_overlaps.py b/egs/wsj/s5/utils/ctm/resolve_ctm_overlaps.py index deb8207c5b7..61c9a3014aa 100755 --- a/egs/wsj/s5/utils/ctm/resolve_ctm_overlaps.py +++ b/egs/wsj/s5/utils/ctm/resolve_ctm_overlaps.py @@ -17,6 +17,7 @@ """ from __future__ import print_function +from __future__ import division import argparse import collections import logging @@ -231,7 +232,7 @@ def resolve_overlaps(ctms, segments): try: index = next( (i for i, line in enumerate(ctm_for_next_utt) - if line[2] + line[3] / 2.0 > overlap / 2.0)) + if line[2] + line[3] / 2.0 > overlap / 2.0)) except StopIteration: # This can happen if there is no word hypothesized after # half the overlap region. @@ -277,7 +278,7 @@ def run(args): segments, reco2utt = read_segments(args.segments) ctms = read_ctm(args.ctm_in, segments) - for reco, utts in reco2utt.iteritems(): + for reco, utts in reco2utt.items(): ctms_for_reco = [] for utt in sorted(utts, key=lambda x: segments[x][1]): if (reco, utt) in ctms: diff --git a/egs/wsj/s5/utils/data/get_uniform_subsegments.py b/egs/wsj/s5/utils/data/get_uniform_subsegments.py index c61b96e0dbb..cc3015564a5 100755 --- a/egs/wsj/s5/utils/data/get_uniform_subsegments.py +++ b/egs/wsj/s5/utils/data/get_uniform_subsegments.py @@ -4,6 +4,7 @@ # 2017 Matthew Maciejewski # Apache 2.0. +from __future__ import print_function import argparse import logging import sys diff --git a/egs/wsj/s5/utils/data/internal/choose_utts_to_combine.py b/egs/wsj/s5/utils/data/internal/choose_utts_to_combine.py index 740b9aa612b..875c238abd5 100755 --- a/egs/wsj/s5/utils/data/internal/choose_utts_to_combine.py +++ b/egs/wsj/s5/utils/data/internal/choose_utts_to_combine.py @@ -89,7 +89,7 @@ def CombineList(min_duration, durations): # for each utterance-index i, group_start[i] gives us the # start-index of the group of utterances of which it's currently # a member. - group_start = range(num_utts) + group_start = list(range(num_utts)) # if utterance-index i currently corresponds to the start of a group # of utterances, then group_durations[i] is the total duration of # that utterance-group, otherwise undefined. @@ -327,7 +327,7 @@ def GetUtteranceGroups(min_duration, spk2utt, utt2dur): utt_groups = GetUtteranceGroups(args.min_duration, spk2utt, utt2dur) # set utt_group names to an array like [ 'utt1', 'utt2-comb2', 'utt4', ... ] -utt_group_names = [ group[0] if len(group)==1 else group[0] + "-comb" + str(len(group)) +utt_group_names = [ group[0] if len(group)==1 else "{0}-comb{1}".format(group[0], len(group)) for group in utt_groups ] diff --git a/egs/wsj/s5/utils/data/internal/perturb_volume.py b/egs/wsj/s5/utils/data/internal/perturb_volume.py index b3bd4225191..c1dfd936358 100755 --- a/egs/wsj/s5/utils/data/internal/perturb_volume.py +++ b/egs/wsj/s5/utils/data/internal/perturb_volume.py @@ -8,6 +8,7 @@ volume of the recordings and writes to stdout the contents of a new wav.scp file. """ +from __future__ import print_function import argparse import re diff --git a/egs/wsj/s5/utils/data/perturb_speed_to_allowed_lengths.py b/egs/wsj/s5/utils/data/perturb_speed_to_allowed_lengths.py index 7924fc4fcf1..ae16e63c945 100755 --- a/egs/wsj/s5/utils/data/perturb_speed_to_allowed_lengths.py +++ b/egs/wsj/s5/utils/data/perturb_speed_to_allowed_lengths.py @@ -60,7 +60,7 @@ def get_args(): args.speed_perturb = True if args.speed_perturb == 'true' else False return args -class Utterance: +class Utterance(object): """ This class represents a Kaldi utterance in a data directory like data/train """ @@ -321,7 +321,7 @@ def main(): "Coverage rate: {}%".format(start_dur, end_dur, 100.0 - args.coverage_factor * 2)) logger.info("There will be {} unique allowed lengths " - "for the utterances.".format(int(math.log(end_dur / start_dur) / + "for the utterances.".format(int(math.log(end_dur / start_dur)/ math.log(args.factor)))) allowed_durations = find_allowed_durations(start_dur, end_dur, args) diff --git a/egs/wsj/s5/utils/filt.py b/egs/wsj/s5/utils/filt.py index 2847c0034dd..9201d9e493f 100755 --- a/egs/wsj/s5/utils/filt.py +++ b/egs/wsj/s5/utils/filt.py @@ -2,6 +2,7 @@ # Apache 2.0 +from __future__ import print_function import sys vocab=set() @@ -11,4 +12,4 @@ with open(sys.argv[2]) as textfile: for line in textfile: - print " ".join(map(lambda word: word if word in vocab else '', line.strip().split())) + print(" ".join([word if word in vocab else '' for word in line.strip().split()])) diff --git a/egs/wsj/s5/utils/lang/bpe/learn_bpe.py b/egs/wsj/s5/utils/lang/bpe/learn_bpe.py index 70f18f2d1d9..f6c6d5a0ebb 100755 --- a/egs/wsj/s5/utils/lang/bpe/learn_bpe.py +++ b/egs/wsj/s5/utils/lang/bpe/learn_bpe.py @@ -13,6 +13,8 @@ """ from __future__ import unicode_literals +from __future__ import division +from __future__ import print_function import sys import codecs diff --git a/egs/wsj/s5/utils/lang/internal/arpa2fst_constrained.py b/egs/wsj/s5/utils/lang/internal/arpa2fst_constrained.py index 19acd311c3d..31dfd08fbd2 100755 --- a/egs/wsj/s5/utils/lang/internal/arpa2fst_constrained.py +++ b/egs/wsj/s5/utils/lang/internal/arpa2fst_constrained.py @@ -4,6 +4,7 @@ # Apache 2.0. from __future__ import print_function +from __future__ import division import sys import argparse import math @@ -44,7 +45,7 @@ print(' '.join(sys.argv), file = sys.stderr) -class HistoryState: +class HistoryState(object): def __init__(self): # note: neither backoff_prob nor the floats # in word_to_prob are in log space. @@ -56,7 +57,7 @@ def __init__(self): self.word_to_prob = dict() -class ArpaModel: +class ArpaModel(object): def __init__(self): # self.orders is indexed by history-length [i.e. 0 for unigram, # 1 for bigram and so on], and is then a dict indexed diff --git a/egs/wsj/s5/utils/lang/make_phone_lm.py b/egs/wsj/s5/utils/lang/make_phone_lm.py index 47d2a45d229..5cc9a8de832 100755 --- a/egs/wsj/s5/utils/lang/make_phone_lm.py +++ b/egs/wsj/s5/utils/lang/make_phone_lm.py @@ -4,6 +4,7 @@ # Apache 2.0. from __future__ import print_function +from __future__ import division import sys import argparse import math @@ -65,7 +66,7 @@ -class CountsForHistory: +class CountsForHistory(object): ## This class (which is more like a struct) stores the counts seen in a ## particular history-state. It is used inside class NgramCounts. ## It really does the job of a dict from int to float, but it also @@ -77,7 +78,7 @@ def __init__(self): self.total_count = 0 def Words(self): - return self.word_to_count.keys() + return list(self.word_to_count.keys()) def __str__(self): # e.g. returns ' total=12 3->4 4->6 -1->2' @@ -109,7 +110,7 @@ def AddCount(self, predicted_word, count): else: self.word_to_count[predicted_word] = new_count -class NgramCounts: +class NgramCounts(object): ## A note on data-structure. Firstly, all words are represented as ## integers. We store n-gram counts as an array, indexed by (history-length ## == n-gram order minus one) (note: python calls arrays "lists") of dicts @@ -187,7 +188,7 @@ def ApplyBackoff(self): # there will be no unigram. if args.verbose >= 1: initial_num_ngrams = self.GetNumNgrams() - for n in reversed(range(args.no_backoff_ngram_order, args.ngram_order)): + for n in reversed(list(range(args.no_backoff_ngram_order, args.ngram_order))): this_order_counts = self.counts[n] for hist, counts_for_hist in this_order_counts.items(): backoff_hist = hist[1:] @@ -276,8 +277,8 @@ def PruneEmptyStates(self): states_removed_per_hist_len = [ 0 ] * args.ngram_order - for n in reversed(range(args.no_backoff_ngram_order, - args.ngram_order)): + for n in reversed(list(range(args.no_backoff_ngram_order, + args.ngram_order))): num_states_removed = 0 for hist, counts_for_hist in self.counts[n].items(): l = len(counts_for_hist.word_to_count) @@ -304,14 +305,14 @@ def EnsureStructurallyNeededNgramsExist(self): # we have a unigram state]. if args.verbose >= 1: num_ngrams_initial = self.GetNumNgrams() - for n in reversed(range(args.no_backoff_ngram_order, - args.ngram_order)): + for n in reversed(list(range(args.no_backoff_ngram_order, + args.ngram_order))): for hist, counts_for_hist in self.counts[n].items(): # This loop ensures that if we have an n-gram like (6, 7, 8) -> 9, # then, say, (7, 8) -> 9 and (8) -> 9 exist. reduced_hist = hist - for m in reversed(range(args.no_backoff_ngram_order, n)): + for m in reversed(list(range(args.no_backoff_ngram_order, n))): reduced_hist = reduced_hist[1:] # shift an element off # the history. counts_for_backoff_hist = self.counts[m][reduced_hist] @@ -321,7 +322,7 @@ def EnsureStructurallyNeededNgramsExist(self): # then, say, (6, 7) -> 8 and (6) -> 7 exist. This will be needed # for FST representations of the ARPA LM. reduced_hist = hist - for m in reversed(range(args.no_backoff_ngram_order, n)): + for m in reversed(list(range(args.no_backoff_ngram_order, n))): this_word = reduced_hist[-1] reduced_hist = reduced_hist[:-1] # pop an element off the # history @@ -346,7 +347,7 @@ def PrintAsFst(self, word_disambig_symbol): # History will map from history (as a tuple) to integer FST-state. hist_to_state = self.GetHistToStateMap() - for n in [ 1, 0 ] + range(2, args.ngram_order): + for n in [ 1, 0 ] + list(range(2, args.ngram_order)): this_order_counts = self.counts[n] # For order 1, make sure the keys are sorted. keys = this_order_counts.keys() if n != 1 else sorted(this_order_counts.keys()) @@ -388,7 +389,7 @@ def GetProtectedNgrams(self): # add the backed-off n-grams (7, 8) -> 9 and (8) -> 9 to # 'protected-ngrams'. reduced_hist = hist - for m in reversed(range(args.no_backoff_ngram_order, n)): + for m in reversed(list(range(args.no_backoff_ngram_order, n))): reduced_hist = reduced_hist[1:] # shift an element off # the history. @@ -399,7 +400,7 @@ def GetProtectedNgrams(self): # history-state (6, 7, 8), then n-grams (6, 7, 8) and (6, 7) are # protected. This assures that the FST states are accessible. reduced_hist = hist - for m in reversed(range(args.no_backoff_ngram_order, n)): + for m in reversed(list(range(args.no_backoff_ngram_order, n))): ans.add(reduced_hist) reduced_hist = reduced_hist[:-1] # pop an element off the # history @@ -499,7 +500,7 @@ def PruningLogprobChange(self, count, discount, backoff_count, backoff_total): # and the 'count' term is zero in the numerator part of the log expression, # because symbol 'a' is completely backed off in 'this' state. this_a_change = augmented_count * \ - math.log((new_discount * new_backoff_count / new_backoff_total) / \ + math.log((new_discount * new_backoff_count / new_backoff_total)/ \ augmented_count) # other_a_change is the log-like change of symbol 'a' coming from all @@ -511,7 +512,7 @@ def PruningLogprobChange(self, count, discount, backoff_count, backoff_total): # doing so gives us an upper bound on the divergence. other_a_change = \ a_other_count * math.log((new_backoff_count / new_backoff_total) / \ - (backoff_count / backoff_total)) + (backoff_count / backoff_total)) # b_change is the log-like change of phantom symbol 'b' coming from # 'this' state (and note: it only comes from this state, that's how we diff --git a/egs/wsj/s5/utils/nnet/gen_dct_mat.py b/egs/wsj/s5/utils/nnet/gen_dct_mat.py index d0f043ad7a4..24139f1c9f8 100755 --- a/egs/wsj/s5/utils/nnet/gen_dct_mat.py +++ b/egs/wsj/s5/utils/nnet/gen_dct_mat.py @@ -20,12 +20,20 @@ # and takes into account that data-layout is along frequency axis, # while DCT is done along temporal axis. +from __future__ import division +from __future__ import print_function from math import * import sys from optparse import OptionParser +def print_on_same_line(text): + if (sys.version_info > (3,0)): + print(text, end=' ') + else: + print text, + parser = OptionParser() parser.add_option('--fea-dim', dest='dim', help='feature dimension') parser.add_option('--splice', dest='splice', help='applied splice value') @@ -49,19 +57,19 @@ #generate sparse DCT matrix -print '[' +print('[') for k in range(dct_basis): for m in range(dim): for n in range(timeContext): - if(n==0): - print m*'0 ', - else: - print (dim-1)*'0 ', - print str(sqrt(2.0/timeContext)*cos(M_PI/timeContext*k*(n+0.5))), + if(n==0): + print_on_same_line(m*'0 ') + else: + print_on_same_line((dim-1)*'0 ') + print_on_same_line(str(sqrt(2.0/timeContext)*cos(M_PI/timeContext*k*(n+0.5)))) if(n==timeContext-1): - print (dim-m-1)*'0 ', - print - print + print_on_same_line((dim-m-1)*'0 ') + print() + print() -print ']' +print(']') diff --git a/egs/wsj/s5/utils/nnet/gen_hamm_mat.py b/egs/wsj/s5/utils/nnet/gen_hamm_mat.py index a4262a8cffd..d7e9d9b7493 100755 --- a/egs/wsj/s5/utils/nnet/gen_hamm_mat.py +++ b/egs/wsj/s5/utils/nnet/gen_hamm_mat.py @@ -18,12 +18,20 @@ # ./gen_hamm_mat.py # script generates diagonal matrix with hamming window values +from __future__ import division +from __future__ import print_function from math import * import sys from optparse import OptionParser +def print_on_same_line(text): + if (sys.version_info > (3,0)): + print(text, end=' ') + else: + print text, + parser = OptionParser() parser.add_option('--fea-dim', dest='dim', help='feature dimension') parser.add_option('--splice', dest='splice', help='applied splice value') @@ -42,16 +50,16 @@ dim_mat=(2*splice+1)*dim timeContext=2*splice+1 -print '[' +print('[') for row in range(dim_mat): for col in range(dim_mat): if col!=row: - print '0', + print_on_same_line('0') else: i=int(row/dim) - print str(0.54 - 0.46*cos((M_2PI * i) / (timeContext-1))), - print + print_on_same_line(str(0.54 - 0.46*cos((M_2PI * i) / (timeContext-1)))) + print() -print ']' +print(']') diff --git a/egs/wsj/s5/utils/nnet/gen_splice.py b/egs/wsj/s5/utils/nnet/gen_splice.py index 0241aeed6ba..3fe76513df6 100755 --- a/egs/wsj/s5/utils/nnet/gen_splice.py +++ b/egs/wsj/s5/utils/nnet/gen_splice.py @@ -18,12 +18,19 @@ # ./gen_splice.py # generates Component +from __future__ import print_function from math import * import sys from optparse import OptionParser +def print_on_same_line(text): + if (sys.version_info > (3,0)): + print(text, end=' ') + else: + print text, + parser = OptionParser() parser.add_option('--fea-dim', dest='dim_in', help='feature dimension') parser.add_option('--splice', dest='splice', help='number of frames to concatenate with the central frame') @@ -40,12 +47,12 @@ dim_out=(2*splice+1)*dim_in -print '', dim_out, dim_in -print '[', +print(' {0} {1}'.format(dim_out, dim_in)) +print_on_same_line('[') -splice_vec = range(-splice*splice_step, splice*splice_step+1, splice_step) +splice_vec = list(range(-splice*splice_step, splice*splice_step+1, splice_step)) for idx in range(len(splice_vec)): - print splice_vec[idx], + print_on_same_line(splice_vec[idx]) -print ']' +print(']') diff --git a/egs/wsj/s5/utils/nnet/make_blstm_proto.py b/egs/wsj/s5/utils/nnet/make_blstm_proto.py index 6e540ec791a..4d269cfdef0 100755 --- a/egs/wsj/s5/utils/nnet/make_blstm_proto.py +++ b/egs/wsj/s5/utils/nnet/make_blstm_proto.py @@ -17,6 +17,7 @@ # Generated Nnet prototype, to be initialized by 'nnet-initialize'. +from __future__ import print_function import sys ### @@ -54,7 +55,7 @@ parser.print_help() sys.exit(1) -(feat_dim, num_leaves) = map(int,args); +(feat_dim, num_leaves) = [int(i) for i in args]; # Original prototype from Jiayu, # @@ -77,18 +78,18 @@ # The BLSTM layers, if o.num_layers == 1: # Single BLSTM, - print " %d %d %s" % (feat_dim, 2*o.proj_dim_last, o.cell_dim) + lstm_extra_opts + print(" %d %d %s" % (feat_dim, 2*o.proj_dim_last, o.cell_dim) + lstm_extra_opts) else: # >1 BLSTM, - print " %d %d %s" % (feat_dim, 2*o.proj_dim, o.cell_dim) + lstm_extra_opts + print(" %d %d %s" % (feat_dim, 2*o.proj_dim, o.cell_dim) + lstm_extra_opts) for l in range(o.num_layers - 2): - print " %d %d %s" % (2*o.proj_dim, 2*o.proj_dim, o.cell_dim) + lstm_extra_opts - print " %d %d %s" % (2*o.proj_dim, 2*o.proj_dim_last, o.cell_dim) + lstm_extra_opts + print(" %d %d %s" % (2*o.proj_dim, 2*o.proj_dim, o.cell_dim) + lstm_extra_opts) + print(" %d %d %s" % (2*o.proj_dim, 2*o.proj_dim_last, o.cell_dim) + lstm_extra_opts) # Adding for more stability, -print " %d %d" % (2*o.proj_dim_last, 2*o.proj_dim_last) +print(" %d %d" % (2*o.proj_dim_last, 2*o.proj_dim_last)) # Softmax layer, -print " %d %d 0.0 0.0" % (2*o.proj_dim_last, num_leaves) + softmax_affine_opts -print " %d %d" % (num_leaves, num_leaves) +print(" %d %d 0.0 0.0" % (2*o.proj_dim_last, num_leaves) + softmax_affine_opts) +print(" %d %d" % (num_leaves, num_leaves)) diff --git a/egs/wsj/s5/utils/nnet/make_cnn2d_proto.py b/egs/wsj/s5/utils/nnet/make_cnn2d_proto.py index 73455563b51..172660da825 100755 --- a/egs/wsj/s5/utils/nnet/make_cnn2d_proto.py +++ b/egs/wsj/s5/utils/nnet/make_cnn2d_proto.py @@ -17,6 +17,8 @@ # Generated Nnet prototype, to be initialized by 'nnet-initialize'. +from __future__ import division +from __future__ import print_function import math, random, sys, warnings from optparse import OptionParser @@ -139,8 +141,8 @@ assert( (o.cnn1_fmap_x_len - o.cnn1_filt_x_len) % o.cnn1_filt_x_step == 0 ) # subsample1 -cnn1_out_fmap_y_len=((1 + (o.cnn1_fmap_y_len - o.cnn1_filt_y_len) / o.cnn1_filt_y_step)) -cnn1_out_fmap_x_len=((1 + (o.cnn1_fmap_x_len - o.cnn1_filt_x_len) / o.cnn1_filt_x_step)) +cnn1_out_fmap_y_len=(1 + (o.cnn1_fmap_y_len - o.cnn1_filt_y_len) / o.cnn1_filt_y_step) +cnn1_out_fmap_x_len=(1 + (o.cnn1_fmap_x_len - o.cnn1_filt_x_len) / o.cnn1_filt_x_step) # fix filt_len and filt_step def fix_filt_step(inp_len, filt_len, filt_step): @@ -149,7 +151,7 @@ def fix_filt_step(inp_len, filt_len, filt_step): return filt_step else: # filt_step <= filt_len - for filt_step in xrange(filt_len, 0, -1): + for filt_step in range(filt_len, 0, -1): if ((inp_len - filt_len) % filt_step == 0): return filt_step @@ -167,29 +169,29 @@ def fix_filt_step(inp_len, filt_len, filt_step): ### # Begin the prototype -print "" +print("") # Convolutional part of network '''1st CNN layer''' cnn1_input_dim=feat_raw_dim * (o.delta_order+1) * (o.splice*2+1) -cnn1_out_fmap_x_len=((1 + (o.cnn1_fmap_x_len - o.cnn1_filt_x_len) / o.cnn1_filt_x_step)) -cnn1_out_fmap_y_len=((1 + (o.cnn1_fmap_y_len - o.cnn1_filt_y_len) / o.cnn1_filt_y_step)) +cnn1_out_fmap_x_len=(1 + (o.cnn1_fmap_x_len - o.cnn1_filt_x_len) / o.cnn1_filt_x_step) +cnn1_out_fmap_y_len=(1 + (o.cnn1_fmap_y_len - o.cnn1_filt_y_len) / o.cnn1_filt_y_step) cnn1_output_dim=o.cnn1_num_filters * cnn1_out_fmap_x_len * cnn1_out_fmap_y_len '''1st Pooling layer''' pool1_input_dim=cnn1_output_dim pool1_fmap_x_len=cnn1_out_fmap_x_len -pool1_out_fmap_x_len=((1 + (pool1_fmap_x_len - o.pool1_x_len) / o.pool1_x_step)) +pool1_out_fmap_x_len=(1 + (pool1_fmap_x_len - o.pool1_x_len) / o.pool1_x_step) pool1_fmap_y_len=cnn1_out_fmap_y_len -pool1_out_fmap_y_len=((1 + (pool1_fmap_y_len - o.pool1_y_len) / o.pool1_y_step)) +pool1_out_fmap_y_len=(1 + (pool1_fmap_y_len - o.pool1_y_len) / o.pool1_y_step) pool1_output_dim=o.cnn1_num_filters*pool1_out_fmap_x_len*pool1_out_fmap_y_len '''2nd CNN layer''' cnn2_input_dim=pool1_output_dim cnn2_fmap_x_len=pool1_out_fmap_x_len -cnn2_out_fmap_x_len=((1 + (cnn2_fmap_x_len - o.cnn2_filt_x_len) / o.cnn2_filt_x_step)) +cnn2_out_fmap_x_len=(1 + (cnn2_fmap_x_len - o.cnn2_filt_x_len) / o.cnn2_filt_x_step) cnn2_fmap_y_len=pool1_out_fmap_y_len -cnn2_out_fmap_y_len=((1 + (cnn2_fmap_y_len - o.cnn2_filt_y_len) / o.cnn2_filt_y_step)) +cnn2_out_fmap_y_len=(1 + (cnn2_fmap_y_len - o.cnn2_filt_y_len) / o.cnn2_filt_y_step) cnn2_output_dim=o.cnn2_num_filters * cnn2_out_fmap_x_len * cnn2_out_fmap_y_len @@ -242,14 +244,14 @@ def fix_filt_step(inp_len, filt_len, filt_step): vector += '%d:1:%d ' % (i, i + feat_raw_dim - 1) for i in range(feat_raw_dim+1, (feat_raw_dim + o.pitch_dim) * (o.delta_order+1) * (o.splice*2+1), feat_raw_dim + o.pitch_dim): vector += '%d:1:%d ' % (i, i + o.pitch_dim - 1) - print ' %d %d %s ' % \ - ((feat_raw_dim + o.pitch_dim) * (o.delta_order+1) * (o.splice*2+1), (feat_raw_dim + o.pitch_dim) * (o.delta_order+1) * (o.splice*2+1), vector) - print ' %d %d %s %s ' % \ - ((feat_raw_dim + o.pitch_dim) * (o.delta_order+1) * (o.splice*2+1), o.num_pitch_neurons + cnn2_output_dim, '%s/nnet.proto.convolution' % o.dirct, '%s/nnet.proto.pitch' % o.dirct) + print(' %d %d %s ' % \ + ((feat_raw_dim + o.pitch_dim) * (o.delta_order+1) * (o.splice*2+1), (feat_raw_dim + o.pitch_dim) * (o.delta_order+1) * (o.splice*2+1), vector)) + print(' %d %d %s %s ' % \ + ((feat_raw_dim + o.pitch_dim) * (o.delta_order+1) * (o.splice*2+1), o.num_pitch_neurons + cnn2_output_dim, '%s/nnet.proto.convolution' % o.dirct, '%s/nnet.proto.pitch' % o.dirct)) num_convolution_output = o.num_pitch_neurons + cnn2_output_dim else: # no pitch - print convolution_proto + print(convolution_proto) # We are done! sys.exit(0) diff --git a/egs/wsj/s5/utils/nnet/make_cnn_proto.py b/egs/wsj/s5/utils/nnet/make_cnn_proto.py index c6aa519ea96..4d8b9ca2946 100755 --- a/egs/wsj/s5/utils/nnet/make_cnn_proto.py +++ b/egs/wsj/s5/utils/nnet/make_cnn_proto.py @@ -17,6 +17,8 @@ # Generated Nnet prototype, to be initialized by 'nnet-initialize'. +from __future__ import division +from __future__ import print_function import math, random, sys from optparse import OptionParser @@ -88,7 +90,7 @@ ### # Begin the prototype -print "" +print("") # Convolutional part of network num_patch1 = 1 + (feat_raw_dim - o.patch_dim1) / o.patch_step1 @@ -150,13 +152,13 @@ vector += '%d:1:%d ' % (i, i + feat_raw_dim - 1) for i in range(feat_raw_dim+1, inputdim_of_cnn + 1, feat_raw_dim + o.pitch_dim): vector += '%d:1:%d ' % (i, i + o.pitch_dim - 1) - print ' %d %d %s ' % \ - (inputdim_of_cnn, inputdim_of_cnn, vector) - print ' %d %d %s %s ' % \ - (inputdim_of_cnn, o.num_pitch_neurons + outputdim_of_cnn, '%s/nnet.proto.convolution' % o.protodir, '%s/nnet.proto.pitch' % o.protodir) + print(' %d %d %s ' % \ + (inputdim_of_cnn, inputdim_of_cnn, vector)) + print(' %d %d %s %s ' % \ + (inputdim_of_cnn, o.num_pitch_neurons + outputdim_of_cnn, '%s/nnet.proto.convolution' % o.protodir, '%s/nnet.proto.pitch' % o.protodir)) else: # no pitch - print convolution_proto + print(convolution_proto) # We are done! sys.exit(0) diff --git a/egs/wsj/s5/utils/nnet/make_lstm_proto.py b/egs/wsj/s5/utils/nnet/make_lstm_proto.py index a2da0a194fc..6818c860ed0 100755 --- a/egs/wsj/s5/utils/nnet/make_lstm_proto.py +++ b/egs/wsj/s5/utils/nnet/make_lstm_proto.py @@ -17,6 +17,7 @@ # Generated Nnet prototype, to be initialized by 'nnet-initialize'. +from __future__ import print_function import sys ### @@ -52,7 +53,7 @@ parser.print_help() sys.exit(1) -(feat_dim, num_leaves) = map(int,args); +(feat_dim, num_leaves) = [int(i) for i in args]; # Original prototype from Jiayu, # @@ -73,14 +74,14 @@ if None != o.param_stddev: softmax_affine_opts += " %f " % o.param_stddev # The LSTM layers, -print " %d %d %s" % (feat_dim, o.proj_dim, o.cell_dim) + lstm_extra_opts +print(" %d %d %s" % (feat_dim, o.proj_dim, o.cell_dim) + lstm_extra_opts) for l in range(o.num_layers - 1): - print " %d %d %s" % (o.proj_dim, o.proj_dim, o.cell_dim) + lstm_extra_opts + print(" %d %d %s" % (o.proj_dim, o.proj_dim, o.cell_dim) + lstm_extra_opts) # Adding for more stability, -print " %d %d" % (o.proj_dim, o.proj_dim) +print(" %d %d" % (o.proj_dim, o.proj_dim)) # Softmax layer, -print " %d %d 0.0 0.0" % (o.proj_dim, num_leaves) + softmax_affine_opts -print " %d %d" % (num_leaves, num_leaves) +print(" %d %d 0.0 0.0" % (o.proj_dim, num_leaves) + softmax_affine_opts) +print(" %d %d" % (num_leaves, num_leaves)) diff --git a/egs/wsj/s5/utils/nnet/make_nnet_proto.py b/egs/wsj/s5/utils/nnet/make_nnet_proto.py index 99198cbe44b..4f60be6c9d0 100755 --- a/egs/wsj/s5/utils/nnet/make_nnet_proto.py +++ b/egs/wsj/s5/utils/nnet/make_nnet_proto.py @@ -17,6 +17,8 @@ # Generated Nnet prototype, to be initialized by 'nnet-initialize'. +from __future__ import division +from __future__ import print_function import math, random, sys, re ### @@ -87,7 +89,7 @@ o.affine_opts = o.affine_opts.replace("_"," ") o.dropout_opts = o.dropout_opts.replace("_"," ") -(feat_dim, num_leaves, num_hid_layers, num_hid_neurons) = map(int,args); +(feat_dim, num_leaves, num_hid_layers, num_hid_neurons) = [int(i) for i in args]; ### End parse options @@ -120,46 +122,46 @@ def Glorot(dim1, dim2): assert(num_hid_layers == 0) if o.bottleneck_trick: # 25% smaller stddev -> small bottleneck range, 10x smaller learning rate - print " %d %d %f %f" % \ + print(" %d %d %f %f" % \ (feat_dim, o.bottleneck_dim, \ - (o.param_stddev_factor * Glorot(feat_dim, o.bottleneck_dim) * 0.75 ), 0.1) + (o.param_stddev_factor * Glorot(feat_dim, o.bottleneck_dim) * 0.75 ), 0.1)) # 25% smaller stddev -> smaller gradient in prev. layer, 10x smaller learning rate for weigts & biases - print " %d %d %f %f %f %f %f %f" % \ + print(" %d %d %f %f %f %f %f %f" % \ (o.bottleneck_dim, num_hid_neurons, o.hid_bias_mean, o.hid_bias_range, \ - (o.param_stddev_factor * Glorot(o.bottleneck_dim, num_hid_neurons) * 0.75 ), 0.1, 0.1, o.max_norm) + (o.param_stddev_factor * Glorot(o.bottleneck_dim, num_hid_neurons) * 0.75 ), 0.1, 0.1, o.max_norm)) else: - print " %d %d %f" % \ + print(" %d %d %f" % \ (feat_dim, o.bottleneck_dim, \ - (o.param_stddev_factor * Glorot(feat_dim, o.bottleneck_dim))) - print " %d %d %f %f %f %f" % \ + (o.param_stddev_factor * Glorot(feat_dim, o.bottleneck_dim)))) + print(" %d %d %f %f %f %f" % \ (o.bottleneck_dim, num_hid_neurons, o.hid_bias_mean, o.hid_bias_range, \ - (o.param_stddev_factor * Glorot(o.bottleneck_dim, num_hid_neurons)), o.max_norm) - print "%s %d %d %s" % (o.activation_type, num_hid_neurons, num_hid_neurons, o.activation_opts) # Non-linearity + (o.param_stddev_factor * Glorot(o.bottleneck_dim, num_hid_neurons)), o.max_norm)) + print("%s %d %d %s" % (o.activation_type, num_hid_neurons, num_hid_neurons, o.activation_opts)) # Non-linearity # Last AffineTransform (10x smaller learning rate on bias) - print " %d %d %f %f %f %f %f" % \ + print(" %d %d %f %f %f %f %f" % \ (num_hid_neurons, num_leaves, 0.0, 0.0, \ - (o.param_stddev_factor * Glorot(num_hid_neurons, num_leaves)), 1.0, 0.1) + (o.param_stddev_factor * Glorot(num_hid_neurons, num_leaves)), 1.0, 0.1)) # Optionaly append softmax if o.with_softmax: if o.block_softmax_dims == "": - print " %d %d" % (num_leaves, num_leaves) + print(" %d %d" % (num_leaves, num_leaves)) else: - print " %d %d %s" % (num_leaves, num_leaves, o.block_softmax_dims) - print "" + print(" %d %d %s" % (num_leaves, num_leaves, o.block_softmax_dims)) + print("") # We are done! sys.exit(0) # NO HIDDEN LAYERS! # Add only last layer (logistic regression) if num_hid_layers == 0: - print " %d %d %f %f %f" % \ - (feat_dim, num_leaves, 0.0, 0.0, (o.param_stddev_factor * Glorot(feat_dim, num_leaves))) + print(" %d %d %f %f %f" % \ + (feat_dim, num_leaves, 0.0, 0.0, (o.param_stddev_factor * Glorot(feat_dim, num_leaves)))) if o.with_softmax: if o.block_softmax_dims == "": - print " %d %d" % (num_leaves, num_leaves) + print(" %d %d" % (num_leaves, num_leaves)) else: - print " %d %d %s" % (num_leaves, num_leaves, o.block_softmax_dims) - print "" + print(" %d %d %s" % (num_leaves, num_leaves, o.block_softmax_dims)) + print("") # We are done! sys.exit(0) @@ -170,63 +172,63 @@ def Glorot(dim1, dim2): # Begin the prototype, # First AffineTranform, -print " %d %d %f %f %f %f %s" % \ +print(" %d %d %f %f %f %f %s" % \ (feat_dim, num_hid_neurons, o.hid_bias_mean, o.hid_bias_range, \ (o.param_stddev_factor * Glorot(feat_dim, num_hid_neurons) * \ - (math.sqrt(1.0/12.0) if o.smaller_input_weights else 1.0)), o.max_norm, o.affine_opts) + (math.sqrt(1.0/12.0) if o.smaller_input_weights else 1.0)), o.max_norm, o.affine_opts)) # Note.: compensating dynamic range mismatch between input features and Sigmoid-hidden layers, # i.e. mapping the std-dev of N(0,1) (input features) to std-dev of U[0,1] (sigmoid-outputs). # This is done by multiplying with stddev(U[0,1]) = sqrt(1/12). # The stddev of weights is consequently reduced with scale 0.29, -print "%s %d %d %s" % (o.activation_type, num_hid_neurons, num_hid_neurons, o.activation_opts) +print("%s %d %d %s" % (o.activation_type, num_hid_neurons, num_hid_neurons, o.activation_opts)) if o.with_dropout: - print " %d %d %s" % (num_hid_neurons, num_hid_neurons, o.dropout_opts) + print(" %d %d %s" % (num_hid_neurons, num_hid_neurons, o.dropout_opts)) # Internal AffineTransforms, for i in range(num_hid_layers-1): - print " %d %d %f %f %f %f %s" % \ + print(" %d %d %f %f %f %f %s" % \ (num_hid_neurons, num_hid_neurons, o.hid_bias_mean, o.hid_bias_range, \ - (o.param_stddev_factor * Glorot(num_hid_neurons, num_hid_neurons)), o.max_norm, o.affine_opts) - print "%s %d %d %s" % (o.activation_type, num_hid_neurons, num_hid_neurons, o.activation_opts) + (o.param_stddev_factor * Glorot(num_hid_neurons, num_hid_neurons)), o.max_norm, o.affine_opts)) + print("%s %d %d %s" % (o.activation_type, num_hid_neurons, num_hid_neurons, o.activation_opts)) if o.with_dropout: - print " %d %d %s" % (num_hid_neurons, num_hid_neurons, o.dropout_opts) + print(" %d %d %s" % (num_hid_neurons, num_hid_neurons, o.dropout_opts)) # Optionaly add bottleneck, if o.bottleneck_dim != 0: assert(o.bottleneck_dim > 0) if o.bottleneck_trick: # 25% smaller stddev -> small bottleneck range, 10x smaller learning rate - print " %d %d %f %f" % \ + print(" %d %d %f %f" % \ (num_hid_neurons, o.bottleneck_dim, \ - (o.param_stddev_factor * Glorot(num_hid_neurons, o.bottleneck_dim) * 0.75 ), 0.1) + (o.param_stddev_factor * Glorot(num_hid_neurons, o.bottleneck_dim) * 0.75 ), 0.1)) # 25% smaller stddev -> smaller gradient in prev. layer, 10x smaller learning rate for weigts & biases - print " %d %d %f %f %f %f %f %f %s" % \ + print(" %d %d %f %f %f %f %f %f %s" % \ (o.bottleneck_dim, num_hid_neurons, o.hid_bias_mean, o.hid_bias_range, \ - (o.param_stddev_factor * Glorot(o.bottleneck_dim, num_hid_neurons) * 0.75 ), 0.1, 0.1, o.max_norm, o.affine_opts) + (o.param_stddev_factor * Glorot(o.bottleneck_dim, num_hid_neurons) * 0.75 ), 0.1, 0.1, o.max_norm, o.affine_opts)) else: # Same learninig-rate and stddev-formula everywhere, - print " %d %d %f" % \ + print(" %d %d %f" % \ (num_hid_neurons, o.bottleneck_dim, \ - (o.param_stddev_factor * Glorot(num_hid_neurons, o.bottleneck_dim))) - print " %d %d %f %f %f %f %s" % \ + (o.param_stddev_factor * Glorot(num_hid_neurons, o.bottleneck_dim)))) + print(" %d %d %f %f %f %f %s" % \ (o.bottleneck_dim, num_hid_neurons, o.hid_bias_mean, o.hid_bias_range, \ - (o.param_stddev_factor * Glorot(o.bottleneck_dim, num_hid_neurons)), o.max_norm, o.affine_opts) - print "%s %d %d %s" % (o.activation_type, num_hid_neurons, num_hid_neurons, o.activation_opts) + (o.param_stddev_factor * Glorot(o.bottleneck_dim, num_hid_neurons)), o.max_norm, o.affine_opts)) + print("%s %d %d %s" % (o.activation_type, num_hid_neurons, num_hid_neurons, o.activation_opts)) if o.with_dropout: - print " %d %d %s" % (num_hid_neurons, num_hid_neurons, o.dropout_opts) + print(" %d %d %s" % (num_hid_neurons, num_hid_neurons, o.dropout_opts)) # Last AffineTransform (10x smaller learning rate on bias) -print " %d %d %f %f %f %f %f" % \ +print(" %d %d %f %f %f %f %f" % \ (num_hid_neurons, num_leaves, 0.0, 0.0, \ - (o.param_stddev_factor * Glorot(num_hid_neurons, num_leaves)), 1.0, 0.1) + (o.param_stddev_factor * Glorot(num_hid_neurons, num_leaves)), 1.0, 0.1)) # Optionaly append softmax if o.with_softmax: if o.block_softmax_dims == "": - print " %d %d" % (num_leaves, num_leaves) + print(" %d %d" % (num_leaves, num_leaves)) else: - print " %d %d %s" % (num_leaves, num_leaves, o.block_softmax_dims) + print(" %d %d %s" % (num_leaves, num_leaves, o.block_softmax_dims)) # We are done! sys.exit(0) diff --git a/egs/wsj/s5/utils/reverse_arpa.py b/egs/wsj/s5/utils/reverse_arpa.py index 5437aec4341..e154a6e0813 100755 --- a/egs/wsj/s5/utils/reverse_arpa.py +++ b/egs/wsj/s5/utils/reverse_arpa.py @@ -2,11 +2,12 @@ # -*- coding: utf-8 -*- # Copyright 2012 Mirko Hannemann BUT, mirko.hannemann@gmail.com +from __future__ import print_function import sys import codecs # for UTF-8/unicode if len(sys.argv) != 2: - print 'usage: reverse_arpa arpa.in' + print('usage: reverse_arpa arpa.in') sys.exit() arpaname = sys.argv[1] @@ -34,13 +35,13 @@ try: file = codecs.open(arpaname, "r", "utf-8") except IOError: - print 'file not found: ' + arpaname + print('file not found: ' + arpaname) sys.exit() text=file.readline() while (text and text[:6] != "\\data\\"): text=file.readline() if not text: - print "invalid ARPA file" + print("invalid ARPA file") sys.exit() #print text, while (text and text[:5] != "ngram"): text=file.readline() @@ -54,7 +55,7 @@ r = ind[0].split() read_n = int(r[1].strip()) if read_n != n+1: - print "invalid ARPA file:", text + print("invalid ARPA file: {}".format(text)) sys.exit() n = read_n cngrams.append(counts) @@ -68,7 +69,7 @@ for n in range(1,len(cngrams)+1): # unigrams, bigrams, trigrams while (text and "-grams:" not in text): text=file.readline() if n != int(text[1]): - print "invalid ARPA file:", text + print("invalid ARPA file:{}".format(text)) sys.exit() #print text,cngrams[n-1] this_ngrams={} # stores all read ngrams @@ -115,7 +116,7 @@ while (text and text[:5] != "\\end\\"): text=file.readline() if not text: - print "invalid ARPA file" + print("invalid ARPA file") sys.exit() file.close() #print text, @@ -133,14 +134,13 @@ #p(ABCD)+b(ABCD)-p(BCD)+p(ABC)-p(BC)+p(AB)-p(B)+p(A) DCBA 0 # compute new reversed ARPA model -print "\\data\\" +print("\\data\\") for n in range(1,len(cngrams)+1): # unigrams, bigrams, trigrams - print "ngram "+str(n)+"="+str(len(ngrams[n-1].keys())) + print("ngram {0} = {1}".format(n, len(ngrams[n-1].keys()))) offset = 0.0 for n in range(1,len(cngrams)+1): # unigrams, bigrams, trigrams - print "\\"+str(n)+"-grams:" - keys = ngrams[n-1].keys() - keys.sort() + print("\\{}-grams:".format(n)) + keys = sorted(ngrams[n-1].keys()) for ngram in keys: prob = ngrams[n-1][ngram] # reverse word order @@ -179,10 +179,10 @@ elif n == 2: revprob = revprob + offset # add weight to bigrams starting with if (prob[1] != inf): # only backoff weights from not newly created ngrams - print revprob,rev_ngram.encode("utf-8"),back + print(revprob,rev_ngram.encode("utf-8"),back) else: - print revprob,rev_ngram.encode("utf-8"),"-100000.0" + print(revprob,rev_ngram.encode("utf-8"),"-100000.0") else: # highest order - no backoff weights if (n==2) and (rev_ngram[:3] == ""): revprob = revprob + offset - print revprob,rev_ngram.encode("utf-8") -print "\\end\\" + print(revprob,rev_ngram.encode("utf-8")) +print("\\end\\") diff --git a/egs/yomdle_fa/v1/local/create_line_image_from_page_image.py b/egs/yomdle_fa/v1/local/create_line_image_from_page_image.py index 77a6791d5d7..7135bb1b242 100755 --- a/egs/yomdle_fa/v1/local/create_line_image_from_page_image.py +++ b/egs/yomdle_fa/v1/local/create_line_image_from_page_image.py @@ -110,7 +110,7 @@ def bounding_area(index, hull): return {'area': len_p * len_o, 'length_parallel': len_p, 'length_orthogonal': len_o, - 'rectangle_center': (min_p + len_p / 2, min_o + len_o / 2), + 'rectangle_center': (min_p + float(len_p) / 2, min_o + float(len_o) / 2), 'unit_vector': unit_vector_p, } @@ -275,8 +275,8 @@ def get_center(im): ------- (int, int): center of the image """ - center_x = im.size[0] / 2 - center_y = im.size[1] / 2 + center_x = float(im.size[0]) / 2 + center_y = float(im.size[1]) / 2 return int(center_x), int(center_y) diff --git a/egs/yomdle_fa/v1/local/gedi2csv.py b/egs/yomdle_fa/v1/local/gedi2csv.py index 43a07421dd1..0b80c2e80bb 100755 --- a/egs/yomdle_fa/v1/local/gedi2csv.py +++ b/egs/yomdle_fa/v1/local/gedi2csv.py @@ -55,7 +55,7 @@ def npbox2string(npar): # cv2.minAreaRect() returns a Box2D structure which contains following detals - ( center (x,y), (width, height), angle of rotation ) # Get 4 corners of the rectangle using cv2.boxPoints() -class GEDI2CSV(): +class GEDI2CSV(object): """ Initialize the extractor""" def __init__(self, logger, args): diff --git a/egs/yomdle_fa/v1/local/yomdle2csv.py b/egs/yomdle_fa/v1/local/yomdle2csv.py index 3641de90324..8f208e2d968 100755 --- a/egs/yomdle_fa/v1/local/yomdle2csv.py +++ b/egs/yomdle_fa/v1/local/yomdle2csv.py @@ -55,7 +55,7 @@ def npbox2string(npar): # cv2.minAreaRect() returns a Box2D structure which contains following detals - ( center (x,y), (width, height), angle of rotation ) # Get 4 corners of the rectangle using cv2.boxPoints() -class GEDI2CSV(): +class GEDI2CSV(object): """ Initialize the extractor""" def __init__(self, logger, args): diff --git a/egs/yomdle_tamil/v1/local/yomdle/create_line_image_from_page_image.py b/egs/yomdle_tamil/v1/local/yomdle/create_line_image_from_page_image.py index dd4bf536692..885f18c7deb 100755 --- a/egs/yomdle_tamil/v1/local/yomdle/create_line_image_from_page_image.py +++ b/egs/yomdle_tamil/v1/local/yomdle/create_line_image_from_page_image.py @@ -116,7 +116,7 @@ def bounding_area(index, hull): return {'area': len_p * len_o, 'length_parallel': len_p, 'length_orthogonal': len_o, - 'rectangle_center': (min_p + len_p / 2, min_o + len_o / 2), + 'rectangle_center': (min_p + float(len_p) / 2, min_o + float(len_o) / 2), 'unit_vector': unit_vector_p, } @@ -221,8 +221,8 @@ def get_center(im): ------- (int, int): center of the image """ - center_x = im.size[0] / 2 - center_y = im.size[1] / 2 + center_x = float(im.size[0]) / 2 + center_y = float(im.size[1]) / 2 return int(center_x), int(center_y) diff --git a/egs/yomdle_tamil/v1/local/yomdle/gedi2csv_enriched.py b/egs/yomdle_tamil/v1/local/yomdle/gedi2csv_enriched.py index 1c9ab618a78..51d7a34e7e8 100755 --- a/egs/yomdle_tamil/v1/local/yomdle/gedi2csv_enriched.py +++ b/egs/yomdle_tamil/v1/local/yomdle/gedi2csv_enriched.py @@ -39,7 +39,7 @@ def npbox2string(npar): # cv2.minAreaRect() returns a Box2D structure which contains following detals - ( center (x,y), (width, height), angle of rotation ) # Get 4 corners of the rectangle using cv2.boxPoints() -class GEDI2CSV(): +class GEDI2CSV(object): ''' Initialize the extractor''' def __init__(self, logger, args): self._logger = logger diff --git a/egs/yomdle_tamil/v1/local/yomdle/yomdle2csv.py b/egs/yomdle_tamil/v1/local/yomdle/yomdle2csv.py index 49fc41aa5cc..d75b8bcbe8b 100755 --- a/egs/yomdle_tamil/v1/local/yomdle/yomdle2csv.py +++ b/egs/yomdle_tamil/v1/local/yomdle/yomdle2csv.py @@ -43,7 +43,7 @@ def npbox2string(npar): # cv2.minAreaRect() returns a Box2D structure which contains following detals - ( center (x,y), (width, height), angle of rotation ) # Get 4 corners of the rectangle using cv2.boxPoints() -class GEDI2CSV(): +class GEDI2CSV(object): ''' Initialize the extractor''' def __init__(self, logger, args): diff --git a/egs/yomdle_zh/v1/local/create_line_image_from_page_image.py b/egs/yomdle_zh/v1/local/create_line_image_from_page_image.py index 77a6791d5d7..7135bb1b242 100755 --- a/egs/yomdle_zh/v1/local/create_line_image_from_page_image.py +++ b/egs/yomdle_zh/v1/local/create_line_image_from_page_image.py @@ -110,7 +110,7 @@ def bounding_area(index, hull): return {'area': len_p * len_o, 'length_parallel': len_p, 'length_orthogonal': len_o, - 'rectangle_center': (min_p + len_p / 2, min_o + len_o / 2), + 'rectangle_center': (min_p + float(len_p) / 2, min_o + float(len_o) / 2), 'unit_vector': unit_vector_p, } @@ -275,8 +275,8 @@ def get_center(im): ------- (int, int): center of the image """ - center_x = im.size[0] / 2 - center_y = im.size[1] / 2 + center_x = float(im.size[0]) / 2 + center_y = float(im.size[1]) / 2 return int(center_x), int(center_y) diff --git a/egs/yomdle_zh/v1/local/gedi2csv.py b/egs/yomdle_zh/v1/local/gedi2csv.py index 43a07421dd1..0b80c2e80bb 100755 --- a/egs/yomdle_zh/v1/local/gedi2csv.py +++ b/egs/yomdle_zh/v1/local/gedi2csv.py @@ -55,7 +55,7 @@ def npbox2string(npar): # cv2.minAreaRect() returns a Box2D structure which contains following detals - ( center (x,y), (width, height), angle of rotation ) # Get 4 corners of the rectangle using cv2.boxPoints() -class GEDI2CSV(): +class GEDI2CSV(object): """ Initialize the extractor""" def __init__(self, logger, args): diff --git a/egs/yomdle_zh/v1/local/yomdle2csv.py b/egs/yomdle_zh/v1/local/yomdle2csv.py index 3641de90324..8f208e2d968 100755 --- a/egs/yomdle_zh/v1/local/yomdle2csv.py +++ b/egs/yomdle_zh/v1/local/yomdle2csv.py @@ -55,7 +55,7 @@ def npbox2string(npar): # cv2.minAreaRect() returns a Box2D structure which contains following detals - ( center (x,y), (width, height), angle of rotation ) # Get 4 corners of the rectangle using cv2.boxPoints() -class GEDI2CSV(): +class GEDI2CSV(object): """ Initialize the extractor""" def __init__(self, logger, args): diff --git a/misc/maintenance/cpplint.py b/misc/maintenance/cpplint.py index 03d0569ab1c..91658705f41 100755 --- a/misc/maintenance/cpplint.py +++ b/misc/maintenance/cpplint.py @@ -83,6 +83,7 @@ We do a small hack, which is to ignore //'s with "'s after them on the same line, but it is far from perfect (in either direction). """ +from __future__ import division import codecs import getopt @@ -564,7 +565,7 @@ def IncrementErrorCount(self, category): def PrintErrorCounts(self): """Print a summary of errors by category, and the total.""" - for category, count in self.errors_by_category.iteritems(): + for category, count in self.errors_by_category.items(): sys.stderr.write('Category \'%s\' errors found: %d\n' % (category, count)) sys.stderr.write('Total errors found: %d\n' % self.error_count) @@ -656,7 +657,7 @@ def Check(self, error, filename, linenum): trigger = base_trigger * 2**_VerboseLevel() if self.lines_in_function > trigger: - error_level = int(math.log(self.lines_in_function / base_trigger, 2)) + error_level = int(math.log(float(self.lines_in_function) / base_trigger, 2)) # 50 => 0, 100 => 1, 200 => 2, 400 => 3, 800 => 4, 1600 => 5, ... if error_level > 5: error_level = 5 @@ -676,7 +677,7 @@ class _IncludeError(Exception): pass -class FileInfo: +class FileInfo(object): """Provides utility functions for filenames. FileInfo provides easy access to the components of a file's path @@ -1012,7 +1013,7 @@ def CheckForCopyright(filename, lines, error): # We'll say it should occur by line 10. Don't forget there's a # dummy line at the front. - for line in xrange(1, min(len(lines), 11)): + for line in range(1, min(len(lines), 11)): if re.search(r'Copyright', lines[line], re.I): break else: # means no copyright line was found error(filename, 0, 'legal/copyright', 5, @@ -1604,7 +1605,7 @@ def CheckForFunctionLengths(filename, clean_lines, linenum, if starting_func: body_found = False - for start_linenum in xrange(linenum, clean_lines.NumLines()): + for start_linenum in range(linenum, clean_lines.NumLines()): start_line = lines[start_linenum] joined_line += ' ' + start_line.lstrip() if Search(r'(;|})', start_line): # Declarations and trivial functions @@ -2073,7 +2074,7 @@ def GetLineWidth(line): The width of the line in column positions, accounting for Unicode combining characters and wide characters. """ - if isinstance(line, unicode): + if isinstance(line, str): width = 0 for c in unicodedata.normalize('NFC', line): if unicodedata.east_asian_width(c) in ('W', 'F'): @@ -2861,7 +2862,7 @@ def CheckForIncludeWhatYouUse(filename, clean_lines, include_state, error, required = {} # A map of header name to linenumber and the template entity. # Example of required: { '': (1219, 'less<>') } - for linenum in xrange(clean_lines.NumLines()): + for linenum in range(clean_lines.NumLines()): line = clean_lines.elided[linenum] if not line or line[0] == '#': continue @@ -2994,7 +2995,7 @@ def ProcessFileData(filename, file_extension, lines, error): RemoveMultiLineComments(filename, lines, error) clean_lines = CleansedLines(lines) - for line in xrange(clean_lines.NumLines()): + for line in range(clean_lines.NumLines()): ProcessLine(filename, file_extension, clean_lines, line, include_state, function_state, class_state, error) class_state.CheckFinished(filename, error) diff --git a/scripts/rnnlm/get_embedding_dim.py b/scripts/rnnlm/get_embedding_dim.py index a5ddb8c25f3..63eaf307498 100755 --- a/scripts/rnnlm/get_embedding_dim.py +++ b/scripts/rnnlm/get_embedding_dim.py @@ -101,4 +101,4 @@ "nnet '{0}': {1} != {2}".format( args.nnet, input_dim, output_dim)) -print(str(input_dim)) +print('{}'.format(input_dim))