Beispiel #1
0
        public void DoesInitialzie()
        {
            var wf = new WordFrequency("foo", 1);

            Assert.AreEqual("foo", wf.Word, "The Word property was not initialized properly");
            Assert.AreEqual(1, wf.Frequency, "The Frequency property was not initialized properly");
        }
 protected virtual void ClearResults()
 {
     LastError  = string.Empty;
     Normalized = null;
     Words.Clear();
     WordFrequency.Clear();
 }
Beispiel #3
0
        } = new Dictionary <char, int>();                                                            // char, frequency

        // Constructors
        public Passage(string fileName)
        {
            this.FileName = fileName;

            using (StreamReader sr = new StreamReader(FileName))
            {
                string[] words = StripPunctuation(sr.ReadToEnd()).Split(" ", StringSplitOptions.RemoveEmptyEntries);
                this.WordCount         = words.Length;
                this.AverageWordLength = words.Average(w => w.Length);

                foreach (string word in words)
                {
                    // Determine frequency of words
                    if (WordFrequency.ContainsKey(word))
                    {
                        WordFrequency[word]++;
                    }
                    else
                    {
                        WordFrequency.Add(word, 1);
                    }

                    // Count starting character of each word
                    if (StartCharFrequency.ContainsKey(word[0]))
                    {
                        StartCharFrequency[word[0]]++;
                    }
                    else
                    {
                        StartCharFrequency.Add(word[0], 1);
                    }
                }
            }
        }
Beispiel #4
0
        public void TestCompareTo()
        {
            var wf     = new WordFrequency("foo", 1);
            var result = wf.CompareTo(wf);

            Assert.AreEqual(0, result);

            var wf2 = new WordFrequency("foo", 1);

            result = wf.CompareTo(wf2);
            Assert.AreEqual(0, result);

            var wf3 = new WordFrequency("faa", 1);

            result = wf.CompareTo(wf3);
            Assert.AreEqual(1, result);

            var wf4 = new WordFrequency("zzz", 1);

            result = wf.CompareTo(wf4);
            Assert.AreEqual(-1, result);

            var wf5 = new WordFrequency("foo", 2);

            result = wf.CompareTo(wf5);
            Assert.AreEqual(1, result);

            var wf6 = new WordFrequency("foo", -2);

            result = wf.CompareTo(wf6);
            Assert.AreEqual(-1, result);
        }
Beispiel #5
0
        static void Main(string[] args)
        {
            // Get data from csv file
            List <Person> personList = CsvFileUtils.GetPersonDataFromCsv("Resources/data.csv");

            if (personList?.Count > 0)
            {
                Console.WriteLine("Successfully read csv file");
                Console.WriteLine($"Total lines found: { personList.Count}");

                // Get name frequency base on firstname and lastname and order by count descending and by name ascending
                var nameCollection = WordFrequency.GetPersonNamesFrequency(personList);
                // Get address sorted by name not by number
                var addressCollection = WordFrequency.GetAddressAlphabatically(personList);

                // Print data to console window
                PrintNameFrequencies(nameCollection);
                PrintAddressFrequencies(addressCollection);

                // Writing our sorted data and frequency data to csv files
                WriteToCsv(nameCollection, addressCollection);
            }
            else
            {
                Console.WriteLine("Total lines found: 0");
            }
            Console.ReadKey();
        }
Beispiel #6
0
        public void TestEquals()
        {
            var wf       = new WordFrequency("foo", 1);
            var areEqual = wf.Equals(null);

            Assert.AreEqual(false, areEqual);

            areEqual = wf.Equals(new object());
            Assert.AreEqual(false, areEqual);

            areEqual = wf.Equals(wf);
            Assert.AreEqual(true, areEqual);

            var wf2 = new WordFrequency("foo", 1);

            areEqual = wf.Equals(wf2);
            Assert.AreEqual(true, areEqual);

            var wf3 = new WordFrequency("fo", 1);

            areEqual = wf.Equals(wf3);
            Assert.AreEqual(false, areEqual);

            var wf4 = new WordFrequency("foo", 2);

            areEqual = wf.Equals(wf4);
            Assert.AreEqual(false, areEqual);
        }
Beispiel #7
0
        public void TestGetAddressAlphabatically()
        {
            var addressSortedList = WordFrequency.GetAddressAlphabatically(PersonList);
            var addressData1      = addressSortedList[0];
            var addressData2      = addressSortedList[1];

            Assert.AreEqual(addressData1, "65 Ambling Way");
            Assert.AreEqual(addressData2, "102 Long Lane");
        }
Beispiel #8
0
        public void FindFrequency_EmptyBook_Test()
        {
            WordFrequency obj    = new WordFrequency("".Split());
            int           actual = obj.FindWordFrequency("book");

            int expected = 0;

            Assert.IsNotNull(obj);
            Assert.AreEqual(expected, actual);
        }
