Example #1
0
        public void TestParsePlaceName()
        {
            IParser p = new PlaceNameParser(GeneralParserTest.CreateParserContext("你好,我在上海,他在北京。", dictAddr));
            ParseResultCollection prc = p.Parse(0);
            Assert.AreEqual(0, prc.Count);

            prc = p.Parse(5);
            Assert.AreEqual(1, prc.Count);
            GeneralParserTest.AssertParseResult(prc[0], "上海", 5, POSType.A_NS);

            prc = p.Parse(10);
            Assert.AreEqual(1, prc.Count);
            GeneralParserTest.AssertParseResult(prc[0], "北京", 10, POSType.A_NS);
        }
        //public static ParseResultCollection Parse(string text)
        //{
        //    return ParseResultCollection.InternalParse(text, new OrgNameParser(text));
        //}
        public ParseResultCollection Parse(int startIndex)
        {
            string _text = context.Text;
            ParseResultCollection prc = new ParseResultCollection();
            string temp = _text.Substring(startIndex, Math.Min(maxChineseOrgNameLength,_text.Length-startIndex));
            int pos = -1;
            string suffix = null;
            for (int i = 0; i < suffixList.Length; i++)
            {
                pos = temp.IndexOf(suffixList[i]);
                if(pos>0)
                {
                    suffix = suffixList[i];
                    break;
                }
            }
            if (pos <= 0)    //找不到后缀,直接返回
                return prc;
            //寻找前置地名
            string placeName = null;
            ParserContext context1 = this.context.Clone();
            context1.Text = temp;
            IParser placeNameParser = new PlaceNameParser(context1);
            ParseResultCollection prc1 = placeNameParser.Parse(0);
            if (prc1.Count > 0)
            {
                placeName = (string)prc1[0].Text;
            }

            if (placeName!=null && pos -placeName.Length < maxMiddlePartLength)
            {
                prc.Add(ParseResult.Create(temp.Substring(0, pos + suffix.Length), startIndex, POSType.A_NT));
            }
            else if (context.Text.IndexOf("(")>0)
            {
                int bracePos = context.Text.IndexOf("(");

                IParser placeNameParser2 = new PlaceNameParser(context);
                ParseResultCollection prc2 = placeNameParser2.Parse(bracePos+1);
                if (prc2.Count > 0)
                {
                    placeName = (string)prc2[0].Text;
                    prc.Add(ParseResult.Create(temp.Substring(0, pos + suffix.Length), startIndex, POSType.A_NT));
                }
            }
            else
            {
                //没有找到地名
                string orgName = MatchOrgName(temp, 0);
                if (orgName != null)
                {
                    prc.Add(ParseResult.Create(orgName, startIndex, POSType.A_NT));
                }
                else
                {
                    //库中没有,使用谓词定位边界
                }
            }

            return prc;
            /*
             * 《现代汉语词汇研究-中文信息处理》
             确定规则
             * a. 如果候选地名字符串前一词为地名指界词,且候选地名字串后一个词为地名特征词,则候选地名左右边界确定
             * b. 如果候选地名字符串前一词为地名指界词,则候选地名左边界确定
             * c. 如果候选地名字串后一个词为地名指界词,则候选地名右边界确定
             * d. 如果两个候选地名字串存在并列关系, 其中一个候选地名被确定,则另一个候选地名也被确定
             否定规则
             * 称谓词否定规则:如果候选地名字串的前一词是人名称谓词,且候选地名字串中没有地名特征词,否定该地名字串。
             * 指界词否定规则:如果候选地名字串的后一词为人名指界词,且候选地名字串中没有地名特征词,否定该地名字串。
             * 并列否定规则:如果两个候选地名字串存在并列关系,其中一个候选地名被否定,另一个候选地名也被否定。
             * 其他物体类否定规则:如果候选地名字符串的后一词为其他物体类特征词,否定该地名字串。如红塔山香烟
             * 非单字词否定规则:如果候选地名字串的前一词不是单字词,或候选地名字串的后一词不是单字词,则否定候选地名
             边界修正规则
             * 称谓词与特征词修正规则:如果候选地名字串的前一词为人名称谓词且候选地名字串中存在地名特征词,则修正地名的边界
             */
        }
Example #3
0
        //public static ParseResultCollection Parse(string text)
        //{
        //    return ParseResultCollection.InternalParse(text, new OrgNameParser(text));
        //}

        public ParseResultCollection Parse(int startIndex)
        {
            string _text = context.Text;
            ParseResultCollection prc = new ParseResultCollection();
            string temp   = _text.Substring(startIndex, Math.Min(maxChineseOrgNameLength, _text.Length - startIndex));
            int    pos    = -1;
            string suffix = null;

            for (int i = 0; i < suffixList.Length; i++)
            {
                pos = temp.IndexOf(suffixList[i]);
                if (pos > 0)
                {
                    suffix = suffixList[i];
                    break;
                }
            }
            if (pos <= 0)    //找不到后缀,直接返回
            {
                return(prc);
            }
            //寻找前置地名
            string        placeName = null;
            ParserContext context1  = this.context.Clone();

            context1.Text = temp;
            IParser placeNameParser    = new PlaceNameParser(context1);
            ParseResultCollection prc1 = placeNameParser.Parse(0);

            if (prc1.Count > 0)
            {
                placeName = (string)prc1[0].Text;
            }

            if (placeName != null && pos - placeName.Length < maxMiddlePartLength)
            {
                prc.Add(ParseResult.Create(temp.Substring(0, pos + suffix.Length), startIndex, POSType.A_NT));
            }
            else if (context.Text.IndexOf("(") > 0)
            {
                int bracePos = context.Text.IndexOf("(");

                IParser placeNameParser2   = new PlaceNameParser(context);
                ParseResultCollection prc2 = placeNameParser2.Parse(bracePos + 1);
                if (prc2.Count > 0)
                {
                    placeName = (string)prc2[0].Text;
                    prc.Add(ParseResult.Create(temp.Substring(0, pos + suffix.Length), startIndex, POSType.A_NT));
                }
            }
            else
            {
                //没有找到地名
                string orgName = MatchOrgName(temp, 0);
                if (orgName != null)
                {
                    prc.Add(ParseResult.Create(orgName, startIndex, POSType.A_NT));
                }
                else
                {
                    //库中没有,使用谓词定位边界
                }
            }

            return(prc);

            /*
             * 《现代汉语词汇研究-中文信息处理》
             * 确定规则
             * a. 如果候选地名字符串前一词为地名指界词,且候选地名字串后一个词为地名特征词,则候选地名左右边界确定
             * b. 如果候选地名字符串前一词为地名指界词,则候选地名左边界确定
             * c. 如果候选地名字串后一个词为地名指界词,则候选地名右边界确定
             * d. 如果两个候选地名字串存在并列关系, 其中一个候选地名被确定,则另一个候选地名也被确定
             * 否定规则
             * 称谓词否定规则:如果候选地名字串的前一词是人名称谓词,且候选地名字串中没有地名特征词,否定该地名字串。
             * 指界词否定规则:如果候选地名字串的后一词为人名指界词,且候选地名字串中没有地名特征词,否定该地名字串。
             * 并列否定规则:如果两个候选地名字串存在并列关系,其中一个候选地名被否定,另一个候选地名也被否定。
             * 其他物体类否定规则:如果候选地名字符串的后一词为其他物体类特征词,否定该地名字串。如红塔山香烟
             * 非单字词否定规则:如果候选地名字串的前一词不是单字词,或候选地名字串的后一词不是单字词,则否定候选地名
             * 边界修正规则
             * 称谓词与特征词修正规则:如果候选地名字串的前一词为人名称谓词且候选地名字串中存在地名特征词,则修正地名的边界
             */
        }