Beispiel #1
0
 private static void SetVernacularLanguagesByUsage(Interlineartext interlinText)
 {
     foreach (var para in interlinText.paragraphs)
     {
         foreach (var phrase in para.phrases)
         {
             foreach (var item in phrase.Items)
             {
                 if (item.type == "txt")
                 {
                     EnsureVernacularLanguage(interlinText, item.lang);
                 }
             }
             if (phrase.WordsContent.Words != null)
             {
                 foreach (var word in phrase.WordsContent.Words)
                 {
                     foreach (var item in word.Items)
                     {
                         if (item.type == "txt")
                         {
                             EnsureVernacularLanguage(interlinText, item.lang);
                         }
                     }
                     // We could dig into the morphemes, but any client generating morphemes probably
                     // does things right, and anyway we don't import that yet.
                 }
             }
         }
     }
 }
        private static string GetInstructions(Interlineartext interlinText, String wsName, String instructions)
        {
            var strBldr = new StringBuilder(wsName);

            strBldr.Append(instructions);
            strBldr.Append(Environment.NewLine); strBldr.Append(Environment.NewLine);
            strBldr.Append(GetPartOfPhrase(interlinText));
            return(strBldr.ToString());
        }
Beispiel #3
0
 private static bool SomeLanguageSpecifiesVernacular(Interlineartext interlinText)
 {
     foreach (var lang in interlinText.languages.language)
     {
         if (lang.vernacularSpecified)
         {
             return(true);
         }
     }
     return(false);
 }
Beispiel #4
0
 private static void EnsureVernacularLanguage(Interlineartext interlinText, string langName)
 {
     foreach (var lang in interlinText.languages.language)
     {
         if (lang.lang == langName)
         {
             lang.vernacularSpecified = true;
             lang.vernacular          = true;
             return;
         }
     }
 }
Beispiel #5
0
        private static void SetTextMetaAndMedia(FdoCache cache, Interlineartext interlinText, ILgWritingSystemFactory wsFactory, FDO.IText newText)
        {
            if (interlinText.Items != null)             // apparently it is null if there are no items.
            {
                foreach (var item in interlinText.Items)
                {
                    switch (item.type)
                    {
                    case "title":
                        newText.Name.set_String(GetWsEngine(wsFactory, item.lang).Handle, item.Value);
                        break;

                    case "title-abbreviation":
                        newText.Abbreviation.set_String(GetWsEngine(wsFactory, item.lang).Handle, item.Value);
                        break;

                    case "source":
                        newText.Source.set_String(GetWsEngine(wsFactory, item.lang).Handle, item.Value);
                        break;

                    case "comment":
                        newText.Description.set_String(GetWsEngine(wsFactory, item.lang).Handle, item.Value);
                        break;
                    }
                }
            }

            //handle media files section (ELAN initiated) needs to be processed before the paragraphs as segmenets could reference
            //these parts.
            if (interlinText.mediafiles != null)
            {
                var mediaFiles = newText.MediaFilesOA = cache.ServiceLocator.GetInstance <ICmMediaContainerFactory>().Create();
                mediaFiles.OffsetType = interlinText.mediafiles.offsetType;
                foreach (var mediaFile in interlinText.mediafiles.media)
                {
                    var media = cache.ServiceLocator.GetInstance <ICmMediaURIFactory>().Create(cache, new Guid(mediaFile.guid));
                    mediaFiles.MediaURIsOC.Add(media);
                    media.MediaURI = mediaFile.location;
                }
            }
        }
        private static string GetPartOfPhrase(Interlineartext interlinText)
        {
            int i       = 0;
            var strBldr = new StringBuilder(ITextStrings.ksImportLangMissingTextStartsWith);

            foreach (var paragraph in interlinText.paragraphs)
            {
                foreach (var phrase in paragraph.phrases)
                {
                    foreach (var word in phrase.WordsContent.Words)
                    {
                        strBldr.Append(word.Items[0].Value);
                        strBldr.Append(" ");
                        i++;
                        if (i > 6)
                        {
                            strBldr.Append(" ...");
                            return(strBldr.ToString());
                        }
                    }
                }
            }
            return(strBldr.ToString());
        }
