Esempio n. 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, boolean one_prediction) throws org.maltparser.core.exception.MaltChainedException
        public virtual bool predict(FeatureVector featureVector, SingleDecision decision, bool one_prediction)
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.ArrayList<org.maltparser.ml.lib.MaltFeatureNode> featureList = new java.util.ArrayList<org.maltparser.ml.lib.MaltFeatureNode>();
            List <MaltFeatureNode> featureList = new List <MaltFeatureNode>();
//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(new MaltFeatureNode(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(new MaltFeatureNode(v, 1));
                            }
                        }
                    }
                }
            }
            try
            {
                if (one_prediction)
                {
                    decision.KBestList.add(model.predict_one(featureList.ToArray()));
                }
                else
                {
                    decision.KBestList.addList(model.predict(featureList.ToArray()));
                }
            }
            catch (System.OutOfMemoryException e)
            {
                throw new LibException("Out of memory. Please increase the Java heap size (-Xmx<size>). ", e);
            }
            return(true);
        }
Esempio n. 2
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void addInstance(org.maltparser.parser.history.action.SingleDecision decision, org.maltparser.core.feature.FeatureVector featureVector) throws org.maltparser.core.exception.MaltChainedException
        public virtual void addInstance(SingleDecision decision, FeatureVector featureVector)
        {
            if (featureVector == null)
            {
                throw new LibException("The feature vector cannot be found");
            }
            else if (decision == null)
            {
                throw new LibException("The decision cannot be found");
            }

            try
            {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final StringBuilder sb = new StringBuilder();
                StringBuilder sb = new StringBuilder();
                sb.Append(decision.DecisionCode + "\t");
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int n = featureVector.size();
                int n = featureVector.Count;
                for (int i = 0; i < n; i++)
                {
                    FeatureValue featureValue = featureVector.GetFeatureValue(i);
                    if (featureValue == null || (excludeNullValues == true && featureValue.NullValue))
                    {
                        sb.Append("-1");
                    }
                    else
                    {
                        if (!featureValue.Multiple)
                        {
                            SingleFeatureValue singleFeatureValue = (SingleFeatureValue)featureValue;
                            if (singleFeatureValue.Value == 1)
                            {
                                sb.Append(singleFeatureValue.IndexCode);
                            }
                            else if (singleFeatureValue.Value == 0)
                            {
                                sb.Append("-1");
                            }
                            else
                            {
                                sb.Append(singleFeatureValue.IndexCode);
                                sb.Append(":");
                                sb.Append(singleFeatureValue.Value);
                            }
                        }
                        else
                        {                         //if (featureValue instanceof MultipleFeatureValue) {
                            ISet <int> values = ((MultipleFeatureValue)featureValue).Codes;
                            int        j      = 0;
                            foreach (int?value in values)
                            {
                                sb.Append(value.ToString());
                                if (j != values.Count - 1)
                                {
                                    sb.Append("|");
                                }
                                j++;
                            }
                        }
                        //					else {
                        //						throw new LibException("Don't recognize the type of feature value: "+featureValue.getClass());
                        //					}
                    }
                    sb.Append('\t');
                }
                sb.Append('\n');
                instanceOutput.Write(sb.ToString());
                instanceOutput.Flush();
                increaseNumberOfInstances();
                //			sb.setLength(0);
            }
            catch (IOException e)
            {
                throw new LibException("The learner cannot write to the instance file. ", e);
            }
        }