示例#1
0
        public virtual double LogIncrementCount(E key, double amount)
        {
            double count = SloppyMath.LogAdd(GetCount(key), amount);

            SetCount(key, count);
            return(GetCount(key));
        }
        private int[] IndicesFront(int[] entries)
        {
            int[] indices = new int[SloppyMath.IntPow(numClasses, windowSize - entries.Length)];
            int   offset  = SloppyMath.IntPow(numClasses, windowSize - entries.Length);
            int   start   = 0;

            foreach (int entry in entries)
            {
                start *= numClasses;
                start += entry;
            }
            start *= offset;
            int end = 0;

            for (int i = 0; i < entries.Length; i++)
            {
                end *= numClasses;
                end += entries[i];
                if (i == entries.Length - 1)
                {
                    end += 1;
                }
            }
            end *= offset;
            for (int i_1 = start; i_1 < end; i_1++)
            {
                indices[i_1 - start] = i_1;
            }
            return(indices);
        }
 public FloatFactorTable(int numClasses, int windowSize)
 {
     this.numClasses = numClasses;
     this.windowSize = windowSize;
     table           = new float[SloppyMath.IntPow(numClasses, windowSize)];
     Arrays.Fill(table, float.NegativeInfinity);
 }
 /// <summary>Format a channel</summary>
 /// <param name="b">The StringBuilder to append to</param>
 /// <param name="channelStr">
 /// The [possibly truncated and/or modified] string
 /// to actually print to the StringBuilder
 /// </param>
 /// <param name="channel">The original channel</param>
 /// <returns>|true| if the channel was printed (that is, appended to the StringBuilder)</returns>
 protected internal virtual bool FormatChannel(StringBuilder b, string channelStr, object channel)
 {
     if (this.channelColors == null && this.channelStyles == null)
     {
         //(regular concat)
         b.Append(channelStr);
     }
     else
     {
         string channelToString = channel.ToString().ToLower(Locale.English);
         //(default: no style)
         Color color = Color.None;
         Edu.Stanford.Nlp.Util.Logging.Style style = Edu.Stanford.Nlp.Util.Logging.Style.None;
         //(get color)
         if (this.channelColors != null)
         {
             Color candColor = this.channelColors[channelToString];
             if (candColor != null)
             {
                 //((case: found a color))
                 color = candColor;
             }
             else
             {
                 if (addRandomColors)
                 {
                     //((case: random colors))
                     color = Color.Values()[SloppyMath.PythonMod(channelToString.GetHashCode(), (Color.Values().Length - 3)) + 3];
                     if (channelToString.Equals(Redwood.Err.ToString().ToLower()))
                     {
                         color = Color.Red;
                     }
                     else
                     {
                         if (channelToString.Equals(Redwood.Warn.ToString().ToLower()))
                         {
                             color = Color.Yellow;
                         }
                     }
                     this.channelColors[channelToString] = color;
                 }
             }
         }
         //(get style)
         if (this.channelStyles != null)
         {
             Edu.Stanford.Nlp.Util.Logging.Style candStyle = this.channelStyles[channelToString];
             if (candStyle != null)
             {
                 style = candStyle;
             }
         }
         //(format)
         Style(b, channelStr, color, style);
     }
     return(true);
 }
        public virtual void MultiplyInEnd(Edu.Stanford.Nlp.IE.Crf.FloatFactorTable other)
        {
            int divisor = SloppyMath.IntPow(numClasses, other.WindowSize());

            for (int i = 0; i < table.Length; i++)
            {
                table[i] += other.GetValue(i % divisor);
            }
        }
        public virtual Edu.Stanford.Nlp.IE.Crf.FloatFactorTable SumOutFront()
        {
            Edu.Stanford.Nlp.IE.Crf.FloatFactorTable ft = new Edu.Stanford.Nlp.IE.Crf.FloatFactorTable(numClasses, windowSize - 1);
            int mod = SloppyMath.IntPow(numClasses, windowSize - 1);

            for (int i = 0; i < table.Length; i++)
            {
                ft.LogIncrementValue(i % mod, table[i]);
            }
            return(ft);
        }
        public virtual double UnnormalizedLogProbFront(int[] labels)
        {
            int startIndex    = IndicesFront(labels);
            int numCellsToSum = SloppyMath.IntPow(numClasses, windowSize - labels.Length);

            // double[] masses = new double[labels.length];
            // for (int i = 0; i < masses.length; i++) {
            //   masses[i] = table[labels[i]];
            // }
            return(ArrayMath.LogSum(table, startIndex, startIndex + numCellsToSum));
        }
        /// <summary>This now returns the first index of the requested entries.</summary>
        /// <remarks>
        /// This now returns the first index of the requested entries.
        /// The run of numClasses ^ (windowSize - entries.length)
        /// successive entries will give all of them.
        /// </remarks>
        /// <param name="entries">The class indices of size windowsSize</param>
        /// <returns>First index of requested entries</returns>
        private int IndicesFront(int[] entries)
        {
            int start = 0;

            foreach (int entry in entries)
            {
                start *= numClasses;
                start += entry;
            }
            int offset = SloppyMath.IntPow(numClasses, windowSize - entries.Length);

            return(start * offset);
        }
