Exemple #1
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);
            }
        }
        public override PhraseAttribute Guess(InformedPhrase word)
        {
            List <KeyValuePair <SpeechPart, double> > kvparts = word.SpeechParts();

            if (kvparts.Count != 1)
            {
                GenderAttribute attr = new GenderAttribute(GenderOptions.Neuter);
                attr.strength = new ProbableStrength(0.0, 0.0); // we don't know anything
                return(attr);
            }

            // If it's a proper noun, we guess it might be a human
            SpeechPart part = kvparts[0].Key;

            if (part == SpeechPart.ProperNoun)
            {
                GenderAttribute attr = new GenderAttribute(GenderOptions.Human);
                attr.strength = new ProbableStrength(1.0, 0.5);
                return(attr);
            }
            else
            {
                GenderAttribute attr = new GenderAttribute(GenderOptions.Neuter);
                attr.strength = new ProbableStrength(1.0, 0.1);
                return(attr);
            }
        }
        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);
        }
Exemple #4
0
        public InformedPhrase ReplaceByName(string before, InformedPhrase after)
        {
            if (name == before)
            {
                return(after);
            }

            List <KeyValuePair <PhraseSense, double> > aftersenses = new List <KeyValuePair <PhraseSense, double> >();

            foreach (KeyValuePair <PhraseSense, double> sense in senses)
            {
                // Recurse on each subphrase
                List <InformedPhrase> afterphrases = new List <InformedPhrase>();
                foreach (InformedPhrase subphrase in sense.Key.Phrases)
                {
                    afterphrases.Add(subphrase.ReplaceByName(before, after));
                }

                PhraseSense aftersense = new PhraseSense(sense.Key.Definition, afterphrases, sense.Key.Attributes);
                aftersenses.Add(new KeyValuePair <PhraseSense, double>(aftersense, sense.Value));
            }

            string aftername = " " + name + " ";

            aftername = aftername.Replace(" " + before + " ", " " + after.name + " ").Trim();
            return(new InformedPhrase(aftername, aftersenses));
        }
Exemple #5
0
        // Is this phrase identical to ours?
        public bool IsIdentical(InformedPhrase other)
        {
            if (name != other.name || senses.Count != other.senses.Count)
            {
                return(false);
            }

            // Each sense must match one in the other
            foreach (KeyValuePair <PhraseSense, double> sense in senses)
            {
                bool found = false;
                foreach (KeyValuePair <PhraseSense, double> othersense in other.senses)
                {
                    if (sense.Key.IsIdentical(othersense.Key))
                    {
                        found = true;
                        break;
                    }
                }

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

            return(true);
        }
Exemple #6
0
        // If the replacement is found in any of the senses, that's used
        public List <string> GenerateWithReplacement(InformedPhrase before, List <string> after, ref bool found)
        {
            // Replace!
            if (this == before)
            {
                return(after);
            }

            // Just plug in the word
            if (IsTerminal)
            {
                List <string> single = new List <string>();
                single.Add(name);
                return(single);
            }

            List <string> result = new List <string>();

            // Try this on each sense, looking for the replacement
            foreach (KeyValuePair <PhraseSense, double> sense in senses)
            {
                // Recurse on each subphrase
                result = new List <string>();
                foreach (InformedPhrase subphrase in sense.Key.Phrases)
                {
                    result.AddRange(subphrase.GenerateWithReplacement(before, after, ref found));
                }
                if (found || before == null)
                {
                    return(result);  // if the replacement was found, use this result
                }
            }

            return(result);
        }
        // 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;
        }
        // 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);
        }
Exemple #10
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);
        }
