예제 #1
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public boolean predict(org.maltparser.core.feature.FeatureVector featureVector, org.maltparser.parser.history.action.SingleDecision decision) throws org.maltparser.core.exception.MaltChainedException
        public virtual bool predict(FeatureVector featureVector, SingleDecision decision)
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.maltparser.ml.lib.FeatureList featureList = new org.maltparser.ml.lib.FeatureList();
            FeatureList featureList = new FeatureList();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int size = featureVector.size();
            int size = featureVector.Count;

            for (int i = 1; i <= size; i++)
            {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.maltparser.core.feature.value.FeatureValue featureValue = featureVector.getFeatureValue(i-1);
                FeatureValue featureValue = featureVector.GetFeatureValue(i - 1);
                if (featureValue != null && !(excludeNullValues == true && featureValue.NullValue))
                {
                    if (!featureValue.Multiple)
                    {
                        SingleFeatureValue singleFeatureValue = (SingleFeatureValue)featureValue;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int index = featureMap.getIndex(i, singleFeatureValue.getIndexCode());
                        int index = featureMap.getIndex(i, singleFeatureValue.IndexCode);
                        if (index != -1 && singleFeatureValue.Value != 0)
                        {
                            featureList.add(index, singleFeatureValue.Value);
                        }
                    }
                    else
                    {
                        foreach (int?value in ((MultipleFeatureValue)featureValue).Codes)
                        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int v = featureMap.getIndex(i, value);
                            int v = featureMap.getIndex(i, value.Value);
                            if (v != -1)
                            {
                                featureList.add(v, 1);
                            }
                        }
                    }
                }
            }
            try
            {
                decision.KBestList.addList(model.predict(featureList.toArray()));
            }
            catch (OutOfMemoryException e)
            {
                throw new LibException("Out of memory. Please increase the Java heap size (-Xmx<size>). ", e);
            }
            return(true);
        }
예제 #2
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: protected void binariesInstances2SVMFileFormat(java.io.InputStreamReader isr, java.io.OutputStreamWriter osw) throws org.maltparser.core.exception.MaltChainedException
        protected internal virtual void binariesInstances2SVMFileFormat(StreamReader isr, StreamWriter osw)
        {
            try
            {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.io.BufferedReader in = new java.io.BufferedReader(isr);
                StreamReader @in = new StreamReader(isr);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.io.BufferedWriter out = new java.io.BufferedWriter(osw);
                StreamWriter @out = new StreamWriter(osw);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.maltparser.ml.lib.FeatureList featureSet = new org.maltparser.ml.lib.FeatureList();
                FeatureList featureSet = new FeatureList();
                while (true)
                {
                    string line = @in.ReadLine();
                    if (ReferenceEquals(line, null))
                    {
                        break;
                    }
                    int y = binariesInstance(line, featureSet);
                    if (y == -1)
                    {
                        continue;
                    }
                    @out.Write(Convert.ToString(y));

                    for (int k = 0; k < featureSet.size(); k++)
                    {
                        MaltFeatureNode x = featureSet.get(k);
                        @out.BaseStream.WriteByte(' ');
                        @out.Write(Convert.ToString(x.Index));
                        @out.BaseStream.WriteByte(':');
                        @out.Write(Convert.ToString(x.Value));
                    }
                    @out.BaseStream.WriteByte('\n');
                }
                @in.Close();
                @out.Close();
            }
            catch (FormatException e)
            {
                throw new LibException("The instance file contain a non-numeric value", e);
            }
            catch (IOException e)
            {
                throw new LibException("Couldn't read from the instance file, when converting the Malt instances into LIBSVM/LIBLINEAR format. ", e);
            }
        }
예제 #3
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private libsvm.svm_problem readProblem(java.io.InputStreamReader isr, java.util.LinkedHashMap<String, String> libOptions) throws org.maltparser.core.exception.MaltChainedException
        private svm_problem readProblem(StreamReader isr, LinkedHashMap <string, string> libOptions)
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final libsvm.svm_problem problem = new libsvm.svm_problem();
            svm_problem problem = new svm_problem();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final libsvm.svm_parameter param = getLibSvmParameters(libOptions);
            svm_parameter param = getLibSvmParameters(libOptions);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.maltparser.ml.lib.FeatureList featureList = new org.maltparser.ml.lib.FeatureList();
            FeatureList featureList = new FeatureList();

            try
            {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.io.BufferedReader fp = new java.io.BufferedReader(isr);
                StreamReader fp = new StreamReader(isr);

                problem.l = NumberOfInstances;
                problem.x = new svm_node[problem.l][];
                problem.y = new double[problem.l];
                int i = 0;

                while (true)
                {
                    string line = fp.ReadLine();
                    if (ReferenceEquals(line, null))
                    {
                        break;
                    }
                    int y = binariesInstance(line, featureList);
                    if (y == -1)
                    {
                        continue;
                    }
                    try
                    {
                        problem.y[i] = y;
                        problem.x[i] = new svm_node[featureList.size()];
                        int p = 0;
                        for (int k = 0; k < featureList.size(); k++)
                        {
                            MaltFeatureNode x = featureList.get(k);
                            problem.x[i][p]       = new svm_node();
                            problem.x[i][p].value = x.Value;
                            problem.x[i][p].index = x.Index;
                            p++;
                        }
                        i++;
                    }
                    catch (IndexOutOfRangeException e)
                    {
                        throw new LibException("Couldn't read libsvm problem from the instance file. ", e);
                    }
                }
                fp.Close();
                if (param.gamma == 0)
                {
                    param.gamma = 1.0 / featureMap.FeatureCounter;
                }
            }
            catch (IOException e)
            {
                throw new LibException("Couldn't read libsvm problem from the instance file. ", e);
            }
            return(problem);
        }
