Beispiel #1
0
        /// <summary>
        /// 取消异步训练操作。
        /// </summary>
        public void CancelAsync()
        {
            if (_cts != null)
                _cts.Cancel();

            TrainingCompletedEventArgs ea =
                new TrainingCompletedEventArgs(null, null, true, null);
            OnTrainingCompleted(ea);
            _isRunning = false;
        }
Beispiel #2
0
 private void OnTrainingCompleted(TrainingCompletedEventArgs e)
 {
     e.Raise(this, ref TrainingCompleted);
 }
Beispiel #3
0
        /// <summary>
        /// 异步在一个文档集上训练。
        /// </summary>
        /// <param name="corpus">训练集</param>
        public void TrainAsync(Corpus corpus)
        {
            if (_isRunning)
                throw new InvalidOperationException(MessageInvalidOperation);

            if (Initialize())
            {
                _isRunning = true;
                _cts = new CancellationTokenSource();

                Task task = Task.Factory.StartNew(() =>
                {
                    try
                    {
                        Model model = LearnCorpus(corpus, _cts.Token);

                        TrainingCompletedEventArgs ea =
                            new TrainingCompletedEventArgs(
                                model, null, false, null);
                        OnTrainingCompleted(ea);
                        _isRunning = false;
                    }
                    catch (Exception ex)
                    {
                        TrainingCompletedEventArgs ea =
                            new TrainingCompletedEventArgs(
                                null, ex, false, null);
                        OnTrainingCompleted(ea);
                        _isRunning = false;
                    }
                }, _cts.Token);
            }
            else
            {
                throw new ApplicationException(MessageInitializeFailed);
            }
        }