Exemple #11
0
        public static ProbableStrength SeemsReferencialVerb(InformedPhrase phrase)
        {
            if (phrase.IsTerminal)
            {
                string verb = phrase.Name.ToLower();
                if (verb == "do" || verb == "did" || verb == "does" || verb == "done" || verb == "doing")
                {
                    return(ProbableStrength.Full);
                }
                if ((verb == "have" || verb == "had" || verb == "has" || verb == "having") ||
                    (verb == "was" || verb == "were" || verb == "am" || verb == "is" || verb == "are" || verb == "being" || verb == "been" || verb == "be"))
                {
                    return(ProbableStrength.Half);
                }

                return(ProbableStrength.Zero);
            }
            else
            {
                if (phrase.Senses.Count == 1)
                {
                    KeyValuePair <PhraseSense, double> sense = phrase.Senses[0];
                    if (sense.Key.Phrases.Count == 1)
                    {
                        return(SeemsReferencialVerb(sense.Key.Phrases[0]));
                    }
                }

                // Could be, but not so simple!
                return(ProbableStrength.None);
            }
        }
Exemple #12
0
        // Merge new sense structures into existing ones
        public InformedPhrase Merge(InformedPhrase other)
        {
            if (this == other || other == null)
            {
                return(this);
            }

            InformedPhrase merged = new InformedPhrase(name, senses);

            // Add each sense
            foreach (KeyValuePair <PhraseSense, double> othersense in other.senses)
            {
                // Does this sense already exist in our list?
                bool found = false;
                foreach (KeyValuePair <PhraseSense, double> sense in senses)
                {
                    if (sense.Key.IsIdentical(othersense.Key))
                    {
                        if (sense.Value == othersense.Value)
                        {
                            found = true;
                            break;
                        }
                    }
                }

                if (!found)
                {
                    merged.senses.Add(othersense);
                }
            }

            return(merged);
        }
        // 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;
        }
        public bool IsContained(InformedPhrase phrase)
        {
            foreach (InformedPhrase subphrase in phrases)
            {
                if (subphrase.IsContained(phrase))
                {
                    return(true);
                }
            }

            return(false);
        }
Exemple #15
0
        // Is this phrase a subphrase of us?
        public bool IsContained(InformedPhrase phrase)
        {
            if (IsIdentical(phrase))
            {
                return(true);    // a kind of contains
            }
            // Look through each phrase
            foreach (KeyValuePair <PhraseSense, double> sense in senses)
            {
                if (sense.Key.IsContained(phrase))
                {
                    return(true);
                }
            }

            return(false);
        }
        // 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;
        }
 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;
     }
 }
Exemple #18
0
 // Inform everything that looks like a name
 public void GenderInformAll(InformedPhrase phrase, IWordLookup informer)
 {
     foreach (KeyValuePair <PhraseSense, double> sense in phrase.Senses)
     {
         // Could this be a person?  Have to check all noun phrases, because DummyEnglishParser doesn't properly specify Nouns
         if (nounparts.Contains(sense.Key.SpeechPart()))
         {
             GenderInform(sense.Key, informer);
         }
         else
         {
             // Otherwise drill down
             foreach (InformedPhrase subphr in sense.Key.Phrases)
             {
                 GenderInformAll(subphr, informer);
             }
         }
     }
 }
Exemple #19
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);
        }
