-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveDuplicates.main.kts
More file actions
210 lines (191 loc) · 8.35 KB
/
RemoveDuplicates.main.kts
File metadata and controls
210 lines (191 loc) · 8.35 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
@file:DependsOn("io.ktor:ktor-client-cio-jvm:2.3.9")
@file:DependsOn("com.aallam.openai:openai-client-jvm:3.7.0")
import com.aallam.openai.api.chat.ChatCompletionRequest
import com.aallam.openai.api.chat.ChatMessage
import com.aallam.openai.api.chat.ChatRole
import com.aallam.openai.api.logging.LogLevel
import com.aallam.openai.api.model.ModelId
import com.aallam.openai.client.LoggingConfig
import java.io.File
import com.aallam.openai.client.OpenAI
import kotlinx.coroutines.runBlocking
import java.nio.charset.Charset
import kotlin.math.absoluteValue
val manualGroups = mutableListOf<DuplicateGroup>()
// Wrap actual code in main to call it from the bottom after everything is initialized
fun main() = runBlocking {
val csvFile = File(args[0])
val groups = determineGroups(csvFile)
for (group in groups.values) {
if (!group.valid) {
println("Skipping group ${group.groupId} INVALID")
manualGroups.add(group)
continue
}
val existingDuplicates = group.getExistingDuplicates()
if (existingDuplicates.size < 2) {
println("Skipping group ${group.groupId}, not enough duplicates")
continue
}
println("Group ${group.groupId}")
for (file in existingDuplicates) {
println("Option ${file.absolutePath}")
}
val messages = mutableListOf(systemMessage)
val choice = messages.determineFileToKeep(existingDuplicates)
if (choice == null) {
println("Skipping group ${group.groupId}, no choice")
manualGroups.add(group)
continue
}
existingDuplicates
.asSequence()
.filter { it != choice }
.filter { it.exists() } // another merge could delete it
.forEach { duplicate ->
println("Deleting ${duplicate.absolutePath}")
duplicate.delete()
val completelyMergeFolders = messages.determineMergeFolder(duplicate.parentFile, choice.parentFile)
mergeFolder(duplicate.parentFile, choice.parentFile, completelyMergeFolders)
}
}
println()
println("FINISHED AUTOMATIC PARSING")
println()
for ((index, group) in manualGroups.withIndex()) {
val existingDuplicates = group.getExistingDuplicates()
if (existingDuplicates.size < 2) {
continue
}
println("Group ${group.groupId} could not be manually resolved. Please chose the file to keep from these options: ($index/${manualGroups.size}")
existingDuplicates.forEachIndexed { i, file ->
println("$i: " + file.absolutePath + " " + file.length())
}
val input = readln()
val indexToKeep = input.toIntOrNull()
if (indexToKeep == null || indexToKeep >= existingDuplicates.size || indexToKeep < 0) {
println("INVALID input $input, skipping group")
} else {
existingDuplicates
.filterIndexed { i, _ -> i != indexToKeep }
.forEach { duplicate ->
println("Deleting ${duplicate.absolutePath}")
duplicate.deleteIfExists()
}
}
}
}
fun determineGroups(csvFile: File): Map<Int, DuplicateGroup> {
return csvFile.readLines(Charset.forName("UTF-16"))
.asSequence()
.drop(1)
.filter { it.isNotBlank() }
.map { line ->
val values = line.split('\t')
val groupId = values[0].toInt()
val filePath = values[2].trim('"')
val size = values[3].toLong()
val file = File(filePath)
Triple(groupId, file, size)
}
.filter { (_, file, _) -> file.extension == "pdf" } // most useful for me
.filterNot { (_, file, _) -> file.name.contains("SessionTracking") } // big group
.filter { (_, file, _) -> file.exists() }
.groupingBy { it.first }
.aggregate { groupId, acc: DuplicateGroup?, (_, file, size), _ ->
if (acc == null) {
DuplicateGroup(groupId, size, mutableListOf(file))
} else {
acc.duplicates.add(file)
if (acc.size != size) {
acc.valid = false
}
acc
}
}
}
fun mergeFolder(sourceDir: File, targetDir: File, completelyMerge: Boolean) {
sourceDir.listFiles()!!.forEach { sourceFile ->
val targetFile = File(targetDir, sourceFile.name)
if (targetFile.exists()) {
val sizeDiff = (targetFile.length() - sourceFile.length()).absoluteValue
if (sizeDiff == 0L) {
println("\tMerge: Deleting ${sourceFile.name}, already exists")
sourceFile.delete()
} else if (completelyMerge && targetFile.length() > sizeDiff * 1000) {
println("\tMerge: Deleting ${sourceFile.name}, already exists and diff only $sizeDiff")
sourceFile.delete()
} else if (completelyMerge) {
println("\tMerge: Skipping ${sourceFile.name}, name taken different size")
manualGroups.add(DuplicateGroup(-1, targetFile.length(), mutableListOf(targetFile, sourceFile), valid = false))
}
} else if (completelyMerge) {
println("\tMerge: Moving ${sourceFile.name}")
sourceFile.renameTo(targetFile)
}
}
if (sourceDir.listFiles()?.isEmpty() == true) {
println("\tMerge: Dir deleted $sourceDir")
sourceDir.delete()
}
}
val openAiClient = OpenAI(token = "api-token", logging = LoggingConfig(LogLevel.None))
val gpt4Model = ModelId("gpt-4")
suspend fun MutableList<ChatMessage>.determineFileToKeep(duplicates: List<File>): File? {
val message = duplicates.joinToString(
prefix = "Which of these files to keep? Only return the path of the file to keep or unclear if the choice is unclear.\n",
separator = "\n",
transform = { "\"${it.absolutePath}\"" },
)
add(ChatMessage(role = ChatRole.User, content = message))
val determineRequest = ChatCompletionRequest(
model = gpt4Model,
temperature = 0.7,
topP = 1.0,
frequencyPenalty = 0.0,
presencePenalty = 0.0,
messages = this,
)
val response = openAiClient.chatCompletion(determineRequest).choices.firstOrNull()?.message!!
add(response)
val keptPath = response.content!!.trim('\"')
println("Chose to keep: $keptPath")
return duplicates.firstOrNull { it.absolutePath == keptPath }
}
suspend fun MutableList<ChatMessage>.determineMergeFolder(obsoleteFolder: File, targetDir: File): Boolean {
println("Merge: Should we merge ${obsoleteFolder.absolutePath} into ${targetDir.absolutePath}")
val message = "Should all files from the folder \"" + obsoleteFolder.absolutePath + "\" be moved to \"" +
targetDir.absolutePath + "\"?\nReturn true or false. " +
"Only return true if you are confident that all files within the first directory make sense in the second directory."
add(ChatMessage(role = ChatRole.User, content = message))
val determineRequest = ChatCompletionRequest(
model = gpt4Model,
temperature = 0.7,
topP = 1.0,
frequencyPenalty = 0.0,
presencePenalty = 0.0,
messages = this,
)
val response = openAiClient.chatCompletion(determineRequest).choices.firstOrNull()?.message!!
add(response)
println("Chose to merge folders: ${response.content}")
return response.content!!.toBoolean()
}
data class DuplicateGroup(
val groupId: Int,
val size: Long,
val duplicates: MutableList<File> = mutableListOf(),
var valid: Boolean = true,
) {
fun getExistingDuplicates(): List<File> = duplicates.filter { it.exists() }
}
fun File.deleteIfExists() {
if (exists()) delete()
}
val systemPrompt = """
You help determine which file to keep between multiple duplicates of a file. Keep the file with the better readable path that is informative and concise. Try to avoid characters that are not human readable.
Ensure your answers match the requirements in the request, as they will be consumed by a software not a human.
If the criteria are contradicting each other or the files appear to not be duplicates, return "unclear"
""".trimIndent()
val systemMessage = ChatMessage(role = ChatRole.System, content = systemPrompt)
main() // run last so that everything is initialized