public override int CompareTo(IPokerHand other)
        {
            if (other is null)
            {
                return(1);
            }
            if (HandRank > other.HandRank)
            {
                return(1);
            }
            if (HandRank < other.HandRank)
            {
                return(-1);
            }

            var otherHand = (ThreeOfAkind)ConvertToThisType(other);

            if (Triple.First().Rank > otherHand.Triple.First().Rank)
            {
                return(1);
            }
            if (Triple.First().Rank < otherHand.Triple.First().Rank)
            {
                return(-1);
            }

            return(Utils.ComapreCards(Kickers, otherHand.Kickers));
        }
Exemple #2
0
 protected internal override double[] TrainWeights(int[][][][] data, int[][] labels, IEvaluator[] evaluators, int pruneFeatureItr, double[][][][] featureVals)
 {
     if (flags.secondOrderNonLinear)
     {
         CRFNonLinearSecondOrderLogConditionalObjectiveFunction func = new CRFNonLinearSecondOrderLogConditionalObjectiveFunction(data, labels, windowSize, classIndex, labelIndices, map, flags, nodeFeatureIndicesMap.Size(), edgeFeatureIndicesMap.Size
                                                                                                                                      ());
         cliquePotentialFunctionHelper = func;
         double[] allWeights = TrainWeightsUsingNonLinearCRF(func, evaluators);
         Quadruple <double[][], double[][], double[][], double[][]> @params = func.SeparateWeights(allWeights);
         this.inputLayerWeights4Edge  = @params.First();
         this.outputLayerWeights4Edge = @params.Second();
         this.inputLayerWeights       = @params.Third();
         this.outputLayerWeights      = @params.Fourth();
     }
     else
     {
         CRFNonLinearLogConditionalObjectiveFunction func = new CRFNonLinearLogConditionalObjectiveFunction(data, labels, windowSize, classIndex, labelIndices, map, flags, nodeFeatureIndicesMap.Size(), edgeFeatureIndicesMap.Size(), featureVals);
         if (flags.useAdaGradFOBOS)
         {
             func.gradientsOnly = true;
         }
         cliquePotentialFunctionHelper = func;
         double[] allWeights = TrainWeightsUsingNonLinearCRF(func, evaluators);
         Triple <double[][], double[][], double[][]> @params = func.SeparateWeights(allWeights);
         this.linearWeights      = @params.First();
         this.inputLayerWeights  = @params.Second();
         this.outputLayerWeights = @params.Third();
     }
     return(null);
 }
Exemple #3
0
        private string GetConllEvalString(IList <L> orderedLabels, bool ignoreNegLabel)
        {
            StringBuilder             sb             = new StringBuilder();
            int                       correctPhrases = GetCorrect() - GetCorrect(negLabel);
            Triple <double, int, int> accuracyInfo   = GetAccuracyInfo();
            int                       totalCount     = accuracyInfo.Second() + accuracyInfo.Third();

            sb.Append("processed " + totalCount + " tokens with " + GetRelevant() + " phrases; ");
            sb.Append("found: " + GetRetrieved() + " phrases; correct: " + correctPhrases + "\n");
            Formatter formatter = new Formatter(sb, Locale.Us);

            formatter.Format("accuracy: %6.2f%%; ", accuracyInfo.First() * 100);
            formatter.Format("precision: %6.2f%%; ", GetPrecision() * 100);
            formatter.Format("recall: %6.2f%%; ", GetRecall() * 100);
            formatter.Format("FB1: %6.2f\n", GetFMeasure() * 100);
            foreach (L label in orderedLabels)
            {
                if (ignoreNegLabel && label.Equals(negLabel))
                {
                    continue;
                }
                formatter.Format("%17s: ", label);
                formatter.Format("precision: %6.2f%%; ", GetPrecision(label) * 100);
                formatter.Format("recall: %6.2f%%; ", GetRecall(label) * 100);
                formatter.Format("FB1: %6.2f  %d\n", GetFMeasure(label) * 100, GetRetrieved(label));
            }
            return(sb.ToString());
        }
