예제 #1
0
        /// <summary>
        /// Recognizes named entities occurring in content text that recorded in the dictionary.
        /// </summary>
        /// <param name="content">The content string to recognize the named entities.</param>
        /// <returns>A set of <see cref="NamedEntityInfo&lt;T&gt;"/> represents named entities that is recognized from the content text.</returns>
        public IList <NamedEntityInfo <T> > Recognize(string content)
        {
            content = content.ToLower();

            IList <string> tokens = NamedEntityDictionary <T> .Tokenize(content);

            IList <NamedEntityInfo <T> > result = new List <NamedEntityInfo <T> >();
            int contentIndex = 0;

            while (tokens.Count > 0)
            {
                PrefixMatch <string, T> match = this.prefixTree.LongestPrefixMatch(tokens);
                if (match.Length == 0)
                {
                    contentIndex += tokens[0].Length;
                    tokens.RemoveAt(0);
                    continue;
                }

                int termLength = 0;
                for (int index = 0; index < match.Length; index++)
                {
                    termLength += tokens[0].Length;
                    tokens.RemoveAt(0);
                }

                NamedEntityInfo <T> entity =
                    new NamedEntityInfo <T>(match.Value, contentIndex, termLength);
                result.Add(entity);
                contentIndex += termLength;
            }

            return(result);
        }
예제 #2
0
        /// <summary>
        /// Removes the value with the specified key from the dictionary.
        /// </summary>
        /// <param name="key">The key of the element to remove.</param>
        /// <returns>True if the element is successfully found and removed; otherwise, false. This method returns false if key is not found in the dictionary.</returns>
        public override bool Remove(string key)
        {
            key = key.ToLower();
            bool dictResult = this.dictionary.Remove(key);
            bool treeResult = this.prefixTree.Remove(NamedEntityDictionary <T> .Tokenize(key));

            Debug.Assert(dictResult == treeResult);
            return(treeResult);
        }
예제 #3
0
 /// <summary>
 /// Gets or sets the value associated with the specified key.
 /// </summary>
 /// <param name="key">The key of the value to get or set.</param>
 public override T this[string key]
 {
     get { return(this.dictionary[key.ToLower()]); }
     set
     {
         key = key.ToLower();
         this.prefixTree.TrySetValue(NamedEntityDictionary <T> .Tokenize(key), value);
         this.dictionary[key] = value;
     }
 }
예제 #4
0
 /// <summary>
 /// Adds the specified key and value to the dictionary.
 /// </summary>
 /// <param name="key">The key of the element to add.</param>
 /// <param name="value">The value of the element to add. The value can be null for reference types.</param>
 public override void Add(string key, T value)
 {
     key = key.ToLower();
     this.prefixTree.SetValue(NamedEntityDictionary <T> .Tokenize(key), value);
     this.dictionary.Add(key, value);
 }