File size: 2,272 Bytes
4977db2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import Foundation

struct ChatGPTAnalysisParser: ImportParser {
    func parse(sourceURL: URL, dedupIndex: DedupIndex) throws -> ImportParseResult {
        let files = try ImportFiles.immediateFiles(
            at: sourceURL,
            expected: "eine ChatGPT-Analysis-.json-Datei oder einen Ordner mit .json-Dateien",
            directFileMatches: { $0.pathExtension.lowercased() == "json" },
            directoryFileMatches: { $0.pathExtension.lowercased() == "json" }
        )
        var accumulator = ImportAccumulator(
            minimumLength: 50,
            maximumLength: 2_000,
            dedupIndex: dedupIndex
        )

        for fileURL in files {
            let root: Any
            do {
                root = try ImportJSON.object(from: fileURL)
            } catch {
                accumulator.skipMalformed()
                continue
            }

            guard let object = root as? [String: Any] else {
                accumulator.skipMalformed()
                continue
            }
            let cluster = object["cluster"] as? String ?? fileURL.lastPathComponent
            guard let quotes = object["quotes_with_source"] as? [Any] else {
                accumulator.skipMalformed()
                continue
            }

            for quote in quotes {
                let text: String?
                if let quote = quote as? String {
                    text = quote
                } else if let quote = quote as? [String: Any] {
                    text = quote["quote"] as? String ?? quote["text"] as? String
                } else {
                    text = nil
                }

                guard let text else {
                    accumulator.skipMalformed(scanned: true)
                    continue
                }
                accumulator.accept(
                    text,
                    sourcePrefix: "chatgpt-import",
                    tags: normalizedClusterTag(cluster)
                )
            }
        }

        return accumulator.result()
    }

    private func normalizedClusterTag(_ cluster: String) -> String {
        cluster
            .lowercased()
            .replacingOccurrences(of: "/", with: ",")
            .replacingOccurrences(of: " ", with: "-")
    }
}