Esempio n. 1
0
    def __log_str(self, model_file=None):
        '''
        ClinerModel::__log_str()
        Build a string of information about training for the model's log file.
        @param model_file.  A path to optionally identify where the model was saved.
        @return  A string of the model's training information
        '''

        assert self._is_trained, 'ClinerModel not trained'
        with io.StringIO() as f:
            write(f, u'\n')
            write(f, '-' * 40)
            write(f, u'\n\n')
            if model_file:
                write(f, 'model    : %s\n' % os.path.abspath(model_file))
                write(f, u'\n')

            if self._use_lstm:
                write(f, u'modeltype: LSTM\n')
            else:
                write(f, u'modeltype: CRF\n')

            if 'hyperparams' in self._score:
                for name, value in self._score['hyperparams'].items():
                    write(f, u'\t%-10s: %s\n' % (name, value))
            write(f, u'\n')

            print_str(f, 'features', self._features)
            write(f, u'\n')

            write(f, u'\n')
            write(f, 'training began: %s\n' % self._time_train_begin)
            write(f, 'training ended: %s\n' % self._time_train_end)
            write(f, u'\n')

            write(f, u'scores\n')
            print_vec(f, 'train precision', self._score['train']['precision'])
            print_vec(f, 'train recall   ', self._score['train']['recall'])
            print_vec(f, 'train f1       ', self._score['train']['f1'])
            write(f, self._score['train']['conf'])

            if 'dev' in self._score:
                print_vec(f, u'dev precision   ',
                          self._score['dev']['precision'])
                print_vec(f, u'dev recall      ', self._score['dev']['recall'])
                print_vec(f, u'dev f1          ', self._score['dev']['f1'])
                write(f, self._score['dev']['conf'])

            if 'test' in self._score:
                print_vec(f, u'test precision   ',
                          self._score['test']['precision'])
                print_vec(f, u'test recall      ',
                          self._score['test']['recall'])
                print_vec(f, u'test f1          ', self._score['test']['f1'])
                write(f, self._score['test']['conf'])

            if 'history' in self._score:
                for label, vec in self._score['history'].items():
                    print_vec(f, '%-16s' % label, vec)
                write(f, u'\n')

            if self._training_files:
                write(f, u'\n')
                write(f, u'Training Files\n')
                if len(self._training_files) < 200:
                    print_files(f, self._training_files)
                else:
                    write(f, '\t%d files\n' % len(self._training_files))
                write(f, u'\n')

            write(f, u'-' * 40)
            write(f, u'\n\n')

            # get output as full string
            contents = f.getvalue()
        return contents
Esempio n. 2
0
# Use the `.split('>')` list method to get the infiles and outfiles
def check_input(input_string):
    "Returns the position of a delimiter in a string"
    position = 0
    for string in '>':
        if string == '>':
            return position  # what?  call combine files and set to a new file?
        else:
            position += 1

    return None


# TODO:  Add a flag for this behavior; fix it to take mult. files.
def add_line_num(infile):
    "Print the contents of a file with line numbers added."
    with open(infile) as f:
        iter = 1
        for line in f.readlines():
            print(f"{iter}\t{line}", end="")
            iter += 1


if __name__ == "__main__":
    output = tools.print_files(argv)
    print(output)

    add_line_num(
        argv[1:])  # not working - needs to take a str, bytes or file obj