Beispiel #7
0
        /// <summary>
        /// This method will create a new Text document from the given BIRD format Interlineartext.
        /// </summary>
        /// <param name="newText">The text to populate, could be set to null.</param>
        /// <param name="textParams"></param>
        private static bool PopulateTextFromBIRDDoc(ref FDO.IText newText, TextCreationParams textParams)
        {
            s_importOptions = textParams.ImportOptions;
            Interlineartext   interlinText = textParams.InterlinText;
            FdoCache          cache        = textParams.Cache;
            IThreadedProgress progress     = textParams.Progress;

            if (s_importOptions.CheckAndAddLanguages == null)
            {
                s_importOptions.CheckAndAddLanguages = CheckAndAddLanguagesInternal;
            }

            ILgWritingSystemFactory wsFactory = cache.WritingSystemFactory;
            char space = ' ';

            //handle the languages(writing systems) section alerting the user if new writing systems are encountered
            if (!s_importOptions.CheckAndAddLanguages(cache, interlinText, wsFactory, progress))
            {
                return(false);
            }

            //handle the header(info or meta) information
            SetTextMetaAndMedia(cache, interlinText, wsFactory, newText);

            //create all the paragraphs
            foreach (var paragraph in interlinText.paragraphs)
            {
                if (newText.ContentsOA == null)
                {
                    newText.ContentsOA = cache.ServiceLocator.GetInstance <IStTextFactory>().Create();
                }
                IStTxtPara newTextPara = newText.ContentsOA.AddNewTextPara("");
                int        offset      = 0;
                if (paragraph.phrases == null)
                {
                    continue;
                }
                foreach (var phrase in paragraph.phrases)
                {
                    ICmObject oldSegment = null;
                    //Try and locate a segment with this Guid.
                    if (!String.IsNullOrEmpty(phrase.guid))
                    {
                        if (cache.ServiceLocator.ObjectRepository.TryGetObject(new Guid(phrase.guid), out oldSegment))
                        {
                            //We aren't merging, but we have this guid in our system, ignore the file Guid
                            oldSegment = cache.ServiceLocator.GetInstance <ISegmentFactory>().Create(newTextPara, offset);
                        }
                        else
                        {
                            //The segment is identified by a Guid, but apparently we don't have it in our current document, so make one with the guid
                            oldSegment = cache.ServiceLocator.GetInstance <ISegmentFactory>().Create(newTextPara, offset, cache,
                                                                                                     new Guid(phrase.guid));
                        }
                    }
                    //set newSegment to the old, or create a brand new one.
                    ISegment newSegment   = oldSegment as ISegment ?? cache.ServiceLocator.GetInstance <ISegmentFactory>().Create(newTextPara, offset);
                    var      tsStrFactory = cache.ServiceLocator.GetInstance <ITsStrFactory>();
                    //Fill in the ELAN time information if it is present.
                    AddELANInfoToSegment(cache, phrase, newSegment);
                    ITsString phraseText = null;
                    bool      textInFile = false;
                    //Add all of the data from <item> elements into the segment.
                    AddSegmentItemData(cache, wsFactory, phrase, newSegment, tsStrFactory, ref textInFile, ref phraseText);
                    bool lastWasWord = false;
                    if (phrase.WordsContent != null && phrase.WordsContent.Words != null)
                    {
                        if (textParams.Version == 0 && PhraseHasExactlyOneTxtItemNotAKnownWordform(newSegment.Cache, phrase))
                        {
                            // It might be a SayMore text that makes the whole segment a single txt item.
                            // We want to add the text anyway (unless a higher level did so), but we will skip making
                            // a wordform. Eventual parsing of the text will do so.
                            if (!textInFile)
                            {
                                UpdatePhraseTextForWordItems(wsFactory, tsStrFactory, ref phraseText, phrase.WordsContent.Words[0], ref lastWasWord, space);
                            }
                        }
                        else
                        {
                            foreach (var word in phrase.WordsContent.Words)
                            {
                                //If the text of the phrase was not given in the document build it from the words.
                                if (!textInFile)
                                {
                                    UpdatePhraseTextForWordItems(wsFactory, tsStrFactory, ref phraseText, word, ref lastWasWord, space);
                                }
                                AddWordToSegment(newSegment, word, tsStrFactory);
                            }
                        }
                    }
                    UpdateParagraphTextForPhrase(newTextPara, ref offset, phraseText);
                }
            }
            return(true);
        }
