Ejemplo n.º 1
0
        /// <summary>
        /// Gets the information the user wishes to analyse and prepares it for the network to analyse
        /// </summary>
        /// <param name="file">the file the user wishes to analyse</param>
        public void GetAnalysedText(FileObj file)
        {
            FileReadWrite frw           = new FileReadWrite();
            CategoryObj   analysingText = new CategoryObj(frw.GetStopWords(), frw.GetSuffixes());

            analysingText.Name = file.FileName;
            analysingText.AddText(file.FileContent);
            _analysingText = analysingText;
        }
        /// <summary>
        /// retrieves the networks from the folder.
        /// </summary>
        /// <returns>returns the bayesingNetwork array</returns>
        public BayesingNetwork[] GetSavedBayesingNetworks()
        {
            //list of networks
            List <BayesingNetwork> bayesingNetworks = new List <BayesingNetwork>();

            try
            {
                //d is the directory which holds the networks infomation
                foreach (string d in Directory.GetDirectories(_BayesingNetworkFolder))
                {
                    //contains the categories for the networks
                    List <CategoryObj> cat = new List <CategoryObj>();
                    //file is the networks categories
                    foreach (string file in Directory.EnumerateFiles(d, "*.txt"))
                    {
                        CategoryObj c = new CategoryObj(GetStopWords(), GetSuffixes())
                        {
                            Name = Path.GetFileName(file)
                        };
                        //collects the dictionary information for the categories

                        string[] information = File.ReadAllLines(file);

                        c.DocumentsUsed = int.Parse(information[0]);
                        Dictionary <string, int> kvp = new Dictionary <string, int>();
                        for (int i = 1; i < information.Length; i++)
                        {
                            //splits the key from the value it holds
                            string[] WordAndCountSplit = information[i].Split('+');

                            int amountOfWords = int.Parse(WordAndCountSplit[1]);

                            kvp.Add(WordAndCountSplit[0], amountOfWords);
                        }
                        //new dictionary entry to be added to the category
                        c.WordInformation = kvp;
                        cat.Add(c);
                    }

                    BayesingNetwork bn = new BayesingNetwork(cat)
                    {
                        Name = d
                    };
                    bayesingNetworks.Add(bn);
                }
            }
            catch (Exception)
            {
                Console.WriteLine("File error, please check the files are input correctly");
            }
            return(bayesingNetworks.ToArray());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Trains the network
        /// </summary>
        public void Train()
        {
            FileReadWrite frw = new FileReadWrite();

            //get the training files
            FileObj[]          files      = frw.GetTrainingData();
            List <CategoryObj> categories = new List <CategoryObj>();
            bool exists = false;

            //classifies the categories using the files
            foreach (FileObj f in files)
            {
                string category = ""; //name of the new category

                //creates the category name
                foreach (char c in f.FileName)
                {
                    if (c != '0' && c != '1' && c != '2' && c != '3' && c != '4' && c != '5' && c != '6' && c != '7' && c != '8' && c != '9' && c != '.')
                    {
                        category += c;
                    }
                    else
                    {
                        break;
                    }
                }
                exists = false;
                //checks if information can be added to an existing category
                foreach (CategoryObj cat in categories)
                {
                    if (cat.Name == category)
                    {
                        exists = true;
                        cat.AddText(f.FileContent);
                    }
                }
                //makes a new category if the category doesn't already exist
                if (!exists)
                {
                    CategoryObj newCategory = new CategoryObj(frw.GetStopWords(), frw.GetSuffixes());
                    newCategory.Name = category;
                    newCategory.AddText(f.FileContent);

                    categories.Add(newCategory);
                }
            }
            //adds the list of categorys tot he known information
            _knownInformation = categories;
        }