Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: GPL-3.0-only
package helium314.keyboard.latin.utils

/** AI proofreading overuses semicolons and long dashes: turn the ones it added into commas. */
object ProofreadPunctuation {
private val SEMICOLON = Regex(";(?=\\s)")
// between words only: not at a line start, after other punctuation, or in a double dash
private val LONG_DASH = Regex("(?<=[\\p{L}\\p{N})\\]\"'’”])[ \\t]*—[ \\t]*(?=[^\\s—])")
// ';' is the Greek question mark
private val GREEK = Regex("\\p{IsGreek}")

/** A character already present in [original] is the writer's own, so it is left alone. */
fun removeAdded(original: String, result: String): String {
var cleaned = result
if (';' !in original && !GREEK.containsMatchIn(cleaned)) cleaned = cleaned.replace(SEMICOLON, ",")
if ('—' !in original) cleaned = cleaned.replace(LONG_DASH, ", ")
return cleaned
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,9 @@ class ProofreadService(private val context: Context) {
val finalOutput = if (targetLanguage != null) {
cleanTranslationOutput(cleanedOutput)
} else {
cleanProofreadOutput(text, cleanedOutput)
val cleanedProofread = cleanProofreadOutput(text, cleanedOutput)
// a custom prompt may ask for any punctuation
if (overridePrompt != null) cleanedProofread else ProofreadPunctuation.removeAdded(text, cleanedProofread)
}

// only the lengths: the texts are private, and the system log can be read by other tools
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,7 @@ Text to proofread:
return inputText.trim()
}

return cleaned
return ProofreadPunctuation.removeAdded(inputText, cleaned)
}

private fun cleanTranslationOutput(inputText: String, outputText: String): String {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-License-Identifier: GPL-3.0-only
package helium314.keyboard.latin.utils

import kotlin.test.Test
import kotlin.test.assertEquals

class ProofreadPunctuationTest {
private fun clean(result: String, original: String = "plain text") = ProofreadPunctuation.removeAdded(original, result)

@Test fun addedSemicolonsAndDashesBecomeCommas() {
assertEquals("I came, I saw, I left", clean("I came; I saw — I left"))
assertEquals("wait, what", clean("wait—what"))
assertEquals("first,\nsecond", clean("first;\nsecond"))
assertEquals("he said \"no\", then left", clean("he said \"no\"—then left"))
}

@Test fun charactersOfTheWriterStay() {
assertEquals("a; b, c", clean("a; b — c", original = "a; b c"))
assertEquals("a, b — c", clean("a; b — c", original = "a b — c"))
}

@Test fun otherUsesStay() {
listOf("see you ;)", "a&amp;b", "end;", "— a list item", "so?—yes", "他说——好", "pages 3–5", "Τι κάνεις; Καλά")
.forEach { assertEquals(it, clean(it)) }
}
}
Loading