/// <summary> /// Adds a suffix to a word /// </summary> /// <param name="word" type="string"> /// <para> /// The word to get the suffix added to /// </para> /// </param> /// <param name="rule" type="NetSpell.SpellChecker.Dictionary.Affix.AffixRule"> /// <para> /// The AffixRule to use when adding the suffix /// </para> /// </param> /// <returns> /// The word with the suffix added /// </returns> public static string AddSuffix(string word, AffixRule rule) { foreach (AffixEntry entry in rule.AffixEntries) { // check that this entry is valid if (word.Length >= entry.ConditionCount) { int passCount = 0; for (int i = 0; i < entry.ConditionCount; i++) { int charCode = (int)word[word.Length - (entry.ConditionCount - i)]; if ((entry.Condition[charCode] & (1 << i)) == (1 << i)) { passCount++; } else { break; } } if (passCount == entry.ConditionCount) { int tempLen = word.Length - entry.StripCharacters.Length; string tempWord = word.Substring(0, tempLen); tempWord += entry.AddCharacters; return tempWord; } } } return word; }
/// <summary> /// Adds a suffix to a word /// </summary> /// <param name="word" type="string"> /// <para> /// The word to get the suffix added to /// </para> /// </param> /// <param name="rule" type="NetSpell.SpellChecker.Dictionary.Affix.AffixRule"> /// <para> /// The AffixRule to use when adding the suffix /// </para> /// </param> /// <returns> /// The word with the suffix added /// </returns> public static string AddSuffix(string word, AffixRule rule) { foreach (AffixEntry entry in rule.AffixEntries) { // check that this entry is valid if (word.Length >= entry.ConditionCount) { int passCount = 0; for (int i = 0; i < entry.ConditionCount; i++) { int charCode = (int)word[word.Length - (entry.ConditionCount - i)]; if ((entry.Condition[charCode] & (1 << i)) == (1 << i)) { passCount++; } else { break; } } if (passCount == entry.ConditionCount) { int tempLen = word.Length - entry.StripCharacters.Length; string tempWord = word.Substring(0, tempLen); tempWord += entry.AddCharacters; return(tempWord); } } } return(word); }
/// <summary> /// Initializes the dictionary by loading and parsing the /// dictionary file and the user file. /// </summary> public void Initialize() { // clean up data first _baseWords.Clear(); _replaceCharacters.Clear(); _prefixRules.Clear(); _suffixRules.Clear(); _phoneticRules.Clear(); _tryCharacters = ""; // the following is used to split a line by white space Regex _spaceRegx = new Regex(@"[^\s]+", RegexOptions.Compiled); MatchCollection partMatches; string currentSection = ""; AffixRule currentRule = null; string dictionaryPath = Path.Combine(_dictionaryFolder, _dictionaryFile); TraceWriter.TraceInfo("Loading Dictionary:{0}", dictionaryPath); // open dictionary file FileStream fs = new FileStream(dictionaryPath, FileMode.Open, FileAccess.Read, FileShare.Read); StreamReader sr = new StreamReader(fs, Encoding.UTF8); // read line by line while (sr.Peek() >= 0) { string tempLine = sr.ReadLine().Trim(); if (tempLine.Length > 0) { // check for section flag switch (tempLine) { case "[Copyright]" : case "[Try]" : case "[Replace]" : case "[Prefix]" : case "[Suffix]" : case "[Phonetic]" : case "[Words]" : // set current section that is being parsed currentSection = tempLine; break; default : // parse line and place in correct object switch (currentSection) { case "[Copyright]" : this.Copyright += tempLine + "\r\n"; break; case "[Try]" : // ISpell try chars this.TryCharacters += tempLine; break; case "[Replace]" : // ISpell replace chars this.ReplaceCharacters.Add(tempLine); break; case "[Prefix]" : // MySpell prefix rules case "[Suffix]" : // MySpell suffix rules // split line by white space partMatches = _spaceRegx.Matches(tempLine); // if 3 parts, then new rule if (partMatches.Count == 3) { currentRule = new AffixRule(); // part 1 = affix key currentRule.Name = partMatches[0].Value; // part 2 = combine flag if (partMatches[1].Value == "Y") currentRule.AllowCombine = true; // part 3 = entry count, not used if (currentSection == "[Prefix]") { // add to prefix collection this.PrefixRules.Add(currentRule.Name, currentRule); } else { // add to suffix collection this.SuffixRules.Add(currentRule.Name, currentRule); } } //if 4 parts, then entry for current rule else if (partMatches.Count == 4) { // part 1 = affix key if (currentRule.Name == partMatches[0].Value) { AffixEntry entry = new AffixEntry(); // part 2 = strip char if (partMatches[1].Value != "0") entry.StripCharacters = partMatches[1].Value; // part 3 = add chars entry.AddCharacters = partMatches[2].Value; // part 4 = conditions AffixUtility.EncodeConditions(partMatches[3].Value, entry); currentRule.AffixEntries.Add(entry); } } break; case "[Phonetic]" : // ASpell phonetic rules // split line by white space partMatches = _spaceRegx.Matches(tempLine); if (partMatches.Count >= 2) { PhoneticRule rule = new PhoneticRule(); PhoneticUtility.EncodeRule(partMatches[0].Value, ref rule); rule.ReplaceString = partMatches[1].Value; _phoneticRules.Add(rule); } break; case "[Words]" : // dictionary word list // splits word into its parts string[] parts = tempLine.Split('/'); Word tempWord = new Word(); // part 1 = base word tempWord.Text = parts[0]; // part 2 = affix keys if (parts.Length >= 2) tempWord.AffixKeys = parts[1]; // part 3 = phonetic code if (parts.Length >= 3) tempWord.PhoneticCode = parts[2]; this.BaseWords.Add(tempWord.Text, tempWord); break; } // currentSection swith break; } //tempLine switch } // if templine } // read line // close files sr.Close(); fs.Close(); TraceWriter.TraceInfo("Dictionary Loaded BaseWords:{0}; PrefixRules:{1}; SuffixRules:{2}; PhoneticRules:{3}", this.BaseWords.Count, this.PrefixRules.Count, this.SuffixRules.Count, this.PhoneticRules.Count); this.LoadUserFile(); _initialized = true; }
/// <summary> /// Determines whether the AffixRuleCollection contains a specific value. /// </summary> /// <param name="value" type="AffixRule"> /// <para> /// The value to locate in the AffixRuleCollection. The value can be a null reference (Nothing in Visual Basic). /// </para> /// </param> /// <returns> /// true if the AffixRuleCollection contains an element with the specified value; otherwise, false. /// </returns> public bool ContainsValue(AffixRule value) { return innerHash.ContainsValue(value); }
/// <summary> /// adds an element with the provided key and value to the AffixRuleCollection. /// </summary> /// <param name="key" type="string"> /// <para> /// The string Object to use as the key of the element to add. /// </para> /// </param> /// <param name="value" type="AffixRule"> /// <para> /// The AffixRule Object to use as the value of the element to add. /// </para> /// </param> public void Add(string key, AffixRule value) { innerHash.Add (key, value); }
/// <summary> /// Determines whether the AffixRuleCollection contains a specific value. /// </summary> /// <param name="value" type="AffixRule"> /// <para> /// The value to locate in the AffixRuleCollection. The value can be a null reference (Nothing in Visual Basic). /// </para> /// </param> /// <returns> /// true if the AffixRuleCollection contains an element with the specified value; otherwise, false. /// </returns> public bool ContainsValue(AffixRule value) { return(innerHash.ContainsValue(value)); }
/// <summary> /// adds an element with the provided key and value to the AffixRuleCollection. /// </summary> /// <param name="key" type="string"> /// <para> /// The string Object to use as the key of the element to add. /// </para> /// </param> /// <param name="value" type="AffixRule"> /// <para> /// The AffixRule Object to use as the value of the element to add. /// </para> /// </param> public void Add(string key, AffixRule value) { innerHash.Add(key, value); }