Example #1
0
 /// <summary>
 /// 添加节点
 /// </summary>
 /// <param name="node">过度的几点</param>
 public void AddTransition(KeyWordTreeNode node)
 {
     _transHash.Add(node.Char, node);
     KeyWordTreeNode[] ar = new KeyWordTreeNode[_transHash.Values.Count];
     _transHash.Values.CopyTo(ar, 0);
     _transitionsAr = ar;
 }
Example #2
0
        /// <summary>
        /// 是否包含
        /// </summary>
        /// <param name="text">要搜索的文本</param>
        /// <returns>是否包含关键词</returns>
        public bool ContainsAny(string text)
        {
            KeyWordTreeNode ptr = _root;

            for (int i = 0; i < text.Length; i++)
            {
                KeyWordTreeNode trans = null;
                while (trans == null)
                {
                    trans = ptr.GetTransition(text[i]);
                    if (ptr == _root)
                    {
                        break;
                    }
                    if (trans == null)
                    {
                        ptr = ptr.Failure;
                    }
                }
                if (trans != null)
                {
                    ptr = trans;
                    if (ptr.Results.Length > 0)
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Example #3
0
        /// <summary>
        /// 搜索第一个关键词
        /// </summary>
        /// <param name="text">要搜索的文本</param>
        /// <returns>搜索到的对象</returns>
        public KeyWordModel FindFirst(string text)
        {
            ArrayList       ret = new ArrayList();
            KeyWordTreeNode ptr = _root;

            for (int i = 0; i < text.Length; i++)
            {
                KeyWordTreeNode trans = null;
                while (trans == null)
                {
                    trans = ptr.GetTransition(text[i]);
                    if (ptr == _root)
                    {
                        break;
                    }
                    if (trans == null)
                    {
                        ptr = ptr.Failure;
                    }
                }
                if (trans != null)
                {
                    ptr = trans;
                    foreach (string found in ptr.Results)
                    {
                        return(new KeyWordModel(i - found.Length + 1, found));
                    }
                }
            }
            return(KeyWordModel.Empty);
        }
Example #4
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="parent">父节点</param>
 /// <param name="c">字符</param>
 public KeyWordTreeNode(KeyWordTreeNode parent, char c)
 {
     _char          = c; _parent = parent;
     _results       = new ArrayList();
     _resultsAr     = new string[] { };
     _transitionsAr = new KeyWordTreeNode[] { };
     _transHash     = new Hashtable();
 }
Example #5
0
 /// <summary>
 /// 生成树
 /// </summary>
 private void BuildTree()
 {
     _root = new KeyWordTreeNode(null, ' ');
     #region 生成树
     foreach (string p in _keywords)
     {
         KeyWordTreeNode nd = _root;
         foreach (char c in p)
         {
             KeyWordTreeNode ndNew = null;
             foreach (KeyWordTreeNode trans in nd.Transitions)
             {
                 if (trans.Char == c)
                 {
                     ndNew = trans;
                     break;
                 }
             }
             if (ndNew == null)
             {
                 ndNew = new KeyWordTreeNode(nd, c);
                 nd.AddTransition(ndNew);
             }
             nd = ndNew;
         }
         nd.AddResult(p);
     }
     #endregion
     ArrayList nodes = new ArrayList();
     //第一层失败节点
     foreach (KeyWordTreeNode nd in _root.Transitions)
     {
         nd.Failure = _root;
         foreach (KeyWordTreeNode trans in nd.Transitions)
         {
             nodes.Add(trans);
         }
     }
     //下级失败节点
     while (nodes.Count != 0)
     {
         ArrayList newNodes = new ArrayList();
         foreach (KeyWordTreeNode nd in nodes)
         {
             KeyWordTreeNode r = nd.Parent.Failure;
             char            c = nd.Char;
             while (r != null && !r.ContainsTransition(c))
             {
                 r = r.Failure;
             }
             if (r == null)
             {
                 nd.Failure = _root;
             }
             else
             {
                 nd.Failure = r.GetTransition(c);
                 foreach (string result in nd.Failure.Results)
                 {
                     nd.AddResult(result);
                 }
             }
             //添加子节点在失败节点中
             foreach (KeyWordTreeNode child in nd.Transitions)
             {
                 newNodes.Add(child);
             }
         }
         nodes = newNodes;
     }
     _root.Failure = _root;
 }