Exemple #21
0
        // Is this actual object in our structure?
        public bool IsObjectContained(InformedPhrase phrase)
        {
            if (Equals(phrase))
            {
                return(true);    // a kind of contains
            }
            // Look through each phrase
            foreach (KeyValuePair <PhraseSense, double> sense in senses)
            {
                foreach (InformedPhrase subphrase in sense.Key.Phrases)
                {
                    if (subphrase.IsObjectContained(phrase))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Exemple #22
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);
        }
Exemple #23
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));
        }
        public bool IsContained(InformedPhrase phrase)
        {
            foreach (InformedPhrase subphrase in phrases)
                if (subphrase.IsContained(phrase))
                    return true;

            return false;
        }
 // What does it look like the attribute for this phrase is?  Consider overriding this.
 public virtual PhraseAttribute Guess(InformedPhrase word)
 {
     return new PhraseAttribute(0, 0);
 }
        // Is this actual object in our structure?
        public bool IsObjectContained(InformedPhrase phrase)
        {
            if (Equals(phrase))
                return true;    // a kind of contains

            // Look through each phrase
            foreach (KeyValuePair<PhraseSense, double> sense in senses)
                foreach (InformedPhrase subphrase in sense.Key.Phrases)
                    if (subphrase.IsObjectContained(phrase))
                        return true;

            return false;
        }
        // Merge new sense structures into existing ones
        public InformedPhrase Merge(InformedPhrase other)
        {
            if (this == other || other == null)
                return this;

            InformedPhrase merged = new InformedPhrase(name, senses);

            // Add each sense
            foreach (KeyValuePair<PhraseSense, double> othersense in other.senses)
            {
                // Does this sense already exist in our list?
                bool found = false;
                foreach (KeyValuePair<PhraseSense, double> sense in senses)
                    if (sense.Key.IsIdentical(othersense.Key))
                        if (sense.Value == othersense.Value)
                        {
                            found = true;
                            break;
                        }

                if (!found)
                    merged.senses.Add(othersense);
            }

            return merged;
        }
 // Does this phrase match a given part of speech?
 public static ProbableStrength SeemsA(InformedPhrase phrase, SpeechPart part)
 {
     return (new PartOSAttribute(part)).Describes(phrase);
 }
        // Is this phrase identical to ours?
        public bool IsIdentical(InformedPhrase other)
        {
            if (name != other.name || senses.Count != other.senses.Count)
                return false;

            // Each sense must match one in the other
            foreach (KeyValuePair<PhraseSense, double> sense in senses)
            {
                bool found = false;
                foreach (KeyValuePair<PhraseSense, double> othersense in other.senses)
                    if (sense.Key.IsIdentical(othersense.Key))
                    {
                        found = true;
                        break;
                    }

                if (!found)
                    return false;
            }

            return true;
        }
 // What does it look like the attribute for this phrase is?  Consider overriding this.
 public virtual PhraseAttribute Guess(InformedPhrase word)
 {
     return(new PhraseAttribute(0, 0));
 }
        public InformedPhrase ReplaceByName(string before, InformedPhrase after)
        {
            if (name == before)
                return after;

            List<KeyValuePair<PhraseSense, double>> aftersenses = new List<KeyValuePair<PhraseSense, double>>();
            foreach (KeyValuePair<PhraseSense, double> sense in senses)
            {
                // Recurse on each subphrase
                List<InformedPhrase> afterphrases = new List<InformedPhrase>();
                foreach (InformedPhrase subphrase in sense.Key.Phrases)
                    afterphrases.Add(subphrase.ReplaceByName(before, after));

                PhraseSense aftersense = new PhraseSense(sense.Key.Definition, afterphrases, sense.Key.Attributes);
                aftersenses.Add(new KeyValuePair<PhraseSense, double>(aftersense, sense.Value));
            }

            string aftername = " " + name + " ";
            aftername = aftername.Replace(" " + before + " ", " " + after.name + " ").Trim();
            return new InformedPhrase(aftername, aftersenses);
        }
        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;
        }
        public static ProbableStrength SeemsReferencialVerb(InformedPhrase phrase)
        {
            if (phrase.IsTerminal)
            {
                string verb = phrase.Name.ToLower();
                if (verb == "do" || verb == "did" || verb == "does" || verb == "done" || verb == "doing")
                    return ProbableStrength.Full;
                if ((verb == "have" || verb == "had" || verb == "has" || verb == "having") ||
                    (verb == "was" || verb == "were" || verb == "am" || verb == "is" || verb == "are" || verb == "being" || verb == "been" || verb == "be"))
                    return ProbableStrength.Half;

                return ProbableStrength.Zero;
            }
            else
            {
                if (phrase.Senses.Count == 1)
                {
                    KeyValuePair<PhraseSense, double> sense = phrase.Senses[0];
                    if (sense.Key.Phrases.Count == 1)
                        return SeemsReferencialVerb(sense.Key.Phrases[0]);
                }

                // Could be, but not so simple!
                return ProbableStrength.None;
            }
        }
