示例#1
0
        /// <summary>
        /// Tworzy dokument na podstawie wcześniej przygotowanego pliku.
        /// </summary>
        /// <param name="fileName">Plik z danymi.</param>
        /// <param name="dictionary">Słownik, na podstawie którego tworzony jest dokument.</param>
        /// <param name="className">Nazwa klasy, do której należy dany dokument lub null, jeśli klasa jest nieznana.</param>
        /// <param name="learningDocInfo">Obiekt zawierający informacje o wszystkich dokumentach uczących.</param>
        public TfIdfDocument(String fileName, Dictionary dictionary, String className, LearningDocInfo learningDocInfo)
            : base(dictionary)
        {
            wordCountList = new WordCountList();
            if (className != null)
            {
                classNo = DocumentClass.GetClassIndex(className);
            }
            //tworze liste wszystkich słów ze wszystkuch dokumentów
            Dictionary <String, WordInfo> allWordsInfo = learningDocInfo.AllWordsInfo;
            double allDocNumber = learningDocInfo.AllDocCount;
            //tworze liste słów w dokumencie
            WordCountList wordsInDoc = new WordCountList(fileName);

            int wordsInDocCount = wordsInDoc.GetAllWordsCount();

            foreach (String word in dictionary)
            {
                if (wordsInDoc[word] != -1)
                {
                    double inclDocCount = allWordsInfo[word].InclDocCount;
                    //double tfIdf = (wordsInDoc[word] / wordsInDocCount) * Math.Log10(allDocNumber/inclDocCount);
                    double tfIdf = PreprocessingUtility.ComputeTfIdf(wordsInDoc[word], wordsInDocCount, allDocNumber, inclDocCount);
                    wordCountList.Add(new WordCountPair(word, tfIdf));
                }
                else
                {
                    wordCountList.Add(new WordCountPair(word, 0));
                }
            }
        }
示例#2
0
        private ICollection <string> GetWordsFromFile(String pathFile)
        {
            //tworze liste
            WordCountList wcl = new WordCountList(pathFile);

            ICollection <string> coll = new List <string>();

            foreach (WordCountPair wcp in wcl)
            {
                coll.Add(wcp.Word);
            }
            return(coll);
        }
示例#3
0
        /// <summary>
        /// Tworzy nowy słownik.
        /// </summary>
        /// <param name="sourceDir">Katalog zawierający wszystkie pliki uczące.</param>
        /// <param name="summaryFile">Plik z podsumowaniem wszystkich plików.</param>
        /// <param name="size">Ilość słów w słowniku</param>
        public CtfIdfDictionary(String sourceDir, String summaryFile, int size)
        {
            Dictionary <String, WordCountPair> tmpDictionary = new Dictionary <string, WordCountPair>();
            //wczytanie informacji o wszystkich słowach
            LearningDocInfo learningDocInfo        = new LearningDocInfo(sourceDir, summaryFile);
            Dictionary <String, WordInfo> allWords = learningDocInfo.AllWordsInfo;
            int allDocCount = learningDocInfo.AllDocCount;
            //tworzenie słownika
            DirectoryInfo sourceDirInfo = new DirectoryInfo(sourceDir);

            foreach (DirectoryInfo dirInfo in sourceDirInfo.GetDirectories()) //przechodzę po wszystkich podkatalogach
            {
                DirectoryInfo stemDir = new DirectoryInfo(dirInfo.FullName + "//stem");
                foreach (FileInfo fileInfo in stemDir.GetFiles()) //przechodzę po wszystkich plikach
                {
                    WordCountList wordsInFile     = new WordCountList(fileInfo.FullName);
                    int           wordsInDocCount = wordsInFile.GetAllWordsCount();
                    foreach (WordCountPair wordCountPair in wordsInFile) //przechodzę po wszsytkich słowach
                    {
                        double tfIdf = PreprocessingUtility.ComputeTfIdf(wordCountPair.Count, wordsInDocCount, allDocCount, allWords[wordCountPair.Word].InclDocCount);
                        if (tmpDictionary.ContainsKey(wordCountPair.Word))
                        {
                            tmpDictionary[wordCountPair.Word].Count += tfIdf;
                        }
                        else
                        {
                            tmpDictionary.Add(wordCountPair.Word, new WordCountPair(wordCountPair.Word, tfIdf));
                        }
                    }
                }
            }
            //wybranie odpowiednich słów
            WordCountPair[] tmpArray = new WordCountPair[tmpDictionary.Count];
            tmpDictionary.Values.CopyTo(tmpArray, 0);
            Array.Sort(tmpArray);
            Array.Reverse(tmpArray);
            //kopiowanie do właściwej listy
            wordList = new List <string>();
            for (int i = 0; i < size; i++)
            {
                wordList.Add(tmpArray[i].Word);
            }
        }
