Created
July 8, 2011 17:37
-
-
Save actsasgeek/1072337 to your computer and use it in GitHub Desktop.
Instance object...scalafied!
This file contains hidden or 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
| object Instance { | |
| def parseCSV( instanceAsCSVString: String): Instance = { | |
| def extractFeatureValues( tokenizedFeatures: List[ String]): List[ Option[Double]] = { | |
| tokenizedFeatures.map { token => | |
| try { | |
| Some( token.toDouble) | |
| } catch { | |
| case nfe: NumberFormatException => { | |
| if ( token == "?") { | |
| None | |
| } else { | |
| throw new MalformedInstanceException | |
| } | |
| } | |
| } | |
| } | |
| } | |
| val tokenizedInstance = instanceAsCSVString.split( ",").toList | |
| val featureValues = extractFeatureValues( tokenizedInstance.init) | |
| val classLabel = tokenizedInstance.last | |
| new Instance( featureValues, Some( classLabel)) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment