コード例 #1
0
 // Non-terminal phrase, with a single sense
 public InformedPhrase(string definition, SpeechPart part, List<InformedPhrase> phrases)
 {
     senses = new List<KeyValuePair<PhraseSense, double>>();
     KeyValuePair<PhraseSense, double> sense = new KeyValuePair<PhraseSense, double>(new PhraseSense(definition, part, phrases), 1.0);
     this.name = sense.Key.Name();
     senses.Add(sense);
 }
コード例 #2
0
        void IFastSerializable.Deserialize(SerializationReader reader)
        {
            base.Deserialize(reader);
            string name = reader.ReadString();

            part = SpeechPart.GetPart(name);
        }
コード例 #3
0
 // Single word phrase, with a single part of speech and definition
 public InformedPhrase(string name, string definition, SpeechPart part)
 {
     this.name = name;
     this.senses = new List<KeyValuePair<PhraseSense, double>>();
     KeyValuePair<PhraseSense, double> sense = new KeyValuePair<PhraseSense, double>(new PhraseSense(definition, part), 1.0);
     senses.Add(sense);
 }
コード例 #4
0
        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);
            }
        }
コード例 #5
0
 // Ctor of a non-terminal node
 public PhraseSense(string definition, SpeechPart part, List<InformedPhrase> phrases)
 {
     this.definition = definition;
     this.phrases = phrases;
     attributes = new List<PhraseAttribute>();
     attributes.Add(new PartOSAttribute(part));
 }
コード例 #6
0
 // Ctor of a non-terminal node
 public PhraseSense(string definition, SpeechPart part, List <InformedPhrase> phrases)
 {
     this.definition = definition;
     this.phrases    = phrases;
     attributes      = new List <PhraseAttribute>();
     attributes.Add(new PartOSAttribute(part));
 }
コード例 #7
0
        // Non-terminal phrase, with a single sense
        public InformedPhrase(string definition, SpeechPart part, List <InformedPhrase> phrases)
        {
            senses = new List <KeyValuePair <PhraseSense, double> >();
            KeyValuePair <PhraseSense, double> sense = new KeyValuePair <PhraseSense, double>(new PhraseSense(definition, part, phrases), 1.0);

            this.name = sense.Key.Name();
            senses.Add(sense);
        }
コード例 #8
0
        // Single word phrase, with a single part of speech and definition
        public InformedPhrase(string name, string definition, SpeechPart part)
        {
            this.name   = name;
            this.senses = new List <KeyValuePair <PhraseSense, double> >();
            KeyValuePair <PhraseSense, double> sense = new KeyValuePair <PhraseSense, double>(new PhraseSense(definition, part), 1.0);

            senses.Add(sense);
        }
コード例 #9
0
 // Find the phrase part of speech characteristic of this part
 public static SpeechPart PhraseOf(SpeechPart part)
 {
     if (part.name.EndsWith("_P"))
     {
         return(part);
     }
     else
     {
         return(GetPart(part.name.Substring(0, 2) + "_P"));
     }
 }
コード例 #10
0
        // Find a sense of the given speech part
        public PhraseSense FindSense(SpeechPart part, bool allSubs)
        {
            foreach (KeyValuePair <PhraseSense, double> sense in senses)
            {
                PartOSAttribute partAttribute = (PartOSAttribute)sense.Key.FindAttribute(typeof(PartOSAttribute));
                if (partAttribute.Part.SeemsA(part, allSubs).IsLikely(.5))
                {
                    return(sense.Key);
                }
            }

            return(null);    // not found
        }
コード例 #11
0
        // Find the known part of speech
        public static SpeechPart GetPart(string name)
        {
            if (catalog.ContainsKey(name))
            {
                return(catalog[name]);
            }
            string underscoreName = name.Insert(name.Length - 1, "_");

            if (catalog.ContainsKey(underscoreName))
            {
                return(catalog[underscoreName]);
            }
            catalog[name] = new SpeechPart(name);
            return(catalog[name]);
        }
コード例 #12
0
        // Is this part of speech like ours?
        public ProbableStrength SeemsA(SpeechPart part, bool allSubs)
        {
            if (name == part.name || (allSubs && name.StartsWith(part.name)))
            {
                return(ProbableStrength.Full);   // exact match!
            }
            else
            {
                // Look for a partial match
                int totalLength;
                if (allSubs)
                {
                    totalLength = part.name.Length;
                }
                else
                {
                    totalLength = Math.Max(name.Length, part.name.Length);
                }

                double strength = 0;
                // while there are more characters in each
                for (int ii = 0; ii < name.Length && ii < part.name.Length; ii++)
                {
                    if (name[ii] == part.name[ii])
                    {
                        strength += 1.0 / (2 << ii);    // add more score
                    }
                    else
                    {
                        break;
                    }
                }

                double maxScore = 1.0 - 1.0 / (2 << totalLength);

                if (strength / maxScore > .5)
                {
                    return(new ProbableStrength(strength / maxScore - .5, strength / maxScore));
                }
                else
                {
                    return(new ProbableStrength(0, 0.5));
                }
            }
        }