Beispiel #8
0
 private static bool CheckAndAddLanguagesInternal(FdoCache cache, Interlineartext interlinText, ILgWritingSystemFactory wsFactory, IThreadedProgress progress)
 {
     if (interlinText.languages != null)
     {
         if (!SomeLanguageSpecifiesVernacular(interlinText))
         {
             // Saymore file? something else that doesn't know to do this? We will confuse the user if we try to treat all as analysis.
             SetVernacularLanguagesByUsage(interlinText);
         }
         foreach (var lang in interlinText.languages.language)
         {
             bool fIsVernacular;
             var  writingSystem = SafelyGetWritingSystem(cache, wsFactory, lang, out fIsVernacular);
             if (fIsVernacular)
             {
                 if (!cache.LanguageProject.CurrentVernacularWritingSystems.Contains(writingSystem.Handle))
                 {
                     //we need to invoke the dialog on the main thread so we can use the progress dialog as the parent.
                     //otherwise the message box can be displayed behind everything
                     IAsyncResult asyncResult = progress.ThreadHelper.BeginInvoke(
                         new ShowDialogAboveProgressbarDelegate(ShowDialogAboveProgressbar),
                         new object[]
                     {
                         progress,
                         writingSystem.LanguageName + ITextStrings.ksImportVernacLangMissing,
                         ITextStrings.ksImportVernacLangMissingTitle,
                         MessageBoxButtons.OKCancel
                     });
                     var result = (DialogResult)progress.ThreadHelper.EndInvoke(asyncResult);
                     if (result == DialogResult.OK)
                     {
                         cache.LanguageProject.AddToCurrentVernacularWritingSystems((IWritingSystem)writingSystem);
                     }
                     else
                     {
                         return(false);
                     }
                 }
             }
             else
             {
                 if (!cache.LanguageProject.CurrentAnalysisWritingSystems.Contains(writingSystem.Handle))
                 {
                     IAsyncResult asyncResult = progress.ThreadHelper.BeginInvoke(
                         new ShowDialogAboveProgressbarDelegate(ShowDialogAboveProgressbar),
                         new object[]
                     {
                         progress,
                         writingSystem.LanguageName + ITextStrings.ksImportAnalysisLangMissing,
                         ITextStrings.ksImportAnalysisLangMissingTitle,
                         MessageBoxButtons.OKCancel
                     });
                     var result = (DialogResult)progress.ThreadHelper.EndInvoke(asyncResult);
                     //alert the user
                     if (result == DialogResult.OK)
                     {
                         //alert the user
                         cache.LanguageProject.AddToCurrentAnalysisWritingSystems((IWritingSystem)writingSystem);
                         // We already have progress indications up.
                         XmlTranslatedLists.ImportTranslatedListsForWs(writingSystem.Id, cache, null);
                     }
                     else
                     {
                         return(false);
                     }
                 }
             }
         }
     }
     return(true);
 }
