Exemplo n.º 1
0
        /// <summary>
        ///     Removes the affix suffix rule entry for the word if valid
        /// </summary>
        /// <param name="word" type="string">
        ///     <para>
        ///         The word to be modified
        ///     </para>
        /// </param>
        /// <param name="entry" type="NetSpell.SpellChecker.Dictionary.Affix.AffixEntry">
        ///     <para>
        ///         The affix rule entry to use
        ///     </para>
        /// </param>
        /// <returns>
        ///     The word after affix removed.  Will be the same word if affix could not be removed.
        /// </returns>
        /// <remarks>
        ///     This method does not verify that the returned word is a valid word, only that the affix can be removed
        /// </remarks>
        public static string RemoveSuffix(string word, AffixEntry entry)
        {
            int tempLength = word.Length - entry.AddCharacters.Length;

            if ((tempLength > 0) &&
                (tempLength + entry.StripCharacters.Length >= entry.ConditionCount) &&
                (word.EndsWith(entry.AddCharacters)))
            {
                // word with out affix
                string tempWord = word.Substring(0, tempLength);
                // add back strip chars
                tempWord += entry.StripCharacters;
                // check that this is valid
                int passCount = 0;
                for (int i = 0; i < entry.ConditionCount; i++)
                {
                    int charCode = tempWord[tempWord.Length - (entry.ConditionCount - i)];
                    if ((entry.Condition[charCode] & (1 << i)) == (1 << i))
                    {
                        passCount++;
                    }
                }
                if (passCount == entry.ConditionCount)
                {
                    return(tempWord);
                }
            }
            return(word);
        }
Exemplo n.º 2
0
        void Awake()
        {
            _itemSlotBtnTemplate.gameObject.SetActive(false);
            _affixBtnTemplate.gameObject.SetActive(false);
            _affixBuilder.gameObject.SetActive(false);
            _createAffixButton.gameObject.SetActive(false);

            if (D4Data.Instance == null)
            {
                gameObject.AddComponent <D4Data>();
            }

            _createAffixButton.onClick.AddListener(delegate()
            {
                var affix      = new AffixEntry();
                affix.ItemSlot = _selectedItemSlot;
                AddAffix(affix);
                _affixBuilder.gameObject.SetActive(true);
                _affixBuilder.Render(affix);
            });

            _compileButton.onClick.AddListener(delegate()
            {
                D4Data.Instance.Hellforge.CompileToSingleFile();
                D4Data.Instance.Hellforge.ReloadData();
            });

            _reloadButton.onClick.AddListener(delegate()
            {
                D4Data.Instance.Hellforge.ReloadData();
                Render();
            });
        }
Exemplo n.º 3
0
        public static string RemoveSuffix(string word, AffixEntry entry)
        {
            int tempLength = word.Length - entry.AddCharacters.Length;

            if ((tempLength > 0) &&
                (tempLength + entry.StripCharacters.Length >= entry.ConditionCount) &&
                (word.EndsWith(entry.AddCharacters)))
            {
                string tempWord = word.Substring(0, tempLength);
                tempWord += entry.StripCharacters;
                int passCount = 0;
                for (int i = 0; i < entry.ConditionCount; i++)
                {
                    int charCode = (int)tempWord[tempWord.Length - (entry.ConditionCount - i)];
                    if (charCode < entry.Condition.Length && (entry.Condition[charCode] & (1 << i)) == (1 << i))
                    {
                        passCount++;
                    }
                }
                if (passCount == entry.ConditionCount)
                {
                    return(tempWord);
                }
            }
            return(word);
        }
Exemplo n.º 4
0
 public Affix(Character character, AffixEntry affixData, int tier = 0, int roll = 100)
 {
     Character = character;
     AffixData = affixData;
     Tier      = tier;
     TierCount = affixData.Data.Length;
     Roll      = roll;
 }
