Exemplo n.º 1
0
        public KeyWordDic Add(char key)
        {
            CallAddCount++;

            KeyWordDic newKv = null;

            if (kvArray == null)
            {
                kvArray = new KeyWordDic[CapLargerCount];
            }

            if (Count == 0)
            {
                newKv      = new KeyWordDic(key);
                kvArray[0] = newKv;
            }
            else
            {
                int md = 0;
                if (Find(key, ref md) != -1)
                {
                    throw new Exception("主键已存在:" + key + "!");
                }
                else
                {
                    if (kvArray[md].Key < key)
                    {
                        md = (UInt16)(md + 1);
                    }

                    for (int i = Count - 1; i >= md; i--)
                    {
                        kvArray[i + 1] = kvArray[i];
                    }

                    newKv       = new KeyWordDic(key);
                    kvArray[md] = newKv;
                }
            }

            Count++;

            if (Count == kvArray.Length)
            {
                int largerSize = Count * 2;
                if (largerSize > char.MaxValue + 1)
                {
                    largerSize = char.MaxValue + 1;
                }

                var newKvArray = new KeyWordDic[largerSize];
                for (int i = 0; i < Count; i++)
                {
                    newKvArray[i] = kvArray[i];
                }
                kvArray = newKvArray;
            }

            return(newKv);
        }
Exemplo n.º 2
0
        public bool TryGetValue(char key, out KeyWordDic val)
        {
            int fd = 0;
            int re = Find(key, ref fd);

            if (re > -1)
            {
                val = kvArray[re];
                return(true);
            }
            else
            {
                val = default(KeyWordDic);
                return(false);
            }
        }
Exemplo n.º 3
0
        //private KeyWordANFStatus CheckChar(char chr, ref KeyWordDic lastDic)
        //{
        //    if (lastDic != null)
        //    {
        //        bool isacc = lastDic.HasEndChar;
        //        if (lastDic.TryGetValue(chr, out lastDic))
        //        {
        //            if (lastDic.HasEndChar)
        //            {

        //                lastDic = null;

        //                if (isacc)
        //                    return KeyWordANFStatus.accept2;

        //                return KeyWordANFStatus.accept;
        //            }
        //            else
        //            {
        //                if (isacc)
        //                    return KeyWordANFStatus.acceptwating;

        //                return KeyWordANFStatus.wating;
        //            }
        //        }

        //        if (isacc)
        //            return KeyWordANFStatus.accept1;
        //    }

        //    if (dics[chr] != null)
        //    {
        //        lastDic = dics[chr];
        //    }

        //    return KeyWordANFStatus.abort;

        //}

        private KeyWordANFStatus CheckChar(char chr, ref KeyWordDic lastDic, KeyWordANFStatus lastStatu)
        {
            if (lastDic != null)
            {
                bool isacc = lastStatu != KeyWordANFStatus.accept && lastDic.HasEndChar;
                if (lastDic.TryGetValue(chr, out lastDic))
                {
                    if (lastDic.HasEndChar)
                    {
                        //lastDic = null;

                        if (isacc)
                        {
                            //lastDic = null;
                            return(KeyWordANFStatus.accept2);
                        }

                        return(KeyWordANFStatus.accept);
                    }
                    else
                    {
                        if (isacc)
                        {
                            return(KeyWordANFStatus.acceptwating);
                        }

                        return(KeyWordANFStatus.wating);
                    }
                }

                if (isacc)
                {
                    lastDic = null;
                    return(KeyWordANFStatus.accept1);
                }
            }

            if (dics[chr] != null)
            {
                lastDic = dics[chr];
            }

            return(KeyWordANFStatus.abort);
        }
Exemplo n.º 4
0
        public void RemoveKeyWord(string word)
        {
            if (string.IsNullOrWhiteSpace(word))
            {
                return;
            }

            try
            {
                dicLock.EnterWriteLock();

                var dic = dics[word[0]];
                if (dic == null)
                {
                    return;
                }

                if (DicTag.ContainsKey(word))
                {
                    DicTag.Remove(word);
                }

                if (!dic.ContainsKey(word[0]))
                {
                    return;
                }

                var        innerDic  = dic[word[0]];
                KeyWordDic dicRemove = dic;
                char       removeKey = word[0];

                for (int i = 1; i < word.Length; i++)
                {
                    var ch = word[i];
                    if (!innerDic.ContainsKey(ch))
                    {
                        return;
                    }

                    if (innerDic.Count > 1)
                    {
                        removeKey = ch;
                        dicRemove = innerDic;
                    }

                    innerDic = innerDic[ch];
                }


                //检查是否结束了
                if (innerDic.Keys().Count > 0 && innerDic.HasEndChar)
                {
                    if (innerDic.Keys().Count == 1)
                    {
                        dicRemove.Remove(removeKey);
                    }
                    else
                    {
                        innerDic.HasEndChar = false;
                    }
                }
            }
            finally
            {
                dicLock.ExitWriteLock();
            }
        }
