Beispiel #1
0
        public override bool Equals(object obj)
        {
            if (this == obj)
            {
                return(true);
            }
            if (obj == null)
            {
                return(false);
            }
            if (GetType() != obj.GetType())
            {
                return(false);
            }
            MaltLiblinearModel other = (MaltLiblinearModel)obj;

            if (BitConverter.DoubleToInt64Bits(bias) != Double.doubleToLongBits(other.bias))
            {
                return(false);
            }
            if (!Arrays.Equals(labels, other.labels))
            {
                return(false);
            }
            if (nr_class != other.nr_class)
            {
                return(false);
            }
            if (nr_feature != other.nr_feature)
            {
                return(false);
            }
            if (solverType == null)
            {
                if (other.solverType != null)
                {
                    return(false);
                }
            }
            else if (!solverType.Equals(other.solverType))
            {
                return(false);
            }
            for (int i = 0; i < w.Length; i++)
            {
                if (other.w.Length <= i)
                {
                    return(false);
                }
                if (!Util.Equals(w[i], other.w[i]))
                {
                    return(false);
                }
            }
            return(true);
        }
Beispiel #2
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: protected void trainExternal(String pathExternalTrain, java.util.LinkedHashMap<String, String> libOptions) throws org.maltparser.core.exception.MaltChainedException
        protected internal override void trainExternal(string pathExternalTrain, LinkedHashMap <string, string> libOptions)
        {
            try
            {
                Configuration config = Configuration;
                if (config.LoggerInfoEnabled)
                {
                    config.logInfoMessage("Creating liblinear model (external) " + getFile(".mod").Name);
                }
                binariesInstances2SVMFileFormat(getInstanceInputStreamReader(".ins"), getInstanceOutputStreamWriter(".ins.tmp"));
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final String[] params = getLibParamStringArray(libOptions);
                string[] @params       = getLibParamStringArray(libOptions);
                string[] arrayCommands = new string[@params.Length + 3];
                int      i             = 0;
                arrayCommands[i++] = pathExternalTrain;
                for (; i <= @params.Length; i++)
                {
                    arrayCommands[i] = @params[i - 1];
                }
                arrayCommands[i++] = getFile(".ins.tmp").AbsolutePath;
                arrayCommands[i++] = getFile(".mod").AbsolutePath;

                if (verbosity == Verbostity.ALL)
                {
                    config.logInfoMessage('\n');
                }
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final Process child = Runtime.getRuntime().exec(arrayCommands);
                Process child = Runtime.Runtime.exec(arrayCommands);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.io.InputStream in = child.getInputStream();
                Stream @in = child.InputStream;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.io.InputStream err = child.getErrorStream();
                Stream err = child.ErrorStream;
                int    c;
                while ((c = @in.Read()) != -1)
                {
                    if (verbosity == Verbostity.ALL)
                    {
                        config.logInfoMessage((char)c);
                    }
                }
                while ((c = err.Read()) != -1)
                {
                    if (verbosity == Verbostity.ALL || verbosity == Verbostity.ERROR)
                    {
                        config.logInfoMessage((char)c);
                    }
                }
                if (child.waitFor() != 0)
                {
                    config.logErrorMessage(" FAILED (" + child.exitValue() + ")");
                }
                @in.Close();
                err.Close();
                if (config.LoggerInfoEnabled)
                {
                    config.logInfoMessage("\nSaving Liblinear model " + getFile(".moo").Name + "\n");
                }
                MaltLiblinearModel xmodel = new MaltLiblinearModel(getFile(".mod"));
                ObjectOutputStream output = new ObjectOutputStream(new BufferedOutputStream(new FileStream(getFile(".moo").AbsolutePath, FileMode.Create, FileAccess.Write)));
                try
                {
                    output.writeObject(xmodel);
                }
                finally
                {
                    output.close();
                }
                bool saveInstanceFiles = ((bool?)Configuration.getOptionValue("lib", "save_instance_files")).Value;
                if (!saveInstanceFiles)
                {
                    getFile(".ins").delete();
                    getFile(".mod").delete();
                    getFile(".ins.tmp").delete();
                }
                if (config.LoggerInfoEnabled)
                {
                    config.logInfoMessage('\n');
                }
            }
            catch (InterruptedException e)
            {
                throw new LibException("Learner is interrupted. ", e);
            }
            catch (ArgumentException e)
            {
                throw new LibException("The learner was not able to redirect Standard Error stream. ", e);
            }
            catch (SecurityException e)
            {
                throw new LibException("The learner cannot remove the instance file. ", e);
            }
            catch (IOException e)
            {
                throw new LibException("The learner cannot save the model file '" + getFile(".mod").AbsolutePath + "'. ", e);
            }
            catch (OutOfMemoryException e)
            {
                throw new LibException("Out of memory. Please increase the Java heap size (-Xmx<size>). ", e);
            }
        }
