예제 #1
0
        public static void Main(string[] args)
        {
            _tokenSource = new CancellationTokenSource();
            _token       = _tokenSource.Token;

            //Create a timer with 2 second interval to continuously output a text.
            _timer = new Timer
            {
                Interval  = 2000,
                AutoReset = true
            };
            _timer.Elapsed += delegate
            {
                using (LorumIpsumStream streamWriter = new LorumIpsumStream())
                {
                    //Generate a random stream of 250 bytes of random chars
                    byte[] streamBytes = new byte[250];

                    streamWriter.ReadAsync(streamBytes, 0, streamBytes.Length);
                    streamWriter.BeginWrite(streamBytes, 0, 0, OnStreamWrite, streamBytes);
                }
            };
            _timer.Start();

            _parser = new Parser();

            Console.WriteLine("Press \'s\' to stop.\r\nPress any key to exit.");
            while (Console.ReadKey().KeyChar == 's')
            {
                _timer.Stop();
                _tokenSource.Cancel();
            }
        }
예제 #2
0
        public void Start()
        {
            var          stream = new LorumIpsumStream();
            StreamReader reader = new StreamReader(stream, Encoding.Unicode);

            do
            {
                var word = string.Empty;

                while (true)
                {
                    char[] cha = new char[1];
                    reader.Read(cha, 0, 1);
                    char ch = cha[0];

                    word = AddCharacterToWord(ch, word);

                    if (IsEndOfWord(ch, word))
                    {
                        AddWordToSummary(word);
                        break;
                    }
                }
            } while (stream.CanRead);

            reader.Close();
            reader.Dispose();
        }
예제 #3
0
        /// <summary>
        /// Read data from Dll provided
        /// </summary>
        /// <returns></returns>
        public StreamReader LoadDataLorumIpsum()
        {
            var          Data   = new LorumIpsumStream();
            StreamReader reader = new StreamReader(Data);

            return(reader);
        }
예제 #4
0
        /// <summary>
        /// This function will help to read data and process
        /// </summary>
        private static void StreamRead()
        {
            try {
                int requiredTotalKB = 100000;
                LorumIpsumStream lorumIpsumStream = new LorumIpsumStream(requiredTotalKB);
                StreamReader     streamReader     = new StreamReader(lorumIpsumStream, Encoding.Unicode);
                string           line             = streamReader.ReadLine();//Reading line by line

                //Continue to read until you reach end of file
                while (line != null)
                {
                    line = line.Replace("\0", "").ToLower();
                    //Removing unwanted characters
                    string[] source = line.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                    //Getting distinct array of words
                    string[] distinctSource = source.Distinct().ToArray();
                    //write the line to console window
                    //Console.WriteLine(line);

                    //Getting total no of characters and words
                    ITotalNumber iTotalNumber            = new TotalNumber();
                    int          totalNumberOFCharacters = iTotalNumber.NoOfCharacters(line);
                    int          totalNumberOFWords      = iTotalNumber.NoOfWords(source);
                    Console.WriteLine("Total Number of Characters: " + totalNumberOFCharacters);
                    Console.WriteLine("Total Number of Words: " + totalNumberOFWords);

                    //Getting largest and smallest five words
                    ILargestSmallest iLargestSmallest = new LargestSmallest();
                    string           largestFiveWords = iLargestSmallest.LargestFiveWords(distinctSource);
                    Console.WriteLine("Largest Five Words: " + largestFiveWords);

                    string smallestFiveWords = iLargestSmallest.SmallestFiveWords(distinctSource);
                    Console.WriteLine("Smallest Five Words: " + smallestFiveWords);

                    //Getting frequently used 10 words
                    IFrequentWords iFrequentWords       = new FrequentWords();
                    string         tenMostFrequentWords = iFrequentWords.MostFrequentWords(source);
                    Console.WriteLine("Most Frequent Ten Words: " + tenMostFrequentWords);

                    //Getting all characters and its frequency
                    IAllCharacters iAllCharacters         = new AllCharacters();
                    string         allCharactersWithCount = iAllCharacters.AllCharactersWithCount(line);
                    Console.WriteLine("All Characters with occurence: " + allCharactersWithCount);

                    //Read the next line
                    line = streamReader.ReadLine();
                    Console.WriteLine("Reading Next Line completed");
                }
                Console.WriteLine("Process Completed");
                //close the file
                lorumIpsumStream.Close();
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
        }
예제 #5
0
        /// <summary>
        ///     Reads from stream.
        /// </summary>
        private void ReadFromStream()
        {
            LorumIpsumStream stream = new LorumIpsumStream(_totalKiloBytesToProcess);
            TextReader       tr     = new StreamReader(stream, Encoding.Unicode);

            char[] buffer    = new char[_charsToRead];
            int    bytesRead = 1; // prime the loop

            while (stream.CanRead && bytesRead > 0)
            {
                while (DataCache.Count > _charsToRead * 20)
                {
                    Thread.Sleep(100);
                }
                bytesRead = tr.Read(buffer, 0, _charsToRead);
                for (int i = 0; i < bytesRead; i++)
                {
                    DataCache.Enqueue(buffer[i]);
                }
            }
            DataCache.Enqueue(' '); // send a final space to finish off the last word
            IsComplete = true;
        }