From efc8a852524cb8552088b2b4dbe248f1b058ebcd Mon Sep 17 00:00:00 2001 From: samuel_d_jack Date: Thu, 29 Sep 2011 15:24:38 +0100 Subject: [PATCH 1/2] Implemented the LoadModel function --- .gitignore | 2 + LibSvm/JavaPorts/StringTokenizer.cs | 34 ++++ LibSvm/LibSvm.csproj | 1 + LibSvm/Svm.cs | 254 +++++++++++++--------------- 4 files changed, 153 insertions(+), 138 deletions(-) create mode 100644 LibSvm/JavaPorts/StringTokenizer.cs diff --git a/.gitignore b/.gitignore index 9e06052..8f0c1e6 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ obj Release Debug + +_ReSharper* \ No newline at end of file diff --git a/LibSvm/JavaPorts/StringTokenizer.cs b/LibSvm/JavaPorts/StringTokenizer.cs new file mode 100644 index 0000000..3ff4824 --- /dev/null +++ b/LibSvm/JavaPorts/StringTokenizer.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace LibSvm.JavaPorts +{ + class StringTokenizer + { + private readonly IList _tokens; + private int currentToken = -1; + private static readonly char[] DefaultDelimeters = new[] {' ', '\t', '\n', '\r', '\f',}; + + public StringTokenizer(string untokenizedString) : this(untokenizedString, DefaultDelimeters) + { + } + + public StringTokenizer(string untokenizedString, char[] delimiters) + { + _tokens = untokenizedString.Split(delimiters, StringSplitOptions.RemoveEmptyEntries); + } + + public string NextToken() + { + currentToken++; + return _tokens[currentToken]; + } + + public int CountTokens() + { + return _tokens.Count - currentToken; + } + } +} diff --git a/LibSvm/LibSvm.csproj b/LibSvm/LibSvm.csproj index f332d83..fd291e0 100644 --- a/LibSvm/LibSvm.csproj +++ b/LibSvm/LibSvm.csproj @@ -44,6 +44,7 @@ + diff --git a/LibSvm/Svm.cs b/LibSvm/Svm.cs index 4098f50..df256e5 100644 --- a/LibSvm/Svm.cs +++ b/LibSvm/Svm.cs @@ -1,7 +1,10 @@ using System; using System.Collections.Generic; +using System.Diagnostics; +using System.IO; using System.Linq; using System.Text; +using LibSvm.JavaPorts; namespace LibSvm { @@ -1093,152 +1096,127 @@ private static int atoi(string s) // return svm_load_model(new BufferedReader(new FileReader(model_file_name))); //} - //implement later - //public static svm_model svm_load_model(BufferedReader fp) - //{ - // // read parameters - - // svm_model model = new svm_model(); - // svm_parameter param = new svm_parameter(); - // model.param = param; - // model.rho = null; - // model.probA = null; - // model.probB = null; - // model.label = null; - // model.nSV = null; - - // while(true) - // { - // String cmd = fp.readLine(); - // String arg = cmd.substring(cmd.indexOf(' ')+1); - - // if(cmd.startsWith("svm_type")) - // { - // int i; - // for(i=0;i Date: Wed, 25 Jul 2012 16:36:15 +0100 Subject: [PATCH 2/2] Implemented SaveModel method --- LibSvm/Svm.cs | 174 +++++++++++++++++++++++++------------------------- 1 file changed, 87 insertions(+), 87 deletions(-) diff --git a/LibSvm/Svm.cs b/LibSvm/Svm.cs index df256e5..da8490c 100644 --- a/LibSvm/Svm.cs +++ b/LibSvm/Svm.cs @@ -991,94 +991,94 @@ public static void CrossValidation(SvmProblem prob, SvmParameter param, int nr_f } } - //static readonly string[] svm_type_table = { "c_svc", "nu_svc", "one_class", "epsilon_svr", "nu_svr", }; - //static readonly string[] kernel_type_table = { "linear", "polynomial", "rbf", "sigmoid", "precomputed" }; + static readonly string[] svm_type_table = { "c_svc", "nu_svc", "one_class", "epsilon_svr", "nu_svr", }; + static readonly string[] kernel_type_table = { "linear", "polynomial", "rbf", "sigmoid", "precomputed" }; - //implement later - //public static void svm_save_model(String model_file_name, svm_model model) - //{ - // DataOutputStream fp = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(model_file_name))); - - // svm_parameter param = model.param; - - // fp.writeBytes("svm_type "+svm_type_table[param.svm_type]+"\n"); - // fp.writeBytes("kernel_type "+kernel_type_table[param.kernel_type]+"\n"); - - // if(param.kernel_type == svm_parameter.POLY) - // fp.writeBytes("degree "+param.degree+"\n"); - - // if(param.kernel_type == svm_parameter.POLY || - // param.kernel_type == svm_parameter.RBF || - // param.kernel_type == svm_parameter.SIGMOID) - // fp.writeBytes("gamma "+param.gamma+"\n"); - - // if(param.kernel_type == svm_parameter.POLY || - // param.kernel_type == svm_parameter.SIGMOID) - // fp.writeBytes("coef0 "+param.coef0+"\n"); - - // int nr_class = model.nr_class; - // int l = model.l; - // fp.writeBytes("nr_class "+nr_class+"\n"); - // fp.writeBytes("total_sv "+l+"\n"); - - // { - // fp.writeBytes("rho"); - // for(int i=0;i