Title Back Colour Keyoti Title Line Title Curve
Blue Box Top

How can I override the text parser rules to check words inside brackets? (C#)

https://keyoti.com/kb/Default.aspx?ToDo=view&questId=147&catId=62
Keyoti KB

Search the Knowledge Base Browse by Category Options
Print this page
Email this to a friend

It's possible to override the text parser rules to include your own logic for the way the spell checker splits words.  In this example we want to treat square brackets as part of words.  The reason for doing this is so that when users enter some text such as "we have [jargon]" we can check that "[jargon]" really exists in the dictionary.  The spell checker does not ordinarily include '[' or ']' as part of a word, since under normal conditions a user could enter something like "this is [what] he said" and of course the spell checker would not want to look up "[what]" in the dictionary.  This might be useful if you use square brackets for marking merge fields for example.
 
 
The easiest way to use this example is within the product demo project - however it can also be used directly in your code.
 
1. Add these classes to a file in your project.
 
using System;
 
namespace Main.Support
{
 /// <summary>
 /// Overrides the parser to treat [ ] chars as word chars
 /// </summary>
 public class BracketParser : Keyoti.RapidSpell.FrameWorkSpecific.AdvancedTextBoundary
 {
 
  protected override bool isAtNonBreakingWhiteSpace(int position, bool previousIsIfThisIs, bool nextIsIfThisIs)
  {
   if(this.theText[position]=='[')
   {
    return true;
   } else if ( this.theText[position]==']'){
    return true;
   } else
    return base.isAtNonBreakingWhiteSpace (position, previousIsIfThisIs, nextIsIfThisIs);
  }
 
  public bool IsOffsetWhitespace(int offset)
  {
   return !this.isAtNonWhiteSpace(offset);
  }
 
 }
 
 public class CustomRapidSpellChecker : Keyoti.RapidSpell.RapidSpellChecker
 {
  
  protected override string GetNextWord()
  {
   wordStart = wordEnd;
   if(wordEnd < TextBoundary.Last())
   {
    wordEnd = TextBoundary.Following( wordEnd );
    
    if(wordEnd-wordStart <= 0 || (TextBoundary as BracketParser).IsOffsetWhitespace(wordStart)) 
     //ignore and move on to next word
     return GetNextWord();
    
 
    //return as a word
    return theText.ToString().Substring(wordStart, wordEnd - wordStart);
   }
   else
   {
    return null;//finished
   }
  }
 }
}
2. In the Form where RapidSpellAsYouType is used, set the following properties.
 
 rapidSpellAsYouType1.RapidSpellChecker = new CustomRapidSpellChecker();
 rapidSpellAsYouType1.RapidSpellChecker.TextBoundary = new BracketParser();
 rapidSpellAsYouType1.TextBoundary = new BracketParser();
This can be done in the Form constructor.
 
 
When running the spell checker with this modification you will notice that words such as "[test]" are underlined, including the square brackets.  This is because "[test]" is not in the dictionary.  Therefore add it to the main dictionary using the Dict Manager tool, or to the user dictionary (which is just a text file).
 
 
 
Complete Form code using the custom parser code.
 
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
 
namespace Main.Support
{

 public class BracketForm : System.Windows.Forms.Form
 {
  private Keyoti.RapidSpell.AYTTextBox aytTextBox1;
  private Keyoti.RapidSpell.RapidSpellAsYouType rapidSpellAsYouType1;
  private System.ComponentModel.IContainer components;
 
  public BracketForm()
  {
   InitializeComponent();
 
   
   rapidSpellAsYouType1.RapidSpellChecker = new CustomRapidSpellChecker();
   rapidSpellAsYouType1.RapidSpellChecker.TextBoundary = new BracketParser();
   rapidSpellAsYouType1.TextBoundary = new BracketParser();
 

  }
 
  /// <summary>
  /// Clean up any resources being used.
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if(components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }
 
  #region Windows Form Designer generated code
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {
   this.components = new System.ComponentModel.Container();
   this.aytTextBox1 = new Keyoti.RapidSpell.AYTTextBox();
   this.rapidSpellAsYouType1 = new Keyoti.RapidSpell.RapidSpellAsYouType(this.components);
   this.SuspendLayout();
   //
   // aytTextBox1
   //
   this.aytTextBox1.ContextMenuDefault = null;
   this.aytTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
   this.aytTextBox1.GetFocusOnContextMenu = false;
   this.aytTextBox1.Location = new System.Drawing.Point(0, 0);
   this.aytTextBox1.Multiline = true;
   this.aytTextBox1.Name = "aytTextBox1";
   this.aytTextBox1.ShowCutCopyPasteContextMenu = false;
   this.aytTextBox1.Size = new System.Drawing.Size(292, 271);
   this.aytTextBox1.TabIndex = 0;
   this.aytTextBox1.Text = "[this]";
   this.aytTextBox1.UnderlineYOffset = 0;
   //
   // rapidSpellAsYouType1
   //
   this.rapidSpellAsYouType1.AddMenuText = "Add";
   this.rapidSpellAsYouType1.AllowAnyCase = false;
   this.rapidSpellAsYouType1.AllowMixedCase = false;
   this.rapidSpellAsYouType1.CheckAsYouType = true;
   this.rapidSpellAsYouType1.CheckCompoundWords = false;
   this.rapidSpellAsYouType1.ConsiderationRange = 500;
   this.rapidSpellAsYouType1.DictFilePath = null;
   this.rapidSpellAsYouType1.FindCapitalizedSuggestions = false;
   this.rapidSpellAsYouType1.GUILanguage = Keyoti.RapidSpell.LanguageType.ENGLISH;
   this.rapidSpellAsYouType1.IgnoreAllMenuText = "Ignore All";
   this.rapidSpellAsYouType1.IgnoreCapitalizedWords = false;
   this.rapidSpellAsYouType1.IgnoreURLsAndEmailAddresses = true;
   this.rapidSpellAsYouType1.IgnoreWordsWithDigits = true;
   this.rapidSpellAsYouType1.IgnoreXML = false;
   this.rapidSpellAsYouType1.IncludeUserDictionaryInSuggestions = false;
   this.rapidSpellAsYouType1.LanguageParser = Keyoti.RapidSpell.LanguageType.ENGLISH;
   this.rapidSpellAsYouType1.LookIntoHyphenatedText = true;
   this.rapidSpellAsYouType1.SeparateHyphenWords = false;
   this.rapidSpellAsYouType1.ShowAddMenuOption = true;
   this.rapidSpellAsYouType1.ShowSuggestionsContextMenu = true;
   this.rapidSpellAsYouType1.ShowSuggestionsWhenTextIsSelected = false;
   this.rapidSpellAsYouType1.SuggestionsMethod = Keyoti.RapidSpell.SuggestionsMethodType.HashingSuggestions;
   this.rapidSpellAsYouType1.SuggestSplitWords = true;
   this.rapidSpellAsYouType1.TextComponent = this.aytTextBox1;
   this.rapidSpellAsYouType1.UnderlineColor = System.Drawing.Color.Red;
   this.rapidSpellAsYouType1.UnderlineStyle = Keyoti.RapidSpell.UnderlineStyle.Wavy;
   this.rapidSpellAsYouType1.UpdateAllTextBoxes = true;
   this.rapidSpellAsYouType1.UserDictionaryFile = null;
   this.rapidSpellAsYouType1.V2Parser = true;
   //
   // BracketForm
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   this.ClientSize = new System.Drawing.Size(292, 271);
   this.Controls.Add(this.aytTextBox1);
   this.Name = "BracketForm";
   this.Text = "BracketForm";
   this.ResumeLayout(false);
 
  }
  #endregion
 }
}
 

Related Questions:

Attachments:

No attachments were found.



About | Contact | Site Map | Privacy Policy

Copyright © 2002- Keyoti Inc.