| 6 |
| 7 | import java.io.IOException |
| 8 | import java.util.regex.Pattern |
| 9 |
| 10 | /** |
| 11 | * Paranoid text spacing for good readability, to automatically insert whitespace between |
| 12 | * CJK (Chinese, Japanese, Korean), half-width English, digit and symbol characters. |
| 13 | * |
| 14 | * |
| 15 | * These whitespaces between English and Chinese characters are called "Pangu Spacing" by sinologist, since it |
| 16 | * separate the confusion between full-width and half-width characters. Studies showed that who dislike to |
| 17 | * add whitespace between English and Chinese characters also have relationship problem. Almost 70 percent of them |
| 18 | * will get married to the one they don't love, the rest only can left the heritage to their cat. Indeed, |
| 19 | * love and writing need some space in good time. |
| 20 | * |
| 21 | * @author Vinta Chen |
| 22 | * @author qwq233 |
| 23 | * @since 1.0.0 |
| 24 | */ |
| 25 | class Pangu { |
| 26 | companion object { |
| 27 |
| 28 | /** |
| 29 | * The space character. |
| 30 | */ |
| 31 | private const val SPACE = " " |
| 32 |
| 33 | /* |
| 34 | * Some capturing group patterns for convenience. |
| 35 | * |
| 36 | * CJK: Chinese, Japanese, Korean |
| 37 | * ANS: Alphabet, Number, Symbol |
| 38 | */ |
| 39 | private val CJK_ANS = Pattern.compile( |
| 40 | "([\\p{InHiragana}\\p{InKatakana}\\p{InBopomofo}\\p{InCJKCompatibilityIdeographs}\\p{InCJKUnifiedIdeographs}])" + |
| 41 | "([a-z0-9`~@\\$%\\^&\\*\\-_\\+=\\|\\\\/])", |
| 42 | Pattern.CASE_INSENSITIVE |
| 43 | ) |
| 44 | private val ANS_CJK = Pattern.compile( |
| 45 | "([a-z0-9`~!\\$%\\^&\\*\\-_\\+=\\|\\\\;:,\\./\\?])" + |
| 46 | "([\\p{InHiragana}\\p{InKatakana}\\p{InBopomofo}\\p{InCJKCompatibilityIdeographs}\\p{InCJKUnifiedIdeographs}])", |
| 47 | Pattern.CASE_INSENSITIVE |
| 48 | ) |
| 49 | private val CJK_QUOTE = Pattern.compile( |
| 50 | "([\\p{InHiragana}\\p{InKatakana}\\p{InBopomofo}\\p{InCJKCompatibilityIdeographs}\\p{InCJKUnifiedIdeographs}])" + |
| 51 | "([\"'])" |
| 52 | ) |
| 53 | private val QUOTE_CJK = Pattern.compile( |
| 54 | "([\"'])" + |
| 55 | "([\\p{InHiragana}\\p{InKatakana}\\p{InBopomofo}\\p{InCJKCompatibilityIdeographs}\\p{InCJKUnifiedIdeographs}])" |
| 56 | ) |
| 57 | private val FIX_QUOTE = Pattern.compile("([\"'])(\\s*)(.+?)(\\s*)([\"'])") |
| 58 | private val CJK_BRACKET_CJK = Pattern.compile( |
| 59 | "([\\p{InHiragana}\\p{InKatakana}\\p{InBopomofo}\\p{InCJKCompatibilityIdeographs}\\p{InCJKUnifiedIdeographs}])" + |
| 60 | "([\\({\\[]+(.*?)[\\)}\\]]+)" + |
| 61 | "([\\p{InHiragana}\\p{InKatakana}\\p{InBopomofo}\\p{InCJKCompatibilityIdeographs}\\p{InCJKUnifiedIdeographs}])" |
| 62 | ) |
| 63 | private val CJK_BRACKET = Pattern.compile( |
| 64 | "([\\p{InHiragana}\\p{InKatakana}\\p{InBopomofo}\\p{InCJKCompatibilityIdeographs}\\p{InCJKUnifiedIdeographs}])" + |
| 65 | "([\\(\\){}\\[\\]<>])" |
| 66 | ) |
| 67 | private val BRACKET_CJK = Pattern.compile( |
| 68 | "([\\(\\){}\\[\\]<>])" + |
| 69 | "([\\p{InHiragana}\\p{InKatakana}\\p{InBopomofo}\\p{InCJKCompatibilityIdeographs}\\p{InCJKUnifiedIdeographs}])" |
| 70 | ) |
| 71 | private val FIX_BRACKET = Pattern.compile("([(\\({\\[)]+)(\\s*)(.+?)(\\s*)([\\)}\\]]+)") |
| 72 | private val CJK_HASH = Pattern.compile( |
| 73 | "([\\p{InHiragana}\\p{InKatakana}\\p{InBopomofo}\\p{InCJKCompatibilityIdeographs}\\p{InCJKUnifiedIdeographs}])" + |
| 74 | "(#(\\S+))" |
| 75 | ) |
| 76 | private val HASH_CJK = Pattern.compile( |
| 77 | "((\\S+)#)" + |
| 78 | "([\\p{InHiragana}\\p{InKatakana}\\p{InBopomofo}\\p{InCJKCompatibilityIdeographs}\\p{InCJKUnifiedIdeographs}])" |
| 79 | ) |
| 80 | } |
| 81 |
| 82 | private fun processUrl(text: String) = Pattern.compile("://").matcher(text).let { matcher -> |
| 83 | if (!matcher.find()) { |
| 84 | throw NullPointerException("No URL found in text") |
| 85 | } |
| 86 | var prefixOffset = 0 |
| 87 | val prefix: String = StringBuilder().apply { |
| 88 | arrayListOf<Char>().apply { |
| 89 | for (i in matcher.start() - 1 downTo 0) { |
| 90 | val c = text[i] |
| 91 | if (c.isWhitespace()) { |
| 92 | prefixOffset = i + 1 |
| 93 | break |
| 94 | } |
| 95 | add(c) |
| 96 | } |
| 97 | }.reversed().forEach { append(it) } |
| 98 | }.toString() |
| 99 |
| 100 | var suffixOffset = text.lastIndex |
| 101 | val suffix: String = StringBuilder().apply { |
| 102 | arrayListOf<Char>().apply { |
| 103 | for (i in matcher.end() until text.length) { |
| 104 | val c = text[i] |
| 105 | if (c.isWhitespace()) { |
| 106 | suffixOffset = i |
| 107 | break |
| 108 | } |
| 109 | add(c) |
| 110 | } |
| 111 | }.forEach { append(it) } |
| 112 | }.toString() |
| 113 |
| 114 | val url = "$prefix://$suffix" |
| 115 | val first: String? = if (0 != prefixOffset) text.substring(0, prefixOffset) else null |
| 116 | val last: String? = if (text.lastIndex != suffixOffset) text.substring(suffixOffset) else null |
| 117 |
| 118 | Triple(first, url, last) |
| 119 | } |
| 120 |
| 121 |
| 122 | /** |
| 123 | * Performs a paranoid text spacing on `text`. |
| 124 | * |
| 125 | * @param text the string you want to process, must not be `null`. |
| 126 | * @return a comfortable and readable version of `text` for paranoiac. |
| 127 | */ |
| 128 | fun spacingText(text: String): String { |
| 129 | var text: String = text |
| 130 |
| 131 | // URL |
| 132 | if (text.contains("://")) { |
| 133 | val (first, url, last) = processUrl(text) |
| 134 | var result = String() |
| 135 | if (first != null) { |
| 136 | result += spacingText(first) |
| 137 | } |
| 138 | result += url |
| 139 | if (last != null) { |
| 140 | result += spacingText(last) |
| 141 | } |
| 142 | return result |
| 143 | } |
| 144 |
| 145 | // CJK and quotes |
| 146 | val cqMatcher = CJK_QUOTE.matcher(text) |
| 147 | text = cqMatcher.replaceAll("$1$SPACE$2") |
| 148 | val qcMatcher = QUOTE_CJK.matcher(text) |
| 149 | text = qcMatcher.replaceAll("$1$SPACE$2") |
| 150 | val fixQuoteMatcher = FIX_QUOTE.matcher(text) |
| 151 | text = fixQuoteMatcher.replaceAll("$1$3$5") |
| 152 |
| 153 | // CJK and brackets |
| 154 | val oldText = text |
| 155 | val cbcMatcher = CJK_BRACKET_CJK.matcher(text) |
| 156 | val newText = cbcMatcher.replaceAll("$1$SPACE$2$SPACE$4") |
| 157 | text = newText |
| 158 | if (oldText == newText) { |
| 159 | val cbMatcher = CJK_BRACKET.matcher(text) |
| 160 | text = cbMatcher.replaceAll("$1$SPACE$2") |
| 161 | val bcMatcher = BRACKET_CJK.matcher(text) |
| 162 | text = bcMatcher.replaceAll("$1$SPACE$2") |
| 163 | } |
| 164 | val fixBracketMatcher = FIX_BRACKET.matcher(text) |
| 165 | text = fixBracketMatcher.replaceAll("$1$3$5") |
| 166 |
| 167 | // CJK and hash |
| 168 | val chMatcher = CJK_HASH.matcher(text) |
| 169 | text = chMatcher.replaceAll("$1$SPACE$2") |
| 170 | val hcMatcher = HASH_CJK.matcher(text) |
| 171 | text = hcMatcher.replaceAll("$1$SPACE$3") |
| 172 |
| 173 | // CJK and ANS |
| 174 | val caMatcher = CJK_ANS.matcher(text) |
| 175 | text = caMatcher.replaceAll("$1$SPACE$2") |
| 176 | val acMatcher = ANS_CJK.matcher(text) |
| 177 | text = acMatcher.replaceAll("$1$SPACE$2") |
| 178 |
| 179 | return text |
| 180 | } |
| 181 |
| 182 | private fun log(text: String) { |
| 183 | println("panguTrace: $text") |
| 184 | } |
| 185 |
| 186 | } |
| 187 |
| 188 | internal object Test { |
| 189 | @Throws(IOException::class) |
| 190 | @JvmStatic |
| 191 | fun main(args: Array<String>) { |
| 192 | val pangu = Pangu() |
| 193 | println(pangu.spacingText("當你凝視著 https://telegra.ph/八尋ぽち-ひみチュッ-中国翻訳-無修正-DL版-06-17-3 ,bug也凝視著 https://telegra.ph/ASDF-DL版-06-17-3")) |
| 194 | } |
| 195 | } |
| 196 |