Пример #1
0
        /// <summary>
        /// 人名和前面的词词性匹配
        /// </summary>
        /// <param name="nextStr"></param>
        /// <returns></returns>
        public bool MatchNameInTail(String preStr)
        {
            bool isReg;

            InnerPos[] preList = _POS.GetPos(preStr, out isReg);

            if (preList.Length == 0)
            {
                return(false);
            }

            foreach (InnerPos pre in preList)
            {
                if (pre == InnerPos.POS_UNK)
                {
                    continue;
                }

                DualityWordBin posBin = new DualityWordBin(pre, InnerPos.POS_D_N);
                if (_PosBinTbl[posBin.HashCode] != null)
                {
                    return(true);
                }

                posBin = new DualityWordBin(pre, InnerPos.POS_A_NR);
                if (_PosBinTbl[posBin.HashCode] != null)
                {
                    return(true);
                }
            }

            return(false);
        }
Пример #2
0
        /// <summary>
        /// 增加单个词
        /// </summary>
        /// <param name="newWord"></param>
        /// <param name="pos"></param>
        public void AddDict(string newWord, int pos)
        {
            if (string.IsNullOrEmpty(newWord))
            {
                return;
            }
            newWord = newWord.Trim();
            if (_POS.GetPos(newWord, out isReg).Length > 0)
            {
                return;
            }

            WordPos word = new WordPos(newWord, pos);

            dictFile.WordPosList.Add(word);
            _POS.AddWordPos(word.Word, word.Pos);
        }
Пример #3
0
        public int ProcessRule(List <string> preWords, int index, List <string> retWords)
        {
            String word = (String)preWords[index];
            bool   isReg;
            int    pos = WordPosBuilder.GetPosFromInnerPosList(_POS.GetPos(word, out isReg));
            String num;

            if ((pos & (int)PosEnum.POS_A_M) == (int)PosEnum.POS_A_M)
            {
                num = word;
                int i = 0;

                for (i = index + 1; i < preWords.Count; i++)
                {
                    String next    = (String)preWords[i];
                    int    nextPos = WordPosBuilder.GetPosFromInnerPosList(_POS.GetPos(next, out isReg));
                    if ((nextPos & (int)PosEnum.POS_A_M) == (int)PosEnum.POS_A_M)
                    {
                        num += next;
                    }
                    else
                    {
                        break;
                    }
                }

                if (num == word)
                {
                    return(-1);
                }
                else
                {
                    retWords.Add(num);

                    return(i);
                }
            }
            else
            {
                return(-1);
            }
        }
Пример #4
0
        /// <summary>
        /// 匹配姓位于单词尾部的情况
        /// </summary>
        /// <param name="preWords"></param>
        /// <param name="index"></param>
        /// <param name="retWords"></param>
        /// <returns></returns>
        private int MatchFamilyNameInTail(List <string> preWords, int index, List <string> retWords)
        {
            if (retWords.Count < 1)
            {
                return(-1);
            }

            String curWord = retWords[retWords.Count - 1];

            if (curWord.Length < 2)
            {
                return(-1);
            }

            String nextWord = preWords[index];

            if (nextWord.Length > 2)
            {
                return(-1);
            }

            String familyName;

            //单姓
            familyName = curWord[curWord.Length - 1].ToString();

            if (_FamilyNameTbl[familyName] == null)
            {
                familyName = curWord.Substring(curWord.Length - 2, 2);
                if (_FamilyNameTbl[familyName] == null)
                {
                    return(-1);
                }
            }

            String remain = curWord.Substring(0, curWord.Length - familyName.Length);

            if (retWords.Count > 0)
            {
                //重新组合前面的词,并判断词性匹配
                String newWord = null;
                bool   isReg;

                if (retWords.Count > 1)
                {
                    newWord = retWords[retWords.Count - 2] + remain;
                    _Pos.GetPos(newWord, out isReg);
                    if (!isReg)
                    {
                        newWord = null;
                    }
                    else
                    {
                        if (!_PosBinRule.MatchNameInTail(newWord))
                        {
                            newWord = null;
                        }
                    }

                    if (newWord != null)
                    {
                        retWords.RemoveAt(retWords.Count - 1);
                        retWords.RemoveAt(retWords.Count - 1);
                    }
                }

                if (newWord == null)
                {
                    newWord = remain;
                    _Pos.GetPos(newWord, out isReg);
                    if (!isReg)
                    {
                        if (retWords.Count > 1)
                        {
                            newWord = retWords[retWords.Count - 2] + remain;
                            retWords.RemoveAt(retWords.Count - 1);
                        }
                    }
                    else
                    {
                        if (!_PosBinRule.MatchNameInTail(newWord))
                        {
                            newWord = null;
                        }
                    }

                    if (newWord != null)
                    {
                        retWords.RemoveAt(retWords.Count - 1);
                    }
                }

                if (newWord != null)
                {
                    retWords.Add(newWord);
                }
                else
                {
                    return(-1);
                }
            }

            String name = familyName + nextWord;

            if (name.Length - familyName.Length == 1)
            {
                //单字名 还要尝试是否是双字名

                if (index < preWords.Count - 1)
                {
                    String nnext = name + (String)preWords[index + 1];
                    nnext = nnext.Substring(familyName.Length, nnext.Length - familyName.Length);

                    if (nnext.Length <= 2)
                    {
                        if (!_PosBinRule.MatchNameInHead(nnext))
                        {
                            name = name + (String)preWords[index + 1];
                            retWords.Add(name);
                            return(index + 2);
                        }
                    }
                }
            }

            retWords.Add(name);
            return(index + 1);
        }