Exemplo n.º 5
0
        public void AddAffix(AffixEntry affix)
        {
            var affixList = D4Data.Instance.Hellforge.GameData.Affixes.ToList();

            affixList.Add(affix);
            D4Data.Instance.Hellforge.GameData.Affixes = affixList.ToArray();
            Render();
        }
Exemplo n.º 6
0
        public void RemoveAffix(AffixEntry entry)
        {
            var affixList = D4Data.Instance.Hellforge.GameData.Affixes.ToList();

            affixList.Remove(entry);
            D4Data.Instance.Hellforge.GameData.Affixes = affixList.ToArray();
            Render();
        }
Exemplo n.º 7
0
 public void Inherit(AffixEntry parent)
 {
     Type        = string.IsNullOrEmpty(Type) ? parent.Type : Type;
     Attribute   = string.IsNullOrEmpty(Attribute) ? parent.Attribute : Attribute;
     Slot        = string.IsNullOrEmpty(Slot) ? parent.Slot : Slot;
     Description = string.IsNullOrEmpty(Description) ? parent.Description : Description;
     Invoke      = string.IsNullOrEmpty(Invoke) ? parent.Invoke : Invoke;
     Data        = Data ?? parent.Data;
     Conditions  = Conditions ?? parent.Conditions;
     Activators  = Activators ?? parent.Activators;
 }
Exemplo n.º 8
0
        public void Render(AffixEntry affix)
        {
            _renderedAffix           = affix;
            _identifierInput.text    = affix.Name;
            _inheritsInput.text      = affix.Inherits;
            _typeDropdown.value      = _typeDropdown.options.FindIndex(x => x.text == affix.Type);
            _attributeDropdown.value = _attributeDropdown.options.FindIndex(x => x.text == affix.Attribute);
            _invokeInput.text        = affix.Invoke;
            _itemSlotDropdown.value  = _itemSlotDropdown.options.FindIndex(x => x.text == affix.ItemSlot);
            _affixSlotDropdown.value = _affixSlotDropdown.options.FindIndex(x => x.text == affix.Slot);
            _descriptionInput.text   = affix.Description;
            _conditionsInput.text    = ArrToLsv(affix.Conditions);
            _activatorsInput.text    = ArrToLsv(affix.Activators);
            _tierDataInput.text      = TiersToText(affix.Data);
            _forTalentToggle.isOn    = affix.ForTalent;

            _saveButton.onClick.RemoveAllListeners();
            _deleteButton.onClick.RemoveAllListeners();
            _duplicateButton.onClick.RemoveAllListeners();

            _saveButton.onClick.AddListener(delegate()
            {
                UpdateRenderedAffix();
                _affixEditor.Render();
            });

            _deleteButton.onClick.AddListener(delegate()
            {
                _affixEditor.RemoveAffix(affix);
                _affixEditor.Render();
                _renderedAffix = null;
                gameObject.SetActive(false);
            });

            _duplicateButton.onClick.AddListener(delegate()
            {
                var duplicate   = InputToAffixEntry();
                duplicate.Name += " (DUPLICATE)";
                _affixEditor.AddAffix(duplicate);
                Render(duplicate);
            });
        }