예제 #4
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private de.bwaldvogel.liblinear.Problem readProblem(java.io.InputStreamReader isr) throws org.maltparser.core.exception.MaltChainedException
        private Problem readProblem(StreamReader isr)
        {
            Problem problem = new Problem();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.maltparser.ml.lib.FeatureList featureList = new org.maltparser.ml.lib.FeatureList();
            FeatureList featureList = new FeatureList();

            if (Configuration.LoggerInfoEnabled)
            {
                Configuration.logInfoMessage("- Read all training instances.\n");
            }
            try
            {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.io.BufferedReader fp = new java.io.BufferedReader(isr);
                StreamReader fp = new StreamReader(isr);

                problem.bias = -1;
                problem.l    = NumberOfInstances;
                problem.x    = new FeatureNode[problem.l][];
                problem.y    = new int[problem.l];
                int i = 0;

                while (true)
                {
                    string line = fp.ReadLine();
                    if (ReferenceEquals(line, null))
                    {
                        break;
                    }
                    int y = binariesInstance(line, featureList);
                    if (y == -1)
                    {
                        continue;
                    }
                    try
                    {
                        problem.y[i] = y;
                        problem.x[i] = new FeatureNode[featureList.size()];
                        int p = 0;
                        for (int k = 0; k < featureList.size(); k++)
                        {
                            MaltFeatureNode x = featureList.get(k);
                            problem.x[i][p++] = new FeatureNode(x.Index, x.Value);
                        }
                        i++;
                    }
                    catch (IndexOutOfRangeException e)
                    {
                        throw new LibException("Couldn't read liblinear problem from the instance file. ", e);
                    }
                }
                fp.Close();
                problem.n = featureMap.size();
            }
            catch (IOException e)
            {
                throw new LibException("Cannot read from the instance file. ", e);
            }

            return(problem);
        }
예제 #5
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: protected int binariesInstance(String line, org.maltparser.ml.lib.FeatureList featureList) throws org.maltparser.core.exception.MaltChainedException
        protected internal virtual int binariesInstance(string line, FeatureList featureList)
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.regex.Pattern tabPattern = java.util.regex.Pattern.compile("\t");
            Pattern tabPattern = Pattern.compile("\t");
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.regex.Pattern pipePattern = java.util.regex.Pattern.compile("\\|");
            Pattern pipePattern = Pattern.compile("\\|");
            int     y           = -1;

            featureList.clear();
            try
            {
                string[] columns = tabPattern.split(line);

                if (columns.Length == 0)
                {
                    return(-1);
                }
                try
                {
                    y = int.Parse(columns[0]);
                }
                catch (FormatException e)
                {
                    throw new LibException("The instance file contain a non-integer value '" + columns[0] + "'", e);
                }
                for (int j = 1; j < columns.Length; j++)
                {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final String[] items = pipePattern.split(columns[j]);
                    string[] items = pipePattern.split(columns[j]);
                    for (int k = 0; k < items.Length; k++)
                    {
                        try
                        {
                            int colon = items[k].IndexOf(':');
                            if (colon == -1)
                            {
                                if (int.Parse(items[k]) != -1)
                                {
                                    int v = featureMap.addIndex(j, int.Parse(items[k]));
                                    if (v != -1)
                                    {
                                        featureList.add(v, 1);
                                    }
                                }
                            }
                            else
                            {
                                int    index = featureMap.addIndex(j, int.Parse(items[k].Substring(0, colon)));
                                double value;
                                if (items[k].Substring(colon + 1).IndexOf('.') != -1)
                                {
                                    value = double.Parse(items[k].Substring(colon + 1));
                                }
                                else
                                {
                                    value = int.Parse(items[k].Substring(colon + 1));
                                }
                                featureList.add(index, value);
                            }
                        }
                        catch (FormatException e)
                        {
                            throw new LibException("The instance file contain a non-numeric value '" + items[k] + "'", e);
                        }
                    }
                }
            }
            catch (IndexOutOfRangeException e)
            {
                throw new LibException("Couln't read from the instance file. ", e);
            }
            return(y);
        }