Exemple #1
0
        /// <summary>
        /// Create a collection of clusters that should be presented to the user
        /// </summary>
        /// <param name="strategy"></param>
        /// <param name="listView"></param>
        /// <param name="column"></param>
        /// <returns></returns>
        virtual protected List <ICluster> Cluster(IClusteringStrategy strategy, ObjectListView listView, OLVColumn column)
        {
            // Build a map that correlates cluster key to clusters
            NullableDictionary <object, ICluster> map = new NullableDictionary <object, ICluster>();
            int count = 0;

            //++Added by KhoiVV
            if (listView.Objects == null)
            {
                return(new List <ICluster>(map.Values));
            }
            //--Added by KhoiVV

            foreach (object model in listView.ObjectsForClustering)
            {
                this.ClusterOneModel(strategy, map, model);

                if (count++ > this.MaxObjectsToConsider)
                {
                    break;
                }
            }

            // Now that we know exactly how many items are in each cluster, create a label for it
            foreach (ICluster cluster in map.Values)
            {
                cluster.DisplayLabel = strategy.GetClusterDisplayLabel(cluster);
            }

            return(new List <ICluster>(map.Values));
        }
        /// <summary>
        /// Create a collection of clusters that should be presented to the user
        /// </summary>
        /// <param name="strategy"></param>
        /// <param name="listView"></param>
        /// <param name="column"></param>
        /// <returns></returns>
        protected virtual List<ICluster> Cluster(IClusteringStrategy strategy, ObjectListView listView, OLVColumn column)
        {
            // Build a map that correlates cluster key to clusters
            var map = new NullableDictionary<object, ICluster>();
            int count = 0;
            foreach (object model in listView.Objects)
            {
                object key = strategy.GetClusterKey(model);
                if (key == DBNull.Value)
                    key = null;
                if (key == null && !TreatNullAsDataValue)
                    continue;
                if (map.ContainsKey(key))
                    map[key].Count += 1;
                else
                    map[key] = strategy.CreateCluster(key);

                // Check our limit
                count += 1;
                if (count > MaxObjectsToConsider)
                    break;
            }

            // Now that we know exactly how many items are in each cluster, create a label for it
            foreach (ICluster cluster in map.Values)
                cluster.DisplayLabel = strategy.GetClusterDisplayLabel(cluster);

            return new List<ICluster>(map.Values);
        }
Exemple #3
0
 public SearchEngine(string[] keywords, IDocumentSanitizer sanitizer)
 {
     this.sanitizer = sanitizer;
     this.stemmer   = new PorterStemmer();
     this.documents = new List <Document>();
     this.keywords  = new HashSet <string>();
     this.inverseDocumentFrequencies = new List <double>();
     this.feedbackCalculator         = new RocchioFeedbackCalculator();
     foreach (var keyword in keywords)
     {
         var stemmed = this.stemmer.StemWord(keyword);
         this.keywords.Add(stemmed);
     }
     this.clusteringStrategy = new KMeansClustering(9, 100, new Random());
 }
Exemple #4
0
        private void ClusterOneModel(IClusteringStrategy strategy, NullableDictionary <object, ICluster> map, object model)
        {
            object clusterKey = strategy.GetClusterKey(model);

            // If the returned value is an IEnumerable, that means the given model can belong to more than one cluster
            IEnumerable keyEnumerable = clusterKey as IEnumerable;

            if (clusterKey is string || keyEnumerable == null)
            {
                keyEnumerable = new object[] { clusterKey }
            }
            ;

            // Deal with nulls and DBNulls
            ArrayList nullCorrected = new ArrayList();

            foreach (object key in keyEnumerable)
            {
                if (key == null || key == System.DBNull.Value)
                {
                    if (this.TreatNullAsDataValue)
                    {
                        nullCorrected.Add(null);
                    }
                }
                else
                {
                    nullCorrected.Add(key);
                }
            }

            // Group by key
            foreach (object key in nullCorrected)
            {
                if (map.ContainsKey(key))
                {
                    map[key].Count += 1;
                }
                else
                {
                    map[key] = strategy.CreateCluster(key);
                }
            }
        }
        /// <summary>
        /// Create a collection of clusters that should be presented to the user
        /// </summary>
        /// <param name="strategy"></param>
        /// <param name="listView"></param>
        /// <param name="column"></param>
        /// <returns></returns>
        virtual protected List <ICluster> Cluster(IClusteringStrategy strategy, ObjectListView listView, OLVColumn column)
        {
            // Build a map that correlates cluster key to clusters
            NullableDictionary <object, ICluster> map = new NullableDictionary <object, ICluster>();
            int count = 0;

            foreach (object model in listView.Objects)
            {
                object key = strategy.GetClusterKey(null, model);
                if (key == System.DBNull.Value)
                {
                    key = null;
                }
                if (key == null && !this.TreatNullAsDataValue)
                {
                    continue;
                }
                if (map.ContainsKey(key))
                {
                    map[key].Count += 1;
                }
                else
                {
                    map[key] = strategy.CreateCluster(key);
                }

                // Check our limit
                count += 1;
                if (count > this.MaxObjectsToConsider)
                {
                    break;
                }
            }

            // Now that we know exactly how many items are in each cluster, create a label for it
            foreach (ICluster cluster in map.Values)
            {
                cluster.DisplayLabel = strategy.GetClusterDisplayLabel(cluster);
            }

            return(new List <ICluster>(map.Values));
        }