Exemplo n.º 9
0
        /// <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 = "";

            //******Below Lines Added*********

            if (_dictionaryFile.StartsWith("."))
            {
                _dictionaryFile = Thread.CurrentThread.CurrentCulture.Name;
            }
            // add the default file extension
            if (!_dictionaryFile.EndsWith(".dic"))
            {
                _dictionaryFile += ".dic";
            }
            //******Above Lines Added*********

            // 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;

            // Simplify our lives by assuming the default path
            if (_dictionaryFolder == null)
            {
                _dictionaryFolder = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "Dictionary");
            }
            string dictionaryPath = Path.Combine(_dictionaryFolder, _dictionaryFile);

            System.Diagnostics.Debug.WriteLine("Loading Dictionary:{0}", dictionaryPath);

            // open dictionary file
            if (!File.Exists(dictionaryPath))
            {
                throw new FileNotFoundException("Cannot find dictonary with location " + dictionaryPath);
            }

            //BinaryReader reader = new BinaryReader(File.Open("foo.txt", FileMode.Open),
            //                           Encoding.GetEncoding(1252));

            FileStream   fs = new FileStream(dictionaryPath, FileMode.Open, FileAccess.Read, FileShare.Read);
            StreamReader sr = new StreamReader(fs, true);

            // 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
                                    if (partMatches[2].Value != "0")
                                    {
                                        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();

            //System.Diagnostics.Debug.WriteLine("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>
        ///     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 = null;

            try
            {
                fs = new FileStream(dictionaryPath, FileMode.Open, FileAccess.Read, FileShare.Read);
                using (var sr = new StreamReader(fs, Encoding.UTF8))
                {
                    fs = null;

                    // read line by line
                    while (sr.Peek() >= 0)
                    {
                        string tempLine = sr.ReadLine().Trim();
                        if (tempLine.Length <= 0)
                        {
                            continue;
                        }
                        // check for section flag
                        if (tempLine.StartsWith("[") && tempLine.EndsWith("]"))
                        {
                            // set current section that is being parsed
                            currentSection = tempLine;
                            continue;
                        }

                        // parse line and place in correct object
                        switch (currentSection)
                        {
                        case "[Copyright]":
                            Copyright += tempLine + "\r\n";
                            break;

                        case "[Try]":     // ISpell try chars
                            TryCharacters += tempLine;
                            break;

                        case "[Replace]":     // ISpell replace chars
                            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
                                    PrefixRules.Add(currentRule.Name, currentRule);
                                }
                                else
                                {
                                    // add to suffix collection
                                    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];
                            }

                            BaseWords.Add(tempWord.Text, tempWord);
                            break;
                        } // currentSection switch
                    }     // read line
                      // close files
                }
            }
            finally
            {
                if (fs != null)
                {
                    fs.Dispose();
                }
            }

            TraceWriter.TraceInfo("Dictionary Loaded BaseWords:{0}; PrefixRules:{1}; SuffixRules:{2}; PhoneticRules:{3}",
                                  BaseWords.Count, PrefixRules.Count, SuffixRules.Count, PhoneticRules.Count);

            LoadUserFile();

            _initialized = true;
        }
Exemplo n.º 11
0
 public LogicNode(Affix affix, Func <Lua> getLuaContext, AffixEntry affixData)
     : base(affix, getLuaContext)
 {
     _data = affixData;
 }
Exemplo n.º 12
0
        /// <summary>
        ///     Generates the condition character array
        /// </summary>
        /// <param name="conditionText" type="string">
        ///     <para>
        ///         the text form of the conditions
        ///     </para>
        /// </param>
        /// <param name="entry" type="NetSpell.SpellChecker.Dictionary.Affix.AffixEntry">
        ///     <para>
        ///         The AffixEntry to add the condition array to
        ///     </para>
        /// </param>
        public static void EncodeConditions(string conditionText, AffixEntry entry)
        {
            // clear the conditions array
            for (int i = 0; i < entry.Condition.Length; i++)
            {
                entry.Condition[i] = 0;
            }

            // if no condition just return
            if (conditionText == ".")
            {
                entry.ConditionCount = 0;
                return;
            }

            bool neg   = false; /* complement indicator */
            bool group = false; /* group indicator */
            bool end   = false; /* end condition indicator */
            int  num   = 0;     /* number of conditions */

            char[] memberChars = new char[200];
            int    numMember   = 0; /* number of member in group */

            foreach (char cond in conditionText)
            {
                // parse member group
                if (cond == '[')
                {
                    group = true;  // start a group
                }
                else if (cond == '^' && group)
                {
                    neg = true; // negative group
                }
                else if (cond == ']')
                {
                    end = true; // end of a group
                }
                else if (group)
                {
                    // add chars to group
                    memberChars[numMember] = cond;
                    numMember++;
                }
                else
                {
                    end = true;  // no group
                }

                // set condition
                if (end)
                {
                    if (group)
                    {
                        if (neg)
                        {
                            // turn all chars on
                            for (int j = 0; j < entry.Condition.Length; j++)
                            {
                                entry.Condition[j] = entry.Condition[j] | (1 << num);
                            }
                            // turn off chars in member group
                            for (int j = 0; j < numMember; j++)
                            {
                                int charCode = memberChars[j];
                                entry.Condition[charCode] = entry.Condition[charCode] & ~(1 << num);
                            }
                        }
                        else
                        {
                            // turn on chars in member group
                            for (int j = 0; j < numMember; j++)
                            {
                                int charCode = memberChars[j];
                                entry.Condition[charCode] = entry.Condition[charCode] | (1 << num);
                            }
                        }
                        group     = false;
                        neg       = false;
                        numMember = 0;
                    } // if group
                    else
                    {
                        if (cond == '.')
                        {
                            // wild card character, turn all chars on
                            for (int j = 0; j < entry.Condition.Length; j++)
                            {
                                entry.Condition[j] = entry.Condition[j] | (1 << num);
                            }
                        }
                        else
                        {
                            // turn on char
                            int charCode = cond;
                            entry.Condition[charCode] = entry.Condition[charCode] | (1 << num);
                        }
                    } // not group

                    end = false;
                    num++;
                } // if end
            }     // foreach char

            entry.ConditionCount = num;
            return;
        }