Exemple #34
0
        // Given a noun phrase sense
        public void GenderInform(PhraseSense sense, IWordLookup informer)
        {
            // Drill down to the first noun word, looking for a current attribute
            PhraseSense     first  = sense;
            GenderAttribute bypart = (GenderAttribute)first.FindAttribute(typeof(GenderAttribute));

            while (bypart == null)
            {
                // Find a noun sub-phrase
                PhraseSense next = null;
                foreach (InformedPhrase subfirst in first.Phrases)
                {
                    foreach (KeyValuePair <PhraseSense, double> firstsense in subfirst.Senses)
                    {
                        if (nounparts.Contains(firstsense.Key.SpeechPart()))
                        {
                            next = firstsense.Key;
                            break;
                        }
                    }
                    if (next != null)
                    {
                        break;
                    }
                }
                if (next == null)
                {
                    break;
                }
                bypart = (GenderAttribute)next.FindAttribute(typeof(GenderAttribute));

                if (next.Phrases.Count == 0)
                {
                    break;
                }
                first = next;
            }

            if (first.SpeechPart() != SpeechPart.ProperNoun)
            {
                if (bypart != null && bypart.Strength.weight > 0.5)
                {
                    return;  // we seem to know!
                }
                List <string> words;
                if (first.Phrases.Count > 0)
                {
                    words = first.Phrases[0].Generate();
                }
                else
                {
                    words = new List <string>();
                    words.Add(first.Name());
                }
                if (informer.GetWeight(words[0], false) < 0.5)
                {
                    return;  // not worth the lookup
                }
                if (bypart == null)
                {
                    GenderAttribute dummy = new GenderAttribute();
                    bypart = (GenderAttribute)dummy.Guess(sense.Phrases[0]);
                }
            }

            // Now guess using the given name
            InformedPhrase tocheck;

            if (first.Phrases.Count > 0)
            {
                tocheck = first.Phrases[0];
            }
            else
            {
                List <KeyValuePair <PhraseSense, double> > senses = new List <KeyValuePair <PhraseSense, double> >();
                senses.Add(new KeyValuePair <PhraseSense, double>(first, 1.0));
                tocheck = new InformedPhrase(sense.Name(), senses);
            }
            GenderAttribute gender = GuessGender(tocheck, informer);

            if (bypart != null)
            {
                gender = MergeGenderAttributes(gender, bypart);
            }

            // Update relevant gender attributes
            sense.AddOrUpdateAttribute(gender);
            if (sense != first)
            {
                first.AddOrUpdateAttribute(gender);
            }
        }
 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;
     }
 }
