Beispiel #1
0
        /// <summary>
        /// Loads the thesaurus from a in memory dictionary.
        /// </summary>
        /// <param name="dictionaryBytes">
        /// The dictionary Bytes.
        /// </param>
        public void Load(byte[] dictionaryBytes)
        {
            if (this.synonyms.Count > 0)
            {
                throw new InvalidOperationException("Thesaurus already loaded");
            }

            int currentPos    = 0;
            int currentLength = this.GetLineLength(dictionaryBytes, currentPos);

            string   fileEncoding = Encoding.ASCII.GetString(dictionaryBytes, currentPos, currentLength);
            Encoding enc          = GetEncoding(fileEncoding);

            currentPos += currentLength;

            string word     = string.Empty;
            var    meanings = new List <ThesMeaning>();

            while (currentPos < dictionaryBytes.Length)
            {
                currentPos   += this.GetCrLfLength(dictionaryBytes, currentPos);
                currentLength = this.GetLineLength(dictionaryBytes, currentPos);
                string lineText = enc.GetString(dictionaryBytes, currentPos, currentLength).Trim();

                if (lineText != null && lineText.Length > 0)
                {
                    string[] tokens = lineText.Split('|');
                    if (tokens.Length > 0)
                    {
                        bool wordLine = true;
                        if (tokens[0].StartsWith("-"))
                        {
                            wordLine = false;
                        }

                        if (tokens[0].StartsWith("(") && tokens[0].EndsWith(")"))
                        {
                            wordLine = false;
                        }

                        if (wordLine)
                        {
                            lock (this.dictionaryLock)
                            {
                                if (word.Length > 0 && !this.synonyms.ContainsKey(word.ToLower()))
                                {
                                    this.synonyms.Add(word.ToLower(), meanings.ToArray());
                                }
                            }

                            meanings = new List <ThesMeaning>();
                            word     = tokens[0];
                        }
                        else
                        {
                            var    currentSynonyms = new List <string>();
                            string description     = null;
                            for (int tokIndex = 1; tokIndex < tokens.Length; ++tokIndex)
                            {
                                currentSynonyms.Add(tokens[tokIndex]);
                                if (tokIndex == 1)
                                {
                                    description = tokens[tokIndex];
                                }
                            }

                            var meaning = new ThesMeaning(description, currentSynonyms);
                            meanings.Add(meaning);
                        }
                    }
                }

                currentPos += currentLength;
            }

            lock (this.dictionaryLock)
            {
                if (word.Length > 0 && !this.synonyms.ContainsKey(word.ToLower()))
                {
                    this.synonyms.Add(word.ToLower(), meanings.ToArray());
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Loads the thesaurus from a in memory dictionary.
        /// </summary>
        /// <param name="dictionaryBytes">
        /// The dictionary Bytes. 
        /// </param>
        public void Load(byte[] dictionaryBytes)
        {
            if (this.synonyms.Count > 0)
            {
                throw new InvalidOperationException("Thesaurus already loaded");
            }

            int currentPos = 0;
            int currentLength = GetLineLength(dictionaryBytes, currentPos);

            string fileEncoding = Encoding.ASCII.GetString(dictionaryBytes, currentPos, currentLength);
            Encoding enc = GetEncoding(fileEncoding);
            currentPos += currentLength;

            string word = string.Empty;
            var meanings = new List<ThesMeaning>();

            while (currentPos < dictionaryBytes.Length)
            {
                currentPos += GetCrLfLength(dictionaryBytes, currentPos);
                currentLength = GetLineLength(dictionaryBytes, currentPos);
                string lineText = enc.GetString(dictionaryBytes, currentPos, currentLength).Trim();

                if (lineText != null && lineText.Length > 0)
                {
                    string[] tokens = lineText.Split('|');
                    if (tokens.Length > 0)
                    {
                        bool wordLine = true;
                        if (tokens[0].StartsWith("-"))
                        {
                            wordLine = false;
                        }

                        if (tokens[0].StartsWith("(") && tokens[0].EndsWith(")"))
                        {
                            wordLine = false;
                        }

                        if (wordLine)
                        {
                            lock (this.dictionaryLock)
                            {
                                if (word.Length > 0 && ! this.synonyms.ContainsKey(word.ToLower()))
                                {
                                    this.synonyms.Add(word.ToLower(), meanings.ToArray());
                                }
                            }

                            meanings = new List<ThesMeaning>();
                            word = tokens[0];
                        }
                        else
                        {
                            var currentSynonyms = new List<string>();
                            string description = null;
                            for (int tokIndex = 1; tokIndex < tokens.Length; ++tokIndex)
                            {
                                currentSynonyms.Add(tokens[tokIndex]);
                                if (tokIndex == 1)
                                {
                                    description = tokens[tokIndex];
                                }
                            }

                            var meaning = new ThesMeaning(description, currentSynonyms);
                            meanings.Add(meaning);
                        }
                    }
                }

                currentPos += currentLength;
            }

            lock (this.dictionaryLock)
            {
                if (word.Length > 0 && !this.synonyms.ContainsKey(word.ToLower()))
                {
                    this.synonyms.Add(word.ToLower(), meanings.ToArray());
                }
            }
        }