Beispiel #9
0
        internal static void ConsoleInterface()
        {
            var list = new WordFrequency <float>("Peter Piper picked a peck of pickled peppers. " +
                                                 "A peck of pickled peppers Peter Piper picked. If Peter Piper picked a peck of pickled peppers, " +
                                                 "Where's the peck of pickled peppers Peter Piper picked?");

            for (int i = 0; i < list.Count; i++)
            {
                Console.WriteLine($"Frequency of the word \"{list.Words[i]}\" is {list[i]}");
            }
        }
Beispiel #10
0
        public void TestGetPersonNamesFrequencyCount()
        {
            var nameFrequencyList = WordFrequency.GetPersonNamesFrequency(PersonList);
            var nameData1         = nameFrequencyList.Single(x => x.Name == "Smith");
            var nameData2         = nameFrequencyList.Single(x => x.Name == "Jimmy");

            Assert.AreEqual(nameData1.Name, "Smith");
            Assert.AreEqual(nameData1.TotalCount, 3);

            Assert.AreEqual(nameData2.Name, "Jimmy");
            Assert.AreEqual(nameData2.TotalCount, 1);
        }
Beispiel #11
0
        public void TestGetPersonNamesFrequencySortedByCountDesc()
        {
            var nameFrequencyList = WordFrequency.GetPersonNamesFrequency(PersonList);
            var nameData1         = nameFrequencyList[0];
            var nameData2         = nameFrequencyList[1];

            Assert.AreEqual(nameData1.Name, "Smith");
            Assert.AreEqual(nameData1.TotalCount, 3);

            Assert.AreEqual(nameData2.Name, "Clive");
            Assert.AreEqual(nameData2.TotalCount, 2);
        }
        public static void Main(string[] args)
        {
            int i = 1;

            WordFrequency wordFreq = new WordFrequency();

            while (i == 1)
            {
                Console.WriteLine("Please type in a sentence"); //Input
                String line = Console.ReadLine();               //Assign
                if (String.IsNullOrEmpty(line))
                {
                    Console.WriteLine("Please Try Again"); //if nothing is put in
                    continue;
                }
                else
                {
                    char[]   charSeparators = new char[] { ' ' };
                    string[] lineSep        = line.Split(charSeparators, 100, StringSplitOptions.RemoveEmptyEntries);
                    var      lineSepProc    = lineSep.Select(s => s.ToLowerInvariant()).ToArray();


                    Dictionary <string, int> dic = new Dictionary <string, int>();

                    dic = wordFreq.MkCount(lineSepProc, dic);

                    var listOfHighest = wordFreq.MkSort(dic, "desc", " ");

                    Console.WriteLine(listOfHighest);
                    Console.WriteLine("Highest Frequency: " + listOfHighest.First());
                }

                i = 3;
                while (i == 3)
                {
                    Console.WriteLine("Would you like to split another sentence? Y/N");
                    string answer = Console.ReadLine();

                    if (answer.ToUpper() == "Y")
                    {
                        i = 1;
                    }
                    else if (answer.ToUpper() == "N")
                    {
                        i = 0;
                    }
                    else
                    {
                        i = 3;
                    }
                }
            }
        }
Beispiel #13
0
        public string MostCommonWord(string paragraph, string[] banned)
        {
            if (string.IsNullOrWhiteSpace(paragraph))
            {
                return(null);
            }

            var lowerCaseParagraph = paragraph.ToLower();

            HashSet <string> bannedSet = new HashSet <string>();

            foreach (var bannedWord in banned)
            {
                bannedSet.Add(bannedWord.ToLower());
            }

            string[] allWords = lowerCaseParagraph.Split(' ', ',', '.', '!', '?', ';', '\'');
            Dictionary <string, WordFrequency> dictionary = new  Dictionary <string, WordFrequency>();

            foreach (string word in allWords)
            {
                if (!bannedSet.Contains(word) && !string.IsNullOrEmpty(word))
                {
                    if (dictionary.ContainsKey(word))
                    {
                        dictionary[word].Frequency++;
                    }
                    else
                    {
                        dictionary[word] = new WordFrequency(word);
                    }
                }
            }
            List <WordFrequency> wordList = new List <WordFrequency>();

            foreach (var wordFrequency in dictionary.Values)
            {
                wordList.Add(wordFrequency);
            }

            wordList.Sort();

            if (wordList.Count < 1)
            {
                return(null);
            }

            return(wordList[0].Word);
        }
Beispiel #14
0
        public void FindFrequency_Test()
        {
            WordFrequency obj      = new WordFrequency("I am book, I am book on Programing. You can book my early edition in book stores. I am sure I will be soon your favorite book".Split());
            Stopwatch     watch    = new Stopwatch();
            int           expected = 5;

            watch.Start();
            for (int i = 0; i < 10000; i++)
            {
                int actual = obj.FindWordFrequency("book");

                Assert.IsNotNull(obj);
                Assert.AreEqual(expected, actual);
            }

            watch.Stop();
        }
