From 3b06ff9ed22910b45df0f7a484de9ecee8207809 Mon Sep 17 00:00:00 2001 From: StaffanArvidsson Date: Mon, 10 Oct 2016 15:52:03 +0200 Subject: [PATCH 1/2] added svm_print_err_string instead of writing directly to System.err --- java/libsvm/svm.java | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/java/libsvm/svm.java b/java/libsvm/svm.java index 442bbce1..cd9b1f2c 100644 --- a/java/libsvm/svm.java +++ b/java/libsvm/svm.java @@ -1304,8 +1304,18 @@ public void print(String s) System.out.flush(); } }; + + private static svm_print_interface svm_print_stderr = new svm_print_interface() { + + public void print(String s) + { + System.err.print(s); + System.err.flush(); + } + }; private static svm_print_interface svm_print_string = svm_print_stdout; + private static svm_print_interface svm_print_err_string = svm_print_stderr; static void info(String s) { @@ -2015,7 +2025,7 @@ public static svm_model svm_train(svm_problem prob, svm_parameter param) if(param.weight_label[i] == label[j]) break; if(j == nr_class) - System.err.print("WARNING: class label "+param.weight_label[i]+" specified in weight is not found\n"); + svm_print_err_string.print("WARNING: class label "+param.weight_label[i]+" specified in weight is not found\n"); else weighted_C[j] *= param.weight[i]; } @@ -2315,7 +2325,7 @@ public static double svm_get_svr_probability(svm_model model) return model.probA[0]; else { - System.err.print("Model doesn't contain information for SVR probability inference\n"); + svm_print_err_string.print("Model doesn't contain information for SVR probability inference\n"); return 0; } } @@ -2569,7 +2579,7 @@ private static boolean read_model_header(BufferedReader fp, svm_model model) } if(i == svm_type_table.length) { - System.err.print("unknown svm type.\n"); + svm_print_err_string.print("unknown svm type.\n"); return false; } } @@ -2586,7 +2596,7 @@ else if(cmd.startsWith("kernel_type")) } if(i == kernel_type_table.length) { - System.err.print("unknown kernel function.\n"); + svm_print_err_string.print("unknown kernel function.\n"); return false; } } @@ -2646,7 +2656,7 @@ else if(cmd.startsWith("SV")) } else { - System.err.print("unknown text in model file: ["+cmd+"]\n"); + svm_print_err_string.print("unknown text in model file: ["+cmd+"]\n"); return false; } } @@ -2676,8 +2686,7 @@ public static svm_model svm_load_model(BufferedReader fp) throws IOException if (read_model_header(fp, model) == false) { - System.err.print("ERROR: failed to read model\n"); - return null; + throw new IOException("ERROR: failed to read model\n"); } // read sv_coef and SV From a9b57f2d3fcaf7797f3ef10c80802a505f8e103c Mon Sep 17 00:00:00 2001 From: StaffanArvidsson Date: Mon, 10 Oct 2016 16:25:21 +0200 Subject: [PATCH 2/2] add setter for print_err_string_function --- java/libsvm/svm.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/java/libsvm/svm.java b/java/libsvm/svm.java index cd9b1f2c..444d6b3a 100644 --- a/java/libsvm/svm.java +++ b/java/libsvm/svm.java @@ -2686,7 +2686,7 @@ public static svm_model svm_load_model(BufferedReader fp) throws IOException if (read_model_header(fp, model) == false) { - throw new IOException("ERROR: failed to read model\n"); + throw new IOException("ERROR: failed to read model"); } // read sv_coef and SV @@ -2855,4 +2855,10 @@ public static void svm_set_print_string_function(svm_print_interface print_func) else svm_print_string = print_func; } + + public static void svm_set_print_err_string_function(svm_print_interface print_func) + { + if (print_func != null) + svm_print_err_string = print_func; + } }