Projekt

Obecné

Profil

Stáhnout (4.77 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
                var finalTags = finalAnnotationTags[finalAnnotation].Select(ft => ft as AnnotationTagGeneric).ToList();
42
                string finalAnnotationInfoJson = DumpAnnotation(finalAnnotation, finalTags, document.Id);
43
                CreateFile(documentDir + "/final.json", finalAnnotationInfoJson, archive);
44
            }
45

    
46
            //archive.Dispose();
47
            ms.Position = 0;
48
            return ms;
49
        }
50

    
51
        private static void CreateFile(string path, string fileContent, ZipArchive archive)
52
        {
53
            var entry = archive.CreateEntry(path);
54
            var entryStream = entry.Open();
55
            entryStream.Write(Encoding.UTF8.GetBytes(fileContent));
56
            entryStream.Close();
57
        }
58

    
59
        private static string DumpDocument(Document document)
60
        {
61
            ExportDocumentInfo documentInfo = new();
62
            documentInfo.Name = document.Name;
63
            documentInfo.Content = document.Content.Content;
64
            documentInfo.DocumentId = document.Id;
65

    
66
            return JsonConvert.SerializeObject(documentInfo);
67
        }
68

    
69
        private static string DumpAnnotation(Annotation annotation, List<AnnotationTagGeneric> tags, Guid documentId)
70
        {
71
            ExportAnnotationInfo annotationInfo = new();
72
            annotationInfo.AnnotatorId = annotation.User.Id;
73
            annotationInfo.DocumentId = documentId;
74

    
75
            foreach (var tag in tags)
76
            {
77
                ExportTagInstanceInfo tagInfo = new()
78
                {
79

    
80
                    Category = tag.Tag.Category.Name,
81
                    TagName = tag.Tag.Name,
82
                    InstanceId = tag.Instance,
83
                    FirstCharPosition = tag.Position,
84
                    Length = tag.Length,
85
                };
86

    
87
                if (tag.Tag.SentimentEnabled && tagInfo.Sentiment != null)
88
                {
89
                    tagInfo.Sentiment = tag.Sentiment.ToString().ToLower();
90
                }
91

    
92
                if (tag.SubTag != null)
93
                {
94
                    tagInfo.SubTagName = tag.SubTag.Name;
95
                }
96

    
97
                annotationInfo.Tags.Add(tagInfo);
98
            }
99

    
100
            return JsonConvert.SerializeObject(annotationInfo);
101
        }
102
    }
103

    
104
    public class ExportDocumentInfo
105
    {
106
        public string Name { get; set; }
107
        public string Content { get; set; }
108
        public Guid DocumentId { get; set; }
109
    }
110

    
111
    public class ExportAnnotationInfo
112
    {
113
        public Guid AnnotatorId { get; set; }
114
        public Guid DocumentId { get; set; }
115
        public List<ExportTagInstanceInfo> Tags { get; set; } = new();
116
    }
117

    
118
    public class ExportTagInstanceInfo
119
    {
120
        public string Category { get; set; }
121
        public string TagName { get; set; }
122
        public string? SubTagName { get; set; }
123
        public Guid InstanceId { get; set; }
124
        public int FirstCharPosition { get; set; }
125
        public int Length { get; set; }
126
        public string? Sentiment { get; set; }
127
    }
128
}
    (1-1/1)