コード例 #13
0
        // Find all phrases with the given speech part
        // Phrases returned may overlap lots
        public List <InformedPhrase> FindPhrases(SpeechPart part, bool allSubs)
        {
            List <InformedPhrase> phrases = new List <InformedPhrase>();

            // Look through each sense
            foreach (KeyValuePair <PhraseSense, double> sense in senses)
            {
                PartOSAttribute partAttribute = (PartOSAttribute)sense.Key.FindAttribute(typeof(PartOSAttribute));
                if (partAttribute.Part.SeemsA(part, allSubs).IsLikely(.5))
                {
                    if (sense.Key.Phrases.Count == 1)
                    {
                        // just add the sub piece, if it matches too
                        List <InformedPhrase> submatches = sense.Key.Phrases[0].FindPhrases(part, allSubs);
                        if (submatches.Count > 0)
                        {
                            phrases.AddRange(submatches);
                        }
                        else if (!phrases.Contains(this))
                        {
                            phrases.Add(this);
                        }
                    }
                    else if (!phrases.Contains(this))
                    {
                        phrases.Add(this);
                    }
                }

                if (sense.Key.Phrases.Count > 1)
                {
                    // Recurse on each phrase
                    foreach (InformedPhrase subphrase in sense.Key.Phrases)
                    {
                        phrases.AddRange(subphrase.FindPhrases(part, allSubs));
                    }
                }
            }

            return(phrases);
        }
コード例 #14
0
 // Find the known part of speech
 public static SpeechPart GetPart(string name)
 {
     if (catalog.ContainsKey(name))
     {
         return catalog[name];
     }
     string underscoreName = name.Insert( name.Length - 1, "_");
     if (catalog.ContainsKey(underscoreName))
     {
         return catalog[underscoreName];
     }
     catalog[name] = new SpeechPart(name);
     return catalog[name];
 }
コード例 #15
0
        // Is this part of speech like ours?
        public ProbableStrength SeemsA(SpeechPart part, bool allSubs)
        {
            if (name == part.name || (allSubs && name.StartsWith(part.name)))
                return ProbableStrength.Full;   // exact match!
            else
            {
                // Look for a partial match
                int totalLength;
                if (allSubs)
                    totalLength = part.name.Length;
                else
                    totalLength = Math.Max(name.Length, part.name.Length);

                double strength = 0;
                // while there are more characters in each
                for (int ii = 0; ii < name.Length && ii < part.name.Length; ii++)
                {
                    if (name[ii] == part.name[ii])
                        strength += 1.0 / (2 << ii);    // add more score
                    else
                        break;
                }

                double maxScore = 1.0 - 1.0 / (2 << totalLength);

                if (strength / maxScore > .5)
                    return new ProbableStrength(strength / maxScore - .5, strength / maxScore);
                else
                    return new ProbableStrength(0, 0.5);
            }
        }
コード例 #16
0
        // Find all phrases with the given speech part
        // Phrases returned may overlap lots
        public List<InformedPhrase> FindPhrases(SpeechPart part, bool allSubs)
        {
            List<InformedPhrase> phrases = new List<InformedPhrase>();

            // Look through each sense
            foreach (KeyValuePair<PhraseSense, double> sense in senses)
            {
                PartOSAttribute partAttribute = (PartOSAttribute)sense.Key.FindAttribute(typeof(PartOSAttribute));
                if (partAttribute.Part.SeemsA(part, allSubs).IsLikely(.5))
                {
                    if (sense.Key.Phrases.Count == 1)
                    {
                        // just add the sub piece, if it matches too
                        List<InformedPhrase> submatches = sense.Key.Phrases[0].FindPhrases(part, allSubs);
                        if (submatches.Count > 0)
                            phrases.AddRange(submatches);
                        else if (!phrases.Contains(this))
                            phrases.Add(this);
                    }
                    else if (!phrases.Contains(this))
                        phrases.Add(this);
                }

                if (sense.Key.Phrases.Count > 1)
                {
                    // Recurse on each phrase
                    foreach (InformedPhrase subphrase in sense.Key.Phrases)
                        phrases.AddRange(subphrase.FindPhrases(part, allSubs));
                }
            }

            return phrases;
        }
コード例 #17
0
 // Does this phrase match a given part of speech?
 public static ProbableStrength SeemsA(InformedPhrase phrase, SpeechPart part)
 {
     return((new PartOSAttribute(part)).Describes(phrase));
 }
コード例 #18
0
 // Encapsulate a given part
 public PartOSAttribute(SpeechPart part)
     : base(1, 1)
 {
     this.part = part;
 }
コード例 #19
0
 // Look up the speech part based on its string
 public PartOSAttribute(string name)
     : base(1, 1)
 {
     this.part = SpeechPart.GetPart(name);
 }
コード例 #20
0
        // Find a sense of the given speech part
        public PhraseSense FindSense(SpeechPart part, bool allSubs)
        {
            foreach (KeyValuePair<PhraseSense, double> sense in senses)
            {
                PartOSAttribute partAttribute = (PartOSAttribute)sense.Key.FindAttribute(typeof(PartOSAttribute));
                if (partAttribute.Part.SeemsA(part, allSubs).IsLikely(.5))
                    return sense.Key;
            }

            return null;    // not found
        }
コード例 #21
0
 // Look up the speech part based on its string
 public PartOSAttribute(string name)
     : base(1, 1)
 {
     this.part = SpeechPart.GetPart(name);
 }
コード例 #22
0
 // Find the phrase part of speech characteristic of this part
 public static SpeechPart PhraseOf(SpeechPart part)
 {
     if (part.name.EndsWith("_P"))
         return part;
     else
         return GetPart(part.name.Substring(0, 2) + "_P");
 }
コード例 #23
0
 void IFastSerializable.Deserialize(SerializationReader reader)
 {
     base.Deserialize(reader);
     string name = reader.ReadString();
     part = SpeechPart.GetPart(name);
 }
コード例 #24
0
 // Does this phrase match a given part of speech?
 public static ProbableStrength SeemsA(InformedPhrase phrase, SpeechPart part)
 {
     return (new PartOSAttribute(part)).Describes(phrase);
 }
コード例 #25
0
 // Encapsulate a given part
 public PartOSAttribute(SpeechPart part)
     : base(1, 1)
 {
     this.part = part;
 }