Ejemplo n.º 1
0
        /// <summary>
        /// returns a
        /// <c>double[]</c>
        /// array of length
        /// <c>depth+1</c>
        /// , containing the conditional counts on a
        /// <c>depth</c>
        /// -length list given each level of conditional
        /// distribution from 0 to
        /// <c>depth</c>
        /// .
        /// </summary>
        public virtual double[] GetCounts(IList <K> l)
        {
            if (l.Count != depth)
            {
                WrongDepth();
            }
            //throws exception
            double[] counts             = new double[depth + 1];
            GeneralizedCounter <K> next = this;

            counts[0] = next.TotalCount();
            IEnumerator <K> i = l.GetEnumerator();
            int             j = 1;
            K o = i.Current;

            while (i.MoveNext())
            {
                next      = next.ConditionalizeHelper(o);
                counts[j] = next.TotalCount();
                o         = i.Current;
                j++;
            }
            counts[depth] = next.GetCount(o);
            return(counts);
        }
Ejemplo n.º 2
0
        /// <summary>for testing purposes only</summary>
        public static void Main(string[] args)
        {
            object[] a1 = new object[] { "a", "b" };
            object[] a2 = new object[] { "a", "b" };
            System.Console.Out.WriteLine(Arrays.Equals(a1, a2));
            GeneralizedCounter <string> gc = new GeneralizedCounter <string>(3);

            gc.IncrementCount(Arrays.AsList(new string[] { "a", "j", "x" }), 3.0);
            gc.IncrementCount(Arrays.AsList(new string[] { "a", "l", "x" }), 3.0);
            gc.IncrementCount(Arrays.AsList(new string[] { "b", "k", "y" }), 3.0);
            gc.IncrementCount(Arrays.AsList(new string[] { "b", "k", "z" }), 3.0);
            System.Console.Out.WriteLine("incremented counts.");
            System.Console.Out.WriteLine(gc.DumpKeys());
            System.Console.Out.WriteLine("string representation of generalized counter:");
            System.Console.Out.WriteLine(gc.ToString());
            gc.PrintKeySet();
            System.Console.Out.WriteLine("entry set:\n" + gc.EntrySet());
            ArrayPrintDouble(gc.GetCounts(Arrays.AsList(new string[] { "a", "j", "x" })));
            ArrayPrintDouble(gc.GetCounts(Arrays.AsList(new string[] { "a", "j", "z" })));
            ArrayPrintDouble(gc.GetCounts(Arrays.AsList(new string[] { "b", "k", "w" })));
            ArrayPrintDouble(gc.GetCounts(Arrays.AsList(new string[] { "b", "k", "z" })));
            GeneralizedCounter <string> gc1 = gc.Conditionalize(Arrays.AsList(new string[] { "a" }));

            gc1.IncrementCount(Arrays.AsList(new string[] { "j", "x" }));
            gc1.IncrementCount2D("j", "z");
            GeneralizedCounter <string> gc2 = gc1.Conditionalize(Arrays.AsList(new string[] { "j" }));

            gc2.IncrementCount1D("x");
            System.Console.Out.WriteLine("Pretty-printing gc after incrementing gc1:");
            gc.PrettyPrint();
            System.Console.Out.WriteLine("Total: " + gc.TotalCount());
            gc1.PrintKeySet();
            System.Console.Out.WriteLine("another entry set:\n" + gc1.EntrySet());
            ClassicCounter <IList <string> > c = gc.CounterView();

            System.Console.Out.WriteLine("string representation of counter view:");
            System.Console.Out.WriteLine(c.ToString());
            double d1 = c.GetCount(Arrays.AsList(new string[] { "a", "j", "x" }));
            double d2 = c.GetCount(Arrays.AsList(new string[] { "a", "j", "w" }));

            System.Console.Out.WriteLine(d1 + " " + d2);
            ClassicCounter <IList <string> > c1 = gc1.CounterView();

            System.Console.Out.WriteLine("Count of {j,x} -- should be 3.0\t" + c1.GetCount(Arrays.AsList(new string[] { "j", "x" })));
            System.Console.Out.WriteLine(c.KeySet() + " size " + c.KeySet().Count);
            System.Console.Out.WriteLine(c1.KeySet() + " size " + c1.KeySet().Count);
            System.Console.Out.WriteLine(c1.Equals(c));
            System.Console.Out.WriteLine(c.Equals(c1));
            System.Console.Out.WriteLine(c.Equals(c));
            System.Console.Out.WriteLine("### testing equality of regular Counter...");
            ClassicCounter <string> z1 = new ClassicCounter <string>();
            ClassicCounter <string> z2 = new ClassicCounter <string>();

            z1.IncrementCount("a1");
            z1.IncrementCount("a2");
            z2.IncrementCount("b");
            System.Console.Out.WriteLine(z1.Equals(z2));
            System.Console.Out.WriteLine(z1.ToString());
            System.Console.Out.WriteLine(z1.KeySet().ToString());
        }