Exemple #6
0
        protected override List <ICluster> Cluster(IClusteringStrategy strategy, ObjectListView listView,
                                                   OLVColumn column)
        {
            if (column is MyOLVColumn mycolumn && mycolumn.ClusterGetter != null)
            {
                var list = mycolumn.ClusterGetter.Invoke(listView.ObjectsForClustering.Cast <CapturePacket>());
                if (strategy is ClusteringStrategy cstrategy)
                {
                    foreach (var c in list)
                    {
                        string format = (c.Count == 1)
                            ? cstrategy.DisplayLabelFormatSingular
                            : cstrategy.DisplayLabelFormatPlural;
                        c.DisplayLabel = string.IsNullOrEmpty(format)
                            ? c.DisplayLabel
                            : string.Format(format, c.DisplayLabel, c.Count);
                    }
                }

                return(list);
            }

            return(base.Cluster(strategy, listView, column));
        }
 /// <summary>
 /// Order the given list of clusters in the manner in which they should be presented to the user.
 /// </summary>
 /// <param name="strategy"></param>
 /// <param name="clusters"></param>
 virtual protected void SortClusters(IClusteringStrategy strategy, List <ICluster> clusters)
 {
     clusters.Sort();
 }
Exemple #8
0
 /// <summary>
 /// Order the given list of clusters in the manner in which they should be presented to the user.
 /// </summary>
 /// <param name="strategy"></param>
 /// <param name="clusters"></param>
 virtual protected void SortClusters(IClusteringStrategy strategy, List<ICluster> clusters) {
     clusters.Sort();
 }
Exemple #9
0
        private void ClusterOneModel(IClusteringStrategy strategy, NullableDictionary<object, ICluster> map, object model) {
            object clusterKey = strategy.GetClusterKey(model);

            // If the returned value is an IEnumerable, that means the given model can belong to more than one cluster
            IEnumerable keyEnumerable = clusterKey as IEnumerable;
            if (clusterKey is string || keyEnumerable == null)
                keyEnumerable = new object[] {clusterKey};

            // Deal with nulls and DBNulls
            ArrayList nullCorrected = new ArrayList();
            foreach (object key in keyEnumerable) {
                if (key == null || key == System.DBNull.Value) {
                    if (this.TreatNullAsDataValue)
                        nullCorrected.Add(null);
                } else nullCorrected.Add(key);
            }

            // Group by key
            foreach (object key in nullCorrected) {
                if (map.ContainsKey(key))
                    map[key].Count += 1;
                else
                    map[key] = strategy.CreateCluster(key);
            }
        }
Exemple #10
0
        /// <summary>
        /// Create a collection of clusters that should be presented to the user
        /// </summary>
        /// <param name="strategy"></param>
        /// <param name="listView"></param>
        /// <param name="column"></param>
        /// <returns></returns>
        virtual protected List<ICluster> Cluster(IClusteringStrategy strategy, ObjectListView listView, OLVColumn column) {
            // Build a map that correlates cluster key to clusters
            NullableDictionary<object, ICluster> map = new NullableDictionary<object, ICluster>();
            int count = 0;
            foreach (object model in listView.ObjectsForClustering) {
                this.ClusterOneModel(strategy, map, model);

                if (count++ > this.MaxObjectsToConsider)
                    break;
            }

            // Now that we know exactly how many items are in each cluster, create a label for it
            foreach (ICluster cluster in map.Values)
                cluster.DisplayLabel = strategy.GetClusterDisplayLabel(cluster);

            return new List<ICluster>(map.Values);
        }
Exemple #11
0
        /// <summary>
        /// Create a collection of clusters that should be presented to the user
        /// </summary>
        /// <param name="strategy"></param>
        /// <param name="listView"></param>
        /// <param name="column"></param>
        /// <returns></returns>
        virtual protected List<ICluster> Cluster(IClusteringStrategy strategy, ObjectListView listView, OLVColumn column) {
            // Build a map that correlates cluster key to clusters
            NullableDictionary<object, ICluster> map = new NullableDictionary<object, ICluster>();
            int count = 0;
            foreach (object model in listView.Objects) {
                object key = strategy.GetClusterKey(model);
                if (key == System.DBNull.Value)
                    key = null;
                if (key == null && !this.TreatNullAsDataValue)
                    continue;
                if (map.ContainsKey(key))
                    map[key].Count += 1;
                else
                    map[key] = strategy.CreateCluster(key);

                // Check our limit
                count += 1;
                if (count > this.MaxObjectsToConsider)
                    break;
            }

            // Now that we know exactly how many items are in each cluster, create a label for it
            foreach (ICluster cluster in map.Values)
                cluster.DisplayLabel = strategy.GetClusterDisplayLabel(cluster);

            return new List<ICluster>(map.Values);
        }