Beispiel #1
0
        public static bool TryMergeEntries(LexEntry entry1, LexEntry entry2, string[] traitsWithMultiplicity, IProgress progress)
        {
            if (!entry1.LexicalForm.CanBeUnifiedWith(entry2.LexicalForm))
            {
                progress.WriteMessageWithColor("gray", "Attempting to merge entries, but could not because their Lexical Forms clash in some writing system.");
                return(false);
            }

            if (!SenseMerger.TryMergeProperties(entry1, entry2, traitsWithMultiplicity, "entries for " + entry1.ToString(), progress))
            {
                return(false);
            }

            // at this point, we're committed to doing the merge

            entry1.LexicalForm.MergeIn(entry2.LexicalForm);

            var senses = entry2.Senses.ToArray();

            foreach (var sense in senses)
            {
                MergeOrAddSense(entry1, sense, traitsWithMultiplicity, progress);
            }

            if (entry2.ModificationTime > entry1.ModificationTime)
            {
                entry1.ModificationTime = entry2.ModificationTime;
            }

            entry1.IsDirty = true;
            return(true);
        }
Beispiel #2
0
        /// <summary>
        /// it can happen that within a single entry, you can have mergable senses.
        /// </summary>
        private static void MergeSensesWithinEntries(LiftLexEntryRepository repo, string[] traitsWithMultiplicity, IProgress progress)
        {
            var ids = new List <RepositoryId>(repo.GetAllItems());

            foreach (var id in ids)
            {
                if (progress.CancelRequested)
                {
                    throw new OperationCanceledException("User cancelled");
                }
                var entry  = repo.GetItem(id);
                var senses = entry.Senses.ToArray();
                if (senses.Length < 2)
                {
                    continue;
                }
                var sensesToRemove = new List <LexSense>();
                foreach (var sense in entry.Senses)
                {
                    if (sensesToRemove.Any(s => ReferenceEquals(s, sense)))
                    {
                        continue;
                    }
                    foreach (var otherSense in entry.Senses)
                    {
                        if (ReferenceEquals(otherSense, sense))                         // Don't try and compare with ourself.
                        {
                            continue;
                        }
                        if (sensesToRemove.Any(s => ReferenceEquals(s, sense)))
                        {
                            continue;
                        }
                        if (!SenseMerger.TryMergeSenseWithSomeExistingSense(sense, otherSense, traitsWithMultiplicity, progress))
                        {
                            continue;
                        }
                        sensesToRemove.Add(otherSense);
                    }
                }
                foreach (var sense in sensesToRemove)
                {
                    entry.Senses.Remove(sense);
                    entry.IsDirty = true;
                }
                if (entry.IsDirty)
                {
                    repo.SaveItem(entry);
                }
            }
        }
Beispiel #3
0
 private static void MergeOrAddSense(LexEntry targetEntry, LexSense incomingSense, string[] traitsWithMultiplicity, IProgress progress)
 {
     if (targetEntry.Senses.Count == 0)
     {
         targetEntry.Senses.Add(incomingSense);                //no problem!
     }
     else
     {
         if (targetEntry.Senses.Count == 1)
         {
             var targetSense = targetEntry.Senses[0];
             if (SenseMerger.TryMergeSenseWithSomeExistingSense(targetSense, incomingSense, traitsWithMultiplicity, progress))
             {
                 //it was merged in
                 return;
             }
         }
     }
     //it needs to be added
     targetEntry.Senses.Add(incomingSense);
 }