Exemplo n.º 13
0
        public static void EncodeConditions(string conditionText, AffixEntry entry)
        {
            for (int i = 0; i < entry.Condition.Length; i++)
            {
                entry.Condition[i] = 0;
            }

            if (conditionText == ".")
            {
                entry.ConditionCount = 0;
                return;
            }

            bool neg   = false;
            bool group = false;
            bool end   = false;
            int  num   = 0;

            char[] memberChars = new char[200];
            int    numMember   = 0;

            foreach (char cond in conditionText)
            {
                if (cond == '[')
                {
                    group = true;
                }
                else if (cond == '^' && group)
                {
                    neg = true;
                }
                else if (cond == ']')
                {
                    end = true;
                }
                else if (group)
                {
                    memberChars[numMember] = cond;
                    numMember++;
                }
                else
                {
                    end = true;
                }

                if (end)
                {
                    if (group)
                    {
                        if (neg)
                        {
                            for (int j = 0; j < entry.Condition.Length; j++)
                            {
                                entry.Condition[j] = entry.Condition[j] | (1 << num);
                            }
                            for (int j = 0; j < numMember; j++)
                            {
                                int charCode = (int)memberChars[j];
                                entry.Condition[charCode] = entry.Condition[charCode] & ~(1 << num);
                            }
                        }
                        else
                        {
                            for (int j = 0; j < numMember; j++)
                            {
                                int charCode = (int)memberChars[j];
                                entry.Condition[charCode] = entry.Condition[charCode] | (1 << num);
                            }
                        }
                        group     = false;
                        neg       = false;
                        numMember = 0;
                    }
                    else
                    {
                        if (cond == '.')
                        {
                            for (int j = 0; j < entry.Condition.Length; j++)
                            {
                                entry.Condition[j] = entry.Condition[j] | (1 << num);
                            }
                        }
                        else
                        {
                            int charCode = (int)cond;
                            entry.Condition[charCode] = entry.Condition[charCode] | (1 << num);
                        }
                    }

                    end = false;
                    num++;
                }
            }

            entry.ConditionCount = num;
            return;
        }
