Projekt

Obecné

Profil

Stáhnout (4.88 KB) Statistiky
| Větev: | Tag: | Revize:
1
using Core.Entities;
2
using Models.Enums;
3
using Newtonsoft.Json;
4
using System;
5
using System.Collections.Generic;
6
using System.IO.Compression;
7
using System.Linq;
8
using System.Text;
9
using System.Threading.Tasks;
10

    
11
namespace Core.ZipUtils
12
{
13
    public class Export
14
    {
15
        public static MemoryStream FullExport(List<Document> documents, Dictionary<Document, List<Annotation>> documentAnnotations, Dictionary<Document, FinalAnnotation> documentFinalAnnotations,
16
            Dictionary<Annotation, List<AnnotationTag>> annotationTags, Dictionary<FinalAnnotation, List<FinalAnnotationTag>> finalAnnotationTags)
17
        {
18
            MemoryStream ms = new MemoryStream();
19
            var archive = new ZipArchive(ms, ZipArchiveMode.Create);
20

    
21
            for (int docIndex = 0; docIndex < documents.Count; docIndex++)
22
            {
23
                var document = documents[docIndex];
24
                string documentDir = $"{docIndex:00000}";
25

    
26
                // Write document content and name into a JSON file
27
                string documentInfoJson = DumpDocument(document);
28
                CreateFile(documentDir + "/document.json", documentInfoJson, archive);
29

    
30
                // Deal with annotations
31
                var annotations = documentAnnotations[document];
32
                for (int annotationIndex = 0; annotationIndex < annotations.Count; annotationIndex++)
33
                {
34
                    var annotation = annotations[annotationIndex];
35
                    var tags = annotationTags[annotation].Select(at => at as AnnotationTagGeneric).ToList();
36
                    string annotationInfoJson = DumpAnnotation(annotation, tags, document.Id);
37
                    CreateFile(documentDir + $"/annotation_{annotationIndex:00000}.json", annotationInfoJson, archive);
38
                }
39

    
40
                var finalAnnotation = documentFinalAnnotations[document];
41
                if (finalAnnotation == null)
42
                {
43
                    continue;
44
                }
45

    
46
                var finalTags = finalAnnotationTags[finalAnnotation].Select(ft => ft as AnnotationTagGeneric).ToList();
47
                string finalAnnotationInfoJson = DumpAnnotation(finalAnnotation, finalTags, document.Id);
48
                CreateFile(documentDir + "/final.json", finalAnnotationInfoJson, archive);
49
            }
50

    
51
            //archive.Dispose();
52
            ms.Position = 0;
53
            return ms;
54
        }
55

    
56
        private static void CreateFile(string path, string fileContent, ZipArchive archive)
57
        {
58
            var entry = archive.CreateEntry(path);
59
            var entryStream = entry.Open();
60
            entryStream.Write(Encoding.UTF8.GetBytes(fileContent));
61
            entryStream.Close();
62
        }
63

    
64
        private static string DumpDocument(Document document)
65
        {
66
            ExportDocumentInfo documentInfo = new();
67
            documentInfo.Name = document.Name;
68
            documentInfo.Content = document.Content.Content;
69
            documentInfo.DocumentId = document.Id;
70

    
71
            return JsonConvert.SerializeObject(documentInfo);
72
        }
73

    
74
        private static string DumpAnnotation(Annotation annotation, List<AnnotationTagGeneric> tags, Guid documentId)
75
        {
76
            ExportAnnotationInfo annotationInfo = new();
77
            annotationInfo.AnnotatorId = annotation.User.Id;
78
            annotationInfo.DocumentId = documentId;
79

    
80
            foreach (var tag in tags)
81
            {
82
                ExportTagInstanceInfo tagInfo = new()
83
                {
84

    
85
                    Category = tag.Tag.Category.Name,
86
                    TagName = tag.Tag.Name,
87
                    InstanceId = tag.Instance,
88
                    FirstCharPosition = tag.Position,
89
                    Length = tag.Length,
90
                };
91

    
92
                if (tag.Tag.SentimentEnabled && tagInfo.Sentiment != null)
93
                {
94
                    tagInfo.Sentiment = tag.Sentiment.ToString().ToLower();
95
                }
96

    
97
                if (tag.SubTag != null)
98
                {
99
                    tagInfo.SubTagName = tag.SubTag.Name;
100
                }
101

    
102
                annotationInfo.Tags.Add(tagInfo);
103
            }
104

    
105
            return JsonConvert.SerializeObject(annotationInfo);
106
        }
107
    }
108

    
109
    public class ExportDocumentInfo
110
    {
111
        public string Name { get; set; }
112
        public string Content { get; set; }
113
        public Guid DocumentId { get; set; }
114
    }
115

    
116
    public class ExportAnnotationInfo
117
    {
118
        public Guid AnnotatorId { get; set; }
119
        public Guid DocumentId { get; set; }
120
        public List<ExportTagInstanceInfo> Tags { get; set; } = new();
121
    }
122

    
123
    public class ExportTagInstanceInfo
124
    {
125
        public string Category { get; set; }
126
        public string TagName { get; set; }
127
        public string? SubTagName { get; set; }
128
        public Guid InstanceId { get; set; }
129
        public int FirstCharPosition { get; set; }
130
        public int Length { get; set; }
131
        public string? Sentiment { get; set; }
132
    }
133
}
    (1-1/1)