Hello,everyone..I am a beginner in using c#.
Now, i have some problems in my project. I hope someone can help me.
Below is my coding:-

    public partial class Form1 : Form
    {
        string file;
        double allTransaction;

        SortedList frequency = new SortedList();
        SortedList<string, double> m_dicAllFrequentItems = new SortedList<string, double>();
        SortedList<int, String> m_dicTransactions = new SortedList<int, string>();
        SortedList<string, double> dicFrequency = new SortedList<string, double>();
        int m_nLastTransId;

        public Form1()
        {
            InitializeComponent();
        }


        //browse file
        private void button1_Click(object sender, EventArgs e)
        {
            int size = -1;
            DialogResult result = new DialogResult(); //Dialog result sets the openfile dialog result
            result = openFileDialog1.ShowDialog(); // Show the dialog.

            if (result == DialogResult.OK) // Test result.
            {
                textBox1.Text = openFileDialog1.FileName;
                file = openFileDialog1.FileName;

                try
                {
                    StringBuilder sb = new StringBuilder();

                    string text = File.ReadAllText(file);
                    textBox2.Text = text;
                    size = text.Length;
                }
                catch (IOException)
                {
                }
            }
        }


        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            //list all the content
        }


        //user enter min.threshold 
        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }


        string[] c;
        List<String> sortlist;
        string[] nCombination;

        private void button2_Click(object sender, EventArgs e)
        {
            ColumnHeader itemset = new ColumnHeader();
            itemset.Text = "Itemset";
            itemset.Width = 100;
            listView1.Columns.Add(itemset);

            ColumnHeader itemsupport = new ColumnHeader();
            itemsupport.Text = "Support";
            itemsupport.Width = 50;
            listView1.Columns.Add(itemsupport);

            listView1.Items.Clear();
            int size = -1;
            string output = "";

            string text = File.ReadAllText(file);
            size = text.Length;
            // int count = 0;
            string b;


            b = text.Replace("\r\n", " ");
            output += b;
            c = output.Split(' ');
            sortlist = new List<String>(c);

            sortlist.Sort();

            var fr = sortlist.GroupBy(n => n).Select(n => new { Value = n.Key, Count = n.Count() });
            StringBuilder sb = new StringBuilder();

            foreach (var f in fr)
            {
                if (f.Count > 1)
                {
                    ListViewItem lvi = new ListViewItem(f.Value);
                    frequency.Add(f.Value, f.Count);
                    dicFrequency.Add(f.Value, f.Count);
                    lvi.SubItems.Add((f.Count).ToString());
                    listView1.Items.Add(lvi);
                }

            }


            nCombination = new string[frequency.Count];
            int ii = 0;

            foreach (string strItem in frequency.Keys)
            {
                nCombination[ii] = strItem;
                ii++;
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private SortedList<string, double> GetFrequentItems(SortedList<string, double> dic_Candidates, double dMinSupport)
        {
            SortedList<string, double> dic_FrequentReturn = new SortedList<string, double>();

            for (int i = dic_Candidates.Count - 1; i >= 0; i--)
            {
                string strItem = dic_Candidates.Keys.ElementAt(i);
                double dSupport = dic_Candidates[strItem];

                if ((dSupport / (double)(m_nLastTransId - 1) >= dMinSupport))
                {
                    dic_FrequentReturn.Add(strItem, dSupport);
                    m_dicAllFrequentItems.Add(strItem, dSupport);
                }
            }

            return dic_FrequentReturn;
        }


        private SortedList<string, double> GenerateCandidates(SortedList<string, double> dic_FrequentItems)
        {
            SortedList<string, double> dic_CandidatesReturn = new SortedList<string, double>();
            for (int i = 0; i < dic_FrequentItems.Count - 1; i++)
            {
                string strFirstItem = dic_FrequentItems.Keys.ElementAt(i);

                for (int j = i + 1; j < dic_FrequentItems.Count; j++)
                {
                    string strSecondItem = dic_FrequentItems.Keys.ElementAt(j);
                    string strGeneratedCandidate = GetCandidate(strFirstItem, strSecondItem);
                    if (strGeneratedCandidate != string.Empty)
                    {
                        double dSupport = GetSupport(strGeneratedCandidate);
                        dic_CandidatesReturn.Add(strGeneratedCandidate, dSupport);

                    }
                }
            }
            return dic_CandidatesReturn;
        }

        private string Alphabetize(string strToken)
        {
            // Convert to string array, then sort and return
            string[] arrToken = strToken.Split(' ');
            Array.Sort(arrToken);
            string text2 = "";
            for (int i = 0; i < arrToken.Length - 1; i++)
            {
                if (i == arrToken.Length - 2)
                    text2 += (arrToken[i]);//no space at the back because to prevent there have space at the end of itemset.the space can give error to other keys
                else
                    text2 += (arrToken[i] + " ");
            }
            return text2;
        }


        private bool IsSubstring(string strChild, string strParent)
        {
            string[] splitlineitem = strChild.Split(' ');
            foreach (string c in splitlineitem)
            {
                if (!strParent.Contains(c))
                {
                    return false;
                }
            }
            return true;
        }


        private string GetCandidate(string strFirstItem, string strSecondItem)
        {

            int nLength = strFirstItem.Length;

            if (nLength == 2)
            {
                return (strFirstItem + " " + strSecondItem);
            }

            else
            {
                string strFirstSubString = strFirstItem.Substring(0, nLength - 2);
                string strSecondSubString = strSecondItem.Substring(0, nLength - 2);
                if (strFirstSubString == strSecondSubString)
                {
                    return (strFirstItem + " " + strSecondItem[nLength - 2] + strSecondItem[nLength - 1]);
                }
                else
                    return string.Empty;
            }
        }

        private SortedList<string, double> GetL1FrequentItems(double dMinSupport)
        {
            m_dicAllFrequentItems.Clear();
            SortedList<string, double> dic_FrequentItemsReturn = new SortedList<string, double>();

            foreach (string lviItem in dicFrequency.Keys)
            {
                double dSupport = GetSupport(lviItem);

                if ((dSupport / (double)(m_nLastTransId - 1) >= dMinSupport))
                {
                    dic_FrequentItemsReturn.Add(lviItem, dSupport);
                    m_dicAllFrequentItems.Add(lviItem, dSupport);

                }
            }
            return dic_FrequentItemsReturn;
        }

        private double GetSupport(string strGeneratedCandidate)
        {
            double dSupportReturn = 0;
            foreach (string strTransaction in m_dicTransactions.Values)
            {

                if (IsSubstring(strGeneratedCandidate, strTransaction))
                {
                    dSupportReturn++;
                }
            }
            return dSupportReturn;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                listView1.Items.Clear();
                string line;
                SortedList patternList = new SortedList();
                StreamReader filename = new StreamReader(file);

                while ((line = filename.ReadLine()) != null)
                {
                    m_dicTransactions.Add(m_nLastTransId, line);
                    m_nLastTransId++;
                }

                double dMinSupport = 0;
                if (textBox3.Text != "")
                {
                    dMinSupport = double.Parse(textBox3.Text) / 100;


                    SortedList<string, double> dic_FrequentItemsL1 = new SortedList<string, double>();
                    SortedList<string, double> dic_FrequentItems = new SortedList<string, double>();

                    dic_FrequentItemsL1 = GetL1FrequentItems(dMinSupport);
                    dic_FrequentItems = dic_FrequentItemsL1;




                    foreach (string oneItem in dic_FrequentItems.Keys)
                    {
                        ListViewItem lvi = new ListViewItem(oneItem);
                        lvi.SubItems.Add(dic_FrequentItems[oneItem].ToString());
                        listView1.Items.Add(lvi);
                    }

                    //declare new dic name dic_Candidate
                    SortedList<string, double> dic_Candidates = new SortedList<string, double>();
                    do
                    {
                        dic_Candidates = GenerateCandidates(dic_FrequentItems);

                        dic_FrequentItems = GetFrequentItems(dic_Candidates, dMinSupport);
                        foreach (string oneItem in dic_FrequentItems.Keys)
                        {
                            ListViewItem lvi = new ListViewItem(oneItem);
                            lvi.SubItems.Add(dic_FrequentItems[oneItem].ToString());
                            listView1.Items.Add(lvi);
                        }
                    }
                    while (dic_Candidates.Count != 0);
                }
                else
                {
                    MessageBox.Show("Must put an integer!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }


        //button to generate result but the association rules cant display???
        private void button5_Click(object sender, EventArgs e)
        {

            ColumnHeader itemset = new ColumnHeader();
            itemset.Text = "Association Rule(s)";
            itemset.Width = 110;
            listView2.Columns.Add(itemset);

            ColumnHeader supp = new ColumnHeader();
            supp.Text = "Support(%)";
            supp.Width = 70;
            listView2.Columns.Add(supp);

            ColumnHeader conf = new ColumnHeader();
            conf.Text = "Confident(%)";
            conf.Width = 90;
            listView2.Columns.Add(conf);

            ColumnHeader lift = new ColumnHeader();
            lift.Text = "Lift";
            lift.Width = 60;
            listView2.Columns.Add(lift);


            listView2.Items.Clear();
            int size = -1;
            string lstStrongRulesReturn = "";


        }



        SortedList<string, double> lstStrongRules = new SortedList<string, double>();

        private void LoadRules(List<clssRules> lstStrongRules)
        {

            foreach (clssRules Rule in lstStrongRules)
            {
                ListViewItem lvi = new ListViewItem(Rule.X + "-->" + Rule.Y);
                lvi.SubItems.Add(String.Format("{0:0.00}", (Rule.Confidence * 100)) + "%");
                listView2.Items.Add(lvi);
            }
        }

        //generate rules but nid to change the text box name testing
        private List<clssRules> GetStrongRules(double dMinConfidence, List<clssRules> lstRules)
        {
            List<clssRules> lstStrongRulesReturn = new List<clssRules>();

            foreach (clssRules Rule in lstRules)
            {
                string strXY = Alphabetize(Rule.X + Rule.Y);
                AddStrongRule(Rule, strXY, ref lstStrongRulesReturn, dMinConfidence);
            }

            lstStrongRulesReturn.Sort();
            return lstStrongRulesReturn;
        }


        private void AddStrongRule(clssRules Rule, string strXY, ref List<clssRules> lstStrongRulesReturn, double dMinConfidence)
        {
            double dConfidence = GetConfidence(Rule.X, strXY);
            clssRules NewRule;

            if (dConfidence >= dMinConfidence)
            {
                NewRule = new clssRules(Rule.X, Rule.Y, dConfidence);
                lstStrongRulesReturn.Add(NewRule);
            }

            dConfidence = GetConfidence(Rule.Y, strXY);

            if (dConfidence >= dMinConfidence)
            {
                NewRule = new clssRules(Rule.Y, Rule.X, dConfidence);
                lstStrongRulesReturn.Add(NewRule);

            }
        }

        private double GetConfidence(string strX, string strXY)
        {
            double dSupport_X, dSupport_XY;
            dSupport_X = m_dicAllFrequentItems[strX];
            dSupport_XY = m_dicAllFrequentItems[strXY];
            return dSupport_XY / dSupport_X;
        }


        private List<clssRules> GenerateRules()
        {
            List<clssRules> lstRulesReturn = new List<clssRules>();

            foreach (string strItem in m_dicAllFrequentItems.Keys)
            {
                if (strItem.Length > 1)
                {
                    int nMaxCombinationLength = strItem.Length / 2;
                    // GenerateCombination(strItem, nMaxCombinationLength, ref lstRulesReturn);
                }
            }

            return lstRulesReturn;

        }




        //list all the AR and confident, lift
        private void listView2_SelectedIndexChanged(object sender, EventArgs e)
        {


        }

        //read total positive AR
        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        //read total negative AR
        private void textBox5_TextChanged(object sender, EventArgs e)
        {

        }

        //button ok
        private void button4_Click(object sender, EventArgs e)
        {
            DialogResult settle = MessageBox.Show("Do you want to exit?", "Exit Confirmation", MessageBoxButtons.YesNo);
            if (settle == DialogResult.Yes)
                Application.Exit();
        }



    }
}

Recommended Answers

All 3 Replies

Are we just supposed to guess at what your problem might be?

Not to guess...because my problem now is the result of the association rules cant be displayed in the listview2.
I do not know what's wrong with it... =(

Dear saw89,
Can you give me your source code? I will help you solve problems. Please send me the following address: MAIL DELETED

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.