Ejemplo n.º 3
0
        /// <summary>Equivalent to incrementCount( new Object[] { first, second, third }, count ).</summary>
        /// <remarks>
        /// Equivalent to incrementCount( new Object[] { first, second, third }, count ).
        /// Makes the special case easier, and also more efficient.
        /// </remarks>
        public virtual void IncrementCount3D(K first, K second, K third, double count)
        {
            if (depth != 3)
            {
                WrongDepth();
            }
            //throws exception
            this.AddToTotal(count);
            GeneralizedCounter <K> next = this.ConditionalizeHelper(first);

            next.IncrementCount2D(second, third, count);
        }
Ejemplo n.º 4
0
        public virtual GeneralizedCounter <K> ReverseKeys()
        {
            GeneralizedCounter <K> result = new GeneralizedCounter <K>();
            ICollection <KeyValuePair <IList <K>, double> > entries = EntrySet();

            foreach (KeyValuePair <IList <K>, double> entry in entries)
            {
                IList <K> list  = entry.Key;
                double    count = entry.Value;
                Java.Util.Collections.Reverse(list);
                result.IncrementCount(list, count);
            }
            return(result);
        }
Ejemplo n.º 5
0
        /// <summary>same as incrementCount(List, double) but as if Object o were at the end of the list</summary>
        public virtual void IncrementCount(IList <K> l, K o, double count)
        {
            if (l.Count != depth - 1)
            {
                WrongDepth();
            }
            GeneralizedCounter <K> next = this;

            foreach (K o2 in l)
            {
                next.AddToTotal(count);
                next = next.ConditionalizeHelper(o2);
            }
            next.AddToTotal(count);
            next.IncrementCount1D(o, count);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Like
        /// <see cref="ClassicCounter{E}"/>
        /// , this currently returns true if the count is
        /// explicitly 0.0 for something
        /// </summary>
        public virtual bool ContainsKey(IList <K> key)
        {
            //     if(! (key instanceof Object[]))
            //       return false;
            //    Object[] o = (Object[]) key;
            GeneralizedCounter <K> next = this;

            for (int i = 0; i < key.Count - 1; i++)
            {
                next = next.ConditionalizeHelper(key[i]);
                if (next == null)
                {
                    return(false);
                }
            }
            return(next.map.Contains(key[key.Count - 1]));
        }
Ejemplo n.º 7
0
 /* haven't decided about access for this one yet */
 private GeneralizedCounter <K> ConditionalizeHelper(K o)
 {
     if (depth > 1)
     {
         GeneralizedCounter <K> next = ErasureUtils.UncheckedCast(map[o]);
         if (next == null)
         {
             // adds a new GeneralizedCounter if needed
             map[o] = (next = new GeneralizedCounter <K>(depth - 1));
         }
         return(next);
     }
     else
     {
         throw new Exception("Error -- can't conditionalize a distribution of depth 1");
     }
 }
Ejemplo n.º 8
0
        /// <summary>
        /// A convenience method equivalent to <code>
        /// <see cref="GeneralizedCounter{K}.GetCounts(System.Collections.IList{E})"/>
        /// ({o1,o2,o3})</code>; works only for depth 3
        /// GeneralizedCounters
        /// </summary>
        public virtual double GetCount(K o1, K o2, K o3)
        {
            if (depth != 3)
            {
                WrongDepth();
            }
            GeneralizedCounter <K> gc1 = ErasureUtils.UncheckedCast <GeneralizedCounter <K> >(map[o1]);

            if (gc1 == null)
            {
                return(0.0);
            }
            else
            {
                return(gc1.GetCount(o2, o3));
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// returns a GeneralizedCounter conditioned on the objects in the
        /// <see cref="System.Collections.IList{E}"/>
        /// argument. The length of the argument
        /// <see cref="System.Collections.IList{E}"/>
        /// must be less than the depth of the GeneralizedCounter.
        /// </summary>
        public virtual GeneralizedCounter <K> Conditionalize(IList <K> l)
        {
            int n = l.Count;

            if (n >= Depth())
            {
                throw new Exception("Error -- attempted to conditionalize a GeneralizedCounter of depth " + Depth() + " on a vector of length " + n);
            }
            else
            {
                GeneralizedCounter <K> next = this;
                foreach (K o in l)
                {
                    next = next.ConditionalizeHelper(o);
                }
                return(next);
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Adds to count for the
        /// <see cref="GeneralizedCounter{K}.Depth()"/>
        /// -dimensional key
        /// <paramref name="l"/>
        /// .
        /// </summary>
        public virtual void IncrementCount(IList <K> l, double count)
        {
            if (l.Count != depth)
            {
                WrongDepth();
            }
            //throws exception
            GeneralizedCounter <K> next = this;
            IEnumerator <K>        i    = l.GetEnumerator();
            K o = i.Current;

            while (i.MoveNext())
            {
                next.AddToTotal(count);
                next = next.ConditionalizeHelper(o);
                o    = i.Current;
            }
            next.IncrementCount1D(o, count);
        }
Ejemplo n.º 11
0
 private void PrettyPrint(PrintWriter pw, string buffer, string bufferIncrement)
 {
     if (depth == 1)
     {
         foreach (KeyValuePair <object, double> e in EntrySet())
         {
             object key   = e.Key;
             double count = e.Value;
             pw.Println(buffer + key + "\t" + count);
         }
     }
     else
     {
         foreach (K key in TopLevelKeySet())
         {
             GeneralizedCounter <K> gc1 = Conditionalize(Arrays.AsList(ErasureUtils.UncheckedCast <K[]>(new object[] { key })));
             pw.Println(buffer + key + "\t" + gc1.TotalCount());
             gc1.PrettyPrint(pw, buffer + bufferIncrement, bufferIncrement);
         }
     }
 }
Ejemplo n.º 12
0
        public virtual string ToString(string param)
        {
            switch (param)
            {
            case "contingency":
            {
                StringBuilder sb = new StringBuilder();
                foreach (K obj in ErasureUtils.SortedIfPossible(TopLevelKeySet()))
                {
                    sb.Append(obj);
                    sb.Append(" = ");
                    GeneralizedCounter <K> gc = ConditionalizeOnce(obj);
                    sb.Append(gc);
                    sb.Append("\n");
                }
                return(sb.ToString());
            }

            case "sorted":
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("{\n");
                foreach (K obj in ErasureUtils.SortedIfPossible(TopLevelKeySet()))
                {
                    sb.Append(obj);
                    sb.Append(" = ");
                    GeneralizedCounter <K> gc = ConditionalizeOnce(obj);
                    sb.Append(gc);
                    sb.Append("\n");
                }
                sb.Append("}\n");
                return(sb.ToString());
            }

            default:
            {
                return(ToString());
            }
            }
        }
Ejemplo n.º 13
0
 internal CounterView(GeneralizedCounter <K> _enclosing)
 {
     this._enclosing = _enclosing;
 }