Example #1
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private void loadModel(java.io.Reader inputReader) throws java.io.IOException
        private void loadModel(Reader inputReader)
        {
            labels = null;
            Pattern      whitespace = Pattern.compile("\\s+");
            StreamReader reader     = null;

            if (inputReader is StreamReader)
            {
                reader = (StreamReader)inputReader;
            }
            else
            {
                reader = new StreamReader(inputReader);
            }

            try
            {
                string line = null;
                while (!ReferenceEquals((line = reader.ReadLine()), null))
                {
                    string[] split = whitespace.split(line);
                    if (split[0].Equals("solver_type"))
                    {
                        SolverType solver = SolverType.valueOf(split[1]);
                        if (solver == null)
                        {
                            throw new Exception("unknown solver type");
                        }
                        solverType = solver;
                    }
                    else if (split[0].Equals("nr_class"))
                    {
                        nr_class = Util.atoi(split[1]);
                        int.Parse(split[1]);
                    }
                    else if (split[0].Equals("nr_feature"))
                    {
                        nr_feature = Util.atoi(split[1]);
                    }
                    else if (split[0].Equals("bias"))
                    {
                        bias = Util.atof(split[1]);
                    }
                    else if (split[0].Equals("w"))
                    {
                        break;
                    }
                    else if (split[0].Equals("label"))
                    {
                        labels = new int[nr_class];
                        for (int i = 0; i < nr_class; i++)
                        {
                            labels[i] = Util.atoi(split[i + 1]);
                        }
                    }
                    else
                    {
                        throw new Exception("unknown text in model file: [" + line + "]");
                    }
                }

                int w_size = nr_feature;
                if (bias >= 0)
                {
                    w_size++;
                }

                int nr_w = nr_class;
                if (nr_class == 2 && solverType != SolverType.MCSVM_CS)
                {
                    nr_w = 1;
                }
//JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java:
//ORIGINAL LINE: w = new double[w_size][nr_w];
                w = RectangularArrays.RectangularDoubleArray(w_size, nr_w);
                int[] buffer = new int[128];

                for (int i = 0; i < w_size; i++)
                {
                    for (int j = 0; j < nr_w; j++)
                    {
                        int b = 0;
                        while (true)
                        {
                            int ch = reader.Read();
                            if (ch == -1)
                            {
                                throw new EOFException("unexpected EOF");
                            }
                            if (ch == ' ')
                            {
                                w[i][j] = Util.atof(new string(buffer, 0, b));
                                break;
                            }
                            else
                            {
                                buffer[b++] = ch;
                            }
                        }
                    }
                }
            }
            finally
            {
                Util.closeQuietly(reader);
            }
        }