示例#9
0
        // In this method, each tag that is incompatible with the current word
        // (e.g., apple_CC) gets a default (constant) score instead of its exact score.
        // The scores of all other tags are computed exactly.
        private double[] GetApproximateScores(History h)
        {
            string[] tags   = StringTagsAt(h.current - h.start + LeftWindow());
            double[] scores = GetHistories(tags, h);
            // log score for each active tag, unnormalized
            // Number of tags that get assigned a default score:
            int    nDefault             = maxentTagger.ySize - tags.Length;
            double logScore             = ArrayMath.LogSum(scores);
            double logScoreInactiveTags = maxentTagger.GetInactiveTagDefaultScore(nDefault);
            double logTotal             = SloppyMath.LogAdd(logScore, logScoreInactiveTags);

            ArrayMath.AddInPlace(scores, -logTotal);
            return(scores);
        }
        private int[] IndicesEnd(int[] entries)
        {
            int[] indices = new int[SloppyMath.IntPow(numClasses, windowSize - entries.Length)];
            int   offset  = SloppyMath.IntPow(numClasses, entries.Length);
            int   index   = 0;

            foreach (int entry in entries)
            {
                index *= numClasses;
                index += entry;
            }
            for (int i = 0; i < indices.Length; i++)
            {
                indices[i] = index;
                index     += offset;
            }
            return(indices);
        }
 public virtual void Clean()
 {
     foreach (K1 key1 in Generics.NewHashSet(map.Keys))
     {
         ClassicCounter <K2> c = map[key1];
         foreach (K2 key2 in Generics.NewHashSet(c.KeySet()))
         {
             if (SloppyMath.IsCloseTo(0.0, c.GetCount(key2)))
             {
                 c.Remove(key2);
             }
         }
         if (c.KeySet().IsEmpty())
         {
             Sharpen.Collections.Remove(map, key1);
         }
     }
 }
示例#12
0
        private int[] IndicesEnd(int[] entries)
        {
            int index = 0;

            foreach (int entry in entries)
            {
                index *= numClasses;
                index += entry;
            }
            int[] indices = new int[SloppyMath.IntPow(numClasses, windowSize - entries.Length)];
            int   offset  = SloppyMath.IntPow(numClasses, entries.Length);

            for (int i = 0; i < indices.Length; i++)
            {
                indices[i] = index;
                index     += offset;
            }
            // log.info("indicesEnd returning: " + Arrays.toString(indices));
            return(indices);
        }
示例#13
0
        /// <summary>
        /// <inheritDoc/>
        ///
        /// </summary>
        public virtual double LogIncrementCount(E key, double count)
        {
            if (tempMDouble == null)
            {
                tempMDouble = new MutableDouble();
            }
            MutableDouble oldMDouble = map[key] = tempMDouble;

            if (oldMDouble != null)
            {
                count       = SloppyMath.LogAdd(count, oldMDouble);
                totalCount += count - oldMDouble;
            }
            else
            {
                totalCount += count;
            }
            tempMDouble.Set(count);
            tempMDouble = oldMDouble;
            return(count);
        }
 public virtual double LogIncrementCount(E key, double value)
 {
     // TODO Inspired by Guava.AtomicLongMap
     // Modify for our use?
     for (; ;)
     {
         AtomicDouble atomic = map[key];
         if (atomic == null)
         {
             atomic = map.PutIfAbsent(key, new AtomicDouble(value));
             if (atomic == null)
             {
                 totalCount.AddAndGet(value);
                 return(value);
             }
         }
         for (; ;)
         {
             double oldValue = atomic.Get();
             if (oldValue == 0.0)
             {
                 // don't compareAndSet a zero
                 if (map.Replace(key, atomic, new AtomicDouble(value)))
                 {
                     totalCount.AddAndGet(value);
                     return(value);
                 }
                 goto outer_continue;
             }
             double newValue = SloppyMath.LogAdd(oldValue, value);
             if (atomic.CompareAndSet(oldValue, newValue))
             {
                 totalCount.AddAndGet(value);
                 return(newValue);
             }
         }
         outer_continue :;
     }
     outer_break :;
 }
        protected internal virtual double BuildOScore(Hook hook)
        {
            double bestOScore = double.NegativeInfinity;
            Edge   iTemp      = new Edge(op.testOptions.exhaustiveTest);
            Edge   oTemp      = new Edge(op.testOptions.exhaustiveTest);

            iTemp.head  = hook.head;
            iTemp.tag   = hook.tag;
            iTemp.state = hook.subState;
            oTemp.head  = hook.head;
            oTemp.tag   = hook.tag;
            oTemp.state = hook.state;
            if (hook.IsPreHook())
            {
                iTemp.end = hook.start;
                oTemp.end = hook.end;
                for (int start = 0; start <= hook.head; start++)
                {
                    iTemp.start = start;
                    oTemp.start = start;
                    double oScore = scorer.OScore(oTemp) + scorer.IScore(iTemp);
                    //log.info("Score for "+hook+" is i "+iTemp+" ("+scorer.iScore(iTemp)+") o "+oTemp+" ("+scorer.oScore(oTemp)+")");
                    bestOScore = SloppyMath.Max(bestOScore, oScore);
                }
            }
            else
            {
                iTemp.start = hook.end;
                oTemp.start = hook.start;
                for (int end = hook.head + 1; end <= length; end++)
                {
                    iTemp.end = end;
                    oTemp.end = end;
                    double oScore = scorer.OScore(oTemp) + scorer.IScore(iTemp);
                    bestOScore = SloppyMath.Max(bestOScore, oScore);
                }
            }
            return(bestOScore);
        }
