Skip to content

Instantly share code, notes, and snippets.

@instanceofme
Created January 17, 2013 17:37

Revisions

  1. Adrien Lavoillotte created this gist Jan 17, 2013.
    31 changes: 31 additions & 0 deletions Legal.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    // in './src/main/scala'
    package com.stackoverflow.legal

    import fr.splayce.rel._
    import Symbols._
    import Implicits._


    object Legal {

    val NAME = α+
    val VS = """v\.?""" ~ """s\.?""".?
    val CASE = NAME("name1") ~ " " ~ VS ~ " " ~ NAME("name2")

    val NUM = ß ~+) ~ ß
    val US = """U\.? ?S\.? """
    val YEAR = ( ("1[7-9]" | "20") ~ δ{2} )("year")

    val REF = CASE ~ ", " ~ // "Doe v. Smith, "
    (NUM ~ " ").? ~ // "123 " (optional)
    US ~ NUM ~ // "U.S. 456"
    (", " ~ NUM).* ~ // ", 678" 0 to N times
    " \\(" ~ YEAR ~ "\\)" // "(1999)"

    }

    object Output extends App {

    println(Legal.REF)

    }
    97 changes: 97 additions & 0 deletions LegalSpec.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,97 @@
    // in './src/test/scala/'
    package com.stackoverflow.legal

    import fr.splayce.rel.Implicits.RE2Regex
    import org.specs2.mutable._


    class LegalSpec extends Specification {

    import Legal._


    "NAME" should {
    "Match plain names" in {
    "Doe" must be matching(NAME)
    "Smith" must be matching(NAME)
    }

    "Not match composed names" in {
    "Doe-Smith" must not be matching(NAME)
    }

    "Not match diacritics" in {
    "Doë" must not be matching(NAME)
    }
    }


    "CASE" should {
    "Match 'vs' form" in { "Doe vs Smith" must be matching(CASE) }
    "Match 'v.s' form" in { "Doe v.s Smith" must be matching(CASE) }
    "Match 'v.s.' form" in { "Doe v.s. Smith" must be matching(CASE) }
    "Match 'v' form" in { "Doe v Smith" must be matching(CASE) }
    "Match 'v.' form" in { "Doe v. Smith" must be matching(CASE) }
    }


    "NUM" should {
    "Match 1+ digits" in {
    "1" must be matching(NUM)
    "12" must be matching(NUM)
    "123" must be matching(NUM)
    "1234" must be matching(NUM)
    "12345" must be matching(NUM)
    "123456" must be matching(NUM)
    }

    "Match only standalone digits" in {
    NUM.findFirstIn(" 123 ") must beSome("123")
    NUM.findFirstIn(" n123 ") must beNone
    }
    }


    "US" should {
    "Match 'US ' form" in { "US " must be matching(US) }
    "Match 'U.S. ' form" in { "U.S. " must be matching(US) }
    "Match 'U. S. ' form" in { "U. S. " must be matching(US) }
    }


    "YEAR" should {
    "Not match 1600s" in {
    "1699" must not be matching(YEAR)
    }

    "Match 1700s to 1900s" in {
    "1700" must be matching(YEAR)
    "1799" must be matching(YEAR)
    "1800" must be matching(YEAR)
    "1899" must be matching(YEAR)
    "1900" must be matching(YEAR)
    "1999" must be matching(YEAR)
    }

    "Match 2000s" in {
    "2000" must be matching(YEAR)
    "2099" must be matching(YEAR)
    }

    "Not match 2100s" in {
    "2100" must not be matching(YEAR)
    }
    }


    "REF" should {
    "Match the full form" in {
    "Doe v. Smith, U.S. 456 (2003)" must be matching(REF)
    "Doe v. Smith, 123 U.S. 456 (2003)" must be matching(REF)
    "Doe v. Smith, 123 U.S. 456, 789 (2003)" must be matching(REF)

    "Doe v.s. Smith, 123 US 456 (2003)" must be matching(REF)
    }
    }

    }
    17 changes: 17 additions & 0 deletions build.sbt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    // in './'

    organization := "com.stackoverflow"

    name := "legal"

    version := "0.1"

    scalaVersion := "2.9.1"

    resolvers += "Imaginatio" at "https://github.com/Imaginatio/Maven-repository/raw/master"

    libraryDependencies += "fr.splayce" %% "rel" % "0.2.3"

    libraryDependencies += "org.specs2" %% "specs2" % "1.10" % "test"

    scalacOptions ++= Seq("-deprecation", "-unchecked", "-encoding", "UTF8")