Пример #1
0
        /// <summary>Checks if two clusters are coreferent according to our sieve pass constraints</summary>
        /// <param name="document"/>
        /// <exception cref="System.Exception"/>
        public virtual bool Coreferent(Document document, CorefCluster mentionCluster, CorefCluster potentialAntecedent, Mention mention2, Mention ant, Dictionaries dict, ICollection <Mention> roleSet)
        {
            bool    ret     = false;
            Mention mention = mentionCluster.GetRepresentativeMention();

            if (flags.UseIncompatibles)
            {
                // Check our list of incompatible mentions and don't cluster them together
                // Allows definite no's from previous sieves to propagate down
                if (document.IsIncompatible(mentionCluster, potentialAntecedent))
                {
                    return(false);
                }
            }
            if (flags.DoPronoun && Math.Abs(mention2.sentNum - ant.sentNum) > 3 && mention2.person != Dictionaries.Person.I && mention2.person != Dictionaries.Person.You)
            {
                return(false);
            }
            if (mention2.LowercaseNormalizedSpanString().Equals("this") && Math.Abs(mention2.sentNum - ant.sentNum) > 3)
            {
                return(false);
            }
            if (mention2.person == Dictionaries.Person.You && document.docType == Document.DocType.Article && mention2.headWord.Get(typeof(CoreAnnotations.SpeakerAnnotation)).Equals("PER0"))
            {
                return(false);
            }
            if (document.conllDoc != null)
            {
                if (ant.generic && ant.person == Dictionaries.Person.You)
                {
                    return(false);
                }
                if (mention2.generic)
                {
                    return(false);
                }
            }
            // chinese newswire contains coref nested NPs with shared headword  Chen & Ng
            if (lang != Locale.Chinese || document.docInfo == null || !document.docInfo.GetOrDefault("DOC_ID", string.Empty).Contains("nw"))
            {
                if (mention2.InsideIn(ant) || ant.InsideIn(mention2))
                {
                    return(false);
                }
            }
            if (flags.UseSpeakermatch)
            {
                string mSpeaker = mention2.headWord.Get(typeof(CoreAnnotations.SpeakerAnnotation));
                string aSpeaker = ant.headWord.Get(typeof(CoreAnnotations.SpeakerAnnotation));
                // <I> from same speaker
                if (mention2.person == Dictionaries.Person.I && ant.person == Dictionaries.Person.I)
                {
                    return(mSpeaker.Equals(aSpeaker));
                }
                // <I> - speaker
                if ((mention2.person == Dictionaries.Person.I && mSpeaker.Equals(int.ToString(ant.mentionID))) || (ant.person == Dictionaries.Person.I && aSpeaker.Equals(int.ToString(mention2.mentionID))))
                {
                    return(true);
                }
            }
            if (flags.UseDiscoursematch)
            {
                string mString   = mention.LowercaseNormalizedSpanString();
                string antString = ant.LowercaseNormalizedSpanString();
                // mention and ant both belong to the same speaker cluster
                if (mention.speakerInfo != null && mention.speakerInfo == ant.speakerInfo)
                {
                    return(true);
                }
                // (I - I) in the same speaker's quotation.
                if (mention.number == Dictionaries.Number.Singular && dict.firstPersonPronouns.Contains(mString) && ant.number == Dictionaries.Number.Singular && dict.firstPersonPronouns.Contains(antString) && CorefRules.EntitySameSpeaker(document, mention,
                                                                                                                                                                                                                                               ant))
                {
                    return(true);
                }
                // (speaker - I)
                if ((mention.number == Dictionaries.Number.Singular && dict.firstPersonPronouns.Contains(mString)) && CorefRules.AntecedentIsMentionSpeaker(document, mention, ant, dict))
                {
                    if (mention.speakerInfo == null && ant.speakerInfo != null)
                    {
                        mention.speakerInfo = ant.speakerInfo;
                    }
                    return(true);
                }
                // (I - speaker)
                if ((ant.number == Dictionaries.Number.Singular && dict.firstPersonPronouns.Contains(antString)) && CorefRules.AntecedentIsMentionSpeaker(document, ant, mention, dict))
                {
                    if (ant.speakerInfo == null && mention.speakerInfo != null)
                    {
                        ant.speakerInfo = mention.speakerInfo;
                    }
                    return(true);
                }
                // Can be iffy if more than two speakers... but still should be okay most of the time
                if (dict.secondPersonPronouns.Contains(mString) && dict.secondPersonPronouns.Contains(antString) && CorefRules.EntitySameSpeaker(document, mention, ant))
                {
                    return(true);
                }
                // previous I - you or previous you - I in two person conversation
                if (((mention.person == Dictionaries.Person.I && ant.person == Dictionaries.Person.You || (mention.person == Dictionaries.Person.You && ant.person == Dictionaries.Person.I)) && (mention.headWord.Get(typeof(CoreAnnotations.UtteranceAnnotation
                                                                                                                                                                                                                              )) - ant.headWord.Get(typeof(CoreAnnotations.UtteranceAnnotation)) == 1) && document.docType == Document.DocType.Conversation))
                {
                    return(true);
                }
                if (dict.reflexivePronouns.Contains(mention.headString) && CorefRules.EntitySubjectObject(mention, ant))
                {
                    return(true);
                }
            }
            if (!flags.UseExactstringmatch && !flags.UseRelaxedExactstringmatch && !flags.UseApposition && !flags.UseWordsInclusion)
            {
                foreach (Mention m in mentionCluster.GetCorefMentions())
                {
                    foreach (Mention a in potentialAntecedent.GetCorefMentions())
                    {
                        // angelx - not sure about the logic here, disable (code was also refactored from original)
                        // vv gabor - re-enabled code (seems to improve performance) vv
                        if (m.person != Dictionaries.Person.I && a.person != Dictionaries.Person.I && (CorefRules.AntecedentIsMentionSpeaker(document, m, a, dict) || CorefRules.AntecedentIsMentionSpeaker(document, a, m, dict)))
                        {
                            document.AddIncompatible(m, a);
                            return(false);
                        }
                        // ^^ end block of code in question ^^
                        int dist = Math.Abs(m.headWord.Get(typeof(CoreAnnotations.UtteranceAnnotation)) - a.headWord.Get(typeof(CoreAnnotations.UtteranceAnnotation)));
                        if (document.docType != Document.DocType.Article && dist == 1 && !CorefRules.EntitySameSpeaker(document, m, a))
                        {
                            string mSpeaker = document.speakers[m.headWord.Get(typeof(CoreAnnotations.UtteranceAnnotation))];
                            string aSpeaker = document.speakers[a.headWord.Get(typeof(CoreAnnotations.UtteranceAnnotation))];
                            if (m.person == Dictionaries.Person.I && a.person == Dictionaries.Person.I)
                            {
                                document.AddIncompatible(m, a);
                                return(false);
                            }
                            if (m.person == Dictionaries.Person.You && a.person == Dictionaries.Person.You)
                            {
                                document.AddIncompatible(m, a);
                                return(false);
                            }
                            // This is weak since we can refer to both speakers
                            if (m.person == Dictionaries.Person.We && a.person == Dictionaries.Person.We)
                            {
                                document.AddIncompatible(m, a);
                                return(false);
                            }
                        }
                    }
                }
                if (document.docType == Document.DocType.Article)
                {
                    foreach (Mention m_1 in mentionCluster.GetCorefMentions())
                    {
                        foreach (Mention a in potentialAntecedent.GetCorefMentions())
                        {
                            if (CorefRules.EntitySubjectObject(m_1, a))
                            {
                                document.AddIncompatible(m_1, a);
                                return(false);
                            }
                        }
                    }
                }
            }
            // Incompatibility constraints - do before match checks
            if (flags.USE_iwithini && CorefRules.EntityIWithinI(mention, ant, dict))
            {
                document.AddIncompatible(mention, ant);
                return(false);
            }
            // Match checks
            if (flags.UseExactstringmatch && CorefRules.EntityExactStringMatch(mention, ant, dict, roleSet))
            {
                return(true);
            }
            //    if(flags.USE_EXACTSTRINGMATCH && Rules.entityExactStringMatch(mentionCluster, potentialAntecedent, dict, roleSet)){
            //      return true;
            //    }
            if (flags.UseNameMatch && CheckEntityMatch(document, mentionCluster, potentialAntecedent, dict, roleSet))
            {
                ret = true;
            }
            if (flags.UseRelaxedExactstringmatch && CorefRules.EntityRelaxedExactStringMatch(mentionCluster, potentialAntecedent, mention, ant, dict, roleSet))
            {
                return(true);
            }
            if (flags.UseApposition && CorefRules.EntityIsApposition(mentionCluster, potentialAntecedent, mention, ant))
            {
                return(true);
            }
            if (flags.UsePredicatenominatives && CorefRules.EntityIsPredicateNominatives(mentionCluster, potentialAntecedent, mention, ant))
            {
                return(true);
            }
            if (flags.UseAcronym && CorefRules.EntityIsAcronym(document, mentionCluster, potentialAntecedent))
            {
                return(true);
            }
            if (flags.UseRelativepronoun && CorefRules.EntityIsRelativePronoun(mention, ant))
            {
                return(true);
            }
            if (flags.UseDemonym && mention.IsDemonym(ant, dict))
            {
                return(true);
            }
            if (flags.UseRoleapposition)
            {
                if (lang == Locale.Chinese)
                {
                    ret = false;
                }
                else
                {
                    if (CorefRules.EntityIsRoleAppositive(mentionCluster, potentialAntecedent, mention, ant, dict))
                    {
                        ret = true;
                    }
                }
            }
            if (flags.UseInclusionHeadmatch && CorefRules.EntityHeadsAgree(mentionCluster, potentialAntecedent, mention, ant, dict))
            {
                ret = true;
            }
            if (flags.UseRelaxedHeadmatch && CorefRules.EntityRelaxedHeadsAgreeBetweenMentions(mentionCluster, potentialAntecedent, mention, ant))
            {
                ret = true;
            }
            if (flags.UseWordsInclusion && ret && !CorefRules.EntityWordsIncluded(mentionCluster, potentialAntecedent, mention, ant))
            {
                return(false);
            }
            if (flags.UseIncompatibleModifier && ret && CorefRules.EntityHaveIncompatibleModifier(mentionCluster, potentialAntecedent))
            {
                return(false);
            }
            if (flags.UseProperheadAtLast && ret && !CorefRules.EntitySameProperHeadLastWord(mentionCluster, potentialAntecedent, mention, ant))
            {
                return(false);
            }
            if (flags.UseAttributesAgree && !CorefRules.EntityAttributesAgree(mentionCluster, potentialAntecedent))
            {
                return(false);
            }
            if (flags.UseDifferentLocation && CorefRules.EntityHaveDifferentLocation(mention, ant, dict))
            {
                if (flags.UseProperheadAtLast && ret && mention.goldCorefClusterID != ant.goldCorefClusterID)
                {
                }
                return(false);
            }
            if (flags.UseNumberInMention && CorefRules.EntityNumberInLaterMention(mention, ant))
            {
                if (flags.UseProperheadAtLast && ret && mention.goldCorefClusterID != ant.goldCorefClusterID)
                {
                }
                return(false);
            }
            if (flags.UseDistance && CorefRules.EntityTokenDistance(mention2, ant))
            {
                return(false);
            }
            if (flags.UseCorefDict)
            {
                // Head match
                if (ant.headWord.Lemma().Equals(mention2.headWord.Lemma()))
                {
                    return(false);
                }
                // Constraint: ignore pairs commonNoun - properNoun
                if (ant.mentionType != Dictionaries.MentionType.Proper && (mention2.headWord.Get(typeof(CoreAnnotations.PartOfSpeechAnnotation)).StartsWith("NNP") || !Sharpen.Runtime.Substring(mention2.headWord.Word(), 1).Equals(Sharpen.Runtime.Substring(mention2
                                                                                                                                                                                                                                                               .headWord.Word(), 1).ToLower())))
                {
                    return(false);
                }
                // Constraint: ignore plurals
                if (ant.headWord.Get(typeof(CoreAnnotations.PartOfSpeechAnnotation)).Equals("NNS") && mention2.headWord.Get(typeof(CoreAnnotations.PartOfSpeechAnnotation)).Equals("NNS"))
                {
                    return(false);
                }
                // Constraint: ignore mentions with indefinite determiners
                if (dict.indefinitePronouns.Contains(ant.originalSpan[0].Lemma()) || dict.indefinitePronouns.Contains(mention2.originalSpan[0].Lemma()))
                {
                    return(false);
                }
                // Constraint: ignore coordinated mentions
                if (ant.IsCoordinated() || mention2.IsCoordinated())
                {
                    return(false);
                }
                // Constraint: context incompatibility
                if (CorefRules.ContextIncompatible(mention2, ant, dict))
                {
                    return(false);
                }
                // Constraint: sentence context incompatibility when the mentions are common nouns
                if (CorefRules.SentenceContextIncompatible(mention2, ant, dict))
                {
                    return(false);
                }
                if (CorefRules.EntityClusterAllCorefDictionary(mentionCluster, potentialAntecedent, dict, 1, 8))
                {
                    return(true);
                }
                if (CorefRules.EntityCorefDictionary(mention, ant, dict, 2, 2))
                {
                    return(true);
                }
                if (CorefRules.EntityCorefDictionary(mention, ant, dict, 3, 2))
                {
                    return(true);
                }
                if (CorefRules.EntityCorefDictionary(mention, ant, dict, 4, 2))
                {
                    return(true);
                }
            }
            if (flags.DoPronoun)
            {
                Mention m;
                if (mention.predicateNominatives != null && mention.predicateNominatives.Contains(mention2))
                {
                    m = mention2;
                }
                else
                {
                    m = mention;
                }
                bool mIsPronoun = (m.IsPronominal() || dict.allPronouns.Contains(m.ToString()));
                bool attrAgree  = HybridCorefProperties.UseDefaultPronounAgreement(props) ? CorefRules.EntityAttributesAgree(mentionCluster, potentialAntecedent) : CorefRules.EntityAttributesAgree(mentionCluster, potentialAntecedent, lang);
                if (mIsPronoun && attrAgree)
                {
                    if (dict.demonymSet.Contains(ant.LowercaseNormalizedSpanString()) && dict.notOrganizationPRP.Contains(m.headString))
                    {
                        document.AddIncompatible(m, ant);
                        return(false);
                    }
                    if (CorefRules.EntityPersonDisagree(document, mentionCluster, potentialAntecedent, dict))
                    {
                        document.AddIncompatible(m, ant);
                        return(false);
                    }
                    return(true);
                }
            }
            if (flags.UseChineseHeadMatch)
            {
                if (mention2.headWord == ant.headWord && mention2.InsideIn(ant))
                {
                    if (!document.IsCoref(mention2, ant))
                    {
                    }
                    // TODO: exclude conjunction
                    // log.info("error in chinese head match: "+mention2.spanToString()+"\t"+ant.spanToString());
                    return(true);
                }
            }
            return(ret);
        }