// 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); }
void IFastSerializable.Deserialize(SerializationReader reader) { base.Deserialize(reader); string name = reader.ReadString(); part = SpeechPart.GetPart(name); }
// 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); }
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); } }
// 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)); }
// 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)); }
// 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); }
// 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); }
// 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")); } }
// 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 }
// 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]); }
// 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)); } } }
// 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); }
// 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]; }
// 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); } }
// 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; }
// Does this phrase match a given part of speech? public static ProbableStrength SeemsA(InformedPhrase phrase, SpeechPart part) { return((new PartOSAttribute(part)).Describes(phrase)); }
// Encapsulate a given part public PartOSAttribute(SpeechPart part) : base(1, 1) { this.part = part; }
// Look up the speech part based on its string public PartOSAttribute(string name) : base(1, 1) { this.part = SpeechPart.GetPart(name); }
// 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 }
// 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"); }
// Does this phrase match a given part of speech? public static ProbableStrength SeemsA(InformedPhrase phrase, SpeechPart part) { return (new PartOSAttribute(part)).Describes(phrase); }