Beispiel #9
0
        private static bool MergeTextWithBIRDDoc(ref FDO.IText newText, TextCreationParams textParams)
        {
            s_importOptions = textParams.ImportOptions;
            Interlineartext   interlinText = textParams.InterlinText;
            FdoCache          cache        = textParams.Cache;
            IThreadedProgress progress     = textParams.Progress;

            if (s_importOptions.CheckAndAddLanguages == null)
            {
                s_importOptions.CheckAndAddLanguages = CheckAndAddLanguagesInternal;
            }

            ILgWritingSystemFactory wsFactory = cache.WritingSystemFactory;
            char space = ' ';

            //handle the languages(writing systems) section alerting the user if new writing systems are encountered
            if (!s_importOptions.CheckAndAddLanguages(cache, interlinText, wsFactory, progress))
            {
                return(false);
            }

            //handle the header(info or meta) information as well as any media-files sections
            SetTextMetaAndMedia(cache, interlinText, wsFactory, newText);

            IStText oldContents = newText.ContentsOA;
            IStText newContents = null;

            //create all the paragraphs NOTE: Currently the paragraph guids are being ignored, this might be wrong.
            foreach (var paragraph in interlinText.paragraphs)
            {
                if (newContents == null)
                {
                    newContents        = cache.ServiceLocator.GetInstance <IStTextFactory>().Create();
                    newText.ContentsOA = newContents;
                }
                IStTxtPara newTextPara = newContents.AddNewTextPara("");
                int        offset      = 0;
                if (paragraph.phrases == null)
                {
                    continue;
                }
                foreach (var phrase in paragraph.phrases)
                {
                    ICmObject oldSegment = null;
                    //Try and locate a segment with this Guid.
                    if (!String.IsNullOrEmpty(phrase.guid))
                    {
                        if (cache.ServiceLocator.ObjectRepository.TryGetObject(new Guid(phrase.guid), out oldSegment))
                        {
                            if (oldSegment as ISegment != null)                             //The segment matches, add it into our paragraph.
                            {
                                newTextPara.SegmentsOS.Add(oldSegment as ISegment);
                            }
                            else if (oldSegment == null)                            //The segment is identified by a Guid, but apparently we don't have it in our current document, so make one
                            {
                                oldSegment = cache.ServiceLocator.GetInstance <ISegmentFactory>().Create(newTextPara, offset, cache, new Guid(phrase.guid));
                            }
                            else                             //The Guid is in use, but not by a segment. This is bad.
                            {
                                return(false);
                            }
                        }
                    }
                    //set newSegment to the old, or create a brand new one.
                    ISegment newSegment   = oldSegment as ISegment ?? cache.ServiceLocator.GetInstance <ISegmentFactory>().Create(newTextPara, offset);
                    var      tsStrFactory = cache.ServiceLocator.GetInstance <ITsStrFactory>();
                    //Fill in the ELAN time information if it is present.
                    AddELANInfoToSegment(cache, phrase, newSegment);

                    ITsString phraseText = null;
                    bool      textInFile = false;
                    //Add all of the data from <item> elements into the segment.
                    AddSegmentItemData(cache, wsFactory, phrase, newSegment, tsStrFactory, ref textInFile, ref phraseText);

                    bool lastWasWord = false;
                    if (phrase.WordsContent != null && phrase.WordsContent.Words != null)
                    {
                        foreach (var word in phrase.WordsContent.Words)
                        {
                            //If the text of the phrase was not found in a "txt" item for this segment then build it from the words.
                            if (!textInFile)
                            {
                                UpdatePhraseTextForWordItems(wsFactory, tsStrFactory, ref phraseText, word, ref lastWasWord, space);
                            }
                            MergeWordToSegment(newSegment, word, tsStrFactory);
                        }
                    }
                    UpdateParagraphTextForPhrase(newTextPara, ref offset, phraseText);
                }
            }
            return(true);
        }