示例#16
0
 internal virtual void LogIncrementValue(int index, double value)
 {
     table[index] = SloppyMath.LogAdd(table[index], value);
 }
 private void LogIncrementValue(int index, float value)
 {
     table[index] = SloppyMath.LogAdd(table[index], value);
 }
示例#18
0
        /// <summary>
        /// Given a latitude and longitude (in degrees) and the
        /// maximum great circle (surface of the earth) distance,
        /// returns a simple Filter bounding box to "fast match"
        /// candidates.
        /// </summary>
        public static Filter GetBoundingBoxFilter(double originLat, double originLng, double maxDistanceKM)
        {
            // Basic bounding box geo math from
            // http://JanMatuschek.de/LatitudeLongitudeBoundingCoordinates,
            // licensed under creative commons 3.0:
            // http://creativecommons.org/licenses/by/3.0

            // TODO: maybe switch to recursive prefix tree instead
            // (in lucene/spatial)?  It should be more efficient
            // since it's a 2D trie...

            // Degrees -> Radians:
            double originLatRadians = originLat.ToRadians();
            double originLngRadians = originLng.ToRadians();

            double angle = maxDistanceKM / (SloppyMath.EarthDiameter(originLat) / 2.0);

            double minLat = originLatRadians - angle;
            double maxLat = originLatRadians + angle;

            double minLng;
            double maxLng;

            if (minLat > -90.ToRadians() && maxLat < 90.ToRadians())
            {
                double delta = Math.Asin(Math.Sin(angle) / Math.Cos(originLatRadians));
                minLng = originLngRadians - delta;
                if (minLng < -180.ToRadians())
                {
                    minLng += 2 * Math.PI;
                }
                maxLng = originLngRadians + delta;
                if (maxLng > 180.ToRadians())
                {
                    maxLng -= 2 * Math.PI;
                }
            }
            else
            {
                // The query includes a pole!
                minLat = Math.Max(minLat, -90.ToRadians());
                maxLat = Math.Min(maxLat, 90.ToRadians());
                minLng = -180.ToRadians();
                maxLng = 180.ToRadians();
            }

            BooleanFilter f = new BooleanFilter();

            // Add latitude range filter:
            f.Add(NumericRangeFilter.NewDoubleRange("latitude", minLat.ToDegrees(), maxLat.ToDegrees(), true, true),
                  Occur.MUST);

            // Add longitude range filter:
            if (minLng > maxLng)
            {
                // The bounding box crosses the international date
                // line:
                BooleanFilter lonF = new BooleanFilter();
                lonF.Add(NumericRangeFilter.NewDoubleRange("longitude", minLng.ToDegrees(), null, true, true),
                         Occur.SHOULD);
                lonF.Add(NumericRangeFilter.NewDoubleRange("longitude", null, maxLng.ToDegrees(), true, true),
                         Occur.SHOULD);
                f.Add(lonF, Occur.MUST);
            }
            else
            {
                f.Add(NumericRangeFilter.NewDoubleRange("longitude", minLng.ToDegrees(), maxLng.ToDegrees(), true, true),
                      Occur.MUST);
            }

            return(f);
        }
        public virtual void LogIncrementValue(int[] label, float value)
        {
            int index = IndexOf(label);

            table[index] = SloppyMath.LogAdd(table[index], value);
        }