/// <summary> /// Adds word to dictionary. Sends event about any words processed. /// Sends last word event in order to notify that full data can be pulled out in sync way /// </summary> /// <param name="sender"></param> /// <param name="args"></param> public void ProcessWord(object sender, WordEventArgs args) { //ignore if (_count >= args.WordCount) { return; } //some unsuccessful parse came if (args.Word == null) { Log.Warn($"Some unsuccessfully parse value has arrived"); _count++; EvaluateEndCondition(args); return; } lock (lockObject) { var key = args.Word.ToLower(); if (_wordCache.Keys.Contains(key)) { _wordCache[key]++; } else { _wordCache.Add(key, 1); } _count++; EvaluateEndCondition(args); } }
private void EvaluateEndCondition(WordEventArgs args) { if (_count == args.WordCount) { //raise an final event here Log.Debug("Last word has arrived. It is the time to notify that results are ready"); LastWordProcessed(this, EventArgs.Empty); } else { //TODO: raise an event here WordProcessed(this, args); } }
/// <summary> /// Splits parameters.Text to words /// Throws event per word /// </summary> /// <param name="parameters">Text contains sentence(or full text)</param> public void Analyze(IProcessSentenceParameters parameters) { _wordProcessor.Reset(); var matches = _regex.Matches(parameters.Text); long counter = 0; foreach (Match match in matches) { //raise events here WordEventArgs args; if (match.Success) { var word = match.Groups["word"].Value; args = new WordEventArgs(word, ++counter, matches.Count); } else { args = new WordEventArgs(null, ++counter, matches.Count); } _wordProcessorHandler(this, args); } }