private double GetConfidence(string X, string XY, ItemsDictionary allFrequentItems)
        {
            double supportX  = allFrequentItems[X].Support;
            double supportXY = allFrequentItems[XY].Support;

            return(supportXY / supportX);
        }
        private Dictionary <string, Dictionary <string, double> > GetClosedItemSets(ItemsDictionary allFrequentItems)
        {
            var closedItemSets = new Dictionary <string, Dictionary <string, double> >();
            int i = 0;

            foreach (var item in allFrequentItems)
            {
                Dictionary <string, double> parents = GetItemParents(item.ItemIDs, ++i, allFrequentItems);

                if (CheckIsClosed(item.ItemIDs, parents, allFrequentItems))
                {
                    closedItemSets.Add(item.ItemIDs, parents);
                }
            }

            return(closedItemSets);
        }
        private Dictionary <string, double> GetItemParents(string child, int index, ItemsDictionary allFrequentItems)
        {
            var parents          = new Dictionary <string, double>();
            int childSplitLength = child.Split(';').Length;

            for (int j = index; j < allFrequentItems.Count; j++)
            {
                string parent = allFrequentItems[j].ItemIDs;

                if (parent.Split(';').Length == childSplitLength + 1)
                {
                    if (CheckIsSubset(child, parent))
                    {
                        parents.Add(parent, allFrequentItems[parent].Support);
                    }
                }
            }

            return(parents);
        }
        private HashSet <Rule> GenerateRules(ItemsDictionary allFrequentItems)
        {
            var rulesSet = new HashSet <Rule>();

            foreach (var item in allFrequentItems)
            {
                //if we have more than one item in the itemset
                if (item.ItemIDs.Contains(";"))
                {
                    IEnumerable <string> subsetsList = GenerateSubsets(item.ItemIDs);

                    foreach (var subset in subsetsList)
                    {
                        string remaining = GetRemaining(subset, item.ItemIDs);
                        Rule   rule      = new Rule(subset, remaining, 0, item.Support);

                        rulesSet.Add(rule);
                    }
                }
            }

            return(rulesSet);
        }
        public Output ProcessTransaction(double minSupport, double minConfidence, IEnumerable <string> items, IEnumerable <string> transactions, bool @class)
        {
            Print(minSupport, minConfidence);
            IList <Item> frequentItems = GetL1FrequentItems(minSupport, items, transactions);

            Print(frequentItems);
            ItemsDictionary allFrequentItems = new ItemsDictionary();

            allFrequentItems.ConcatItems(frequentItems);
            IDictionary <string, double> candidates = new Dictionary <string, double>();
            double transactionsCount = transactions.Count();

            do
            {
                candidates    = GenerateCandidates(frequentItems, transactions);
                frequentItems = GetFrequentItems(candidates, minSupport, transactionsCount);
                Print(frequentItems);
                allFrequentItems.ConcatItems(frequentItems);
            }while (candidates.Count != 0);

            HashSet <Rule> rules       = GenerateRules(allFrequentItems);
            IList <Rule>   strongRules = GetStrongRules(minConfidence, rules, allFrequentItems);
            Dictionary <string, Dictionary <string, double> > closedItemSets = GetClosedItemSets(allFrequentItems);
            IList <string> maximalItemSets = GetMaximalItemSets(closedItemSets);

            Print(strongRules);

            // Rule Prunning
            var prune = new RulePrunning();
            var rulesAfterPrunning = prune
                                     .DatabaseCoverageMethod(strongRules, transactions.ToList());

            Console.WriteLine("Rules AFTER prunning with Database Coverage method.");
            Print(rulesAfterPrunning);

            // Rule Ranking
            var rank        = new RuleRanking();
            var rankedRules = rank.ACS(rulesAfterPrunning);

            Console.WriteLine("Rules ranked using ACS");
            Print(rankedRules);

            rankedRules = rank.CSA(rulesAfterPrunning);
            Console.WriteLine("Rules ranked using CSA");
            Print(rankedRules);

            var sb = new StringBuilder($"Itemset\t\tClass\n=====================\n");

            foreach (var rule in rankedRules)
            {
                sb.AppendFormat("{0};{1}\t{2}\n", rule.X, rule.Y, @class);
            }
            sb.AppendLine();
            Console.WriteLine(sb);


            //return new Output
            //{
            //    StrongRules = strongRules,
            //    MaximalItemSets = maximalItemSets,
            //    ClosedItemSets = closedItemSets,
            //    FrequentItems = allFrequentItems
            //};
            return(null);
        }
        private void AddStrongRule(Rule rule, string XY, List <Rule> strongRules, double minConfidence, ItemsDictionary allFrequentItems)
        {
            double confidence = GetConfidence(rule.X, XY, allFrequentItems);

            if (confidence >= minConfidence)
            {
                Rule newRule = new Rule(rule.X, rule.Y, confidence, rule.Support);
                strongRules.Add(newRule);
            }

            confidence = GetConfidence(rule.Y, XY, allFrequentItems);

            if (confidence >= minConfidence)
            {
                Rule newRule = new Rule(rule.Y, rule.X, confidence, rule.Support);
                strongRules.Add(newRule);
            }
        }
        private IList <Rule> GetStrongRules(double minConfidence, HashSet <Rule> rules, ItemsDictionary allFrequentItems)
        {
            var strongRules = new List <Rule>();

            foreach (Rule rule in rules)
            {
                string xy = _sorter.Sort(rule.X + ";" + rule.Y);
                AddStrongRule(rule, xy, strongRules, minConfidence, allFrequentItems);
            }

            strongRules.Sort();
            return(strongRules);
        }
        private bool CheckIsClosed(string child, Dictionary <string, double> parents, ItemsDictionary allFrequentItems)
        {
            foreach (string parent in parents.Keys)
            {
                if (allFrequentItems[child].Support == allFrequentItems[parent].Support)
                {
                    return(false);
                }
            }

            return(true);
        }