Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Obeying static analysis harder (whitespace)
  • Loading branch information
HairyFotr committed Jan 5, 2016
1 parent 96e2ba7 commit 7580934
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 31 deletions.
12 changes: 6 additions & 6 deletions Caption.scala
Expand Up @@ -68,7 +68,7 @@ final object Caption extends RegexParsers {
def checkLengths(args: Seq[Any]): Unit =
if (parts.length != args.length + 1)
throw new IllegalArgumentException("wrong number of arguments for interpolated string")

def c(args: Any*): String = Caption(standardInterpolator(treatEscapes, args))

def standardInterpolator(process: String => String, args: Seq[Any]): String = {
Expand All @@ -85,26 +85,26 @@ final object Caption extends RegexParsers {
.replace("{", "%%%lbrace%%%")
.replace("}", "%%%rbrace%%%")
.replace("@", "%%%atsign%%%")

builder append ("@@@"+arg+"@@@")
val part = partsIter.next()
builder append process(part)
}
builder.toString
}
}

def test() = {
val str1 = "hello"
val str2 = "hello [nope nope@ nope] @@@{cheeki breeki} @ ayy @@@lmao @"
val str3 = """https://www.google.si/maps/dir/August-Bebel-Stra%C3%9Fe,+Bielefeld,+Germany/August-Bebel-Stra%C3%9Fe+8,+33602+Bielefeld,+Germany/@52.0306075,8.5365767,16z/data=!4m14!4m13!1m5!1m1!1s0x47ba3d0fcb66b3a3:0xd3692e498e1463e7!2m2!1d8.5397073!2d52.0223094!1m5!1m1!1s0x47ba3d6d57e68a65:0x3339d46a21206dce!2m2!1d8.539759!2d52.0314863!3e2?hl=en"""
val str4 = "hello [nope n{ope@ nope] @@@{che]eki breeki} @ ay}y {}{[][]}{][}{}@@@lmao @"

def cases() = {
val special = Seq("|", "[", "]", "{", "}", "@", "@@@", "%", "%%%%")
val rand = Array.fill(100)(if (nextDouble > 0.8) scala.util.Random.nextPrintableChar.toString else special.random).mkString
val cleanRand = special.foldLeft(rand)((acc, spec) => acc.replace(spec, ""))

Vector(
(c"[hello|hi], welcome to #psywerx", Set("hello, welcome to #psywerx", "hi, welcome to #psywerx")),
(c"{hello|hi}, welcome to #psywerx",
Expand Down Expand Up @@ -153,7 +153,7 @@ final object Caption extends RegexParsers {
(c"$rand {:)|!|^_^}", Set(rand+" :)", rand+"!", rand+" ^_^"))
)
}

for (i <- 1 to 5000; (cap, resSet) <- cases(); if (!resSet.contains(cap))) {
println
println(cap)
Expand Down
14 changes: 7 additions & 7 deletions OCR.scala
Expand Up @@ -46,14 +46,14 @@ object OCR {
resSplit.foldLeft(0d)((acc, res) => acc + res.size) / resSplit.size.toDouble
))
}

//TODO: put this in a case class or something :)

val SIZE = 0
val COMMON = 1
val UNCOMMON = 2
val AVGLEN = 3

Some((attrs.sortWith { case ((_, a), (_, b)) =>
if (a(COMMON) == b(COMMON)) {
(a(AVGLEN) > b(AVGLEN))
Expand Down Expand Up @@ -84,7 +84,7 @@ object OCR {
case 1 => "-shear 1.0x1.0 -deskew 60% -negate -morphology Convolve Diamond:1 -swirl 0.2 -auto-gamma -threshold 58% -scale 100%x97% -colorspace Gray -sigmoidal-contrast 12x80%"
case 2 => "-scale 107% -negate -scale 112%x100% -liquid-rescale 99%x101% -sharpen 3x6 -contrast-stretch 0x38% -threshold 12% -deskew 70%"
})

//TODO: fails with spaces, quoting doesn't help
val convertResult = (s"""convert $fileStr $convertParams $tempImgName""").!
if (convertResult != 0) None
Expand All @@ -95,7 +95,7 @@ object OCR {
case 1 => Seq("gocr", "-C", "a-zA-Z", "-i", tempImgName)
case 2 => Seq("ocrad", "-lf", "--filter=letters", "--format=utf8", tempImgName)
}).!!)

if (ocrText.isEmpty) None
else Some(ocrText)
}
Expand All @@ -111,10 +111,10 @@ object OCR {
println(results.mkString("\n"))
selectResult(results)
} catch {
case e: Exception =>
case e: Exception =>
println(s"EXCEPTION in $file ... $e")
None
}

}

2 changes: 1 addition & 1 deletion WordNet.scala
Expand Up @@ -113,7 +113,7 @@ class WordNet(folder: String) {
}

val words = in.split(" ").toList flatMap {
case wordReg(prefix, word, suffix) =>
case wordReg(prefix, word, suffix) =>
if (word.size >= 3) {
preprocess(word) filter { w => wn_sByWord contains w } flatMap { w =>
if (!stoplist.contains(w)) {
Expand Down
33 changes: 16 additions & 17 deletions util.scala
Expand Up @@ -65,7 +65,7 @@ final object util {
sd(s1.toList, s2.toList)
}
}

//implicit class MaybeSI(val sc: StringContext) extends AnyVal { def maybe(args: Any*): String = sc.parts.iterator.mkString("").maybe }
implicit class IntImplicits(val i: Int) extends AnyVal { def ~(j: Int): Int = nextInt(j-i+1)+i }

Expand All @@ -76,8 +76,7 @@ final object util {
implicit class Maps[A, B](val m: Map[A, B]) {
def apply(a: A, b: B): B = m.getOrElse(a, b)
}



implicit class OptSeq[L <: Seq[_]](val optseq: Option[L]) { def emptyToNone: Option[L] = optseq filterNot { _.isEmpty } }

implicit class D(val d: Double) { def prob: Boolean = nextDouble < d } //0.5.prob #syntaxabuse
Expand Down Expand Up @@ -107,7 +106,7 @@ final object util {

//def using[A <: {def close(): Unit}, B](param: A)(func: A => B): B = try { func(param) } finally { param.close() }
def using[A <: Closeable, B](param: A)(func: A => B): B = try { func(param) } finally { param.close() }

def appendToFile(fileName: String, allowFail: Boolean = false)(textData: String): Unit =
printToFile(fileName, allowFail)(textData, append = true)
def printToFile(fileName: String, allowFail: Boolean = false)(textData: String, append: Boolean = false): Unit =
Expand All @@ -134,13 +133,13 @@ object Memes {
val it_was_you = """http://bit.ly/zg0jQt"""
val thanks_alot = """http://i.imgur.com/NAE5F9i.png"""
}

object Time {
//wish I knew a better way.
import java.util.Date
import java.text._
import util._

private val timeReg = """[\s,]{0,2}(ob\s)?((\d{1,2}:\d{2})|(\d{1,2}h))"""
private val dateFormats = List[(matching.Regex, SimpleDateFormat)](
(timeReg.r, new SimpleDateFormat("HH:mm")),
Expand Down Expand Up @@ -206,7 +205,7 @@ object Time {
} else if (date.getYear == 70) {
date.setYear((new Date).getYear)
}

date
}
.filter(filter)
Expand All @@ -216,7 +215,7 @@ object Time {
)

def getFutureDates(text: String): List[Date] = getDates(text, _.after(new Date))

private val timeDivisor = 1000000L*1000L //s
//val timeDivisor = 1000000L //ms
def now: Int = (System.nanoTime/timeDivisor).toInt
Expand All @@ -232,16 +231,16 @@ object Time {
val time = sinceTimes.getOrElseUpdate(hash, 0)
val nowTime = now
sinceTimes(hash) = nowTime

nowTime-time
}

// used for uptime
private def pad(i: Int, p: Int = 2): String = "0"*(p-i.toString.size)+i.toString
def getSinceZeroString(time: Int): String = {
val secs = time
val (days, hours, minutes) = ((secs/60/60/24), pad((secs/60/60)%24), pad((secs/60)%60))

(((days: @switch) match {
case 0 => ""
case 1 => s"$days day "
Expand All @@ -256,7 +255,7 @@ object Net {
import java.net._
import util._
val extractor = KeepEverythingExtractor.INSTANCE

//TODO: use download + file -i or some proper mime solution, this is risky as fuck
def badExts: List[String] = getFile(util.folder+"badexts.db")
def scrapeURLs(urls: String*): String =
Expand All @@ -279,7 +278,7 @@ object Net {
def tempDownload(url: String): Option[File] = {
val tempFile = Files.createTempFile("tempdl_", null).toFile
tempFile.deleteOnExit

if (download(url, tempFile)) {
Some(tempFile)
} else {
Expand Down Expand Up @@ -309,7 +308,7 @@ object Net {
fos.close
}
}, 10.seconds)

true
} catch {
case e: Exception =>
Expand All @@ -324,10 +323,10 @@ final object Store { def apply(file: String): Store = new Store(file) }
final class Store(file: String, keyFormat: String = """([-_a-zA-Z0-9]{1,16})""") {
import sys.process._
import util.{ getFile, appendToFile, printToFile }

type Row = (String, String)
private def rowToString(kv: Row): String = if (kv._2 != null) kv._1 + " " + kv._2 else kv._1

def isValidKey(s: String): Boolean = s matches keyFormat
def replaceWith(kvs: Seq[Row]): Unit = printToFile(file)(kvs.map(rowToString).mkString("\n"))
def ++=(kvs: Iterable[Row]): Unit = appendToFile(file) { kvs.map(rowToString).mkString("\n") }
Expand All @@ -343,7 +342,7 @@ final class Store(file: String, keyFormat: String = """([-_a-zA-Z0-9]{1,16})""")
val sep = res.indexOf(' ')
if (sep == -1) (res, null) else (res.substring(0, sep), res.substring(sep+1))
}

def toList = *
def toMap = *.toMap

Expand Down

0 comments on commit 7580934

Please sign in to comment.