示例#1
0
        private void beautifySubs_Click(object sender, EventArgs e)
        {
            InitDictionaryManagerPaths();
            if (excelApp == null)
            {
                string pathToExcel = FileSaver.OpenExcelFile();
                if (pathToExcel == "")
                {
                    MessageBox.Show("Файл " + DM.PathToExcel + " не найден");
                    return;
                }
                excelApp = new ExcelManager();
                excelApp.Open(DM.PathToExcel);
            }
            var    pathSubs = @"C:\3\subs.srt";
            string text     = File.ReadAllText(pathSubs, Encoding.UTF8);

            if (text == "")
            {
                MessageBox.Show("Файл " + DM.fileToAnalizePath + " не найден");
                return;
            }

            WordsFormer allWords = new WordsFormer();

            allWords.analyzeAll(text);

            List <string> lWords = excelApp.GetColumn(DM.Columns.word);
            List <string> lLvl   = excelApp.GetColumn(DM.Columns.level);
            List <string> lSrs   = excelApp.GetColumn(DM.Columns.src);



            for (int i = 0; i < allWords.dict.Count; i++)
            {
                string   word = allWords.dict[i].word;
                Stylizer st   = new Stylizer();
                this.Text = i.ToString();

                int ind = lWords.IndexOf(word);
                if (ind < 0)
                {
                    continue;
                }
                string sign;


                if (lLvl[ind] == "0")
                {
                    if (lSrs[ind] == sourceTextBox.Text)
                    {
                        sign = " * ";
                    }
                    else
                    {
                        sign = " + ";
                    }

                    int tIndex = text.IndexOf(word, StringComparison.CurrentCultureIgnoreCase);
                    if (tIndex < 0)
                    {
                        continue;
                    }
                    do
                    {
                        if (st.IsComplete(word, text, tIndex))
                        {
                            text = text.Insert(tIndex, sign);
                            text = text.Insert(tIndex + sign.Length + word.Length, sign);
                        }
                        tIndex = text.IndexOf(word, tIndex + sign.Length + word.Length + 2, StringComparison.CurrentCultureIgnoreCase);
                    } while (tIndex > 0);
                }
            }

            File.WriteAllText(@"C:\3\subs2.srt", text);


            bool isOnlyNewWords = true;

            if (isOnlyNewWords)
            {
                var           lines    = File.ReadAllLines(@"C:\3\subs2.srt", Encoding.UTF8).ToList();
                List <string> resLines = new List <string>();


                for (int i = 0; i < lines.Count; i++)
                {
                    bool containsNewWords = lines[i].Contains("*") || lines[i].Contains("+");

                    if (containsNewWords)
                    {
                        int st  = findStart(lines, i);
                        int end = findEnd(lines, i);
                        for (int k = st; k <= end; k++)
                        {
                            resLines.Add(lines[k]);
                        }
                        i = end;
                    }
                }
                File.WriteAllLines(@"C:\3\subs23.srt", resLines);
            }

            beautifyButton_Click(null, null);

            /*
             * excelApp.Quit();
             * Application.Exit();*/
        }