public void ShouldCountNumberOfObjectsOfGivenType()
        {
            var bag = new Dictionary<string, object>();
            Assert.That(bag.GetCount(typeof(Url)), Is.EqualTo(0));

            bag.Add("1", new Url("/1"));
            bag.Add("2", new Url("/2"));
            bag.Add("3", new Url("/3"));

            Assert.That(bag.GetCount(typeof(Url)), Is.EqualTo(3));
        }
Ejemplo n.º 2
0
        public void ShouldCountNumberOfObjectsOfGivenType()
        {
            var bag = new Dictionary <string, object>();

            Assert.That(bag.GetCount(typeof(Url)), Is.EqualTo(0));

            bag.Add("1", new Url("/1"));
            bag.Add("2", new Url("/2"));
            bag.Add("3", new Url("/3"));

            Assert.That(bag.GetCount(typeof(Url)), Is.EqualTo(3));
        }
        /// <summary>
        ///     Extract device capabilities
        /// </summary>
        /// <param name="portableDeviceClass"></param>
        internal void ExtractDeviceCapabilities(PortableDeviceClass portableDeviceClass)
        {
            if (portableDeviceClass == null)
            {
                throw new ArgumentNullException("portableDeviceClass");
            }

            try
            {
                IPortableDeviceCapabilities capabilities;
                portableDeviceClass.Capabilities(out capabilities);

                if (capabilities == null)
                {
                    Trace.WriteLine("Cannot extract capabilities from device");
                    throw new PortableDeviceException("Cannot extract capabilities from device");
                }

                IPortableDevicePropVariantCollection functionalCategories;
                capabilities.GetFunctionalCategories(out functionalCategories);

                if (functionalCategories == null)
                {
                    throw new PortableDeviceException("Failed to extract functionnal categories");
                }

                uint countCategories = 1;
                functionalCategories.GetCount(ref countCategories);
                var values = new tag_inner_PROPVARIANT();

                string categoryName;
                Guid   currentGuid;
                for (uint i = 0; i < countCategories; i++)
                {
                    functionalCategories.GetAt(i, ref values);
                    var pValues = (IPortableDeviceValues) new PortableDeviceValuesClass();

                    pValues.SetValue(ref PortableDevicePKeys.WPD_COMMAND_CAPABILITIES_GET_SUPPORTED_FUNCTIONAL_CATEGORIES, ref values);
                    pValues.GetStringValue(ref PortableDevicePKeys.WPD_COMMAND_CAPABILITIES_GET_SUPPORTED_FUNCTIONAL_CATEGORIES, out categoryName);
                    currentGuid = new Guid(categoryName);
                    this.functionalCategories.Add(currentGuid, new FunctionalCategory(
                                                      portableDeviceClass,
                                                      currentGuid,
                                                      PortableDeviceHelpers.GetKeyNameFromGuid(currentGuid)));
                }
            }
            catch (Exception ex)
            {
                throw new PortableDeviceException("Error on extract device capabilities", ex);
            }
        }
