public int CalculateFrequencyForWord(string text, string word)
        {
            var wordsList         = _textParser.ParseText(text);
            var wordFrequencyItem = wordsList.FirstOrDefault(x => x.Word == word);

            return(wordFrequencyItem?.Frequency ?? 0);
        }
        public async Task <bool> Handle(StockQuoteRequestModel request, CancellationToken cancellationToken)
        {
            if (!cancellationToken.IsCancellationRequested)
            {
                if (request.Command != string.Empty && request.Command?.Length > 0)
                {
                    Stock            stock;
                    CacheKey <Stock> cacheStockKey = _cacheStock.GetKey(request.Command);
                    if (cacheStockKey == null)
                    {
                        string stockInfo = await _apiRequester
                                           .MakeGetRequest($"{_stockApiConfig.Url}&s={request.Command}");

                        stock = _textParser.ParseText(stockInfo);
                        _cacheStock.PutKey(new CacheKey <Stock> {
                            Key     = request.Command,
                            Message = stock
                        });
                        _logger.LogInformation($"Command ({request.Command}) not found in cache. Caching it...");
                    }
                    else
                    {
                        _logger.LogInformation($"Command ({request.Command}) found in cache.");
                        stock = cacheStockKey.Message;
                    }
                    _messageQueue.EnqueueMessage(new QueueMessage {
                        Command = request.Command,
                        RoomId  = request.RoomId,
                        Quote   = $"{stock.Symbol} quote is {stock.Open} per share."
                    });
                    return(true);
                }
                _logger.LogInformation($"Command ({request.Command}) was, either empty or length lesser than 1.");
            }
            return(false);
        }
Esempio n. 3
0
        public static void Start(ITaskFirst taskFirst, ITextParser textParser)
        {
            do
            {
                Console.Clear();
                Console.WriteLine(menu);
                switch (Console.ReadLine())
                {
                case "1":
                    taskFirst.SentencesInAscendingOrder.ToList().ForEach(sent => Console.Write(sent));
                    Console.WriteLine("Press any key to continue");
                    Console.ReadKey();
                    break;

                case "2":
                    Console.WriteLine("Input the length of the word");
                    if (int.TryParse(Console.ReadLine(), out int length))
                    {
                        if (length <= 0)
                        {
                            Console.WriteLine("The length must be bigger than 0");
                        }
                        else
                        {
                            Console.WriteLine("Words:");
                            taskFirst.GetWordsFromIssueSentencesByGivenLength(length).ToList().ForEach(word => Console.WriteLine(word));
                        }
                    }
                    else
                    {
                        Console.WriteLine("Incorrect input");
                    }
                    Console.WriteLine("Press any key to continue");
                    Console.ReadKey();
                    break;

                case "3":
                    Console.WriteLine("Input the length of the word");
                    if (int.TryParse(Console.ReadLine(), out int lengthWord))
                    {
                        if (lengthWord <= 0)
                        {
                            Console.WriteLine("The length must be bigger than 0");
                        }
                        else
                        {
                            taskFirst.SentencesWithoutWordsStartWithConsonant(lengthWord);
                            Console.WriteLine(taskFirst.Text);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Incorrect input");
                    }
                    Console.ReadKey();
                    break;

                case "4":
                    Console.WriteLine("Input the srtrig to insert");
                    string inserting = Console.ReadLine();
                    Console.WriteLine("Input the index of sentence (start with 0)");
                    if (int.TryParse(Console.ReadLine(), out int indexSentence))
                    {
                        if (indexSentence >= 0 && indexSentence < taskFirst.Text.AmountOfSentences)
                        {
                            Console.WriteLine("Input the length of the word");
                            if (int.TryParse(Console.ReadLine(), out int wordLength))
                            {
                                if (wordLength <= 0)
                                {
                                    Console.WriteLine("The length must be bigger than 0");
                                }
                                else
                                {
                                    taskFirst.ReplaceWordOfGivenLengthInSentenceByElements(indexSentence, wordLength, textParser.ParseText(inserting).ToArray());
                                    Console.WriteLine(taskFirst.Text);
                                }
                            }
                            else
                            {
                                Console.WriteLine("Incorrect input");
                            }
                        }
                        else
                        {
                            Console.WriteLine("There is no sentence with this index in text");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Incorrect input");
                    }
                    Console.WriteLine("Press any key to continue");
                    Console.ReadKey();
                    break;

                case "5":
                    Console.WriteLine(taskFirst.Text);
                    Console.WriteLine("Press any key to continue");
                    Console.ReadKey();
                    break;

                default:
                    Console.WriteLine("You have finished work");
                    return;
                }
            } while (true);
        }