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);
|
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
|
|
49
|
return ms;
|
50
|
}
|
51
|
|
52
|
private static void CreateFile(string path, string fileContent, ZipArchive archive)
|
53
|
{
|
54
|
var entry = archive.CreateEntry(path);
|
55
|
var entryStream = entry.Open();
|
56
|
entryStream.Write(Encoding.UTF8.GetBytes(fileContent));
|
57
|
entryStream.Close();
|
58
|
}
|
59
|
|
60
|
private static string DumpDocument(Document document)
|
61
|
{
|
62
|
ExportDocumentInfo documentInfo = new();
|
63
|
documentInfo.Name = document.Name;
|
64
|
documentInfo.Content = document.Content.Content;
|
65
|
documentInfo.DocumentId = document.Id;
|
66
|
|
67
|
return JsonConvert.SerializeObject(documentInfo);
|
68
|
}
|
69
|
|
70
|
private static string DumpAnnotation(Annotation annotation, List<AnnotationTagGeneric> tags, Guid documentId)
|
71
|
{
|
72
|
ExportAnnotationInfo annotationInfo = new();
|
73
|
annotationInfo.AnnotatorId = annotation.User.Id;
|
74
|
annotationInfo.DocumentId = documentId;
|
75
|
|
76
|
foreach (var tag in tags)
|
77
|
{
|
78
|
ExportTagInstanceInfo tagInfo = new()
|
79
|
{
|
80
|
|
81
|
Category = tag.Tag.Category.Name,
|
82
|
TagName = tag.Tag.Name,
|
83
|
InstanceId = tag.Instance,
|
84
|
FirstCharPosition = tag.Position,
|
85
|
Length = tag.Length,
|
86
|
};
|
87
|
|
88
|
if (tag.Tag.SentimentEnabled && tagInfo.Sentiment != null)
|
89
|
{
|
90
|
tagInfo.Sentiment = tag.Sentiment.ToString().ToLower();
|
91
|
}
|
92
|
|
93
|
if (tag.SubTag != null)
|
94
|
{
|
95
|
tagInfo.SubTagName = tag.SubTag.Name;
|
96
|
}
|
97
|
|
98
|
annotationInfo.Tags.Add(tagInfo);
|
99
|
}
|
100
|
|
101
|
return JsonConvert.SerializeObject(annotationInfo);
|
102
|
}
|
103
|
}
|
104
|
|
105
|
public class ExportDocumentInfo
|
106
|
{
|
107
|
public string Name { get; set; }
|
108
|
public string Content { get; set; }
|
109
|
public Guid DocumentId { get; set; }
|
110
|
}
|
111
|
|
112
|
public class ExportAnnotationInfo
|
113
|
{
|
114
|
public Guid AnnotatorId { get; set; }
|
115
|
public Guid DocumentId { get; set; }
|
116
|
public List<ExportTagInstanceInfo> Tags { get; set; } = new();
|
117
|
}
|
118
|
|
119
|
public class ExportTagInstanceInfo
|
120
|
{
|
121
|
public string Category { get; set; }
|
122
|
public string TagName { get; set; }
|
123
|
public string? SubTagName { get; set; }
|
124
|
public Guid InstanceId { get; set; }
|
125
|
public int FirstCharPosition { get; set; }
|
126
|
public int Length { get; set; }
|
127
|
public string? Sentiment { get; set; }
|
128
|
}
|
129
|
}
|