Ejemplo n.º 1
0
        private async Task <IReadOnlyList <IText> > CreateTextsAsync(IEnumerable <string> projects,
                                                                     TextCorpusType type)
        {
            StringTokenizer wordTokenizer = new LatinWordTokenizer();
            IMongoDatabase  database      = _mongoClient.GetDatabase(_dataAccessOptions.Value.MongoDatabaseName);
            IMongoCollection <BsonDocument> textDataColl = database.GetCollection <BsonDocument>(
                _realtimeService.GetCollectionName <TextData>());
            var texts = new List <IText>();

            foreach (string projectId in projects)
            {
                var project = await _realtimeService.GetSnapshotAsync <SFProject>(projectId);

                if (string.IsNullOrWhiteSpace(project.TranslateConfig.Source?.ProjectRef))
                {
                    throw new DataNotFoundException("The source project reference is missing");
                }
                string textCorpusProjectId = type switch
                {
                    TextCorpusType.Source => project.TranslateConfig.Source.ProjectRef,
                    TextCorpusType.Target => projectId,
                    _ => throw new InvalidEnumArgumentException(nameof(type), (int)type, typeof(TextCorpusType)),
                };
                foreach (TextInfo text in project.Texts.Where(t => t.HasSource))
                {
                    foreach (Chapter chapter in text.Chapters)
                    {
                        string id = TextData.GetTextDocId(textCorpusProjectId, text.BookNum, chapter.Number);
                        FilterDefinition <BsonDocument> filter = Builders <BsonDocument> .Filter.Eq("_id", id);

                        BsonDocument doc = await textDataColl.Find(filter).FirstOrDefaultAsync();

                        if (doc != null && doc.TryGetValue("ops", out BsonValue ops) && ops as BsonArray != null)
                        {
                            texts.Add(new SFScriptureText(wordTokenizer, projectId, text.BookNum, chapter.Number, doc));
                        }
                    }
                }
            }

            return(texts);
        }
Ejemplo n.º 2
0
        private async Task <IReadOnlyList <IText> > CreateTextsAsync(IEnumerable <string> projects,
                                                                     TextCorpusType type)
        {
            StringTokenizer wordTokenizer = new LatinWordTokenizer();
            IMongoDatabase  database      = _mongoClient.GetDatabase(_dataAccessOptions.Value.MongoDatabaseName);
            IMongoCollection <BsonDocument> textDataColl = database.GetCollection <BsonDocument>(
                _realtimeService.GetCollectionName <TextData>());
            var texts = new List <IText>();

            foreach (string projectId in projects)
            {
                var project = await _realtimeService.GetSnapshotAsync <SFProject>(projectId);

                if (string.IsNullOrWhiteSpace(project.TranslateConfig.Source?.ProjectRef))
                {
                    throw new DataNotFoundException("The source project reference is missing");
                }

                string textCorpusProjectId;
                string paratextId;
                switch (type)
                {
                case TextCorpusType.Source:
                    textCorpusProjectId = project.TranslateConfig.Source.ProjectRef;
                    paratextId          = project.TranslateConfig.Source.ParatextId;
                    break;

                case TextCorpusType.Target:
                    textCorpusProjectId = projectId;
                    paratextId          = project.ParatextId;
                    break;

                default:
                    throw new InvalidEnumArgumentException(nameof(type), (int)type, typeof(TextCorpusType));
                }

                foreach (TextInfo text in project.Texts.Where(t => t.HasSource))
                {
                    foreach (Chapter chapter in text.Chapters)
                    {
                        string id = TextData.GetTextDocId(textCorpusProjectId, text.BookNum, chapter.Number);
                        FilterDefinition <BsonDocument> filter = Builders <BsonDocument> .Filter.Eq("_id", id);

                        BsonDocument doc = await textDataColl.Find(filter).FirstOrDefaultAsync();

                        if (doc != null && doc.TryGetValue("ops", out BsonValue ops) && ops as BsonArray != null)
                        {
                            texts.Add(new SFScriptureText(wordTokenizer, projectId, text.BookNum, chapter.Number, doc));
                        }
                    }
                }

                string termRenderingsFileName = Path.Combine(_siteOptions.Value.SiteDir, "sync", paratextId,
                                                             "target", "TermRenderings.xml");
                if (_fileSystemService.FileExists(termRenderingsFileName))
                {
                    using var stream = _fileSystemService.OpenFile(termRenderingsFileName, FileMode.Open);
                    XDocument termRenderingsDoc = await XDocument.LoadAsync(stream, LoadOptions.None,
                                                                            CancellationToken.None);

                    texts.Add(new SFBiblicalTermsText(wordTokenizer, projectId, termRenderingsDoc));
                }
            }

            return(texts);
        }