Exemple #36
0
 // Does this phrase match a given part of speech?
 public static ProbableStrength SeemsA(InformedPhrase phrase, SpeechPart part)
 {
     return((new PartOSAttribute(part)).Describes(phrase));
 }
        public override PhraseAttribute Guess(InformedPhrase word)
        {
            List<KeyValuePair<SpeechPart, double>> kvparts = word.SpeechParts();
            if (kvparts.Count != 1)
            {
                GenderAttribute attr = new GenderAttribute(GenderOptions.Neuter);
                attr.strength = new ProbableStrength(0.0, 0.0); // we don't know anything
                return attr;
            }

            // If it's a proper noun, we guess it might be a human
            SpeechPart part = kvparts[0].Key;
            if (part == SpeechPart.ProperNoun)
            {
                GenderAttribute attr = new GenderAttribute(GenderOptions.Human);
                attr.strength = new ProbableStrength(1.0, 0.5);
                return attr;
            }
            else
            {
                GenderAttribute attr = new GenderAttribute(GenderOptions.Neuter);
                attr.strength = new ProbableStrength(1.0, 0.1);
                return attr;
            }
        }
        // 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;
        }
        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;
        }
        // If the replacement is found in any of the senses, that's used
        public List<string> GenerateWithReplacement(InformedPhrase before, List<string> after, ref bool found)
        {
            // Replace!
            if (this == before)
                return after;

            // Just plug in the word
            if (IsTerminal)
            {
                List<string> single = new List<string>();
                single.Add(name);
                return single;
            }

            List<string> result = new List<string>();
            // Try this on each sense, looking for the replacement
            foreach (KeyValuePair<PhraseSense, double> sense in senses)
            {
                // Recurse on each subphrase
                result = new List<string>();
                foreach (InformedPhrase subphrase in sense.Key.Phrases)
                    result.AddRange(subphrase.GenerateWithReplacement(before, after, ref found));
                if (found || before == null)
                    return result;  // if the replacement was found, use this result
            }

            return result;
        }
        public static List <PhraseAttribute> TreebankToAttributes(string part, IWordLookup informer, string word, bool simplified)
        {
            List <PhraseAttribute> attributes = new List <PhraseAttribute>();
            InformedPhrase         informed   = (informer == null || word == null || word.Contains(" ")) ? null : informer.GetInformed(word, false);

            if (part == "NN")
            {
                attributes.Add(new PartOSAttribute(Noun));
                attributes.Add(new NumberAttribute(NumberAttribute.NumberOptions.One));
                PhraseSense sense = informed != null?informed.FindSense(SpeechPart.Noun, true) : null;

                return(OverrideAttributes(sense, attributes));
            }

            if (part == "NNS")
            {
                attributes.Add(new PartOSAttribute(Noun));
                attributes.Add(new NumberAttribute(NumberAttribute.NumberOptions.Many));
                PhraseSense sense = informed != null?informed.FindSense(SpeechPart.Noun, false) : null;

                return(OverrideAttributes(sense, attributes));
            }

            if (part == "NNP")
            {
                attributes.Add(new PartOSAttribute(ProperNoun));
                attributes.Add(new NumberAttribute(NumberAttribute.NumberOptions.One));
                return(attributes);
            }

            if (part == "NNPS")
            {
                attributes.Add(new PartOSAttribute(ProperNoun));
                attributes.Add(new NumberAttribute(NumberAttribute.NumberOptions.Many));
                return(attributes);
            }

            if (part == "PRP" || part == "PRP$" || part == "WDT" || part == "WP" || part == "WP$" || part == "WDT" || part == "WRB")
            {
                if (informer == null || word == null || informed == null)
                {
                    if (part == "PRP")
                    {
                        attributes.Add(new PartOSAttribute(PersonalPronoun));
                    }
                    else if (part == "PRP$")
                    {
                        attributes.Add(new PartOSAttribute(PossessivePronoun));
                    }
                    else if (part == "WP")
                    {
                        attributes.Add(new PartOSAttribute(WhPronoun));
                    }
                    else if (part == "WP$")
                    {
                        attributes.Add(new PartOSAttribute(PossiveWhPronoun));
                    }
                    else
                    {
                        attributes.Add(new PartOSAttribute(part));
                    }
                    return(attributes);
                }

                PhraseSense sense = informed.FindSense(PersonalPronoun, true);
                if (sense == null)
                {
                    sense = informed.FindSense(ArticulatePronoun, true);
                }
                if (sense == null)
                {
                    sense = informed.Senses[0].Key;
                }

                return(sense.Attributes);
            }

            // Convert phrases
            if (part.StartsWith("NP"))
            {
                part = "NN_P" + part.Substring(2);
            }
            else if (part.StartsWith("VP"))
            {
                part = "VB_P" + part.Substring(2);
            }
            else if (part.StartsWith("PP"))
            {
                part = "IN_P" + part.Substring(2);
            }

            if (simplified)
            {
                int dash = part.IndexOf('-');
                if (dash > 0)
                {
                    part = part.Substring(0, dash);
                }
            }

            if (!catalog.ContainsKey(part))
            {
                attributes.Add(new PartOSAttribute(new SpeechPart(part)));
            }
            else
            {
                attributes.Add(new PartOSAttribute(part));
            }
            return(attributes);
        }
        // Is this phrase a subphrase of us?
        public bool IsContained(InformedPhrase phrase)
        {
            if (IsIdentical(phrase))
                return true;    // a kind of contains

            // Look through each phrase
            foreach (KeyValuePair<PhraseSense, double> sense in senses)
                if (sense.Key.IsContained(phrase))
                    return true;

            return false;
        }