示例#4
0
        /// <summary>
        /// Tworzy dokument na podstawie wcześniej przygotowanego pliku.
        /// </summary>
        /// <param name="fileName">Plik z danymi.</param>
        /// <param name="dictionary">Słownik, na podstawie którego tworzony jest dokument.</param>
        /// <param name="className">Nazwa klasy, do której należy dany dokument lub null, jeśli klasa jest nieznana.</param>
        public BinaryDocument(String fileName, Dictionary dictionary, String className) : base(dictionary)
        {
            wordCountList = new WordCountList();
            if (className != null)
            {
                classNo = DocumentClass.GetClassIndex(className);
            }

            WordCountList listFromFile = new WordCountList(fileName);

            foreach (String word in dictionary)
            {
                if (listFromFile[word] != -1)
                {
                    wordCountList.Add(new DocClass.Src.Preprocessing.WordCountPair(word, 1));
                }
                else
                {
                    wordCountList.Add(new DocClass.Src.Preprocessing.WordCountPair(word, 0));
                }
            }
        }
示例#5
0
        /// <summary>
        /// Tworzy dokument na podstawie wcześniej przygotowanego pliku.
        /// </summary>
        /// <param name="fileName">Plik z danymi.</param>
        /// <param name="dictionary">Słownik, na podstawie którego tworzony jest dokument.</param>
        /// <param name="className">Nazwa klasy, do której należy dany dokument lub null, jeśli klasa jest nieznana.</param>
        public OwnDocument(String fileName, Dictionary dictionary, String className) : base(dictionary)
        {
            wordCountList = new WordCountList();
            if (className != null)
            {
                classNo = DocumentClass.GetClassIndex(className);
            }
            WordCountList listFromFile = new WordCountList(fileName);
            int           wordsInDoc   = listFromFile.GetAllWordsCount();

            foreach (String word in dictionary)
            {
                if (listFromFile[word] != -1)
                {
                    wordCountList.Add(new WordCountPair(word, listFromFile[word] / wordsInDoc));
                }
                else
                {
                    wordCountList.Add(new WordCountPair(word, 0));
                }
            }
        }
示例#6
0
        /// <summary>
        /// Metoda zwracające odpowiednią reprezentacjie słownika na
        /// podstweie wybranej opcji.
        /// </summary>
        /// <param name="pathSummary"></param>
        /// <returns></returns>
        private Dictionary DictionaryFactory(String pathSummary)
        {
            WordCountList wordCountList = new WordCountList(pathSummary);

            if (learningDocInfo == null)
            {
                learningDocInfo = new LearningDocInfo(Settings.Default.pathLearningDir,
                                                      Settings.Default.pathSummaryFile);
            }
            switch ((DictionaryType)Settings.Default.dictionaryType)
            {
            case DictionaryType.CtfIdf:
                return(new CtfIdfDictionary(Settings.Default.pathLearningDir, pathSummary, wordCountList.GetUniqueWordsCount()));

            case DictionaryType.Fixed:
                return(new FixedDictionary(Settings.Default.pathLearningDir, wordCountList.GetUniqueWordsCount()));

            case DictionaryType.Frequent:
                return(new FrequentDictionary(pathSummary, wordCountList.GetUniqueWordsCount()));

            default:
                throw new NotImplementedException("Nieznany typ słownika.");
            }
        }