Exemple #4
0
		public override Triple<int[][][], int[], double[][][]> DocumentToDataAndLabels(IList<In> document)
		{
			Triple<int[][][], int[], double[][][]> result = base.DocumentToDataAndLabels(document);
			int[][][] data = result.First();
			data = TransformDocData(data);
			return new Triple<int[][][], int[], double[][][]>(data, result.Second(), result.Third());
		}
Exemple #5
0
        /// <summary>Returns a String summarizing overall accuracy that will print nicely.</summary>
        public virtual string GetAccuracyDescription(int numDigits)
        {
            NumberFormat nf = NumberFormat.GetNumberInstance();

            nf.SetMaximumFractionDigits(numDigits);
            Triple <double, int, int> accu = GetAccuracyInfo();

            return(nf.Format(accu.First()) + "  (" + accu.Second() + "/" + (accu.Second() + accu.Third()) + ")");
        }
        public virtual string GetRecallDescription(int numDigits, L label)
        {
            NumberFormat nf = NumberFormat.GetNumberInstance();

            nf.SetMaximumFractionDigits(numDigits);
            Triple <double, int, int> recall = GetRecallInfo(label);

            return(nf.Format(recall.First()) + "  (" + recall.Second() + "/" + (recall.Second() + recall.Third()) + ")");
        }
        public virtual string GetPrecisionDescription(int numDigits, L label)
        {
            NumberFormat nf = NumberFormat.GetNumberInstance();

            nf.SetMaximumFractionDigits(numDigits);
            Triple <double, int, int> prec = GetPrecisionInfo(label);

            return(nf.Format(prec.First()) + "  (" + prec.Second() + "/" + (prec.Second() + prec.Third()) + ")");
        }
Exemple #8
0
 private static void EmitSortedTrees(PriorityQueue <Triple <double, Tree, Tree> > queue, int worstKTreesToEmit, string filePrefix)
 {
     if (queue == null)
     {
         log.Info("Queue was not initialized properly");
     }
     try
     {
         PrintWriter         guessPw    = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePrefix + ".kworst.guess"), "UTF-8")));
         PrintWriter         goldPw     = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePrefix + ".kworst.gold"), "UTF-8")));
         IConstituentFactory cFact      = new LabeledScoredConstituentFactory();
         PrintWriter         guessDepPw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePrefix + ".kworst.guess.deps"), "UTF-8")));
         PrintWriter         goldDepPw  = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePrefix + ".kworst.gold.deps"), "UTF-8")));
         System.Console.Out.Printf("F1s of %d worst trees:\n", worstKTreesToEmit);
         for (int i = 0; queue.Peek() != null && i < worstKTreesToEmit; i++)
         {
             Triple <double, Tree, Tree> trees = queue.Poll();
             System.Console.Out.WriteLine(trees.First());
             //Output the trees
             goldPw.Println(trees.Second().ToString());
             guessPw.Println(trees.Third().ToString());
             //Output the set differences
             ICollection <Constituent> goldDeps = Generics.NewHashSet();
             Sharpen.Collections.AddAll(goldDeps, trees.Second().Constituents(cFact));
             goldDeps.RemoveAll(trees.Third().Constituents(cFact));
             foreach (Constituent c in goldDeps)
             {
                 goldDepPw.Print(c.ToString() + "  ");
             }
             goldDepPw.Println();
             ICollection <Constituent> guessDeps = Generics.NewHashSet();
             Sharpen.Collections.AddAll(guessDeps, trees.Third().Constituents(cFact));
             guessDeps.RemoveAll(trees.Second().Constituents(cFact));
             foreach (Constituent c_1 in guessDeps)
             {
                 guessDepPw.Print(c_1.ToString() + "  ");
             }
             guessDepPw.Println();
         }
         guessPw.Close();
         goldPw.Close();
         goldDepPw.Close();
         guessDepPw.Close();
     }
     catch (UnsupportedEncodingException e)
     {
         Sharpen.Runtime.PrintStackTrace(e);
     }
     catch (FileNotFoundException e)
     {
         Sharpen.Runtime.PrintStackTrace(e);
     }
 }
