/// ------------------------------------------------------------------------------------ /// <summary> /// Initializes a new instance of the <see cref="FwCharacterCategorizer"/> class. /// </summary> /// <param name="validChars">The valid characters. If null, will fall back on the /// specified character property engine.</param> /// <param name="charPropEngine">The character property engine.</param> /// ------------------------------------------------------------------------------------ public FwCharacterCategorizer(ValidCharacters validChars, ILgCharacterPropertyEngine charPropEngine) { if (charPropEngine == null) throw new ArgumentNullException("charPropEngine"); m_validChars = validChars; m_charPropEngine = charPropEngine; }
/// ------------------------------------------------------------------------------------ /// <summary> /// Verifies the default word forming characters. /// </summary> /// <param name="validChars">The valid chars.</param> /// ------------------------------------------------------------------------------------ private static void VerifyDefaultWordFormingCharacters(ValidCharacters validChars) { string[] expectedWordFormingChars = (string[])ReflectionHelper.GetField( typeof(ValidCharacters), "s_defaultWordformingChars"); ValidCharsWrapper validCharsW = new ValidCharsWrapper(validChars); Assert.AreEqual(expectedWordFormingChars, validCharsW.WordFormingCharacters.ToArray(), "We expect the load method to have a fallback to the default word-forming characters"); Assert.AreEqual(0, validCharsW.NumericCharacters.Count); Assert.AreEqual(0, validCharsW.OtherCharacters.Count); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Verifies the sort order of characters added to the specified valid characters /// object. /// </summary> /// ------------------------------------------------------------------------------------ private void VerifySortOrder(ValidCharacters validChars) { ValidCharsWrapper validCharsW = new ValidCharsWrapper(validChars); Assert.AreEqual("b", validCharsW.WordFormingCharacters[0]); Assert.AreEqual("c", validCharsW.WordFormingCharacters[1]); Assert.AreEqual("t", validCharsW.WordFormingCharacters[2]); Assert.AreEqual("z", validCharsW.WordFormingCharacters[3]); validChars.AddCharacter("8"); validChars.AddCharacter("7"); validChars.AddCharacter("6"); validChars.AddCharacter("5"); Assert.AreEqual("5", validCharsW.NumericCharacters[0]); Assert.AreEqual("6", validCharsW.NumericCharacters[1]); Assert.AreEqual("7", validCharsW.NumericCharacters[2]); Assert.AreEqual("8", validCharsW.NumericCharacters[3]); }
/// -------------------------------------------------------------------------------- /// <summary> /// Initializes a new instance of the <see cref="ValidCharsWrapper"/> class. /// </summary> /// <param name="validCharacters">An instance of the valid characters class.</param> /// -------------------------------------------------------------------------------- public ValidCharsWrapper(ValidCharacters validCharacters) { m_validChars = validCharacters; }
/// --------------------------------------------------------------------------------- /// <summary> /// Initializes the valid characters explorer bar with three valid character grids. /// </summary> /// --------------------------------------------------------------------------------- internal void Init(CharacterGrid gridWf, CharacterGrid gridOther, CharacterGrid gridNum, IWritingSystem ws, IApp app) { m_ws = ws; m_app = app; gridWf.Font = new Font(ws.DefaultFontName, gridWf.Font.Size); gridNum.Font = new Font(ws.DefaultFontName, gridNum.Font.Size); gridOther.Font = new Font(ws.DefaultFontName, gridOther.Font.Size); gridWf.BackgroundColor = SystemColors.Window; gridNum.BackgroundColor = SystemColors.Window; gridOther.BackgroundColor = SystemColors.Window; gridWf.MultiSelect = true; gridNum.MultiSelect = true; gridOther.MultiSelect = true; gridWf.CellPainting += HandleGridCellPainting; gridNum.CellPainting += HandleGridCellPainting; gridOther.CellPainting += HandleGridCellPainting; gridWf.Enter += HandleGridEnter; gridNum.Enter += HandleGridEnter; gridOther.Enter += HandleGridEnter; gridWf.CellFormatting += HandleCellFormatting; gridNum.CellFormatting += HandleCellFormatting; gridOther.CellFormatting += HandleCellFormatting; gridWf.CellMouseClick += HandleCharGridCellMouseClick; gridNum.CellMouseClick += HandleCharGridCellMouseClick; gridOther.CellMouseClick += HandleCharGridCellMouseClick; gridWf.SelectionChanged += HandleCharGridSelectionChanged; gridOther.SelectionChanged += HandleCharGridSelectionChanged; m_gridWordForming = gridWf; m_gridNumbers = gridNum; m_gridOther = gridOther; m_validChars = ValidCharacters.Load(ws, LoadException, FwDirectoryFinder.LegacyWordformingCharOverridesFile); RefreshCharacterGrids(ValidCharacterType.All); }
/// <summary> /// Insert breaks for the text in the indicated range of the indicate StTxtPara /// </summary> /// <param name="start"></param> /// <param name="limit"></param> /// <param name="hvoPara"></param> public void Guess(int start, int limit, int hvoPara) { Setup(hvoPara); ITsString tss = m_sda.get_StringProp(hvoPara, StTxtParaTags.kflidContents); ITsStrBldr bldr = tss.GetBldr(); //Guessing wordbreaks in a string that isn't there is not a good idea. if (tss.Text == null) { return; } string txt = tss.Text.Substring(start, limit > start && limit < tss.Length ? limit - start : tss.Length - start); int offset = 0; // offset in tsb caused by previously introduced spaces. //Attempt to handle the problem of invalid words being introduced accounting for punctuation //replace every kind of punctuation character in the writing system with ., then split on . // //just grab the system from the first run, seems unlikely you'll be guessing wordbreaks on strings with runs in different writing systems var wsID = tss.get_WritingSystem(0); //get the writing system from the cache IWritingSystem ws = (IWritingSystem)m_cache.WritingSystemFactory.get_EngineOrNull(wsID); //get the ValidCharacters for the writing system. ValidCharacters vc = ws != null?ValidCharacters.Load(ws, (ArgumentException e) => { }) : null; //split the text on everything found in the OtherCharacters section string[] distinctPhrases = vc != null?txt.Split(vc.OtherCharacters.ToArray(), StringSplitOptions.None) //ws info was good, use it : Regex.Replace(txt, "\\p{P}", ".").Split('.'); //bad ws info, replace all punct with . and split on . Set <WordLoc> allWords = new Set <WordLoc>(); int adjustment = 0; foreach (var distinctPhrase in distinctPhrases) { if (distinctPhrase.Length > 0) //split will give us an empty string wherever there was a punctuation { Set <WordLoc> foundWords = FindAllMatches(0, distinctPhrase.Length, distinctPhrase); foreach (var foundWord in foundWords) { foundWord.Start += adjustment; } allWords.AddRange(foundWords); adjustment += distinctPhrase.Length; } ++adjustment; //rather than just adding 1 to the adjustment above adjust here. This will handle oddities like ,, or ". } List <WordLoc> bestWords = BestMatches(txt, allWords); foreach (var word in bestWords) //for each word in our list of the best result { //unless the word starts at the beginning of the input or the word starts right after the end of the last word we found //insert a space before the word. if (word.Start != 0) { //insert a space before the word. if (!SpaceAt(bldr, start + word.Start + offset - 1)) //if there isn't already a space { InsertSpace(bldr, start + word.Start + offset); ++offset; } } if (word.Start + word.Length != txt.Length) //unless the word ends at the end of our input { if (!SpaceAt(bldr, start + word.Start + word.Length + offset)) //if there isn't already a space { InsertSpace(bldr, start + word.Start + word.Length + offset); //insert a space at the end of the word ++offset; } } } if (offset > 0) { UndoableUnitOfWorkHelper.Do(ITextStrings.ksUndoGuessWordBreaks, ITextStrings.ksRedoGuessWordBreaks, m_cache.ActionHandlerAccessor, () => m_sda.SetString(hvoPara, StTxtParaTags.kflidContents, bldr.GetString())); } }
private bool IsValid(char c) { return(ValidCharacters.Contains(c)); }