Ejemplo n.º 1
0
        // Does this appear to be a reference for something?
        // Checks the part of our senses, and our sub-parts
        public static ProbableStrength SeemsAnaphora(InformedPhrase phrase)
        {
            ProbableStrength total          = new ProbableStrength(0, 0);
            double           strengthFactor = total.ImproveStrengthStart();

            foreach (KeyValuePair <PhraseSense, double> sense in phrase.Senses)
            {
                if (sense.Key.Phrases.Count == 1)
                {
                    total.ImproveStrength(SeemsAnaphora(sense.Key.Phrases[0]), ref strengthFactor);
                }
                else
                {
                    ProbableStrength singletotal = sense.Key.SpeechPart().SeemsAnaphora().DownWeight(1.0 / (sense.Key.Phrases.Count + 1));

                    // Combine together results from subphrases
                    ProbableStrength phrasetotal  = new ProbableStrength(0, 0);
                    double           phraseFactor = phrasetotal.ImproveStrengthStart();
                    foreach (InformedPhrase subphrase in sense.Key.Phrases)
                    {
                        phrasetotal.ImproveStrength(SeemsAnaphora(subphrase).DownWeight(1.0 / sense.Key.Phrases.Count), ref phraseFactor);
                    }
                    phrasetotal.ImproveStrengthFinish(phraseFactor);

                    // Use both our and the recursive result
                    total.ImproveStrength(singletotal.Combine(phrasetotal), ref strengthFactor);
                }
            }

            total.ImproveStrengthFinish(strengthFactor);

            return(total);
        }
        public virtual ProbableStrength Describes(PhraseSense sense)
        {
            ProbableStrength total          = new ProbableStrength(0, 0);
            double           strengthFactor = total.ImproveStrengthStart();

            foreach (PhraseAttribute attribute in sense.Attributes)
            {
                total.ImproveStrength(Match(attribute), ref strengthFactor);
            }

            if (strengthFactor == 0)
            {
                // do we match subphrases then?
                foreach (InformedPhrase subphrase in sense.Phrases)
                {
                    total.ImproveStrength(Describes(subphrase).DownWeight(1.0 / sense.Phrases.Count), ref strengthFactor);
                }
            }

            if (strengthFactor == 0)
            {
                // We never found an appropriate attribute-- so guess!
                List <KeyValuePair <PhraseSense, double> > senses = new List <KeyValuePair <PhraseSense, double> >();
                senses.Add(new KeyValuePair <PhraseSense, double>(sense, 1.0));
                InformedPhrase   dummy  = new InformedPhrase(sense.Name(), senses);
                ProbableStrength result = Match(Guess(dummy));
                total.ImproveStrength(result, ref strengthFactor);
            }

            total.ImproveStrengthFinish(strengthFactor);
            return(total);
        }
        // Guess whether or not a phrase is plural
        public static ProbableStrength SeemsPlural(InformedPhrase phrase)
        {
            ProbableStrength result = new ProbableStrength(0, 0);
            double strengthFactor = result.ImproveStrengthStart();

            // Noun count is well determined by ending in s
            ProbableStrength nounness = PartOSAttribute.SeemsA(phrase, SpeechPart.Noun);
            ProbableStrength nisplural;
            if (phrase.Name.ToLower().EndsWith("s"))
                nisplural = new ProbableStrength(1.0, 0.8);
            else
                nisplural = new ProbableStrength(0.0, 0.5);

            result.ImproveStrength(nounness.Relative(nisplural), ref strengthFactor);

            // Verbs that end in s are probably not plural
            ProbableStrength verbness = PartOSAttribute.SeemsA(phrase, SpeechPart.Verb);
            if (phrase.Name.ToLower().EndsWith("s"))
            {
                ProbableStrength visplural = new ProbableStrength(0.0, 0.8);
                result.ImproveStrength(verbness.Relative(visplural), ref strengthFactor);
            }

            result.ImproveStrengthFinish(strengthFactor);

            return result;
        }
Ejemplo n.º 4
0
        public GenderAttribute GuessGender(InformedPhrase word, IWordLookup informer)
        {
            List <string> parts = word.Generate();
            string        part  = parts[0];

            ProbableStrength given = IsGivenName(informer, part);

            if (given.strength < .25 && given.weight > 0.75)
            {
                GenderAttribute attr = new GenderAttribute(GenderAttribute.GenderOptions.Neuter);
                attr.Strength = given.InverseProbability();
                return(attr);
            }

            ProbableStrength female = IsFemaleName(part);

            if (female.strength > 0.5)
            {
                GenderAttribute attr = new GenderAttribute(GenderAttribute.GenderOptions.Female);
                attr.Strength = (new ProbableStrength(2.0 * (female.strength - 0.5), female.weight)).Relative(given);
                return(attr);
            }
            else if (female.strength < 0.5 && female.weight > 0.25)
            {
                GenderAttribute attr = new GenderAttribute(GenderAttribute.GenderOptions.Male);
                attr.Strength = (new ProbableStrength(2.0 * (0.5 - female.strength), female.weight)).Relative(given);
                return(attr);
            }
            else
            {
                GenderAttribute attr = new GenderAttribute(GenderAttribute.GenderOptions.Human);
                attr.Strength = given;
                return(attr);
            }
        }
