// AddWord adds a Word into the structure, or increments counter for existing word // returns -1 if unable to add - array is full public int AddWord(String theWord) { int location = FindIndexOfWord(theWord); // check whether Word is in the list if (location == -1) { // Word now found, so add word to our list, setting counter to zero if (currentSize < theList.Length) { theList[currentSize] = new WordCountPair(theWord); currentSize++; } else { return(-1); // could not add, array is full } } else { // So Word is already in list; increment counter theList[location].theCount = theList[location].theCount + 1; } // All is OK so leave and return 0 return(0); }
/// <summary> /// Tworzy nowy słownik na podstawie pliku podsumowującego wszsytkie kategorie. /// </summary> /// <param name="summaryFile">Nazwa pliku zawierającego podsumowanie wszsytkich kategorii.</param> /// <param name="size">Rozmiar słownika - ilość słów w słowniku.</param> public FrequentDictionary(String summaryFile, int size) { List <WordCountPair> wordList = new List <WordCountPair>(); StreamReader sr = new StreamReader(summaryFile); //wczytywanie wszystkich słów String data = sr.ReadToEnd(); sr.Close(); String[] lines = data.Split(Environment.NewLine.ToCharArray()); //podział na linie //utworzenie listy WordCountPairów foreach (String tmpLine in lines) { if (tmpLine != null && tmpLine.Length > 0) { wordList.Add(WordCountPair.Parse(tmpLine)); } } //sortowanie wordList.Sort(); wordList.Reverse(); //przepisanie do listy słów this.wordList = new List <string>(); for (int i = 0; i < Math.Min(wordList.Count, size); i++) { this.wordList.Add(wordList[i].Word); } }
/// <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); } }
private List <WordCountPair> getSortedWordCountList(string fileName) { List <WordCountPair> result = new List <WordCountPair>(); StreamReader sr = new StreamReader(fileName); //wczytywanie wszystkich słów String data = sr.ReadToEnd(); sr.Close(); String[] lines = data.Split(Environment.NewLine.ToCharArray()); //podział na linie //utworzenie listy WordCountPairów foreach (String tmpLine in lines) { if (tmpLine != null && tmpLine.Length > 0) { result.Add(WordCountPair.Parse(tmpLine)); } } //sortowanie result.Sort(); result.Reverse(); return(result); }