def from_tags(cls, tags_file: str, tags_folder: str) -> 'TrainData':
        """
        Load a set of data from a text file with tags in the following format:
            <file_id>  (tab)  <tag>
            <file_id>  (tab)  <tag>

            file_id: identifier of file such that the following
                     file exists: {tags_folder}/{data_id}.wav
            tag: "wake-word" or "not-wake-word"
        """
        if not tags_file:
            num_ignored_wavs = len(glob(join(tags_folder, '*.wav')))
            if num_ignored_wavs > 10:
                print('WARNING: Found {} wavs but no tags file specified!'.format(num_ignored_wavs))
            return cls(([], []), ([], []))
        if not isfile(tags_file):
            raise RuntimeError('Database file does not exist: ' + tags_file)

        train_groups = {}
        train_group_file = join(tags_file.replace('.txt', '') + '.groups.json')
        if isfile(train_group_file):
            try:
                with open(train_group_file) as f:
                    train_groups = json.load(f)
            except ValueError:
                pass

        tags_files = {
            'wake-word': [],
            'not-wake-word': []
        }
        with open(tags_file) as f:
            for line in f.read().split('\n'):
                if not line:
                    continue
                file, tag = line.split('\t')
                tags_files[tag.strip()].append(join(tags_folder, file.strip() + '.wav'))

        train_files, test_files = ([], []), ([], [])
        for label, rows in enumerate([tags_files['wake-word'], tags_files['not-wake-word']]):
            for fn in rows:
                if not isfile(fn):
                    print('Missing file:', fn)
                    continue
                if fn not in train_groups:
                    train_groups[fn] = (
                        'test' if md5(fn.encode('utf8')).hexdigest() > 'c' * 32
                        else 'train'
                    )
                {
                    'train': train_files,
                    'test': test_files
                }[train_groups[fn]][label].append(fn)

        with open(train_group_file, 'w') as f:
            json.dump(train_groups, f)

        return cls(train_files, test_files)
def from_tags ( cls , tags_file : str , tags_folder : str ) -> 'TrainData' : if not tags_file : num_ignored_wavs = len ( glob ( join ( tags_folder , '*.wav' ) ) ) if num_ignored_wavs > 10 : print ( 'WARNING: Found {} wavs but no tags file specified!' . format ( num_ignored_wavs ) ) return cls ( ( [ ] , [ ] ) , ( [ ] , [ ] ) ) if not isfile ( tags_file ) : raise RuntimeError ( 'Database file does not exist: ' + tags_file ) train_groups = { } train_group_file = join ( tags_file . replace ( '.txt' , '' ) + '.groups.json' ) if isfile ( train_group_file ) : try : with open ( train_group_file ) as f : train_groups = json . load ( f ) except ValueError : pass tags_files = { 'wake-word' : [ ] , 'not-wake-word' : [ ] } with open ( tags_file ) as f : for line in f . read ( ) . split ( '\n' ) : if not line : continue file , tag = line . split ( '\t' ) tags_files [ tag . strip ( ) ] . append ( join ( tags_folder , file . strip ( ) + '.wav' ) ) train_files , test_files = ( [ ] , [ ] ) , ( [ ] , [ ] ) for label , rows in enumerate ( [ tags_files [ 'wake-word' ] , tags_files [ 'not-wake-word' ] ] ) : for fn in rows : if not isfile ( fn ) : print ( 'Missing file:' , fn ) continue if fn not in train_groups : train_groups [ fn ] = ( 'test' if md5 ( fn . encode ( 'utf8' ) ) . hexdigest ( ) > 'c' * 32 else 'train' ) { 'train' : train_files , 'test' : test_files } [ train_groups [ fn ] ] [ label ] . append ( fn ) with open ( train_group_file , 'w' ) as f : json . dump ( train_groups , f ) return cls ( train_files , test_files )
Load a set of data from a text file with tags in the following format:
            <file_id>  (tab)  <tag>
            <file_id>  (tab)  <tag>

            file_id: identifier of file such that the following
                     file exists: {tags_folder}/{data_id}.wav
            tag: "wake-word" or "not-wake-word"