Exemplo n.º 14
0
 /// <summary>
 ///     Removes the affix suffix rule entry for the word if valid
 /// </summary>
 /// <param name="word" type="string">
 ///     <para>
 ///         The word to be modified
 ///     </para>
 /// </param>
 /// <param name="entry" type="NetSpell.SpellChecker.Dictionary.Affix.AffixEntry">
 ///     <para>
 ///         The affix rule entry to use
 ///     </para>
 /// </param>
 /// <returns>
 ///     The word after affix removed.  Will be the same word if affix could not be removed.
 /// </returns>
 /// <remarks>
 ///		This method does not verify that the returned word is a valid word, only that the affix can be removed
 /// </remarks>
 public static string RemoveSuffix(string word, AffixEntry entry)
 {
     int tempLength = word.Length - entry.AddCharacters.Length;
     if ((tempLength > 0)
         &&  (tempLength + entry.StripCharacters.Length >= entry.ConditionCount)
         && (word.EndsWith(entry.AddCharacters)))
     {
         // word with out affix
         string tempWord = word.Substring(0, tempLength);
         // add back strip chars
         tempWord += entry.StripCharacters;
         // check that this is valid
         int passCount = 0;
         for (int i = 0;  i < entry.ConditionCount; i++)
         {
             int charCode = (int)tempWord[tempWord.Length - (entry.ConditionCount - i)];
             if ((entry.Condition[charCode] & (1 << i)) == (1 << i))
             {
                 passCount++;
             }
         }
         if (passCount == entry.ConditionCount)
         {
             return tempWord;
         }
     }
     return word;
 }
Exemplo n.º 15
0
        /// <summary>
        ///     Generates the condition character array
        /// </summary>
        /// <param name="conditionText" type="string">
        ///     <para>
        ///         the text form of the conditions
        ///     </para>
        /// </param>
        /// <param name="entry" type="NetSpell.SpellChecker.Dictionary.Affix.AffixEntry">
        ///     <para>
        ///         The AffixEntry to add the condition array to
        ///     </para>
        /// </param>
        public static void EncodeConditions(string conditionText, AffixEntry entry)
        {
            // clear the conditions array
            for (int i=0; i < entry.Condition.Length; i++)
            {
                entry.Condition[i] = 0;
            }

            // if no condition just return
            if (conditionText == ".")
            {
                entry.ConditionCount = 0;
                return;
            }

            bool neg = false;  /* complement indicator */
            bool group = false;  /* group indicator */
            bool end = false;   /* end condition indicator */
            int num = 0;    /* number of conditions */

            char [] memberChars = new char[200];
            int numMember = 0;   /* number of member in group */

            foreach (char cond in conditionText)
            {
                // parse member group
                if (cond == '[')
                {
                    group = true;  // start a group
                }
                else if (cond == '^' && group)
                {
                    neg = true; // negative group
                }
                else if (cond == ']')
                {
                    end = true; // end of a group
                }
                else if (group)
                {
                    // add chars to group
                    memberChars[numMember] = cond;
                    numMember++;
                }
                else
                {
                    end = true;  // no group
                }

                // set condition
                if (end)
                {
                    if (group)
                    {
                        if (neg)
                        {
                            // turn all chars on
                            for (int j=0; j < entry.Condition.Length; j++)
                            {
                                entry.Condition[j] = entry.Condition[j] | (1 << num);
                            }
                            // turn off chars in member group
                            for (int j=0; j < numMember; j++)
                            {
                                int charCode = (int)memberChars[j];
                                entry.Condition[charCode] = entry.Condition[charCode] & ~(1 << num);
                            }
                        }
                        else
                        {
                            // turn on chars in member group
                            for (int j=0; j < numMember; j++)
                            {
                                int charCode = (int)memberChars[j];
                                entry.Condition[charCode] = entry.Condition[charCode] | (1 << num);
                            }
                        }
                        group = false;
                        neg = false;
                        numMember = 0;
                    } // if group
                    else
                    {
                        if (cond == '.')
                        {
                            // wild card character, turn all chars on
                            for (int j=0; j < entry.Condition.Length; j++)
                            {
                                entry.Condition[j] = entry.Condition[j] | (1 << num);
                            }
                        }
                        else
                        {
                            // turn on char
                            int charCode = (int)cond;
                            entry.Condition[charCode] = entry.Condition[charCode] | (1 << num);
                        }
                    } // not group

                    end = false;
                    num++;
                } // if end
            } // foreach char

            entry.ConditionCount = num;
            return;
        }
