#! /usr/bin/Rscript

args <- commandArgs(trailingOnly = TRUE)
if (length(args) > 0){
    outfile = args[1]
} else {
    outfile = "freqPlot.pdf"
}

f <- file("stdin")
open(f)
words <- readLines(f)
close(f)
head(words)

logWordFreq <- sort(log(table(words)),decreasing = TRUE)
logWordFreqRank <- log(1:length(logWordFreq))
head(logWordFreq)

logFreqFreq <- sort(log(table(logWordFreq)), decreasing = TRUE)
logFreqFreqRank <- log(1:length(logFreqFreq))
head(logFreqFreq)

print(paste("Save plot to", outfile))
a4width <- 8.3
pdf(outfile, width=a4width, height=a4width/2)
par(mfcol=c(1,2))
plot(logWordFreqRank,
     logWordFreq,
     type='l',
     xlab="log rank",
     ylab="log word frequency")
plot(logFreqFreqRank,
     logFreqFreq,
     type='l',
     xlab="log rank",
     ylab="log frequency of word frequency")
