示例#1
0
        protected Dictionary GetDictionary(FileInfo dictionaryXml, string path)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(dictionaryXml.FullName);
            XmlNodeList root = xml.GetElementsByTagName("Dictionary");
            XmlNode node = null;
            if (root.Count > 0)
            {
                node = root.Item(0);
            }

            Dictionary dictionary = null;

            if (null != node)
            {
                DictionaryLoader loader = this.CreateDefaultLoader();
                XmlElement el = node as XmlElement;

                string name = node.Attributes["locale"].Value;
                char[] alphabet = node.Attributes["alphabet"].Value.ToCharArray();
                char[] specialChars = null;
                Dictionary<char, List<char>> accentPairs = null;
                string regex = null;
                if (el.HasAttribute("allowedSpecialChars"))
                {
                    specialChars = node.Attributes["allowedSpecialChars"].Value.ToCharArray();
                }
                if (el.HasAttribute("wordBoundaryRegex"))
                {
                    regex = node.Attributes["wordBoundaryRegex"].Value;
                }
                if (el.HasAttribute("accentPairs"))
                {
                    string[] pairs = node.Attributes["accentPairs"].Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    accentPairs = this.ParsePairs(pairs);
                }
                dictionary = new Dictionary(loader, name, path, alphabet, specialChars, regex, accentPairs);
                foreach (XmlNode file in node.ChildNodes)
                {
                    DictionaryFileType type = DictionaryFileType.Unknown;
                    switch (file.Attributes["type"].Value)
                    {
                        case "LineDictionary":
                            type = DictionaryFileType.LineDictionary;
                            break;

                        case "Dictionary":
                            type = DictionaryFileType.Dictionary;
                            break;

                        case "Affix":
                            type = DictionaryFileType.Affix;
                            break;

                        case "OneCharFrequences":
                            type = DictionaryFileType.OneCharFrequences;
                            break;

                        case "TwoCharFrequences":
                            type = DictionaryFileType.TwoCharFrequences;
                            break;

                        case "DeletionsMatrix":
                            type = DictionaryFileType.DeletetionsMatrix;
                            break;

                        case "InsertionsMatrix":
                            type = DictionaryFileType.InsertionsMatrix;
                            break;

                        case "TranspositionsMatrix":
                            type = DictionaryFileType.TranspositionsMatrix;
                            break;

                        case "SubstitutionsMatrix":
                            type = DictionaryFileType.SubstitutionsMatrix;
                            break;

                        case "UnigramFrequences":
                            type = DictionaryFileType.UnigramFrequences;
                            break;

                        case "DigramFrequences":
                            type = DictionaryFileType.DigramFrequences;
                            break;

                        case "TrigramFrequences":
                            type = DictionaryFileType.TrigramFrequences;
                            break;
                    }

                    dictionary.AddFile(type, file.InnerText.Trim());
                }

            }

            return dictionary;
        }