Beispiel #10
0
 bool DummyCheckAndAddLanguagesInternal(FdoCache cache, Interlineartext interlinText, ILgWritingSystemFactory wsFactory, IThreadedProgress progress)
 {
     return(true);
 }
        /// <summary>
        /// Set text metadata, create or merge media file URI's.
        /// <note>media files (ELAN initiated) need to be processed before the paragraphs, as segments could reference these parts.</note>
        /// </summary>
        /// <param name="cache"></param>
        /// <param name="interlinText">The source text</param>
        /// <param name="wsFactory"></param>
        /// <param name="newText">The target text</param>
        /// <param name="merging">True if we are merging into an existing text; False if we are creating everything new</param>
        private static void SetTextMetaAndMergeMedia(FdoCache cache, Interlineartext interlinText, ILgWritingSystemFactory wsFactory,
                                                     FDO.IText newText, bool merging)
        {
            if (interlinText.Items != null)             // apparently it is null if there are no items.
            {
                foreach (var item in interlinText.Items)
                {
                    switch (item.type)
                    {
                    case "title":
                        newText.Name.set_String(GetWsEngine(wsFactory, item.lang).Handle, item.Value);
                        break;

                    case "title-abbreviation":
                        newText.Abbreviation.set_String(GetWsEngine(wsFactory, item.lang).Handle, item.Value);
                        break;

                    case "source":
                        newText.Source.set_String(GetWsEngine(wsFactory, item.lang).Handle, item.Value);
                        break;

                    case "comment":
                        newText.Description.set_String(GetWsEngine(wsFactory, item.lang).Handle, item.Value);
                        break;
                    }
                }
            }

            if (interlinText.mediafiles != null)
            {
                if (newText.MediaFilesOA == null)
                {
                    newText.MediaFilesOA = cache.ServiceLocator.GetInstance <ICmMediaContainerFactory>().Create();
                }
                newText.MediaFilesOA.OffsetType = interlinText.mediafiles.offsetType;

                foreach (var mediaFile in interlinText.mediafiles.media)
                {
                    ICmObject extantObject;
                    cache.ServiceLocator.ObjectRepository.TryGetObject(new Guid(mediaFile.guid), out extantObject);
                    var media = extantObject as ICmMediaURI;
                    if (media == null)
                    {
                        media = cache.ServiceLocator.GetInstance <ICmMediaURIFactory>().Create(cache, new Guid(mediaFile.guid));
                        newText.MediaFilesOA.MediaURIsOC.Add(media);
                    }
                    else if (!merging)
                    {
                        // If a media URI with the same GUID exists, and we are not merging, create a new media URI with a new GUID
                        media = cache.ServiceLocator.GetInstance <ICmMediaURIFactory>().Create();
                        newText.MediaFilesOA.MediaURIsOC.Add(media);

                        // Update references to this Media URI
                        foreach (var phrase in interlinText.paragraphs.SelectMany(para => para.phrases))
                        {
                            if (mediaFile.guid.Equals(phrase.mediaFile))
                            {
                                phrase.mediaFile = media.Guid.ToString();
                            }
                        }
                    }
                    // else, the media URI already exists and we are merging; simply update the location
                    media.MediaURI = mediaFile.location;
                }
            }
        }
 private static bool SomeLanguageSpecifiesVernacular(Interlineartext interlinText)
 {
     // return true if any language in the languages section is vernacular
     return(interlinText.languages.language.Any(lang => lang.vernacularSpecified));
 }