Exemple #9
0
            public virtual int Compare(Triple <double, Tree, Tree> o1, Triple <double, Tree, Tree> o2)
            {
                double firstF1  = o1.First();
                double secondF1 = o2.First();

                if (firstF1 < secondF1)
                {
                    return(-1);
                }
                else
                {
                    if (firstF1 == secondF1)
                    {
                        return(0);
                    }
                }
                return(1);
            }
Exemple #10
0
        public virtual void InitMC(List <Triple <double, int, int> > dataScores)
        {
            IPriorityQueue <Pair <int, Triple <double, int, int> > > q = new BinaryHeapPriorityQueue <Pair <int, Triple <double, int, int> > >();

            for (int i = 0; i < dataScores.Count; i++)
            {
                q.Add(new Pair <int, Triple <double, int, int> >(int.Parse(i), dataScores[i]), -dataScores[i].First());
            }
            IList <Pair <int, Triple <double, int, int> > > sorted = q.ToSortedList();

            scores  = new double[sorted.Count];
            classes = new int[sorted.Count];
            guesses = new int[sorted.Count];
            logger.Info("incoming size " + dataScores.Count + " resulting " + sorted.Count);
            for (int i_1 = 0; i_1 < sorted.Count; i_1++)
            {
                Triple <double, int, int> next = sorted[i_1].Second();
                scores[i_1]  = next.First();
                classes[i_1] = next.Second();
                guesses[i_1] = next.Third();
            }
            Init();
        }