Beispiel #3
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: protected void trainInternal(java.util.LinkedHashMap<String, String> libOptions) throws org.maltparser.core.exception.MaltChainedException
        protected internal override void trainInternal(LinkedHashMap <string, string> libOptions)
        {
            Configuration config = Configuration;

            if (config.LoggerInfoEnabled)
            {
                config.logInfoMessage("Creating Liblinear model " + getFile(".moo").Name + "\n");
            }
            double[]  wmodel     = null;
            int[]     labels     = null;
            int       nr_class   = 0;
            int       nr_feature = 0;
            Parameter parameter  = getLiblinearParameters(libOptions);

            try
            {
                Problem problem = readProblem(getInstanceInputStreamReader(".ins"));
                bool    res     = checkProblem(problem);
                if (res == false)
                {
                    throw new LibException("Abort (The number of training instances * the number of classes) > " + int.MaxValue + " and this is not supported by LibLinear. ");
                }
                if (config.LoggerInfoEnabled)
                {
                    config.logInfoMessage("- Train a parser model using LibLinear.\n");
                }
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.io.PrintStream out = System.out;
                PrintStream @out = System.out;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.io.PrintStream err = System.err;
                PrintStream err = System.err;
                System.Out = NoPrintStream.NO_PRINTSTREAM;
                System.Err = NoPrintStream.NO_PRINTSTREAM;
                Model model = Linear.train(problem, parameter);
                System.Out = err;
                System.Out = @out;
                problem    = null;
                wmodel     = model.FeatureWeights;
                labels     = model.Labels;
                nr_class   = model.NrClass;
                nr_feature = model.NrFeature;
                bool saveInstanceFiles = ((bool?)Configuration.getOptionValue("lib", "save_instance_files")).Value;
                if (!saveInstanceFiles)
                {
                    getFile(".ins").delete();
                }
            }
            catch (OutOfMemoryException e)
            {
                throw new LibException("Out of memory. Please increase the Java heap size (-Xmx<size>). ", e);
            }
            catch (ArgumentException e)
            {
                throw new LibException("The Liblinear learner was not able to redirect Standard Error stream. ", e);
            }
            catch (SecurityException e)
            {
                throw new LibException("The Liblinear learner cannot remove the instance file. ", e);
            }
            catch (NegativeArraySizeException e)
            {
                throw new LibException("(The number of training instances * the number of classes) > " + int.MaxValue + " and this is not supported by LibLinear.", e);
            }

            if (config.LoggerInfoEnabled)
            {
                config.logInfoMessage("- Optimize the memory usage\n");
            }
            MaltLiblinearModel xmodel = null;

            try
            {
                //			System.out.println("Nr Features:" +  nr_feature);
                //			System.out.println("nr_class:" + nr_class);
                //			System.out.println("wmodel.length:" + wmodel.length);
                double[][] wmatrix = convert2(wmodel, nr_class, nr_feature);
                xmodel = new MaltLiblinearModel(labels, nr_class, wmatrix.Length, wmatrix, parameter.SolverType);
                if (config.LoggerInfoEnabled)
                {
                    config.logInfoMessage("- Save the Liblinear model " + getFile(".moo").Name + "\n");
                }
            }
            catch (OutOfMemoryException e)
            {
                throw new LibException("Out of memory. Please increase the Java heap size (-Xmx<size>). ", e);
            }
            try
            {
                if (xmodel != null)
                {
                    ObjectOutputStream output = new ObjectOutputStream(new BufferedOutputStream(new FileStream(getFile(".moo").AbsolutePath, FileMode.Create, FileAccess.Write)));
                    try
                    {
                        output.writeObject(xmodel);
                    }
                    finally
                    {
                        output.close();
                    }
                }
            }
            catch (OutOfMemoryException e)
            {
                throw new LibException("Out of memory. Please increase the Java heap size (-Xmx<size>). ", e);
            }
            catch (ArgumentException e)
            {
                throw new LibException("The Liblinear learner was not able to redirect Standard Error stream. ", e);
            }
            catch (SecurityException e)
            {
                throw new LibException("The Liblinear learner cannot remove the instance file. ", e);
            }
            catch (IOException e)
            {
                throw new LibException("The Liblinear learner cannot save the model file '" + getFile(".mod").AbsolutePath + "'. ", e);
            }
        }