Beispiel #13
0
        /// <summary>
        /// Merge the contents of the given Text into the exising one. If this fails
        /// for some reason then return false to tell the calling method to abort the import.
        /// </summary>
        /// <param name="newText"></param>
        /// <param name="textParams"></param>
        /// <returns>The imported text may be in a writing system that is not part of this project. Return false if the user
        /// rejects the text  which tells the caller of this method to abort the import.</returns>
        private static bool MergeTextWithBIRDDoc(ref LCModel.IText newText, TextCreationParams textParams)
        {
            s_importOptions = textParams.ImportOptions;
            Interlineartext   interlinText = textParams.InterlinText;
            LcmCache          cache        = textParams.Cache;
            IThreadedProgress progress     = textParams.Progress;

            if (s_importOptions.CheckAndAddLanguages == null)
            {
                s_importOptions.CheckAndAddLanguages = CheckAndAddLanguagesInternal;
            }

            ILgWritingSystemFactory wsFactory = cache.WritingSystemFactory;
            char space = ' ';

            //handle the languages(writing systems) section alerting the user if new writing systems are encountered
            if (!s_importOptions.CheckAndAddLanguages(cache, interlinText, wsFactory, progress))
            {
                return(false);
            }

            //handle the header(info or meta) information as well as any media-files sections
            SetTextMetaAndMergeMedia(cache, interlinText, wsFactory, newText, true);

            IStText newContents = newText.ContentsOA;

            //create or reuse the paragraphs available. NOTE: Currently the paragraph guids are being ignored, this might be wrong.
            foreach (var paragraph in interlinText.paragraphs)
            {
                IStTxtPara newTextPara = null;
                if (newContents == null)
                {
                    newContents        = cache.ServiceLocator.GetInstance <IStTextFactory>().Create();
                    newText.ContentsOA = newContents;
                    newTextPara        = newContents.AddNewTextPara("");
                }
                int offset = 0;
                if (paragraph.phrases == null)
                {
                    continue;
                }
                foreach (var phrase in paragraph.phrases)
                {
                    ICmObject oldSegment = null;
                    //Try and locate a segment with this Guid. Assign newTextPara to the paragraph we're working on if we haven't already
                    if (!String.IsNullOrEmpty(phrase.guid))
                    {
                        if (cache.ServiceLocator.ObjectRepository.TryGetObject(new Guid(phrase.guid), out oldSegment))
                        {
                            if (oldSegment as ISegment != null)                             //The segment matches, add it into our paragraph.
                            {
                                IStTxtPara segmentOwner = newContents.ParagraphsOS.FirstOrDefault(para =>
                                                                                                  para.Guid.Equals((oldSegment as ISegment).Owner.Guid)) as IStTxtPara;
                                if (segmentOwner != null && newTextPara == null)                                 //We found the StTxtPara that correspond to this paragraph
                                {
                                    newTextPara = segmentOwner;
                                }
                            }
                            else if (oldSegment == null)                             //The segment is identified by a Guid, but apparently we don't have it in our current document, so make one
                            {
                                if (newTextPara == null)
                                {
                                    newTextPara = newContents.AddNewTextPara("");
                                }
                                oldSegment = cache.ServiceLocator.GetInstance <ISegmentFactory>().Create(newTextPara, offset, cache, new Guid(phrase.guid));
                            }
                            else                             //The Guid is in use, but not by a segment. This is bad.
                            {
                                return(false);
                            }
                        }
                    }
                    //If newTextPara is null, try to use a paragraph that a sibling phrase belongs to.
                    //Note: newTextPara is only assigned once, and won't be reassigned until we iterate through a new paragraph.
                    if (newTextPara == null)
                    {
                        var phraseGuids = paragraph.phrases.Select(p => p.guid);
                        foreach (IStTxtPara para in newContents.ParagraphsOS)
                        {
                            if (para.SegmentsOS.Any(seg => phraseGuids.Contains(seg.Guid.ToString())))
                            {
                                newTextPara = para;
                                break;
                            }
                        }
                    }
                    //Can't find any paragraph for our phrase, create a brand new paragraph
                    if (newTextPara == null)
                    {
                        newTextPara = newContents.AddNewTextPara("");
                    }

                    //set newSegment to the old, or create a brand new one.
                    ISegment newSegment = oldSegment as ISegment;
                    if (newSegment == null)
                    {
                        if (!string.IsNullOrEmpty(phrase.guid))
                        {
                            //The segment is identified by a Guid, but apparently we don't have it in our current document, so make one with the guid
                            newSegment = cache.ServiceLocator.GetInstance <ISegmentFactory>().Create(newTextPara, offset, cache, new Guid(phrase.guid));
                        }
                        else
                        {
                            newSegment = cache.ServiceLocator.GetInstance <ISegmentFactory>().Create(newTextPara, offset);
                        }
                    }
                    //Fill in the ELAN time information if it is present.
                    AddELANInfoToSegment(cache, phrase, newSegment);

                    ITsString phraseText = null;
                    bool      textInFile = false;
                    //Add all of the data from <item> elements into the segment.
                    AddSegmentItemData(cache, wsFactory, phrase, newSegment, ref textInFile, ref phraseText);

                    bool lastWasWord = false;
                    if (phrase.WordsContent != null && phrase.WordsContent.Words != null)
                    {
                        //Rewrite our analyses
                        newSegment.AnalysesRS.Clear();
                        foreach (var word in phrase.WordsContent.Words)
                        {
                            //If the text of the phrase was not found in a "txt" item for this segment then build it from the words.
                            if (!textInFile)
                            {
                                UpdatePhraseTextForWordItems(wsFactory, ref phraseText, word, ref lastWasWord, space);
                            }
                            MergeWordToSegment(newSegment, word);
                        }
                    }
                    UpdateParagraphTextForPhrase(newTextPara, ref offset, phraseText);
                }
            }
            return(true);
        }