Created
October 28, 2016 13:24
-
-
Save sungkmi/67c7a7f09c2015dd4643094fa1afdd0d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lascala | |
object Titanic extends App { | |
def loadData(lines: Seq[String]): Seq[Passenger] = lines map { line => | |
val Array(id, survived, pclass, lastName, firstName, sex, age, sibSp, parch, ticket, fare, cabin, embarked) = (line + " ").split(",") | |
Passenger( | |
id = id.toInt, | |
survived = survived == "1", | |
pclass = pclass.toInt, | |
name = s"${lastName.tail.trim}, ${firstName.init.trim}", | |
isMale = sex == "male", | |
age = util.Try(age.toDouble).toOption, | |
sibSp = sibSp.toInt, | |
parch = parch.toInt, | |
ticket = ticket, | |
fare = fare.toDouble, | |
cabin = cabin, | |
embarked = embarked.trim match { | |
case "C" => Cherbourg | |
case "Q" => Queenstown | |
case "S" => Southampton | |
case "" => Unknown | |
} | |
) | |
} | |
loadData(io.Source.fromFile("train.csv").getLines.toList.tail) foreach println | |
} | |
case class Passenger( | |
id: Int, | |
survived: Boolean, | |
pclass: Int, | |
name: String, | |
isMale: Boolean, | |
age: Option[Double], | |
sibSp: Int, | |
parch: Int, | |
ticket: String, | |
fare: Double, | |
cabin: String, | |
embarked: Embarked | |
) | |
sealed trait Embarked | |
case object Cherbourg extends Embarked | |
case object Queenstown extends Embarked | |
case object Southampton extends Embarked | |
case object Unknown extends Embarked |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lascala | |
import Titanic._ | |
import utest._ | |
object TitanicTest extends TestSuite { | |
val tests = this{ | |
'loadData{ | |
val lines = """1,0,3,"Braund, Mr. Owen Harris",male,22,1,0,A/5 21171,7.25,,S | |
2,1,1,"Cumings, Mrs. John Bradley (Florence Briggs Thayer)",female,38,1,0,PC 17599,71.2833,C85,C | |
62,1,1,"Icard, Miss. Amelie",female,38,0,0,113572,80,B28,""".lines.toSeq | |
val result = loadData(lines).toList | |
val expected = Seq( | |
Passenger(1,false,3,"Braund, Mr. Owen Harris",true,Some(22),1,0,"A/5 21171",7.25,"",Southampton), | |
Passenger(2,true,1,"Cumings, Mrs. John Bradley (Florence Briggs Thayer)",false,Some(38),1,0,"PC 17599",71.2833,"C85",Cherbourg), | |
Passenger(62,true,1,"Icard, Miss. Amelie",false,Some(38),0,0,"113572",80,"B28",Unknown) | |
) | |
assert(expected == result) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment