public void AssignCorrectSpelling(string strBadWord) { if (String.IsNullOrEmpty(m_strConverterSpec)) { throw new ExternalException("No project selected! Did you open a project?"); } // in case it was deleted by the user, recreate it now. if (File.Exists(m_strConverterSpec)) { // the file already exists... see if this word would otherwise already be altered by the cc table if (ChaChaChaChaChanges(strBadWord)) { DialogResult res = MessageBox.Show(String.Format("There is already a replacement rule that affects the string ({0}). {1}{1}Click 'Retry' to display the existing rule or 'Ignore' to add a new rule{1}(which must be a longer string than the existing rule to override it).", strBadWord, Environment.NewLine), cstrCaption, MessageBoxButtons.AbortRetryIgnore); if (res == DialogResult.Abort) { return; } else if (res == DialogResult.Retry) { this.FindReplacementRule(strBadWord); return; } } } else { LoginSF.CreateCCTable(m_strConverterSpec, SpellFixerEncConverterName, PunctuationAndWhiteSpace, null, !this.m_bLegacy); } QueryAndAppend(strBadWord); }
public void QueryForSpellingCorrectionIfTableEmpty(string strBadWord) { // this is just a convenience method, so if things aren't configured correctly, just exit if (String.IsNullOrEmpty(m_strConverterSpec)) { return; } // even if the file exists, it might have no rules, so double-check if (File.Exists(m_strConverterSpec)) { DataTable myTable; Encoding enc = GetEncoding; if (!InitializeDataTableFromCCTable(m_strConverterSpec, enc, WordBoundaryDelimiter, out myTable) || (myTable.Rows.Count > 0)) { return; // don't query for a record if there are already spelling corrections in the file } } else { LoginSF.CreateCCTable(m_strConverterSpec, SpellFixerEncConverterName, PunctuationAndWhiteSpace, null, !this.m_bLegacy); } QueryAndAppend(strBadWord); }
internal void WriteCCTableFromDataTable(string strFilename, Encoding enc, DataTable tbl, int nTableIndex, int nNumRows) { if (File.Exists(strFilename)) { File.Delete(strFilename); } StreamWriter sw = new StreamWriter(strFilename, false, enc); LoginSF.CreateCCTable(sw, SpellFixerEncConverterName, PunctuationAndWhiteSpace, null, !m_bLegacy); AppendCCTableFromDataTable(sw, WordBoundaryDelimiter, PunctuationAndWhiteSpace, tbl, nTableIndex, nNumRows); sw.Flush(); sw.Close(); }
public void EditSpellingFixes() { if (m_strConverterSpec != null) { // Open the CC table that has the mappings and put them in a DataTable. if (!File.Exists(m_strConverterSpec)) { LoginSF.CreateCCTable(m_strConverterSpec, SpellFixerEncConverterName, PunctuationAndWhiteSpace, null, !this.m_bLegacy); } // if it was legacy encoded, then we need to convert the data to narrow using // the code page the user specified (or we got out of the repository) Encoding enc = GetEncoding; DataTable myTable; if (InitializeDataTableFromCCTable(m_strConverterSpec, enc, WordBoundaryDelimiter, out myTable)) { // now put up an editable grid with this data. DialogResult res = DialogResult.Cancel; try { ViewBadGoodPairsDlg dlg = new ViewBadGoodPairsDlg(myTable, m_font); res = dlg.ShowDialog(); } #if DEBUG catch (Exception ex) { MessageBox.Show(ex.Message, SpellingFixerEC.cstrCaption); } #else catch { } #endif if (res == DialogResult.OK) { LoginSF.ReWriteCCTableHeader(m_strConverterSpec, PunctuationAndWhiteSpace, enc); AppendCCTableFromDataTable(m_strConverterSpec, enc, WordBoundaryDelimiter, PunctuationAndWhiteSpace, myTable); } } } }
public SpellingFixerEC(string strProjectName) { LoginSF login = new LoginSF(); if (login.LoadProject(strProjectName)) { m_font = login.FontToUse; m_bLegacy = login.IsLegacy; if (m_bLegacy) { m_cp = login.CpToUse; } m_strConverterSpec = login.ConverterSpec; SpellFixerEncConverterName = login.EncConverterName; WordBoundaryDelimiter = login.WordBoundaryDelimiter; PunctuationAndWhiteSpace = login.Punctuation; } else { throw new ExternalException("No project selected"); } }
/// <summary> /// If you use the default ctor, and want to log into a SpellFixer project (as opposed to CscProject) /// then use this method. /// </summary> public void LoginProject() { LoginSF login = new LoginSF(); if (login.ShowDialog() == DialogResult.OK) { m_font = login.FontToUse; m_bLegacy = login.IsLegacy; if (m_bLegacy) { m_cp = login.CpToUse; } m_strConverterSpec = login.ConverterSpec; SpellFixerEncConverterName = login.EncConverterName; WordBoundaryDelimiter = login.WordBoundaryDelimiter; PunctuationAndWhiteSpace = login.Punctuation; } else { throw new ExternalException("No project selected"); } }
public void AssignCorrectSpelling(string strBadWord, string strReplacement) { // in case it was deleted by the user, recreate it now. if (File.Exists(m_strConverterSpec)) { // the file already exists... see if this word would otherwise already be altered by the cc table if (ChaChaChaChaChanges(strBadWord)) { DialogResult res = MessageBox.Show(String.Format("There is already a replacement rule that affects the string ({0}). {2}{2}Click 'Retry' to display the existing rule or 'Ignore' to continuing adding the new rule ({0})->({1}).", strBadWord, strReplacement, Environment.NewLine), cstrCaption, MessageBoxButtons.AbortRetryIgnore); if (res == DialogResult.Abort) { return; } else if (res == DialogResult.Retry) { this.FindReplacementRule(strBadWord); return; } } } else { LoginSF.CreateCCTable(m_strConverterSpec, SpellFixerEncConverterName, PunctuationAndWhiteSpace, null, !this.m_bLegacy); } // if it was legacy encoded, then we need to convert the data to narrow using // the code page the user specified (or we got out of the repository) Encoding enc = GetEncoding; // get a stream writer for this encoding and append StreamWriter sw = new StreamWriter(m_strConverterSpec, true, enc); sw.WriteLine(FormatSubstitutionRule(strBadWord, strReplacement, WordBoundaryDelimiter, strBadWord)); sw.Flush(); sw.Close(); }
public void FindReplacementRule(string strWord) { // first make sure the CC table exists if ((m_strConverterSpec != null) && File.Exists(m_strConverterSpec)) { CleanWord(ref strWord); // Open the CC table that has the mappings and put them in a DataTable. DataTable myTable; Encoding enc = GetEncoding; if (InitializeDataTableFromCCTable(m_strConverterSpec, enc, WordBoundaryDelimiter, out myTable)) { // temporary filename for temporary CC tables (to check portions of the file at a time) string strTempName = Path.GetTempFileName(); // get a CC table EncConverter IEncConverter aEC = new EncConverters().NewEncConverterByImplementationType(EncConverters.strTypeSILcc); // check to make sure that the whole table has a rule which changes it (it might not) int nFoundIndex = -1; if (ChaChaChaChaChanges(aEC, m_strConverterSpec, strWord)) { // do a binary search to find the one replacement rule that causes a change int nLength = myTable.Rows.Count, nIndex = 0; nFoundIndex = nIndex; DataTable tblTestingRules = GetDataTable; while (nLength > 1) { // check the lower half int nLowHalfLength = nLength / 2; // GetPortionOfTable(myTable, nIndex, nLowHalfLength, ref tblTestingRules); if (ChaChaChaChaChanges(aEC, strTempName, enc, strWord, myTable, nIndex, nLowHalfLength)) { // found in the lower half nFoundIndex = nIndex; nLength = nLowHalfLength; } else { // otherwise check in the upper half // GetPortionOfTable(myTable, nIndex + nLowHalfLength, nLength - nLowHalfLength, ref tblTestingRules); if (ChaChaChaChaChanges(aEC, strTempName, enc, strWord, myTable, nIndex + nLowHalfLength, nLength - nLowHalfLength)) { // found in the upper half nIndex += nLowHalfLength; nFoundIndex = nIndex; nLength -= nLowHalfLength; } } } } // clean up the temporary file. File.Delete(strTempName); // if we didn't see any rules that manipulate the input string, then see if any generate // the input string (i.e. compare the word against the right-hand side) if (nFoundIndex == -1) { // let's trim it of external spaces first strWord = strWord.Trim(); for (nFoundIndex = 0; nFoundIndex < myTable.Rows.Count; nFoundIndex++) { DataRow row = myTable.Rows[nFoundIndex]; if (strWord == (string)row[strColumnRhs]) { break; } } } if (nFoundIndex == myTable.Rows.Count) { // none found MessageBox.Show(String.Format("There are no substitution rules that apply to this word ({0})!", strWord), cstrCaption); } else if ((nFoundIndex >= 0) && (nFoundIndex < myTable.Rows.Count)) { DataRow row = myTable.Rows[nFoundIndex]; QueryGoodSpelling aQuery = new QueryGoodSpelling(m_font); DialogResult res = aQuery.ShowDialog((string)row[strColumnLhs], (string)row[strColumnRhs], GetComment(row), true); bool bRewrite = false; if (res == DialogResult.Abort) { // this means to delete the bad substitution rule myTable.Rows.RemoveAt(nFoundIndex); bRewrite = true; } // if the user clicks OK and has made a change... if ((res == DialogResult.OK) && (((string)row[strColumnLhs] != aQuery.BadSpelling) || ((string)row[strColumnRhs] != aQuery.GoodSpelling) ) ) { // update the table and rewrite row[strColumnLhs] = aQuery.BadSpelling; row[strColumnRhs] = aQuery.GoodSpelling; row[strColumnCmt] = strWord; bRewrite = true; } if (bRewrite) { // write the newly updated DataTable LoginSF.ReWriteCCTableHeader(m_strConverterSpec, PunctuationAndWhiteSpace, enc); AppendCCTableFromDataTable(m_strConverterSpec, enc, WordBoundaryDelimiter, PunctuationAndWhiteSpace, myTable); } } } } }