/// <summary> /// Change the <see cref="Gloss"/> for the last added segment. /// This method is intended for use with Collection Initializers. /// </summary> /// <param name="gloss"></param> public void Add(Gloss gloss) { if (_Frozen) { throw new InvalidOperationException("Modifying gloss of a frozen GlossyString."); } if (LastAdded < 0) { throw new InvalidOperationException("Modifying gloss of a GlossyString without a valid segment."); } var last = LastAdded; LastAdded = -1; if (last == 0) { return; } var p = Pairs[Pairs.Count - 1]; if (p.Text.Length == last) { Pairs[Pairs.Count - 1] = new Pair(p.Text, gloss); } else { Pairs[Pairs.Count - 1] = new Pair(p.Text.Substring(0, p.Text.Length - last), p.Gloss); Pairs.Add(new Pair(p.Text.Substring(p.Text.Length - last), gloss)); } }
public GlossyString Append(string text, Gloss gloss) { if (text == null) { throw new ArgumentNullException("text"); } if (_Frozen) { throw new InvalidOperationException("Appending to a frozen GlossyString."); } LastAdded = text.Length; if (text.Length > 0) { if (Pairs.Count > 0 && Pairs[Pairs.Count - 1].Gloss == gloss) { Pairs[Pairs.Count - 1] = new Pair(Pairs[Pairs.Count - 1].Text + text, gloss); } else { Pairs.Add(new Pair(text, gloss)); } } return(this); }
public void TestEqualsNull() { var gloss = new Gloss { Language = Language, Def = Def }; Assert.IsFalse(gloss.Equals(null)); }
public void TestEquals() { var gloss = new Gloss { Language = Language, Def = Def }; Assert.That(gloss.Equals(new Gloss { Language = Language, Def = Def })); }
public override IPalasoDataObjectProperty Clone() { var clone = new LexEtymology(Type, Source); clone.Gloss = (MultiText)Gloss.Clone(); clone.Comment = (MultiText)Comment.Clone(); clone.Traits = new List <LexTrait>(Traits.Select(t => t.Clone())); clone.Fields = new List <LexField>(Fields.Select(f => (LexField)f.Clone())); //copies from MultiText clone.EmbeddedXmlElements = new List <string>(EmbeddedXmlElements); clone.Forms = Forms.Select(f => (LanguageForm)f.Clone()).ToArray(); return(clone); }
/// <summary> /// Create a frozen instance of a single string in a uniform gloss. /// </summary> /// <param name="text"></param> /// <param name="gloss"></param> public GlossyString(string text, Gloss gloss) { if (text == null) { throw new ArgumentNullException("text"); } if (text.Length == 0) { Pairs = new Pair[0]; } else { Pairs = new Pair[] { new Pair(text, gloss) }; } _Frozen = true; }
public bool Equals(LexEtymology other) { if (ReferenceEquals(null, other)) { return(false); } if (ReferenceEquals(this, other)) { return(true); } if (!base.Equals(other)) { return(false); } if (!Gloss.Equals(other.Gloss)) { return(false); } if (!Comment.Equals(other.Comment)) { return(false); } if (!Traits.SequenceEqual(other.Traits)) { return(false); //order matters because we expose a list interface } if (!Fields.SequenceEqual(other.Fields)) { return(false); //order matters because we expose a list interface } if (!Type.Equals(other.Type)) { return(false); } if (!Source.Equals(other.Source)) { return(false); } return(true); }
/// <summary> /// Instantiates the current synset. If idSynset is non-null, related synsets references are set to those from /// idSynset; otherwise, related synsets are created as shells. /// </summary> /// <param name="definition">Definition line of synset from data file</param> /// <param name="idSynset">Lookup for related synsets. If null, all related synsets will be created as shells.</param> internal void Instantiate(string definition, Dictionary <string, SynSet> idSynset) { // don't re-instantiate if (Instantiated) { throw new Exception("Synset has already been instantiated"); } /* get lexicographer file name...the enumeration lines up precisely with the wordnet spec (see the lexnames file) except that * it starts with None, so we need to add 1 to the definition line's value to get the correct file name */ var lexicographerFileNumber = int.Parse(GetField(definition, 1)) + 1; if (lexicographerFileNumber <= 0) { throw new Exception("Invalid lexicographer file name number. Should be >= 1."); } LexicographerFileName = (LexicographerFileName)lexicographerFileNumber; // get number of words in the synset and the start character of the word list int wordStart; var numWords = int.Parse(GetField(definition, 3, out wordStart), NumberStyles.HexNumber); wordStart = definition.IndexOf(' ', wordStart) + 1; // get words in synset Words = new List <string>(numWords); for (var i = 0; i < numWords; ++i) { var wordEnd = definition.IndexOf(' ', wordStart + 1) - 1; var wordLen = wordEnd - wordStart + 1; var word = definition.Substring(wordStart, wordLen); if (word.Contains(' ')) { throw new Exception("Unexpected space in word: " + word); } Words.Add(word); // skip lex_id field wordStart = definition.IndexOf(' ', wordEnd + 2) + 1; } // get gloss Gloss = definition.Substring(definition.IndexOf('|') + 1).Trim(); if (Gloss.Contains('|')) { throw new Exception("Unexpected pipe in gloss"); } // get number and start of relations var relationCountField = 3 + (Words.Count * 2) + 1; int relationFieldStart; var numRelations = int.Parse(GetField(definition, relationCountField, out relationFieldStart)); relationFieldStart = definition.IndexOf(' ', relationFieldStart) + 1; // grab each related synset relationSynSets = new Dictionary <SynSetRelation, List <SynSet> >(); lexicalRelations = new Dictionary <SynSetRelation, Dictionary <SynSet, Dictionary <int, List <int> > > >(); for (var relationNum = 0; relationNum < numRelations; ++relationNum) { string relationSymbol = null; var relatedSynSetOffset = -1; var relatedSynSetPOS = WordNetPos.None; var sourceWordIndex = -1; var targetWordIndex = -1; // each relation has four columns for (var relationField = 0; relationField <= 3; ++relationField) { var fieldEnd = definition.IndexOf(' ', relationFieldStart + 1) - 1; var fieldLen = fieldEnd - relationFieldStart + 1; var fieldValue = definition.Substring(relationFieldStart, fieldLen); // relation symbol if (relationField == 0) { relationSymbol = fieldValue; } // related synset offset else if (relationField == 1) { relatedSynSetOffset = int.Parse(fieldValue); } // related synset POS else if (relationField == 2) { relatedSynSetPOS = GetPos(fieldValue); } // source/target word for lexical relation else if (relationField == 3) { sourceWordIndex = int.Parse(fieldValue.Substring(0, 2), NumberStyles.HexNumber); targetWordIndex = int.Parse(fieldValue.Substring(2), NumberStyles.HexNumber); } else { throw new Exception(); } relationFieldStart = definition.IndexOf(' ', relationFieldStart + 1) + 1; } // get related synset...create shell if we don't have a lookup var relatedSynSet = idSynset != null ? idSynset[relatedSynSetPOS + ":" + relatedSynSetOffset] : new SynSet(relatedSynSetPOS, relatedSynSetOffset, wordNet); // get relation var relation = WordNet.GetSynSetRelation(Pos, relationSymbol); // add semantic relation if we have neither a source nor a target word index if (sourceWordIndex == 0 && targetWordIndex == 0) { relationSynSets.EnsureContainsKey(relation, typeof(List <SynSet>)); relationSynSets[relation].Add(relatedSynSet); } // add lexical relation else { lexicalRelations.EnsureContainsKey(relation, typeof(Dictionary <SynSet, Dictionary <int, List <int> > >)); lexicalRelations[relation].EnsureContainsKey(relatedSynSet, typeof(Dictionary <int, List <int> >)); lexicalRelations[relation][relatedSynSet].EnsureContainsKey(sourceWordIndex, typeof(List <int>)); if (!lexicalRelations[relation][relatedSynSet][sourceWordIndex].Contains(targetWordIndex)) { lexicalRelations[relation][relatedSynSet][sourceWordIndex].Add(targetWordIndex); } } } Instantiated = true; }
public int CompareTo(Localization other) { return(Gloss.CompareTo(other.Gloss)); }
public Pair(string text, Gloss gloss) { Text = text; Gloss = gloss; }
public void HaveIdenticalGlossTest() { const string def = "YesGloss"; const string lang = "YesLang"; var glossYY = new Gloss { Def = def, Language = lang }; var glossYN = new Gloss { Def = def, Language = "NoLang" }; var glossNY = new Gloss { Def = "NoGloss", Language = lang }; var senseEmpty = new Sense { Glosses = new List <Gloss> { new Gloss() } }; var senseEmptyGYY = new Sense { Glosses = new List <Gloss> { new Gloss(), glossYY } }; var senseEmptyGNYGYY = new Sense { Glosses = new List <Gloss> { new Gloss(), glossNY, glossYY } }; var senseGYNGNY = new Sense { Glosses = new List <Gloss> { glossYN, glossNY } }; var wordWithOnlyGYY = new Word { Senses = new List <Sense> { new Sense(), senseEmpty, senseEmptyGYY } }; var wordAlsoWithGYY = new Word { Senses = new List <Sense> { senseGYNGNY, new Sense(), senseEmptyGNYGYY, senseEmpty } }; var wordWithoutGYY = new Word { Senses = new List <Sense> { senseEmpty, senseGYNGNY, new Sense() } }; Assert.IsFalse(DuplicateFinder.HaveIdenticalGloss(new Word(), new Word())); Assert.IsFalse(DuplicateFinder.HaveIdenticalGloss(new Word(), wordWithOnlyGYY)); Assert.IsFalse(DuplicateFinder.HaveIdenticalGloss(wordWithoutGYY, new Word())); Assert.IsFalse(DuplicateFinder.HaveIdenticalGloss(wordWithOnlyGYY, wordWithoutGYY)); Assert.IsTrue(DuplicateFinder.HaveIdenticalGloss(wordWithOnlyGYY, wordAlsoWithGYY)); Assert.IsTrue(DuplicateFinder.HaveIdenticalGloss(wordAlsoWithGYY, wordWithOnlyGYY)); }