// _____ _ _ _____ _ ______ _ _ // | __ \ | | | | | __ \ | | | ____(_) | // | |__) |___| | ___ __ _ __| | | | | | __ _| |_ __ _ | |__ _| | ___ // | _ // _ \ |/ _ \ / _` |/ _` | | | | |/ _` | __/ _` | | __| | | |/ _ \ // | | \ \ __/ | (_) | (_| | (_| | | |__| | (_| | || (_| | | | | | | __/ // |_| \_\___|_|\___/ \__,_|\__,_| |_____/ \__,_|\__\__,_| |_| |_|_|\___| private void ReloadCSVButton_Click(object sender, EventArgs e) { ColumnNameCheckedListbox.Items.Clear(); FilenameLabel.Text = "Clearing old preview... (This might take a while for previews with a large number of columns.)"; FilenameLabel.Invalidate(); FilenameLabel.Update(); FilenameLabel.Refresh(); dataGridView1.DataSource = null; FilenameLabel.Text = "Ready to load a data file preview."; if (FilenameDisplayBox.Text != "No file selected...") { DisableButtons(); BgWorkerInformation BgData = new BgWorkerInformation(); BgData.InputFile = FilenameDisplayBox.Text; BgData.HasHeaders = HeaderRowDropdown.SelectedItem.ToString(); BgData.Delimiters = DelimiterTextBox.Text.ToString(); BgData.UsingQuotes = EnclosedInQuotesDropdown.SelectedItem.ToString(); LoadCSVPreview_BGWorker.RunWorkerAsync(BgData); } }
// _ _ _____ _ ____ _ _ // | | | | | __ \ | | | _ \ | | | | // | | ___ __ _ __| | | | | | __ _| |_ __ _ | |_) |_ _| |_| |_ ___ _ __ // | | / _ \ / _` |/ _` | | | | |/ _` | __/ _` | | _ <| | | | __| __/ _ \| '_ \ // | |___| (_) | (_| | (_| | | |__| | (_| | || (_| | | |_) | |_| | |_| || (_) | | | | // |______\___/ \__,_|\__,_| |_____/ \__,_|\__\__,_| |____/ \__,_|\__|\__\___/|_| |_| // private void GeneratePreviewButton_Click(object sender, EventArgs e) { FirstColumnComboBox.Items.Clear(); LastColumnComboBox.Items.Clear(); TokenColumnComboBox.Items.Clear(); FilenameDisplayBox.Text = "No file selected..."; FilenameLabel.Text = "Clearing old preview... (This might take a while for previews with a large number of columns.)"; FilenameLabel.Invalidate(); FilenameLabel.Update(); FilenameLabel.Refresh(); dataGridView1.DataSource = null; FilenameLabel.Text = "Ready to load a data file preview."; openFileDialog.Title = "Please select you data file..."; DialogResult InputFileDialog = openFileDialog.ShowDialog(); if (InputFileDialog != DialogResult.Cancel) { DisableButtons(); string InputFile = openFileDialog.FileName; FilenameDisplayBox.Text = InputFile; FilenameDisplayBox.Focus(); // Move the caret to the end of the text box FilenameDisplayBox.Select(FilenameDisplayBox.Text.Length, 0); BgWorkerInformation BgData = new BgWorkerInformation(); BgData.InputFile = FilenameDisplayBox.Text; BgData.HasHeaders = HeaderRowDropdown.SelectedItem.ToString(); BgData.Delimiters = DelimiterTextBox.Text.ToString(); BgData.UsingQuotes = EnclosedInQuotesDropdown.SelectedItem.ToString(); LoadCSVPreview_BGWorker.RunWorkerAsync(BgData); } else { FilenameDisplayBox.Text = "No file selected..."; StartButton.Enabled = false; ReloadCSVButton.Enabled = false; FirstColumnComboBox.Items.Clear(); LastColumnComboBox.Items.Clear(); TokenColumnComboBox.Items.Clear(); } }
private void LoadCSVPreview_BGWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { FilenameLabel.Text = "Please wait while preview is being generated... (This might take a while for files with a large number of columns.)"; FilenameLabel.Invalidate(); FilenameLabel.Update(); FilenameLabel.Refresh(); Application.DoEvents(); //bind the results to the datagridview try { dataGridView1.DataSource = e.Result; EnableButtons(); ReloadCSVButton.Enabled = true; StartButton.Enabled = true; MessageBox.Show("Your data file preview has been loaded." + "\r\n\r\n" + "If your preview window appears to be empty, you most likely need to edit your settings under the \"Options for Reading Data File\" section.", "Preview Loaded", MessageBoxButtons.OK, MessageBoxIcon.Information); foreach (DataGridViewColumn column in dataGridView1.Columns) { TokenColumnComboBox.Items.Add(column.HeaderText); FirstColumnComboBox.Items.Add(column.HeaderText); LastColumnComboBox.Items.Add(column.HeaderText); } TokenColumnComboBox.SelectedIndex = 0; FirstColumnComboBox.SelectedIndex = 0; LastColumnComboBox.SelectedIndex = LastColumnComboBox.Items.Count - 1; } catch { ReloadCSVButton.Enabled = false; StartButton.Enabled = false; MessageBox.Show("Your spreadsheet file could not be properly parsed" + "\r\n" + "with the current settings. Please make sure that the" + "\r\n" + "file is not open elsewhere, check your settings, and" + "\r\n" + "try again.", "Data Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } FilenameLabel.Text = "Finished creating dataset preview."; }
private void BgWorkerClean_DoWork(object sender, DoWorkEventArgs e) { DictionaryData BGWorkerData = (DictionaryData)e.Argument; TranslationClient client = TranslationClient.Create(); //selects the text encoding based on user selection Encoding InputSelectedEncoding = null; Encoding OutputSelectedEncoding = null; this.Invoke((MethodInvoker) delegate() { InputSelectedEncoding = Encoding.GetEncoding(InputEncodingDropdown.SelectedItem.ToString()); OutputSelectedEncoding = Encoding.GetEncoding(OutputEncodingDropdown.SelectedItem.ToString()); }); //get the list of files var SearchDepth = SearchOption.TopDirectoryOnly; if (ScanSubfolderCheckbox.Checked) { SearchDepth = SearchOption.AllDirectories; } var files = Directory.EnumerateFiles(BGWorkerData.TextFileFolder, BGWorkerData.FileExtension, SearchDepth); try { foreach (string fileName in files) { if (e.Cancel) { break; } //set up our variables to report string Filename_Clean = Path.GetFileName(fileName); string SubDirStructure = Path.GetDirectoryName(fileName).Replace(BGWorkerData.TextFileFolder, "").TrimStart('\\'); //creates subdirs if they don't exist string Output_Location = BGWorkerData.OutputFileLocation + '\\' + SubDirStructure; if (!Directory.Exists(Output_Location)) { Directory.CreateDirectory(Output_Location); } Output_Location = Path.Combine(Output_Location, Path.GetFileName(fileName)); //report what we're working on FilenameLabel.Invoke((MethodInvoker) delegate { FilenameLabel.Text = "Processing: " + Filename_Clean; FilenameLabel.Invalidate(); FilenameLabel.Update(); FilenameLabel.Refresh(); Application.DoEvents(); }); // __ __ _ _ ___ _ _ // \ \ / / __(_) |_ ___ / _ \ _ _| |_ _ __ _ _| |_ // \ \ /\ / / '__| | __/ _ \ | | | | | | | __| '_ \| | | | __| // \ V V /| | | | || __/ | |_| | |_| | |_| |_) | |_| | |_ // \_/\_/ |_| |_|\__\___| \___/ \__,_|\__| .__/ \__,_|\__| // |_| using (StreamReader inputfile = new StreamReader(fileName, InputSelectedEncoding)) { if (e.Cancel) { break; } string readText = inputfile.ReadToEnd(); string[] readText_Chunked = new string[0]; if (!string.IsNullOrWhiteSpace(readText)) { readText_Chunked = SplitStringByLength(readText, BGWorkerData.MaxCharsPerRequest); } StringBuilder TranslatedText_Output = new StringBuilder(); for (int i = 0; i < readText_Chunked.Length; i++) { if (e.Cancel) { break; } try { if (e.Cancel) { break; } StatusLabel.Invoke((MethodInvoker) delegate { StatusLabel.Text = "Status: Sending request " + (i + 1).ToString() + "/" + readText_Chunked.Length.ToString() + " to API..."; StatusLabel.Invalidate(); StatusLabel.Update(); StatusLabel.Refresh(); Application.DoEvents(); }); var response = client.TranslateText(readText_Chunked[i], sourceLanguage: BGWorkerData.InputLang, targetLanguage: BGWorkerData.OutputLang); TranslatedText_Output.Append(response.TranslatedText + " "); } catch (Google.GoogleApiException ex) { if (e.Cancel) { break; } if (ex.Error.Code == 403) { if (ex.Error.Message.Contains("Daily Limit Exceeded")) { //report what we're working on StatusLabel.Invoke((MethodInvoker) delegate { StatusLabel.Text = "Status: " + ex.Error.Message; StatusLabel.Invalidate(); StatusLabel.Update(); StatusLabel.Refresh(); Application.DoEvents(); }); MessageBox.Show("The Google Translate API reports that you have exceeded your daily use limit. You will need to visit the \"Quotas\" section of the Google Cloud Dashboard to increase your limits or, alternatively, wait until midnight for your quota to reset.", "Daily Limit Exceeded", MessageBoxButtons.OK, MessageBoxIcon.Stop); e.Cancel = true; break; } else { if (e.Cancel) { break; } int retry_counter = 0; while (retry_counter < BGWorkerData.MaxRetries) { retry_counter++; int TimerCounter = 0; DateTime d = DateTime.Now; while (TimerCounter < BGWorkerData.DurationLength + 2) { TimeSpan ts = DateTime.Now.Subtract(d); if (ts.Seconds >= 1) { //do some work TimerCounter += ts.Seconds; d = DateTime.Now; //report what we're working on StatusLabel.Invoke((MethodInvoker) delegate { StatusLabel.Text = "Status: Rate limit reached. Sleeping for " + (BGWorkerData.DurationLength - TimerCounter + 1).ToString() + "..."; StatusLabel.Invalidate(); StatusLabel.Update(); StatusLabel.Refresh(); Application.DoEvents(); }); } } try { //report what we're working on StatusLabel.Invoke((MethodInvoker) delegate { StatusLabel.Text = "Status: Sending request " + (i + 1).ToString() + "/" + readText_Chunked.Length.ToString() + " to API... Retry #" + retry_counter.ToString(); StatusLabel.Invalidate(); StatusLabel.Update(); StatusLabel.Refresh(); Application.DoEvents(); }); var response = client.TranslateText(readText_Chunked[i], sourceLanguage: BGWorkerData.InputLang, targetLanguage: BGWorkerData.OutputLang); TranslatedText_Output.Append(response.TranslatedText + " "); retry_counter = BGWorkerData.MaxRetries; } catch { } } } } else if (ex.Error.Code == 429 || (ex.Error.Code >= 500 && ex.Error.Code < 600)) { int retry_counter = 0; while (retry_counter < BGWorkerData.MaxRetries) { retry_counter++; int TimerCounter = 0; DateTime d = DateTime.Now; while (TimerCounter < System.Math.Pow(retry_counter, 2)) { TimeSpan ts = DateTime.Now.Subtract(d); if (ts.Seconds >= 1) { //do some work TimerCounter += ts.Seconds; d = DateTime.Now; //report what we're working on StatusLabel.Invoke((MethodInvoker) delegate { StatusLabel.Text = "Status: Error " + ex.Error.Code.ToString() + "; " + ex.Error.Message + " -- Retrying in " + (BGWorkerData.DurationLength - TimerCounter + 1).ToString() + "..."; StatusLabel.Invalidate(); StatusLabel.Update(); StatusLabel.Refresh(); Application.DoEvents(); }); } } try { //report what we're working on StatusLabel.Invoke((MethodInvoker) delegate { StatusLabel.Text = "Status: Sending request " + (i + 1).ToString() + "/" + readText_Chunked.Length.ToString() + " to API... Retry #" + retry_counter.ToString(); StatusLabel.Invalidate(); StatusLabel.Update(); StatusLabel.Refresh(); Application.DoEvents(); }); var response = client.TranslateText(readText_Chunked[i], sourceLanguage: BGWorkerData.InputLang, targetLanguage: BGWorkerData.OutputLang); TranslatedText_Output.Append(response.TranslatedText + " "); retry_counter = BGWorkerData.MaxRetries; } catch { } } } else { //report what we're working on StatusLabel.Invoke((MethodInvoker) delegate { StatusLabel.Text = "Status: " + ex.Error.Message; StatusLabel.Invalidate(); StatusLabel.Update(); StatusLabel.Refresh(); Application.DoEvents(); }); } } } //open up the output file using (StreamWriter outputFile = new StreamWriter(new FileStream(Output_Location, FileMode.Create), OutputSelectedEncoding)) { outputFile.Write(TranslatedText_Output.ToString()); } } } } catch (Exception ex) { MessageBox.Show("Transmogrifier encountered an issue somewhere while trying to translate your texts. The most common cause of this is trying to open your output file(s) while the program is still running. Did any of your input files move, or is your output file being opened/modified by another application? " + "After clicking the \"OK\" Button, you will receive an error code. Please write down this error code (or take a screenshot) and contact the software's author ([email protected]) for additional help.", "Error while translating", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show(ex.ToString(), "Error Code", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void BgWorkerClean_DoWork(object sender, DoWorkEventArgs e) { DictionaryData DictData = (DictionaryData)e.Argument; //selects the text encoding based on user selection Encoding InputSelectedEncoding = null; Encoding OutputSelectedEncoding = null; this.Invoke((MethodInvoker) delegate() { InputSelectedEncoding = Encoding.GetEncoding(InputEncodingDropdown.SelectedItem.ToString()); OutputSelectedEncoding = Encoding.GetEncoding(OutputEncodingDropdown.SelectedItem.ToString()); }); //get the list of files var SearchDepth = SearchOption.TopDirectoryOnly; if (ScanSubfolderCheckbox.Checked) { SearchDepth = SearchOption.AllDirectories; } var files = Directory.EnumerateFiles(DictData.TextFileFolder, DictData.FileExtension, SearchDepth); try { //we want to be conservative and limit the number of threads to the number of processors that we have var options = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }; Parallel.ForEach(files, options, (string fileName) => { //set up our variables to report string Filename_Clean = Path.GetFileName(fileName); string SubDirStructure = Path.GetDirectoryName(fileName).Replace(DictData.TextFileFolder, "").TrimStart('\\'); //creates subdirs if they don't exist string Output_Location = DictData.OutputFileLocation + '\\' + SubDirStructure; if (!Directory.Exists(Output_Location)) { Directory.CreateDirectory(Output_Location); } Output_Location = Path.Combine(Output_Location, Path.GetFileName(fileName)); //report what we're working on FilenameLabel.Invoke((MethodInvoker) delegate { FilenameLabel.Text = "Processing: " + Filename_Clean; FilenameLabel.Invalidate(); FilenameLabel.Update(); FilenameLabel.Refresh(); Application.DoEvents(); }); // __ __ _ _ ___ _ _ // \ \ / / __(_) |_ ___ / _ \ _ _| |_ _ __ _ _| |_ // \ \ /\ / / '__| | __/ _ \ | | | | | | | __| '_ \| | | | __| // \ V V /| | | | || __/ | |_| | |_| | |_| |_) | |_| | |_ // \_/\_/ |_| |_|\__\___| \___/ \__,_|\__| .__/ \__,_|\__| // |_| using (StreamReader inputfile = new StreamReader(fileName, InputSelectedEncoding)) { string readText = inputfile.ReadToEnd(); if (DictData.FixNULtermination) { readText = string.Join("", readText.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries)); } //open up the output file using (StreamWriter outputFile = new StreamWriter(new FileStream(Output_Location, FileMode.Create), OutputSelectedEncoding)) { outputFile.Write(readText); } } }); } catch { MessageBox.Show("TranscodeTXT encountered an issue somewhere while trying to analyze your texts. The most common cause of this is trying to open your output file(s) while the program is still running. Did any of your input files move, or is your output file being opened/modified by another application?", "Error while transcoding", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void BgWorkerClean_DoWork(object sender, DoWorkEventArgs e) { DictionaryData DictData = (DictionaryData)e.Argument; //selects the text encoding based on user selection Encoding OutputSelectedEncoding = null; this.Invoke((MethodInvoker) delegate() { OutputSelectedEncoding = Encoding.GetEncoding(OutputEncodingDropdown.SelectedItem.ToString()); }); //get the list of files var SearchDepth = SearchOption.TopDirectoryOnly; if (ScanSubfolderCheckbox.Checked) { SearchDepth = SearchOption.AllDirectories; } var files = Directory.EnumerateFiles(DictData.TextFileFolder, DictData.FileExtension, SearchDepth); try { using (StreamWriter outputFile = new StreamWriter(new FileStream(DictData.OutputFileLocation, FileMode.Create), OutputSelectedEncoding)) { outputFile.WriteLine("\"Filename\",\"Created\",\"FileSizeKB\",\"Encoding\""); //add some CODE TO WRITE THE HEADER FOR YOUR CSV FILE HERE!!!! foreach (string fileName in files) { //set up our variables to report string Filename_Clean = Path.GetFileName(fileName); //report what we're working on FilenameLabel.Invoke((MethodInvoker) delegate { FilenameLabel.Text = "Analyzing: " + Filename_Clean; FilenameLabel.Invalidate(); FilenameLabel.Update(); FilenameLabel.Refresh(); Application.DoEvents(); }); string[] OutputString = new string[4]; FileInfo oFileInfo = new FileInfo(fileName); string FileEncodingDetected = ExamineTXT.SimpleHelpers.FileEncoding.DetectFileEncoding(fileName); string DetectedEncodingString = "[UNKNOWN]"; if (FileEncodingDetected != null) { DetectedEncodingString = FileEncodingDetected; } OutputString[0] = fileName; OutputString[1] = oFileInfo.CreationTime.ToString(); OutputString[2] = (oFileInfo.Length / 1024.0).ToString("#.##"); OutputString[3] = DetectedEncodingString; outputFile.WriteLine("\"" + string.Join("\",\"", OutputString) + "\""); } } } catch { MessageBox.Show("ExamineTXT encountered an issue somewhere while trying to analyze your texts. The most common cause of this is trying to open your output file(s) while the program is still running. Did any of your input files move, or is your output file being opened/modified by another application?", "Error while transcoding", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void BgWorkerClean_DoWork(object sender, DoWorkEventArgs e) { BGWorkerData BGData = (BGWorkerData)e.Argument; BGData.NumberOfMatches = new uint[BGData.RegexArray.Length]; BGData.TotalFilesMatched = new uint[BGData.RegexArray.Length]; for (int i = 0; i < BGData.NumberOfMatches.Length; i++) { BGData.NumberOfMatches[i] = 0; BGData.TotalFilesMatched[i] = 0; } //selects the text encoding based on user selection Encoding SelectedEncoding = null; this.Invoke((MethodInvoker) delegate() { SelectedEncoding = Encoding.GetEncoding(EncodingDropdown.SelectedItem.ToString()); }); //get the list of files var SearchDepth = SearchOption.TopDirectoryOnly; if (ScanSubfolderCheckbox.Checked) { SearchDepth = SearchOption.AllDirectories; } var files = Directory.EnumerateFiles(BGData.TextFileFolder, BGData.Filetype, SearchDepth); try { //we want to be conservative and limit the number of threads to the number of processors that we have var options = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }; Parallel.ForEach(files, options, (string fileName) => { //set up our variables to report string Filename_Clean = Path.GetFileName(fileName); string SubDirStructure = Path.GetDirectoryName(fileName).Replace(BGData.TextFileFolder, "").TrimStart('\\'); //creates subdirs if they don't exist string Output_Location = BGData.OutputFileLocation + '\\' + SubDirStructure; if (!Directory.Exists(Output_Location)) { Directory.CreateDirectory(Output_Location); } Output_Location = Path.Combine(Output_Location, Path.GetFileName(fileName)); //report what we're working on FilenameLabel.Invoke((MethodInvoker) delegate { FilenameLabel.Text = "Processing: " + Filename_Clean; FilenameLabel.Invalidate(); FilenameLabel.Update(); FilenameLabel.Refresh(); Application.DoEvents(); }); //read in the text file, convert everything to lowercase string readText = File.ReadAllText(fileName, SelectedEncoding); if (BGData.CompactWhitespace) { readText = Regex.Replace(readText, @"\s+", " "); } // _ _ _____ _ // / \ _ __ __ _| |_ _ _______ |_ _|____ _| |_ // / _ \ | '_ \ / _` | | | | |_ / _ \ | |/ _ \ \/ / __| // / ___ \| | | | (_| | | |_| |/ / __/ | | __/> <| |_ // /_/ \_\_| |_|\__,_|_|\__, /___\___| |_|\___/_/\_\\__| // |___/ for (int i = 0; i < BGData.RegexArray.Length; i++) { int NumMatches = BGData.RegexArray[i].Matches(readText).Count; if (NumMatches == 0) { continue; } BGData.NumberOfMatches[i] += (uint)NumMatches; BGData.TotalFilesMatched[i] += 1; readText = BGData.RegexArray[i].Replace(readText, BGData.ReplacementArray[i]); } // __ __ _ _ ___ _ _ // \ \ / / __(_) |_ ___ / _ \ _ _| |_ _ __ _ _| |_ // \ \ /\ / / '__| | __/ _ \ | | | | | | | __| '_ \| | | | __| // \ V V /| | | | || __/ | |_| | |_| | |_| |_) | |_| | |_ // \_/\_/ |_| |_|\__\___| \___/ \__,_|\__| .__/ \__,_|\__| // |_| //open up the output file using (StreamWriter outputFile = new StreamWriter(new FileStream(Output_Location, FileMode.Create), SelectedEncoding)) { outputFile.Write(readText); } }); using (StreamWriter outputFile = new StreamWriter(new FileStream(Path.Combine(BGData.OutputFileLocation, "__TextEmend-Report.csv"), FileMode.Create), SelectedEncoding)) { outputFile.WriteLine("\"RegEx\",\"Replacement\",\"NumberOfMatches\",\"FilesWithPattern\""); for (int i = 0; i < BGData.RegexArray.Length; i++) { outputFile.WriteLine("\"" + BGData.RegexArray[i].ToString() + "\"," + "\"" + BGData.ReplacementArray[i] + "\"," + BGData.NumberOfMatches[i].ToString() + "," + BGData.TotalFilesMatched[i].ToString()); } } } catch { MessageBox.Show("TextEmend encountered an issue somewhere while trying to analyze your texts. The most common cause of this is trying to open your output file(s) while the program is still running. Did any of your input files move, or is your output file being opened/modified by another application? Are you sure that your regular expressions are properly formed?", "Error while analyzing", MessageBoxButtons.OK, MessageBoxIcon.Error); } }