T2TUtility.cpp 3.11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/* NiuTrans.Tensor - an open-source tensor library
 * Copyright (C) 2018, Natural Language Processing Lab, Northestern University. 
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * $Created by: XIAO Tong (xiaotong@mail.neu.edu.cn) 2018-07-31
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

namespace transformer
{

xiaotong committed
29
FILE * tmpFILE;
xiaotong committed
30 31
int llnum = 0;
FILE * tf = NULL;
xiaotong committed
32

33
void LoadParamString(int argc, char ** argv, const char * name, char * p, const char * defaultP)
34 35 36 37 38 39 40
{
    char vname[128];
    vname[0] = '-';
    strcpy(vname + 1, name);
    bool hit = false;
    for(int i = 0; i < argc; i++){
        if(!strcmp(argv[i], vname) && i + 1 < argc){
41 42
            strcpy(p, argv[i + 1]);
            //fprintf(stderr, " %s=%s\n", name, argv[i + 1]);
43 44 45 46 47 48 49
            hit = true;
        }
    }
    if(!hit)
        strcpy(p, defaultP);
}

50
void LoadParamInt(int argc, char ** argv, const char * name, int * p, int defaultP)
51 52 53 54 55 56 57 58
{
    char vname[128];
    vname[0] = '-';
    strcpy(vname + 1, name);
    bool hit = false;
    for(int i = 0; i < argc; i++){
        if(!strcmp(argv[i], vname) && i + 1 < argc){
            *(int*)p = atoi(argv[i + 1]);
59
            //fprintf(stderr, " %s=%s\n", name, argv[i + 1]);
60 61 62 63 64 65 66
            hit = true;
        }
    }
    if(!hit)
        *p = defaultP;
}

67
void LoadParamBool(int argc, char ** argv, const char * name, bool * p, bool defaultP)
68 69 70 71 72 73 74 75
{
    char vname[128];
    vname[0] = '-';
    strcpy(vname + 1, name);
    bool hit = false;
    for(int i = 0; i < argc; i++){
        if(!strcmp(argv[i], vname)){
            *(bool*)p = true;
76 77
            //fprintf(stderr, " %s=%s\n", name, "true");
            hit = true;
78 79 80 81 82 83
        }
    }
    if(!hit)
        *p = defaultP;
}

84
void LoadParamFloat(int argc, char ** argv, const char * name, float * p, float defaultP)
85 86 87 88 89 90 91
{
    char vname[128];
    vname[0] = '-';
    strcpy(vname + 1, name);
    bool hit = false;
    for(int i = 0; i < argc; i++){
        if(!strcmp(argv[i], vname) && i + 1 < argc){
92 93 94
            *p = (float)atof(argv[i + 1]);
            //fprintf(stderr, " %s=%s\n", name, argv[i + 1]);
            hit = true;
95 96 97 98 99 100
        }
    }
    if(!hit)
        *p = defaultP;
}

101
void ShowParams(int argc, char ** argv)
102 103 104
{
    fprintf(stderr, "args:\n");
    for(int i = 0; i < argc; i++){
xiaotong committed
105 106 107
        if(argv[i][1] == 0)
            continue;
        if(argv[i][0] == '-' && (argv[i][1] < '1' || argv[i][1] > '9')){
108 109 110 111 112 113 114 115 116
            if(i + 1 < argc && argv[i + 1][0] != '-')
                fprintf(stderr, " %s=%s\n", argv[i], argv[i + 1]);
            else
                fprintf(stderr, " %s=yes\n", argv[i]);
        }
    }
    fprintf(stderr, "\n");
}

117
}