blob: afc24355f3d6146df6d714cd087519d2c38fe750 [file] [log] [blame]
#!/bin/env groovy
@Grab('info.picocli:picocli-groovy:4.6.3')
@Grab('org.tukaani:xz:1.9')
@Grab('org.codehaus.gpars:gpars:1.2.1')
@Grab('org.apache.commons:commons-compress:1.22')
import groovy.cli.Option
import groovy.cli.Unparsed
import groovy.cli.commons.CliBuilder
import groovyx.gpars.GParsPool
import org.apache.commons.compress.compressors.CompressorStreamFactory
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.atomic.LongAdder
import java.util.logging.Logger
def tag = "generate_pseudonym_key"
interface GeneratePseudonymKeyArgs {
@Option(shortName = 'c', defaultValue = '0', description = 'generate pseudonyms for column n')
int column()
@Option(shortName = 's', defaultValue = "«END»,«START»", convert = { it.split(",")}, description = "comma separated special keys (will get pseudonyms -n..-1)")
String[] specialKeys()
@Option(shortName = 'h')
boolean help()
@Unparsed
List files()
}
CliBuilder cli = new CliBuilder(usage: "${tag} [options] file [files]")
def options = cli.parseFromSpec(GeneratePseudonymKeyArgs, args)
if (options.help() || !options.files() || options.files()[0].startsWith('-')) {
cli.usage()
System.exit(-1)
}
def freqList = new ConcurrentHashMap<String, LongAdder>(10000000, 0.75, options.files().size())
System.setProperty("java.util.logging.SimpleFormatter.format", '[%1$tF %1$tT]:%4$s: %5$s%n')
Logger log = Logger.getLogger("org.ids_mannheim.${tag}")
log.info("Generating pseudonym key for column ${options.column()} in ${options.files()}")
GParsPool.withPool {
options.files().eachParallel(fname -> {
log.info("Reading ${fname}")
def input = new CompressorStreamFactory().createCompressorInputStream(new BufferedInputStream(new FileInputStream(fname)))
input.splitEachLine("\t") { fields ->
freqList.computeIfAbsent(fields[options.column()], k -> new LongAdder()).add(Long.parseLong(fields[fields.size() - 1]))
}
log.info("Done reading ${fname}")
})
}
log.info("Sorting and writing...")
def j = -options.specialKeys().size()
options.specialKeys().each {
freqList.remove(it)
println("${it}\t${j++}")
}
def i = 0
freqList.entrySet()
.parallelStream()
.sorted({ a, b -> def ret = b.value <=> a.value; if (ret == 0) a.key <=> b.key else ret })
.forEachOrdered({ e -> println "${e.key}\t${i++}" })