Exemplo n.º 5
0
        public IEnumerable <KeyWordMatchResult> MatchKeyWord(string text)
        {
            if (text == null)
            {
                yield break;
            }

            KeyWordDic       innerDic = null;
            int              iStart   = 0;
            KeyWordANFStatus statu    = KeyWordANFStatus.abort;
            string           substr;

            try
            {
                dicLock.EnterReadLock();
                var tlen = text.Length;
                for (int i = 0; i < tlen; i++)
                {
                    if (innerDic == null)
                    {
                        innerDic = dics[text[i]];
                        iStart   = i;
                    }
                    else
                    {
                        statu = CheckChar(text[i], ref innerDic, statu);
                        if (statu == KeyWordANFStatus.abort)
                        {
                            //if (innerDic == null)
                            //    iStart = i + 1;
                            //else
                            {
                                int j = 1;
                                for (; j < i - iStart; j++)
                                {
                                    if (dics[text[iStart + j]] != null)
                                    {
                                        break;
                                    }
                                }
                                iStart  += j;
                                i        = iStart;
                                innerDic = dics[text[i]];
                            }
                        }
                        else if (statu == KeyWordANFStatus.accept1)
                        {
                            var    wm  = text[i - 1].ToString();
                            object tag = null;
                            if (DicTag.ContainsKey(wm))
                            {
                                tag = DicTag[wm];
                            }
                            yield return(new KeyWordMatchResult
                            {
                                KeyWordMatched = wm,
                                PostionStart = iStart,
                                PostionEnd = i,
                                Tag = tag,
                            });

                            i--;
                            iStart = i;
                        }
                        else if (statu == KeyWordANFStatus.accept2)
                        {
                            var    wm  = text[i - 1].ToString();
                            object tag = null;
                            if (DicTag.ContainsKey(wm))
                            {
                                tag = DicTag[wm];
                            }
                            yield return(new KeyWordMatchResult
                            {
                                KeyWordMatched = text[i - 1].ToString(),
                                PostionStart = iStart,
                                PostionEnd = i,
                                Tag = tag,
                            });

                            wm  = text.Substring(iStart, 2);
                            tag = null;
                            if (DicTag.ContainsKey(wm))
                            {
                                tag = DicTag[wm];
                            }
                            yield return(new KeyWordMatchResult
                            {
                                KeyWordMatched = text.Substring(iStart, 2),
                                PostionStart = iStart,
                                PostionEnd = i,
                                Tag = tag,
                            });

                            iStart = i;
                        }
                        else if (statu == KeyWordANFStatus.acceptwating)
                        {
                            var    wm  = text[i - 1].ToString();
                            object tag = null;
                            if (DicTag.ContainsKey(wm))
                            {
                                tag = DicTag[wm];
                            }

                            yield return(new KeyWordMatchResult
                            {
                                KeyWordMatched = text[i - 1].ToString(),
                                PostionStart = iStart,
                                PostionEnd = i,
                                Tag = tag,
                            });

                            iStart = i - 1;
                        }
                        else if (statu == KeyWordANFStatus.accept)
                        {
                            substr = text.Substring(iStart, i - iStart + 1);
                            object tag = null;
                            if (DicTag.ContainsKey(substr))
                            {
                                tag = DicTag[substr];
                            }

                            yield return(new KeyWordMatchResult
                            {
                                KeyWordMatched = substr,
                                PostionStart = iStart,
                                PostionEnd = i,
                                Tag = tag,
                            });

                            //for (int j = 1; j < substr.Length; j++)
                            //{
                            //    innerDic = dics[substr[j]];
                            //    if (innerDic != null)
                            //    {
                            //        i = iStart + j;
                            //        iStart = i;
                            //        break;
                            //    }
                            //}
                        }
                    }
                }
            }
            finally
            {
                dicLock.ExitReadLock();
            }
        }
Exemplo n.º 6
0
        public void AddKeyWord(string word, object tag = null)
        {
            if (string.IsNullOrWhiteSpace(word))
            {
                return;
            }

            try
            {
                dicLock.EnterWriteLock();
                word = word.Trim();

                if (tag != null && !DicTag.ContainsKey(word))
                {
                    DicTag.Add(word, tag);
                }

                var dic = dics[word[0]];
                if (dic == null)
                {
                    dic = new KeyWordDic(word[0]);
                }

                if (word.Length == 1)
                {
                    dic.HasEndChar = true;
                    return;
                }

                KeyWordDic innerDic = null;

                if (dic.ContainsKey(word[1]))
                {
                    innerDic = dic[word[1]];
                }
                else
                {
                    innerDic = dic.Add(word[1]);
                }

                for (int i = 2; i < word.Length; i++)
                {
                    var ch = word[i];
                    if (!innerDic.ContainsKey(ch))
                    {
                        innerDic = innerDic.Add(ch);
                    }
                    else
                    {
                        innerDic = innerDic[ch];
                    }
                }

                innerDic.HasEndChar = true;
                dics[word[0]]       = dic;
            }
            finally
            {
                dicLock.ExitWriteLock();
            }
        }