Beispiel #15
0
 public TagCloudCommand(
     TagCloudPainter tagCloudPainter,
     WordFrequency wordFrequency,
     IImageSizeProvider imageSizeProvider,
     IFontFamilyProvider fontFamilyProvider,
     Func <Point, ILayoutAlgorithm> algorithmGenerator,
     Func <ILayoutAlgorithm, TagCloudLayouter> layouterGenerator,
     FileReader fileReader)
 {
     this.tagCloudPainter    = tagCloudPainter;
     this.wordFrequency      = wordFrequency;
     this.imageSizeProvider  = imageSizeProvider;
     this.fontFamilyProvider = fontFamilyProvider;
     this.algorithmGenerator = algorithmGenerator;
     this.layouterGenerator  = layouterGenerator;
     this.fileReader         = fileReader;
 }
Beispiel #16
0
        private void Run_Click(object sender, EventArgs e)
        {
            DateTime old = DateTime.Now;

            status.Text = "";
            string str = string.Empty;

            string[] patterns = this.burnPatterns();
            status.Text += "\n\n系统正在读取文件";
            if (unique.Checked == true)
            {
                if (this._unPath == "" || (this._unPath != "" && UnqiueFilePath.Text != this._unPath))
                {
                    MessageBox.Show("单个文件选择有错误");
                    return;
                }
                str = string.Join("", System.IO.File.ReadAllLines(@UnqiueFilePath.Text, System.Text.Encoding.GetEncoding("gb2312")));
            }
            else
            {
                if (this._maPath == "" || (this._maPath != "" && this._maPath != ManyFilePath.Text))
                {
                    MessageBox.Show("文件夹选择有错误");
                    return;
                }
                for (int i = 0; i < this._files.Length; i++)
                {
                    str += string.Join("", System.IO.File.ReadAllLines(this._files[i].FullName, System.Text.Encoding.GetEncoding("gb2312")));
                }
            }
            WordFrequency wf = new WordFrequency();

            status.Text += "\n\n系统工具正在统计词频...";
            List <Node> list = wf.burnList(str, patterns);

            wf.burnResult(list);
            status.Text += "\n\n系统工具正在写入文件...";
            DateTime now = DateTime.Now;

            status.Text += "\n\n耗时:" + (now - old).TotalSeconds.ToString() + "秒";
            this.write(list);
        }
        public int CompareFrequency(string x, string y)
        {
            var xFrequencyIsKnown = WordFrequency.TryGetValue(x, out var xFrequency);
            var yFrequencyIsKnown = WordFrequency.TryGetValue(y, out var yFrequency);

            if (xFrequencyIsKnown && yFrequencyIsKnown)
            {
                return(Comparer <int> .Default.Compare(xFrequency, yFrequency));
            }
            else if (xFrequencyIsKnown)
            {
                return(-1);
            }
            else if (yFrequencyIsKnown)
            {
                return(1);
            }
            else
            {
                return(0);
            }
        }
Beispiel #18
0
        public void SetUp()
        {
            var imageSavers = new List <IImageSaver> {
                new PngSaver(), new JpgSaver()
            };
            var fileReaders = new List <IFileAllLinesReader> {
                new TxtReader(), new DocReader()
            };
            var fileReader         = new FileReader(new FileReaderProvider(fileReaders));
            var imageHolder        = new ImageHolder(new ImageSaverProvider(imageSavers));
            var imageSettings      = new ImageSettings();
            var wordFrequency      = new WordFrequency(new GrammemeChecker());
            var painter            = new TagCloudPainter(imageHolder, imageSettings);
            var algorithmGenerator = new Func <Point, ILayoutAlgorithm>(x => new SpiralAlgorithm(x));
            var layoutGenerator    = new Func <ILayoutAlgorithm, TagCloudLayouter>(x => new TagCloudLayouter(x));

            tagCloudCommand = new TagCloudCommand(
                painter, wordFrequency, imageSettings, imageSettings, algorithmGenerator, layoutGenerator, fileReader);
            saveCommand         = new SaveCommand(imageHolder);
            addColorCommand     = new AddColorCommand(imageSettings);
            setFontCommand      = new SetFontCommand(imageSettings);
            setImageSizeCommand = new SetImageSizeCommand(imageSettings);
        }
Beispiel #19
0
        public void TestExtractAddressName()
        {
            var address = WordFrequency.ExtractAddressName("65 Ambling Way");

            Assert.AreEqual(address, "Ambling Way");
        }
Beispiel #20
0
 public void FindFrequency_EmptyWord_Test()
 {
     WordFrequency obj    = new WordFrequency("This is test".Split());
     int           actual = obj.FindWordFrequency("");
 }
Beispiel #21
0
 public void SetUp()
 {
     testData      = new[] { "ASD", "aSd", "asd", "zxc", "zxc", "qwe" };
     wordChecker   = A.Fake <IWordChecker>();
     wordFrequency = new WordFrequency(wordChecker);
 }
Beispiel #22
0
        public void TestToString()
        {
            var wf = new WordFrequency("foo", 1);

            Assert.AreEqual("foo - 1", wf.ToString(), "String representations are not equal");
        }