Exemple #11
0
        private void RunParallelApplyPats(IDictionary <string, DataInstance> sents, string label, E pattern, TwoDimensionalCounter <CandidatePhrase, E> wordsandLemmaPatExtracted, CollectionValuedMap <E, Triple <string, int, int> > matchedTokensByPat, ICollection
                                          <CandidatePhrase> alreadyLabeledWords)
        {
            Redwood.Log(Redwood.Dbg, "Applying pattern " + pattern + " to a total of " + sents.Count + " sentences ");
            IList <string> notAllowedClasses = new List <string>();
            IList <string> sentids           = CollectionUtils.ToList(sents.Keys);

            if (constVars.doNotExtractPhraseAnyWordLabeledOtherClass)
            {
                foreach (string l in constVars.GetAnswerClass().Keys)
                {
                    if (!l.Equals(label))
                    {
                        notAllowedClasses.Add(l);
                    }
                }
                notAllowedClasses.Add("OTHERSEM");
            }
            IDictionary <TokenSequencePattern, E> surfacePatternsLearnedThisIterConverted = null;
            IDictionary <SemgrexPattern, E>       depPatternsLearnedThisIterConverted     = null;

            if (constVars.patternType.Equals(PatternFactory.PatternType.Surface))
            {
                surfacePatternsLearnedThisIterConverted = new Dictionary <TokenSequencePattern, E>();
                string patternStr = null;
                try
                {
                    patternStr = pattern.ToString(notAllowedClasses);
                    TokenSequencePattern pat = ((TokenSequencePattern)TokenSequencePattern.Compile(constVars.env[label], patternStr));
                    surfacePatternsLearnedThisIterConverted[pat] = pattern;
                }
                catch (Exception e)
                {
                    log.Info("Error applying pattern " + patternStr + ". Probably an ill formed pattern (can be because of special symbols in label names). Contact the software developer.");
                    throw;
                }
            }
            else
            {
                if (constVars.patternType.Equals(PatternFactory.PatternType.Dep))
                {
                    depPatternsLearnedThisIterConverted = new Dictionary <SemgrexPattern, E>();
                    SemgrexPattern pat = SemgrexPattern.Compile(pattern.ToString(notAllowedClasses), new Env(constVars.env[label].GetVariables()));
                    depPatternsLearnedThisIterConverted[pat] = pattern;
                }
                else
                {
                    throw new NotSupportedException();
                }
            }
            //Apply the patterns and extract candidate phrases
            int num;
            int numThreads = constVars.numThreads;

            //If number of sentences is less, do not create so many threads
            if (sents.Count < 50)
            {
                numThreads = 1;
            }
            if (numThreads == 1)
            {
                num = sents.Count;
            }
            else
            {
                num = sents.Count / (numThreads - 1);
            }
            IExecutorService executor = Executors.NewFixedThreadPool(constVars.numThreads);
            IList <IFuture <Triple <TwoDimensionalCounter <CandidatePhrase, E>, CollectionValuedMap <E, Triple <string, int, int> >, ICollection <CandidatePhrase> > > > list = new List <IFuture <Triple <TwoDimensionalCounter <CandidatePhrase, E>, CollectionValuedMap <E
                                                                                                                                                                                                                                                                            , Triple <string, int, int> >, ICollection <CandidatePhrase> > > >();

            for (int i = 0; i < numThreads; i++)
            {
                ICallable <Triple <TwoDimensionalCounter <CandidatePhrase, E>, CollectionValuedMap <E, Triple <string, int, int> >, ICollection <CandidatePhrase> > > task = null;
                if (pattern.type.Equals(PatternFactory.PatternType.Surface))
                {
                    //Redwood.log(Redwood.DBG, "Applying pats: assigning sentences " + i*num + " to " +Math.min(sentids.size(), (i + 1) * num) + " to thread " + (i+1));
                    task = new ApplyPatterns(sents, num == sents.Count ? sentids : sentids.SubList(i * num, Math.Min(sentids.Count, (i + 1) * num)), surfacePatternsLearnedThisIterConverted, label, constVars.removeStopWordsFromSelectedPhrases, constVars.removePhrasesWithStopWords
                                             , constVars);
                }
                else
                {
                    task = new ApplyDepPatterns(sents, num == sents.Count ? sentids : sentids.SubList(i * num, Math.Min(sentids.Count, (i + 1) * num)), depPatternsLearnedThisIterConverted, label, constVars.removeStopWordsFromSelectedPhrases, constVars.removePhrasesWithStopWords
                                                , constVars);
                }
                IFuture <Triple <TwoDimensionalCounter <CandidatePhrase, E>, CollectionValuedMap <E, Triple <string, int, int> >, ICollection <CandidatePhrase> > > submit = executor.Submit(task);
                list.Add(submit);
            }
            // Now retrieve the result
            foreach (IFuture <Triple <TwoDimensionalCounter <CandidatePhrase, E>, CollectionValuedMap <E, Triple <string, int, int> >, ICollection <CandidatePhrase> > > future in list)
            {
                try
                {
                    Triple <TwoDimensionalCounter <CandidatePhrase, E>, CollectionValuedMap <E, Triple <string, int, int> >, ICollection <CandidatePhrase> > result = future.Get();
                    Redwood.Log(ConstantsAndVariables.extremedebug, "Pattern " + pattern + " extracted phrases " + result.First());
                    wordsandLemmaPatExtracted.AddAll(result.First());
                    matchedTokensByPat.AddAll(result.Second());
                    Sharpen.Collections.AddAll(alreadyLabeledWords, result.Third());
                }
                catch (Exception e)
                {
                    executor.ShutdownNow();
                    throw new Exception(e);
                }
            }
            executor.Shutdown();
        }
        public static Pair <string, IFileFilter> GetTreebankDescription(string[] args, int argIndex, string flag)
        {
            Triple <string, IFileFilter, double> description = GetWeightedTreebankDescription(args, argIndex, flag);

            return(Pair.MakePair(description.First(), description.Second()));
        }