Exemplo n.º 16
0
        /// <summary>
        /// Initializes the dictionary by loading and parsing the
        /// dictionary file and the user file.
        /// </summary>
        public WordDictionary(TextReader inputDictionary)
        {
            for (byte i = 0; i < 128; i++)
            {
                _encodeTable[i] = i;
                _decodeTable[i] = (char)i;
            }
            MatchCollection partMatches;

            string    currentSection = "";
            AffixRule currentRule    = null;

            while (inputDictionary.Peek() >= 0)
            {
                string tempLine = encode(inputDictionary.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
                            _tryCharacters += tempLine;
                            break;

                        case "[Replace]":         // ISpell replace chars
                            _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
                                    _prefixRules.Add(currentRule.Name, currentRule);
                                }
                                else
                                {
                                    // add to suffix collection
                                    _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];
                            }

                            _baseWords[tempWord.Text] = tempWord;
                            break;
                        }
                        break;
                    }
                }
            }
        }
Exemplo n.º 17
0
        /// <summary>
        /// Searches all contained word lists for word.
        /// </summary>
        /// <param name="word" type="string">
        /// The word to search for.
        /// </param>
        /// <returns>
        /// Returns true if word is found.
        /// </returns>
        public ContainsResult Contains(string word)
        {
            word = encode(word);
            // clean up possible base word list
            List <string> possibleBaseWords = new List <string>();

            // Step 2 Search BaseWords
            if (_baseWords.ContainsKey(word))
            {
                return(new ContainsResult(true, null));
            }

            // Step 3 Remove suffix, Search BaseWords

            // save suffixed words for use when removing prefix
            List <string> suffixWords = new List <string>();

            // Add word to suffix word list
            suffixWords.Add(word);

            foreach (AffixRule rule in _suffixRules.Values)
            {
                AffixEntry[] entries    = rule.AffixEntries.ToArray();
                int          entryCount = entries.Length;
                for (int i = 0; i < entryCount; i++)
                {
                    AffixEntry entry    = entries[i];
                    string     tempWord = AffixUtility.RemoveSuffix(word, entry);
                    if (tempWord != null)
                    {
                        if (_baseWords.ContainsKey(tempWord))
                        {
                            if (verifyAffixKey(tempWord, rule.Name[0]))
                            {
                                return(new ContainsResult(true, null));
                            }
                        }

                        if (rule.AllowCombine)
                        {
                            // saving word to check if it is a word after prefix is removed
                            suffixWords.Add(tempWord);
                        }
                        else
                        {
                            // saving possible base words for use in generating suggestions
                            possibleBaseWords.Add(tempWord);
                        }
                    }
                }
            }

            string[] suffixWordsArr = suffixWords.ToArray();
            // saving possible base words for use in generating suggestions
            possibleBaseWords.AddRange(suffixWords);

            // Step 4 Remove Prefix, Search BaseWords
            foreach (AffixRule rule in _prefixRules.Values)
            {
                AffixEntry[] entries    = rule.AffixEntries.ToArray();
                int          entryCount = entries.Length;
                for (int i = 0; i < entryCount; i++)
                {
                    AffixEntry entry            = entries[i];
                    int        suffixWordsCount = suffixWordsArr.Length;
                    for (int j = 0; j < suffixWordsCount; j++)
                    {
                        string tempWord = AffixUtility.RemovePrefix(suffixWordsArr[j], entry);
                        if (tempWord != null)
                        {
                            if (_baseWords.ContainsKey(tempWord))
                            {
                                if (verifyAffixKey(tempWord, rule.Name[0]))
                                {
                                    return(new ContainsResult(true, null));
                                }
                            }

                            possibleBaseWords.Add(tempWord);
                        }
                    }
                }
            }

            return(new ContainsResult(false, possibleBaseWords.ToArray()));
        }