Ejemplo n.º 5
0
        public GenderAttribute MergeGenderAttributes(GenderAttribute byname, GenderAttribute bypart)
        {
            if (byname.Gender == GenderAttribute.GenderOptions.Neuter && bypart.Gender == GenderAttribute.GenderOptions.Neuter)
            {
                GenderAttribute attr = new GenderAttribute(GenderAttribute.GenderOptions.Neuter);
                attr.Strength = byname.Strength.Combine(bypart.Strength);
                return(attr);
            }

            // Human likelihood
            ProbableStrength human = byname.Match(GenderAttribute.IsHuman).Combine(bypart.Match(GenderAttribute.IsHuman));

            if (human.strength > 0.5)
            {
                if (byname.Gender == GenderAttribute.GenderOptions.Neuter)
                {
                    // we don't have male/female info
                    bypart.Strength = human;
                    return(bypart);
                }
                else
                {
                    // use male/female info or just human
                    byname.Strength = human;
                    return(byname);
                }
            }
            else
            {
                GenderAttribute attr = new GenderAttribute(GenderAttribute.GenderOptions.Neuter);
                attr.Strength = human.InverseProbability();
                return(attr);
            }
        }
        // Is this sense identical to us?
        public bool IsIdentical(PhraseSense other)
        {
            if (definition != other.definition ||
                attributes.Count != other.attributes.Count ||
                phrases.Count != other.phrases.Count)
            {
                return(false);
            }

            // Check if the attributes all produce perfect matches
            // Only one needs to match for each
            foreach (PhraseAttribute attribute in attributes)
            {
                bool found = false;
                foreach (PhraseAttribute otherattribute in other.attributes)
                {
                    ProbableStrength match = attribute.Match(otherattribute);
                    if (match.strength == 1.0)
                    {
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    return(false);
                }
            }

            // Apply IsIdentical to each subphrase-- only one must match for each
            foreach (InformedPhrase phrase in phrases)
            {
                bool found = false;
                foreach (InformedPhrase otherphrase in other.phrases)
                {
                    if (phrase.IsIdentical(otherphrase))
                    {
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    return(false);
                }
            }

            return(true);
        }
        // Is this an anaphora-type part of speech
        public ProbableStrength SeemsAnaphora()
        {
            ProbableStrength itis = SeemsA(PersonalPronoun, true).
                                    Improve(SeemsA(DescriptivePronoun, true)).
                                    Improve(SeemsA(WhPronoun, true)).
                                    Improve(SeemsA(DefiniteArticle, true).DownWeight(.5));

            if (itis.strength == 0)
            {
                return(itis);
            }

            ProbableStrength itsnot = SeemsA(ProperNoun, true);

            return(new ProbableStrength(itis.strength * itis.strength / (itis.strength + itsnot.strength), (itis.weight + itsnot.weight) / 2.0));
        }
        public override bool IsMatch(IParsedPhrase check)
        {
            if (check.Part == "PP")
            {
                GroupPhrase groupPhrase = new GroupPhrase(check);
                // Take everything except the preposition
                string datetime = groupPhrase.GetBranch(1).Text;

                ProbableStrength time = ValidateUtilities.SeemsTime(datetime);
                return(time.IsLikely(0.5));
            }
            else
            {
                return(ValidateUtilities.SeemsSingleDay(check.Text).IsLikely(0.5));
            }
        }
 public override PhraseAttribute Guess(InformedPhrase word)
 {
     ProbableStrength plural = SeemsPlural(word);
     if (plural.strength > .5)
     {
         NumberAttribute result = new NumberAttribute(NumberOptions.Many);
         result.strength.strength = 2.0 * (plural.strength - 0.5);
         result.strength.weight = plural.weight;
         return result;
     }
     else
     {
         NumberAttribute result = new NumberAttribute(NumberOptions.One);
         result.strength.strength = 2.0 * (.5 - plural.strength);
         result.strength.weight = plural.weight;
         return result;
     }
 }
Ejemplo n.º 10
0
        // Could this be an anaphora for the given phrase?  Check the attributes
        public static ProbableStrength AnaphoraOf(InformedPhrase anaphora, PhraseSense prime)
        {
            if (anaphora.IsContained(prime) || prime.IsContained(anaphora))
            {
                return(ProbableStrength.Zero);   // can't refer to included structure
            }
            ProbableStrength total          = new ProbableStrength(0, 0);
            double           strengthFactor = total.ImproveStrengthStart();

            foreach (KeyValuePair <PhraseSense, double> sense in anaphora.Senses)
            {
                ProbableStrength match = AnaphoraOf(sense.Key, prime);
                total.ImproveStrength(match.DownWeight(sense.Value), ref strengthFactor);
            }

            total.ImproveStrengthFinish(strengthFactor);

            return(total);
        }
        // Does this attribute describe the word?  Consider overriding this
        public ProbableStrength Describes(InformedPhrase word)
        {
            ProbableStrength total          = new ProbableStrength(0, 0);
            double           strengthFactor = total.ImproveStrengthStart();

            foreach (KeyValuePair <PhraseSense, double> sense in word.Senses)
            {
                total.ImproveStrength(Describes(sense.Key).DownWeight(sense.Value), ref strengthFactor);
            }

            if (strengthFactor == 0)
            {
                // We never found an appropriate attribute-- so guess!
                ProbableStrength result = Match(Guess(word));
                total.ImproveStrength(result, ref strengthFactor);
            }

            total.ImproveStrengthFinish(strengthFactor);
            return(total);
        }
Ejemplo n.º 12
0
        public static ProbableStrength SeemsReferencePhrase(InformedPhrase phrase, bool not)
        {
            ProbableStrength total          = new ProbableStrength(0, 0);
            double           strengthFactor = total.ImproveStrengthStart();

            foreach (KeyValuePair <PhraseSense, double> sense in phrase.Senses)
            {
                if (sense.Key.Phrases.Count == 1)
                {
                    total.ImproveStrength(SeemsReferencialVerb(sense.Key.Phrases[0]), ref strengthFactor);
                }
                else
                {
                    // Is this a "helper" verb followed by an anaphora?
                    ProbableStrength foundHelper = ProbableStrength.None, foundBoth = ProbableStrength.None;
                    foreach (InformedPhrase subphr in sense.Key.Phrases)
                    {
                        if (!not)
                        {
                            foundHelper = foundHelper.Better(SeemsReferencialVerb(subphr));
                            foundBoth   = foundBoth.Better(foundHelper.Relative(SeemsAnaphora(subphr)));
                        }
                        else
                        {
                            foundHelper = foundHelper.Better(SeemsReferencialVerb(subphr).InverseProbability());
                            foundBoth   = foundBoth.Better(foundHelper.Relative(SeemsAnaphora(subphr).InverseProbability()));
                        }
                    }

                    foundBoth.weight = foundBoth.weight + .5 - foundBoth.weight * .5;

                    // Use both our and the recursive result
                    total.ImproveStrength(foundBoth, ref strengthFactor);
                }
            }

            total.ImproveStrengthFinish(strengthFactor);

            return(total);
        }
Ejemplo n.º 13
0
        public static ProbableStrength SeemsReferee(InformedPhrase phrase, IWordLookup informer, ProbableStrength anaphora)
        {
            List <string> words = phrase.Generate();

            double weight = 0;

            foreach (string word in words)
            {
                weight += informer.GetWeight(word, false);
            }

            /* words weight result
             * 1     0      0
             * 1     1      1
             * 2     0      0
             * 2     .5     .4
             * 2     1      2/3
             * 2     1.5    .86
             * 2     2      1
             * 3     .5     .3
             * 3     1      .5
             * 3     2      .8
             */
            double myweight = 2.0 * weight / (words.Count + weight);

            if (words.Count == 1)
            {
                myweight /= 2.0;    // down-score 1-word answers
            }
            if (words.Count > 2)
            {
                myweight /= Math.Log(words.Count);
            }

            ProbableStrength notanaphora = anaphora.InverseProbability();

            return(new ProbableStrength(Math.Sqrt(myweight * notanaphora.strength), notanaphora.weight + .5 - notanaphora.weight * .5));
        }
Ejemplo n.º 14
0
        public static ProbableStrength AnaphoraOf(PhraseSense anaphora, PhraseSense prime)
        {
            ProbableStrength total          = new ProbableStrength(0, 0);
            double           strengthFactor = total.ImproveStrengthStart();

            // Check if each attribute could describe the phrase
            foreach (PhraseAttribute attribute in anaphora.Attributes)
            {
                if (attribute is PartOSAttribute)
                {
                    continue;   // ignore
                }
                total.ImproveStrength(attribute.Describes(prime), ref strengthFactor);
            }

            // Check if subphrases could be anaphors for the given phrase
            foreach (InformedPhrase subphrase in anaphora.Phrases)
            {
                total.ImproveStrength(AnaphoraOf(subphrase, prime).DownWeight(1.0 / anaphora.Phrases.Count), ref strengthFactor);
            }

            total.ImproveStrengthFinish(strengthFactor);
            return(total);
        }
 // Base constructor: just stores "how strong" the attribute is
 public PhraseAttribute(double strength, double weight)
 {
     this.strength = new ProbableStrength(strength, weight);
 }
        // Wrapper on IsMatch to apply the weighting
        public ProbableStrength Match(PhraseAttribute attribute)
        {
            ProbableStrength match = IsMatch(attribute);

            return(new ProbableStrength(match.strength, match.weight * (strength.weight + attribute.strength.weight) / 2.0));
        }
 public void Deserialize(SerializationReader reader)
 {
     strength = (ProbableStrength)reader.ReadPointer();
 }