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 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); }
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; }
// This is specific for when we know we want to recognize one of us public static ProbableStrength DescribesOneOfUs(PhraseSense sense) { PhraseAttribute attr = sense.FindAttribute(typeof(GenderAttribute)); if (attr != null && ((GenderAttribute)attr).gender == GenderOptions.OneOfUs) { return(ProbableStrength.Full); } return(ProbableStrength.Zero); }
public bool IsContained(PhraseSense sense) { // Look through each phrase foreach (KeyValuePair <PhraseSense, double> mysense in senses) { if (mysense.Key.IsContained(sense)) { return(true); } } return(false); }
public void Deserialize(SerializationReader reader) { name = reader.ReadString(); int count = reader.ReadInt32(); senses = new List <KeyValuePair <PhraseSense, double> >(count); for (int ii = 0; ii < count; ii++) { PhraseSense sense = (PhraseSense)reader.ReadPointer(); double weight = reader.ReadDouble(); senses.Add(new KeyValuePair <PhraseSense, double>(sense, weight)); } }
// 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); }
public bool IsContained(PhraseSense sense) { if (IsIdentical(sense)) { return(true); // a kind of contains } foreach (InformedPhrase subphrase in phrases) { if (subphrase.IsContained(sense)) { return(true); } } return(false); }
public static List <PhraseAttribute> OverrideAttributes(PhraseSense sense, List <PhraseAttribute> attrs) { if (sense == null) { return(attrs); } foreach (PhraseAttribute attr in attrs) { PhraseAttribute already = sense.FindAttribute(attr.GetType()); sense.Attributes.Remove(already); sense.Attributes.Add(attr); } return(sense.Attributes); }
// 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; }
// 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 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; }
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); }
public static List<PhraseAttribute> OverrideAttributes(PhraseSense sense, List<PhraseAttribute> attrs) { if (sense == null) return attrs; foreach (PhraseAttribute attr in attrs) { PhraseAttribute already = sense.FindAttribute(attr.GetType()); sense.Attributes.Remove(already); sense.Attributes.Add(attr); } return sense.Attributes; }
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); }
public bool IsContained(PhraseSense sense) { // Look through each phrase foreach (KeyValuePair<PhraseSense, double> mysense in senses) if (mysense.Key.IsContained(sense)) return true; return false; }
// 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; }
public bool IsContained(PhraseSense sense) { if (IsIdentical(sense)) return true; // a kind of contains foreach (InformedPhrase subphrase in phrases) if (subphrase.IsContained(sense)) return true; return false; }
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); }
// 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); } }
// This is specific for when we know we want to recognize one of us public static ProbableStrength DescribesOneOfUs(PhraseSense sense) { PhraseAttribute attr = sense.FindAttribute(typeof(GenderAttribute)); if (attr != null && ((GenderAttribute)attr).gender == GenderOptions.OneOfUs) return ProbableStrength.Full; return ProbableStrength.Zero; }