Ejemplo n.º 4
0
        private void ExtractContentType(PortableDeviceClass portableDeviceClass, Guid functionalCategory)
        {
            if (portableDeviceClass == null)
            {
                throw new ArgumentNullException("portableDeviceClass");
            }

            try
            {
                PortableDeviceApiLib.IPortableDeviceCapabilities capabilities;
                portableDeviceClass.Capabilities(out capabilities);

                if (capabilities == null)
                {
                    System.Diagnostics.Trace.WriteLine("Cannot extract capabilities from device");
                    throw new PortableDeviceException("Cannot extract capabilities from device");
                }


                PortableDeviceApiLib.IPortableDeviceValues pValues = (PortableDeviceApiLib.IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValuesClass();


                //Functional objects variables
                IPortableDevicePropVariantCollection contentTypes;
                uint countObjects            = 1;
                tag_inner_PROPVARIANT values = new tag_inner_PROPVARIANT();
                string contentTypeName;
                Guid   currentContentTypeGuid;
                capabilities.GetSupportedContentTypes(ref functionalCategory, out contentTypes);

                contentTypes.GetCount(ref countObjects);
                for (uint i = 0; i < countObjects; i++)
                {
                    contentTypes.GetAt(i, ref values);

                    pValues.SetValue(ref PortableDevicePKeys.WPD_COMMAND_CAPABILITIES_GET_SUPPORTED_CONTENT_TYPES, ref values);
                    pValues.GetStringValue(ref PortableDevicePKeys.WPD_COMMAND_CAPABILITIES_GET_SUPPORTED_CONTENT_TYPES, out contentTypeName);
                    currentContentTypeGuid = new Guid(contentTypeName);
                    this.contentTypes.Add(currentContentTypeGuid, new ContentType(
                                              portableDeviceClass,
                                              currentContentTypeGuid,
                                              PortableDeviceHelpers.GetKeyNameFromGuid(currentContentTypeGuid)));
                }
            }
            catch (Exception ex)
            {
                throw new PortableDeviceException("Error on extract functional object", ex);
            }
        }
Ejemplo n.º 5
0
        private void ExtractSupportedFormat(PortableDeviceClass portableDeviceClass, Guid contentType)
        {
            if (portableDeviceClass == null)
            {
                throw new PortableDeviceException("");
            }

            IPortableDeviceCapabilities capabilities;

            portableDeviceClass.Capabilities(out capabilities);

            if (capabilities == null)
            {
                Trace.WriteLine("Cannot extract capabilities from device");
                throw new PortableDeviceException("Cannot extract capabilities from device");
            }


            var pValues = (IPortableDeviceValues) new PortableDeviceValuesClass();


            //Functional objects variables
            IPortableDevicePropVariantCollection formats;
            uint   countObjects = 1;
            var    values       = new tag_inner_PROPVARIANT();
            string formatName;
            Guid   currentFormat;

            capabilities.GetSupportedFormats(ref contentType, out formats);

            formats.GetCount(ref countObjects);
            for (uint i = 0; i < countObjects; i++)
            {
                formats.GetAt(i, ref values);

                pValues.SetValue(ref PortableDevicePKeys.WPD_COMMAND_CAPABILITIES_GET_SUPPORTED_FORMATS, ref values);
                pValues.GetStringValue(ref PortableDevicePKeys.WPD_COMMAND_CAPABILITIES_GET_SUPPORTED_FORMATS, out formatName);
                currentFormat = new Guid(formatName);
                this.formats[currentFormat] = PortableDeviceHelpers.GetKeyNameFromGuid(currentFormat);
            }
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Gets the children count.
 /// </summary>
 /// <returns>The children count.</returns>
 public int GetChildrenCount()
 {
     return(children.GetCount());
 }
        /// <summary>
        ///     Extract event supported by device
        /// </summary>
        /// <param name="portableDeviceClass"></param>
        internal void ExtractEvents(PortableDeviceClass portableDeviceClass)
        {
            IPortableDeviceCapabilities capabilities;

            portableDeviceClass.Capabilities(out capabilities);

            IPortableDevicePropVariantCollection events;

            capabilities.GetSupportedEvents(out events);

            uint countEvents = 0;

            events.GetCount(ref countEvents);

            var pValues = (IPortableDeviceValues) new PortableDeviceValuesClass();
            var evt     = new tag_inner_PROPVARIANT();

            Guid eventName;
            IPortableDeviceValues          eventOptions;
            PortableDeviceEventDescription eventDescription;

            for (uint i = 0; i < countEvents; i++)
            {
                events.GetAt(i, ref evt);
                pValues.SetValue(ref PortableDevicePKeys.WPD_EVENT_PARAMETER_EVENT_ID, ref evt);
                pValues.GetGuidValue(ref PortableDevicePKeys.WPD_EVENT_PARAMETER_EVENT_ID, out eventName);

                eventDescription = new PortableDeviceEventDescription(eventName, PortableDeviceHelpers.GetKeyNameFromGuid(eventName));

                //Retrieve options
                try
                {
                    // Event option isn't always present, so ...
                    eventOptions = (IPortableDeviceValues) new PortableDeviceValuesClass();
                    capabilities.GetEventOptions(ref eventName, out eventOptions);


                    //eventOptions.GetBoolValue(ref PortableDevicePKeys.WPD_EVENT_OPTION_IS_AUTOPLAY_EVENT, out isAutoPlayEvent);
                    //eventOptions.GetBoolValue(ref PortableDevicePKeys.WPD_EVENT_OPTION_IS_BROADCAST_EVENT, out isBroadcastEvent);

                    //eventDescription.AddOptions(new PortableDeviceEventOption()
                    //{
                    //    Guid = PortableDevicePKeys.WPD_EVENT_OPTION_IS_BROADCAST_EVENT.fmtid,
                    //    Name = PortableDeviceHelpers.GetKeyNameFromGuid(PortableDevicePKeys.WPD_EVENT_OPTION_IS_BROADCAST_EVENT.fmtid),
                    //    Value = isBroadcastEvent,
                    //    ValueType = TypeCode.Boolean
                    //});

                    //eventDescription.AddOptions(new PortableDeviceEventOption()
                    //{
                    //    Guid = PortableDevicePKeys.WPD_EVENT_OPTION_IS_AUTOPLAY_EVENT.fmtid,
                    //    Name = PortableDeviceHelpers.GetKeyNameFromGuid(PortableDevicePKeys.WPD_EVENT_OPTION_IS_AUTOPLAY_EVENT.fmtid),
                    //    Value = isAutoPlayEvent,
                    //    ValueType = TypeCode.Boolean
                    //});
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                    Debug.WriteLine(ex.StackTrace);
                }

                this.events.Add(eventDescription, PortableDevicePKeys.WPD_EVENT_PARAMETER_EVENT_ID);
            }
        }
Ejemplo n.º 8
0
        public static void TagTrigram(Dictionary WordsCount, Dictionary NGrams)
        {
            //extract tags from n-grams
            var S = NGrams.Keys.SelectMany(p => p.Split(' ')).Distinct().Where(p => p != "*" && p != "STOP").ToArray();

            string line;
            while ((line = Console.ReadLine()) != null)
            {
                var words = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();

                var pi_k_1_w_u = new Dictionary<string, double> { { "* *", 1 } };
                var bp = new List<Dictionary<string, string>>();

                for (var k = 0; k < words.Length; k++)
                {
                    string word = words[k];
                    string[] S_k = null;
                    string[] S_k_1 = null;

                    if (k == 0)
                    {
                        S_k = S_k_1 = new[] { "*" };
                    }
                    else if (k == 1)
                    {
                        S_k = S.ToArray();
                        S_k_1 = new[] { "*" };
                    }
                    else
                    {
                        S_k = S_k_1 = S.ToArray();
                    }

                    var pi_k_u_v = new Dictionary<string, double>();

                    bp.Add(new Dictionary<string, string>());

                    foreach (var u in S_k)
                    {
                        foreach (var v in S)
                        {
                            var pi_k_tmp = new Dictionary<string, double>();

                            foreach (var w in S_k_1)
                            {
                                var ngram = w + " " + u + " " + v;
                                var ngram_1 = w + " " + u;

                                var cnt_word_y = WordsCount.GetSafely(word + " " + v);
                                var cnt_y = WordsCount.GetCount(v, 1);

                                if (cnt_word_y == null)
                                {
                                    if (WordsCount.GetCount(word, 0) != 0)
                                    {
                                        //exist in dictionary
                                        cnt_word_y = 0;
                                    }
                                    else
                                    {
                                        //not exist in dictionary => it is rare
                                        cnt_word_y = WordsCount.GetSafely("_RARE_ " + v);
                                    }
                                }

                                var q_v_w_u = (double)(NGrams.GetSafely(ngram) / (double)NGrams.GetSafely(ngram_1));
                                var e_v = (double)(cnt_word_y / (double)cnt_y);

                                var p = pi_k_1_w_u[ngram_1] * q_v_w_u * e_v * 100;

                                pi_k_tmp.Add(w, p);
                            }

                            var max_w = pi_k_tmp.OrderByDescending(p => p.Value).First();

                            pi_k_u_v.Add(u + " " + v, max_w.Value);

                            bp.Last().Add(u + " " + v, max_w.Key);
                        }
                    }

                    pi_k_1_w_u = pi_k_u_v;

                    if (words.Length - 1 == k)
                    {
                        var tags = new string[words.Length];

                        double max_p = 0;
                        //Last word
                        foreach (var u in S_k)
                        {
                            foreach (var v in S)
                            {
                                var ngram = u + " " + v + " STOP";
                                var ngram_1 = u + " " + v;

                                var q_stop = (double)(NGrams.GetSafely(ngram) / (double)NGrams.GetSafely(ngram_1));
                                var p = q_stop * pi_k_u_v[ngram_1];

                                if (max_p < p)
                                {
                                    max_p = p;
                                    tags[k] = v;
                                    tags[k - 1] = u;
                                }
                            }
                        }

                        //backword iter
                        for (int i = (k - 2); i != -1; i--)
                        {
                            var ptr = bp.ElementAt(i + 2);

                            tags[i] = ptr[tags[i + 1] + " " + tags[i + 2]];
                        }

                        for (var n = 0; n < words.Length; n++)
                        {
                            Console.Write("{0} {1}", words[n], tags[n]);
                            Console.Write("\t");
                        }
                    }
                }

                Console.Write("\n");
            }
        }