示例#1
0
        /**
         * BUTTONS START HERE!
         *
         */


        private void Read_File_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();//clear listBox

            if (myAVLTree.ROOT != null)
            {
                myAVLTree = new AVLTree <Word>(); //reset whole tree in case button was already pressed before
            }
            const int MAX_FILE_LINES = 50000;

            string[] AllLines = new string[MAX_FILE_LINES];
            // set the path where the file to be read is saved
            string path = @"C:\Users\adamb\source\repos\CSharp_CourseWork_16059158\myText.txt";

            AllLines = File.ReadAllLines(path);
            int lineNumber = 0;

            foreach (string line in AllLines)
            {
                lineNumber++;            //line number only add at every new line
                int         wordPos = 1; //re-initialise word position every new line
                Word        myWord;
                Location    wordLocation;
                Node <Word> myData;

                string[] words = line.Split(' ', ',', '.', '?', ';');
                words = words.Where(val => val != "").ToArray();

                foreach (String word in words)
                {
                    listBox1.Items.Add(word);
                    if (word != "")
                    {
                        myWord = new Word(word);
                        myData = findNode(myWord, myAVLTree.ROOT);            //fetch node from which i can reatreave DATA Word

                        if (myData == null)                                   //if avl does not already contains Word obj than add it
                        {
                            wordLocation = new Location(wordPos, lineNumber); //create a new location for the word
                            myWord.addEdge(wordLocation);                     //add a new link of position for the word
                            myAVLTree.InsertItem(myWord);                     //insert data in avl
                            wordPos++;                                        //position of the word + 1 for each word
                        }
                        else //avl contains the data already
                        {
                            wordLocation = new Location(wordPos, lineNumber);
                            myData.Data.addEdge(wordLocation);
                            wordPos++;//position of the word + 1 for each word
                        }
                    }
                }
            }
            Info info = new Info();

            SendTree           = myAVLTree; //create tree
            info.RECEIVED_TREE = SendTree;  //send tree to info class
            listBox1.Items.Add(" YOUR TEXT FILE was LOADED..... ");
            int uniqueWordCount = myAVLTree.Count();

            Number_Of_Words.Items.Clear();
            Number_Of_Words.Items.Add(uniqueWordCount);
        }
        private void save_Click(object sender, EventArgs e)
        {
            Form1 form1 = new Form1();

            String word = word_text.Text;
            int    occ  = int.Parse(occurences_text.Text);

            RECEIVED_TREE.removeItemAVLT(USE_RECEIVED_NODE.Data);

            Word newWord = new Word(word);

            String[] lines = line_text.Text.Split(',');
            lines = lines.Where(val => val != "").ToArray();
            String[] positions = positions_text.Text.Split(',');
            positions = positions.Where(val => val != "").ToArray();

            bool keepData_Source = ((occ - lines.Length) == 0) && ((occ - positions.Length) == 0);//if zero we don't need to add more links, just overWrite existing info
            int  linePos         = lines.Length - positions.Length;

            testXX.Items.Add(USE_RECEIVED_NODE.Data.LOCATIONS.Count() - lines.Length);

            /**
             * check if we have to keep the source data intact or not
             *
             */

            if (keepData_Source) ////
            {
                int v = 0;       //counter
                foreach (Location lc in newWord.LOCATIONS)
                {
                    if (v < lines.Length)
                    {
                        lc.LineNUMBER = int.Parse(lines[v]);
                    }
                    if (v < positions.Length)
                    {
                        lc.POSITION = int.Parse(positions[v]);
                    }
                    v++;
                }
                v = 0;//reset
            }
            else ///data has been changed
            {
                newWord.LOCATIONS.Clear();//clear list
                bool     positionAddedInfo = false;
                bool     lineAddedInfo     = false;
                bool     same_size         = false;
                string[] myArray;
                if (lines.Length < positions.Length)
                {
                    myArray           = positions;
                    positionAddedInfo = true;
                }
                else if (lines.Length > positions.Length)
                {
                    myArray       = lines;
                    lineAddedInfo = true;
                }
                else
                {
                    myArray   = lines;
                    same_size = true;
                }

                for (int i = 0; i < myArray.Length; i++)
                {
                    if (positionAddedInfo)
                    {
                        Location loc = new Location(int.Parse(myArray[i]), 0);
                        newWord.addEdge(loc);
                    }
                    else if (lineAddedInfo)
                    {
                        Location loc = new Location(0, int.Parse(myArray[i]));
                        newWord.addEdge(loc);
                    }
                    else
                    {
                        Location loc = new Location(int.Parse(positions[i]), int.Parse(myArray[i]));
                        newWord.addEdge(loc);
                    }
                }

                if (positionAddedInfo)
                {
                    int v = 0;
                    foreach (Location loca in newWord.LOCATIONS)
                    {
                        if (v >= lines.Length)
                        {
                            break;
                        }
                        if (v < lines.Length)
                        {
                            loca.LineNUMBER = int.Parse(lines[v]);
                        }
                        v++;
                    }
                    v = 0;
                }

                if (lineAddedInfo)
                {
                    int v = 0;
                    foreach (Location loca in newWord.LOCATIONS)
                    {
                        if (v >= positions.Length)
                        {
                            break;
                        }
                        if (v < positions.Length)
                        {
                            loca.POSITION = int.Parse(positions[v]);
                        }
                        v++;
                    }
                    v = 0;
                }
                if (newWord.LOCATIONS.Count() < occ)
                {
                    int condition = Math.Abs(newWord.LOCATIONS.Count() - occ);
                    for (int i = 0; i < condition; i++)
                    {
                        Location loc = new Location(0, 0);
                        newWord.addEdge(loc);
                    }
                }
            }

            RECEIVED_TREE.InsertItem(newWord);
            form1.SendTree = RECEIVED_TREE;


            //form1.listBox1.Items.Clear();
            //form1.listBox1.Items.Add(" >>>"+ InfoWord_Text);
            //form1.More_Than_TextBox.Text = InfoWord_Text;
            this.Close();
        }