⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 libs-predictor.c

📁 Language, Script, and Encoding Identification with String Kernel Classifiers
💻 C
字号:
/*** Copyright (C) 2006 Thai Computational Linguistics Laboratory (TCL)** National Institute of Information and Communications Technology (NICT)** Canasai Kruengkrai <canasai xx gmail yy com, where xx=at and yy=dot>**** This file is part of the `libs' library.**** This library is free software; you can redistribute it and/or modify** it under the terms of the GNU General Public License as published by** the Free Software Foundation; either version 2 of the License, or** (at your option) any later version.**** This program is distributed in the hope that it will be useful,** but WITHOUT ANY WARRANTY; without even the implied warranty of** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the** GNU General Public License for more details.**** You should have received a copy of the GNU General Public License** along with this program; if not, write to the Free Software** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/#include <stdio.h>#include <ctype.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include "libsvm-string-2.71/svm.h"#define file_error( msg )   fprintf( stderr, "\nFILE ERROR: Could not open the file: %s\n", msg ), exit( 0 )#define fatal_error( msg )  fprintf( stderr, "\n%s\n", msg ), exit( 0 )#define Malloc( type, n ) ( type * )malloc( ( n )*sizeof( type ) )void exit_with_help( char *prog_name ){    fprintf( stderr, "\nUsage:"                     "\n------"                     "\n%s [options] model_file test_file result_file"                     "\noptions:"                     "\n    -m multi-class classification method"                     "\n        0 -> one-against-one"                     "\n        1 -> DAGSVM"                     "\n",                     prog_name );    exit( 0 );}void predict( svm_model *model,              char *test_file,              char *result_file,              int mc_method ){    int correct = 0;    int total = 0;    double error = 0;    int num_tokens   = 0;    int num_lines    = 0;    int longest_line = 0;    text_scan( test_file, &num_tokens, &num_lines, &longest_line );    char *line = Malloc( char, longest_line );    FILE *file_ptr = fopen( test_file, "r" );    FILE *file_ptr2 = fopen( result_file, "w" );    while( fgets( line, longest_line, file_ptr ) != NULL )    {        if( text_not_blank( line ) )        {            line[ strlen( line ) - 1 ] = '\0';            char *tmp_ptr = text_copy2( line );            char *tmp_ptr2 = tmp_ptr;            tmp_ptr2 = strchr( tmp_ptr2, ' ' );            if( tmp_ptr2 == NULL )                fatal_error( "ERROR: In function `predict': Every sample must have a class label" );            *tmp_ptr2 = '\0';            ++tmp_ptr2;            double label = atof( tmp_ptr );            char *string = text_copy( tmp_ptr2 );            //fprintf( stderr, "input string: %g \"%s\"\n", label, string );            int str_length = strlen( string );            struct svm_node *x = Malloc( struct svm_node, str_length+10 );            int i;            for( i = 0; i < str_length; i++ )            {                x[i].index = i;                x[i].value = ( unsigned char )string[i];            }            x[i++].index = -1;            // predict this sample            double v = -1;            if( mc_method == ONE_AGAINST_ONE )                v = svm_predict( model, x );            else if( mc_method == DAGSVM )                v = dagsvm_predict( model, x );            fprintf( stderr, "%g\n", v );            fprintf( file_ptr2, "%g\n", v );            if( v == label )                ++correct;            error += ( v - label ) * ( v - label );            ++total;            free( x );            free( string );             free( tmp_ptr );         }    }    fprintf( file_ptr2, "Accuracy = %g%% (%d/%d) (classification)\n", ( double )correct/total*100, correct, total );    fprintf( stderr, "Accuracy = %g%% (%d/%d) (classification)\n", ( double )correct/total*100, correct, total );    fclose( file_ptr );    fclose( file_ptr2 );}void run( int argc, char **argv ){    int i, j;    int mc_method = 0;    // parse options    for( i = 1; i < argc; i++ )    {        if( argv[i][0] != '-')            break;        ++i;        switch( argv[i-1][1] )        {            case 'm':                mc_method = atoi( argv[i] );                if( mc_method != 0 && mc_method != 1 )                {                    fprintf( stderr, "unknown multi-class classification method: mc_method != 0 and mc_method != 1\n" );                    exit_with_help( argv[0] );                }                break;            default:                fprintf( stderr, "unknown option\n" );                exit_with_help( argv[0] );        }    }    for( j = i; j < argc-1; j++ )    {        if( access( argv[j], R_OK ) != 0 )            file_error( argv[j] );    }    fprintf( stderr, "mc_method    = %d\n", mc_method );    fprintf( stderr, "model_file   = %s\n", argv[i] );    fprintf( stderr, "test_file    = %s\n", argv[i+1] );    fprintf( stderr, "result_file = %s\n", argv[i+2] );    svm_model *model = svm_load_model( argv[i] );    predict( model, argv[i+1], argv[i+2], mc_method );    svm_destroy_model( model );}int main( int argc, char **argv ){    if( argc < 4 )        exit_with_help( argv[0] );    run( argc, argv );    return( 0 );}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -