示例#1
0
        private AhoCorasickTreeNode(AhoCorasickTreeNode parent, char value)
        {
            Value   = value;
            _parent = parent;

            _results = new List <string>();

            _entries = new Entry[0];
        }
示例#2
0
        public AhoCorasickTree(IEnumerable <string> keywords)
        {
            Root = new AhoCorasickTreeNode();

            if (keywords != null)
            {
                foreach (var p in keywords)
                {
                    AddPatternToTree(p);
                }

                SetFailureNodes();
            }
        }
示例#3
0
        private AhoCorasickTreeNode GetTransition(char c, ref AhoCorasickTreeNode pointer)
        {
            AhoCorasickTreeNode transition = null;

            while (transition == null)
            {
                transition = pointer.GetTransition(c);

                if (pointer == Root)
                {
                    break;
                }

                if (transition == null)
                {
                    pointer = pointer.Failure;
                }
            }
            return(transition);
        }
示例#4
0
        private bool Contains(string text, bool onlyStarts)
        {
            var pointer = Root;

            for (var i = 0; i < text.Length; i++)
            {
                AhoCorasickTreeNode transition = null;
                while (transition == null)
                {
                    transition = pointer.GetTransition(text[i]);

                    if (pointer == Root)
                    {
                        break;
                    }

                    if (transition == null)
                    {
                        pointer = pointer.Failure;
                    }
                }

                if (transition != null)
                {
                    pointer = transition;
                }
                else if (onlyStarts)
                {
                    return(false);
                }

                if (pointer.Results.Count > 0)
                {
                    return(true);
                }
            }
            return(false);
        }
示例#5
0
        public AhoCorasickTreeNode AddTransition(char c)
        {
            var node = new AhoCorasickTreeNode(this, c);

            if (_size == 0)
            {
                Resize();
            }

            while (true)
            {
                var ind = c & (_size - 1);

                if (_entries[ind].Key != 0 && _entries[ind].Key != c)
                {
                    Resize();
                    continue;
                }

                _entries[ind].Key   = c;
                _entries[ind].Value = node;
                return(node);
            }
        }