예제 #1
0
        public void CopyFile(FileAnalysisInfo file, string prefix = null)
        {
            mres.Wait();
            if (allTXTFiles.Where(a => a.PathToFile == file.PathToFile && a.CountWords > 0).FirstOrDefault() != null)
            {
                int    index = 0;
                string tmp;
                lock (this)
                {
                    do
                    {
                        mres.Wait();
                        if (token.IsCancellationRequested)
                        {
                            token.ThrowIfCancellationRequested();
                        }
                        tmp = prefix == null?Path.Combine(resultFolderPath, Path.GetFileNameWithoutExtension(file.PathToFile) + (index == 0 ? null : $"({index})") + Path.GetExtension(file.PathToFile)) : Path.Combine(resultFolderPath, $"{prefix}" + Path.GetFileNameWithoutExtension(file.PathToFile) + (index == 0 ? null : $"({index})") + Path.GetExtension(file.PathToFile));

                        index++;
                    } while (File.Exists(tmp));

                    file.PathToCopyFile = tmp;
                    File.Copy(file.PathToFile, tmp, false);
                }
            }

            mres.Set();
            Thread.Sleep(2000);
        }
        public void Analysis(FileAnalysisInfo fai, ManualResetEventSlim mres, CancellationToken token)
        {
            string[] TempArray = new string[WordsCount.Count];
            WordsCount.Keys.CopyTo(TempArray, 0);

            using (StreamReader sr = new StreamReader(File.OpenRead(fai.PathToFile)))
            {
                mres.Wait();
                while (!sr.EndOfStream)
                {
                    if (token.IsCancellationRequested)
                    {
                        return;
                    }
                    string text = sr.ReadLine();


                    string[] words = text.Split(new char[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries);
                    foreach (var word in words)
                    {
                        foreach (var item in TempArray)
                        {
                            if (token.IsCancellationRequested)
                            {
                                return;
                            }

                            if (word.Contains(item.ToString()))
                            {
                                lock (this)
                                {
                                    if (token.IsCancellationRequested)
                                    {
                                        token.ThrowIfCancellationRequested();
                                    }
                                    System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                                    {
                                        WordsCount[item.ToString()]++;
                                        fai.CountWords++;
                                    }));
                                }
                            }
                        }
                    }
                }
                mres.Set();
                Thread.Sleep(2000);
            }
        }
        public void Replace(FileAnalysisInfo fai, ManualResetEventSlim mres)
        {
            using (StreamReader sr = new StreamReader(File.OpenRead(fai.PathToFile)))
            {
                mres.Wait();
                string result = "";
                while (!sr.EndOfStream)
                {
                    string    line  = sr.ReadLine();
                    string [] words = SplitToWords(line);

                    for (int i = 0; i < words.Length; i++)
                    {
                        {
                            foreach (var item in WordsCount.Keys)
                            {
                                if (words[i].ToLower().Contains(item.ToLower()))
                                {
                                    words[i] = "*******";
                                }
                            }
                        }
                    }
                    foreach (var item in words)
                    {
                        result += item;
                    }
                    result += "\n";
                }
                using (StreamWriter sw = new StreamWriter(File.OpenWrite(fai.PathToCopyFile)))
                {
                    sw.Write(result);
                    sw.Close();
                }
                mres.Set();
                sr.Close();
            }
        }