private void onContentUpdate(NewsPair p)
 {
     if (p == CurrentNewsPair)
     {
         OnNewsContentUpdate?.Invoke();
     }
 }
 public void LoadPreviousNewsPair()
 {
     if (currentNewsPairIndex > 0 && currentNewsPairIndex < currentActivePairs.Count())
     {
         CurrentNewsPair = currentActivePairs.ElementAt(--currentNewsPairIndex);
     }
     OnPreviousNewsPair?.Invoke(currentNewsPairIndex > 0);
 }
 private string getOutputString(NewsPair pair)
 {
     string[] tmp = new string[pair.OriginalContent.Length];
     for (int i = 0; i < tmp.Length; i++)
     {
         tmp[i] = pair.OriginalContent[i];
     }
     tmp[config.RelationLabelIndex] = pair.RelationLabel;
     return(string.Join(config.SeparatorWrite, tmp));
 }
 private void writeNewsPairToFile()
 {
     if (currentActivePairs.Count() == backLimit)
     {
         NewsPair val = currentActivePairs.ElementAt(0);
         handler.WriteToOutputFile(val);
         currentNewsPairIndex--;
         currentActivePairs.RemoveAt(0);
     }
 }
        private void WriteToUncertainOutputFile(NewsPair pair)
        {
            if (uncertainOutputFileWriter == null)
            {
                uncertainOutputFileWriter = new StreamWriter(new FileStream(labelWork.UncertainOutputFileName, FileMode.Append, FileAccess.Write), Encoding.UTF8);
            }
            string val = getUncertainOutputString(pair);

            uncertainOutputFileWriter.WriteLine(val);
            uncertainOutputFileWriter.Flush();
        }
        public void WriteToOutputFile(NewsPair pair)
        {
            string val = getOutputString(pair);

            outputFileWriter.WriteLine(val);
            outputFileWriter.Flush();
            if (pair.LabelUncertain)
            {
                WriteToUncertainOutputFile(pair);
            }
        }
 private string getUncertainOutputString(NewsPair pair)
 {
     string[] tmp = new string[pair.OriginalContent.Length + 1];
     for (int i = 0; i < pair.OriginalContent.Length; i++)
     {
         tmp[i] = pair.OriginalContent[i];
     }
     tmp[config.RelationLabelIndex]   = pair.RelationLabel;
     tmp[pair.OriginalContent.Length] = pair.LineNum.ToString();
     return(string.Join(config.SeparatorWrite, tmp));
 }
        private bool addNextNewsPairToList()
        {
            NewsPair next = handler.ParseNextLine();

            if (next == null)
            {
                return(true);
            }
            currentActivePairs.Add(next);
            return(false);
        }
 private void startContentReadWorker(NewsPair pair)
 {
     if (pair == null)
     {
         return;
     }
     contentReadWorker = new BackgroundWorker();
     contentReadWorker.WorkerReportsProgress      = false;
     contentReadWorker.WorkerSupportsCancellation = true;
     contentReadWorker.DoWork             += worker_DoWork;
     contentReadWorker.RunWorkerCompleted += worker_RunWorkerCompleted;
     contentReadWorker.RunWorkerAsync(pair);
 }
        public void InitActiveNewsPairList()
        {
            int i = 0;

            while (i++ < backLimit && !addNextNewsPairToList())
            {
                ;
            }
            if (currentActivePairs.Count() > 0)
            {
                CurrentNewsPair = currentActivePairs.ElementAt(currentNewsPairIndex);
                OnNextNewsPair?.Invoke(currentNewsPairIndex > 0);
            }
            else
            {
                OnEndOfFileReached?.Invoke(currentActivePairs.Count() > 0);
            }
        }
        public void DestroyNewsFileHandler()
        {
            if (handler == null)
            {
                return;
            }
            int end = isAtEOF ? currentActivePairs.Count() : Math.Min(currentActivePairs.Count() - 1, currentNewsPairIndex);

            for (int i = 0; i < end; i++)
            {
                handler.WriteToOutputFile(currentActivePairs.ElementAt(i));
            }
            handler.Destroy();
            currentActivePairs.Clear();
            currentNewsPairIndex = 0;
            handler         = null;
            CurrentNewsPair = null;
            OnNewsFileHandlerDestroy?.Invoke();
        }
 public void LoadNextNewsPair()
 {
     isAtEOF = false;
     if (currentNewsPairIndex >= currentActivePairs.Count() - 1)
     {
         writeNewsPairToFile();
         isAtEOF = addNextNewsPairToList();
     }
     if (!isAtEOF)
     {
         currentNewsPairIndex++;
         CurrentNewsPair = currentActivePairs.ElementAt(currentNewsPairIndex);
         OnNextNewsPair?.Invoke(currentNewsPairIndex > 0);
     }
     else
     {
         OnEndOfFileReached?.Invoke(currentActivePairs.Count() > 0);
     }
 }
        public NewsPair ParseNextLine()
        {
            string line = inputFileReader.ReadLine();

            if (line == null)
            {
                return(null);
            }
            CurrentLineNum++;
            string[] split = line.Split(config.SeparatorRead, StringSplitOptions.None);
            NewsPair rv    = null;

            try
            {
                if (split.Length >= config.NumElementFullSourceFile)
                {
                    rv = new NewsPair(CurrentLineNum, split, split[config.News1TitleIndex],
                                      split[config.News1ContentIndex],
                                      split[config.News2TitleIndex],
                                      split[config.News2ContentIndex],
                                      (int)double.Parse(split[config.CommonTitleWordsIndex])
                                      );
                }
                else if (split.Length >= config.NumElementSimplifiedSourceFile)
                {
                    rv = new NewsPair(CurrentLineNum, split, split[config.News1TitleIndex],
                                      LOADING,
                                      split[config.News2TitleIndex],
                                      LOADING,
                                      (int)double.Parse(split[config.CommonTitleWordsIndex])
                                      );
                    contentReadQ.Enqueue(rv);
                    notifyContentReadWorker();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }
            return(rv);
        }
        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            NewsPair         pair   = e.Argument as NewsPair;

            if ((worker.CancellationPending == true))
            {
                e.Cancel = true;
                return;
            }
            string[] rv = new string[config.NumElementNewsContentFile];
            for (int i = 0; i < rv.Length; i++)
            {
                rv[i] = "(无内容)";
            }
            lock (sharedWorkerResourceLock)
            {
                if (newsFileReader == null)
                {
                    setNewsContentForPair(pair, rv);
                    return;
                }
                string line = newsFileReader.ReadLine();
                if (line == null)
                {
                    setNewsContentForPair(pair, rv);
                    return;
                }
                CurrentContentFileLineNum++;
                string[] split = line.Split(config.SeparatorRead, StringSplitOptions.None);
                if (split.Length != config.NumElementNewsContentFile)
                {
                    setNewsContentForPair(pair, rv);
                    return;
                }
                setNewsContentForPair(pair, split);
            }
            e.Result = pair;
        }
 private void setNewsContentForPair(NewsPair pair, string[] contentArray)
 {
     pair.News1Content = contentArray[config.News1ContentIndexInContentFile];
     pair.News2Content = contentArray[config.News2ContentIndexInContentFile];
 }