Skip to content

Instantly share code, notes, and snippets.

@AkazaRenn
Last active December 17, 2021 17:51
Show Gist options
  • Save AkazaRenn/533b8064a885ffaa1faaa99f72d4bbc8 to your computer and use it in GitHub Desktop.
Save AkazaRenn/533b8064a885ffaa1faaa99f72d4bbc8 to your computer and use it in GitHub Desktop.
import java.io.IOException
import java.util.Scanner
import java.net.URL
import java.util.HashMap
val TS_CHAR_URL = URL("https://github.com/BYVoid/OpenCC/raw/master/data/dictionary/TSCharacters.txt")
val ST_CHAR_URL = URL("https://github.com/BYVoid/OpenCC/raw/master/data/dictionary/STCharacters.txt")
enum class ChineseTypes {
SIMPLIFIED_CHINESE,
TRADITIONAL_CHINESE,
JAPANESE,
NONE
}
var charMap = HashMap<Char, ChineseTypes>();
fun buildMap() {
try {
var scanner = Scanner(TS_CHAR_URL.openStream())
while(scanner.hasNext()) {
var chars = scanner.nextLine().trim().split('\t')
if(!chars[1].contains(chars[0])) {
charMap.put(chars[0][0], ChineseTypes.TRADITIONAL_CHINESE)
}
}
} catch (e: IOException) {
println("Error saving TS_CHAR_URL")
}
try {
var scanner = Scanner(ST_CHAR_URL.openStream())
while(scanner.hasNext()) {
var chars = scanner.nextLine().trim().split('\t')
if(!chars[1].contains(chars[0])) {
if(charMap.get(chars[0][0]) != null) {
charMap.remove(chars[0][0])
}
else {
charMap.put(chars[0][0], ChineseTypes.SIMPLIFIED_CHINESE)
}
}
}
} catch (e: IOException) {
println("Error saving ST_CHAR_URL")
}
}
fun checkString(str: String): ChineseTypes {
var result = ChineseTypes.NONE
if (str != null) {
for (char in str) {
result = charMap.getOrDefault(char, ChineseTypes.NONE)
if (result != ChineseTypes.NONE) {
break
}
}
}
return result
}
fun main(args: Array<String>) {
buildMap();
when(checkString(args[0])) {
ChineseTypes.TRADITIONAL_CHINESE -> println("TC")
ChineseTypes.SIMPLIFIED_CHINESE -> println("SC")
else -> {
println("Both or non-Chinese")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment