Ejemplo n.º 1
0
        private AhoCorasickTreeNode(AhoCorasickTreeNode parent, char value)
        {
            Value   = value;
            _parent = parent;

            _results = new List <string>();

            _entries = new Entry[0];
        }
Ejemplo n.º 2
0
        public AhoCorasickTree(IEnumerable <string> keywords)
        {
            Root = new AhoCorasickTreeNode();

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

                SetFailureNodes();
            }
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
            }
        }